package com.hotel.app.controller;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import com.hotel.app.service.AppCuisineService;
import com.hotel.app.service.AppDetailService;
import com.hotel.app.service.AppFoodService;
import com.hotel.app.service.AppOrderService;
import com.hotel.pojo.Cuisine;
import com.hotel.pojo.Detail;
import com.hotel.pojo.Food;
import com.hotel.pojo.Order;
import com.hotel.util.HttpClientUtil;
/**
* @Title AppOrderServlet
* @Description
* @Company Ait Interent
* @author DaiZhenWei
* @date 2016-10-26 下午9:51:45
*/
public class AppOrderServlet extends HttpServlet {
private AppOrderService aos = new AppOrderService();
private AppDetailService ads = new AppDetailService();
private AppFoodService afs = new AppFoodService();
private AppCuisineService acs = new AppCuisineService();
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
switch (method) {
case "clientCar":
clientCar(request, response);
break;
case "changeCount":
changeCount(request, response);
break;
case "decideOrder":
try {
decideOrder(request, response);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "clientOrderList":
clientOrderList(request, response);
break;
case "removeOrder":
removeOrder(request, response);
break;
}
}
/**
* 取消菜品
* @param request
* @param response
*/
private void removeOrder(HttpServletRequest request,
HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("id"));
Map<Food,String> foods = getClientCar(request, response);
foods.remove(afs.getFoodById(id));
}
/**
* 查看清单列表
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void clientOrderList(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
double sumprice = 0;
Map<Food, String> foodmap = getClientCar(request, response);
if (foodmap != null) {
Set<Food> foods = foodmap.keySet();
for (Food fo : foods) {
int num = Integer.parseInt(foodmap.get(fo));
sumprice += num * fo.getPrice();
}
}
List<Cuisine> foodTypeList = acs.getFoodTypeList();
request.setAttribute("foodTypeList", foodTypeList);
request.setAttribute("sumprice", sumprice);
request.getRequestDispatcher("app/clientOrderList.jsp").forward(
request, response);
}
/**
* 确认结账
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
* @throws ParseException
*/
@Test
private void decideOrder(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
ParseException {
int tableId = Integer.parseInt((String) request.getSession()
.getAttribute("tableId"));
// 获取已选菜品
Map<Food, String> foodmap = getClientCar(request, response);
/*String count = request.getParameter("count");
int id = Integer.parseInt(request.getParameter("foodid"));
Food food = afs.getFoodById(id);
boolean flag = false;
//判断之前是否选定次菜品,如果选中过,就将数量增加即可
//如果没有选过,则将菜品新增加进map
if (foodmap == null) {
foodmap = new HashMap<Food, String>();
foodmap.put(food, count);
} else {
Set<Food> foods = foodmap.keySet();
for (Food fo : foods) {
if (fo.getFoodName().equals(food.getFoodName())) {
int num = Integer.parseInt(count)
+ Integer.parseInt(foodmap.get(fo));
foodmap.remove(fo);
foodmap.put(fo, num + "");
flag = true;
break;
} else {
flag = false;
}
}
if (!flag) {
foodmap.put(food, count);
}
}*/
// 计算出总消费
double sumprice = 0;
if (foodmap != null) {
Set<Food> foods = foodmap.keySet();
for (Food fo : foods) {
int num = Integer.parseInt(foodmap.get(fo));
sumprice += num * fo.getPrice();
}
}
// 首先查看session中是否有本次订单的id值,
// 如果有证明本次操作是更新操作,只需向数据库中更新totalprice值即可
// 如果没有则是插入操作,要想数据库中存入详细数据
String orderid = (String) request.getSession().getAttribute("orderId");
DateFormat dateFormat;
Timestamp dateTime = null;
String orid = null;
if (orderid == null) {
// 向数据库添加数据
Order order = new Order();
order.setTableId(tableId);
dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",
Locale.ENGLISH);// 设定格式
// dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",
// Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(new Date()
.toLocaleString());// util类型
dateTime = new java.sql.Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
order.setOrderDate(dateTime);
order.setTotalPrice(sumprice);
order.setOrderStatus(1);
aos.addOrder(order);
orid = aos.getOrderIdByDate(dateTime, tableId);
request.getSession().setAttribute("orderId", orid); //将订单id存入session中
}else{
//执行更新操作
orid = orderid;
aos.updateOrderPrice(Integer.parseInt(orid), sumprice);
}
//修改订单详细表时要先把该订单下的所有详细删除,再将新的详细表插入
int orderId = Integer.parseInt(orid);
ads.deleteDetail(orderId);
Detail detail = new Detail();
Set<Food> foods = foodmap.keySet();
for(Food fo : foods){
detail.setOrderId(orderId);
detail.setFoodId(fo.getId());
detail.setFoodCount(Integer.parseInt(foodmap.get(fo)));
ads.addDetail(detail);
}
//移除所有session
request.getSession().invalidate();
HttpClientUtil.doGet("http://localhost:8080/Hotel/TableServlet?method=updateStatus&id="+tableId+"&status=1");
request.getRequestDispatcher("AppTableServlet?method=searchByStatus").forward(request, response);
// 更新餐车
/*request.getSession().setAttribute("timestamp", dateTime);
request.getSession().setAttribute("foodmap", foodmap);
clientOrderList(request, response);*/
}
/**
* 修改餐车菜品数量
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void changeCount(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String count = request.getParameter("count");
if(count==null||count.equals("")){
count = "1";
}
// 获取已选菜品
Map<Food, String> foodmap = getClientCar(request, response);
Food food = afs.getFoodById(id);
boolean flag = false;
//判断之前是否选定次菜品,如果选中过,就将数量增加即可
//如果没有选过,则将菜品新增加进map
if (foodmap == null) {
foodmap = new HashMap<Food, String>();
foodmap.put(food, count);
} else {
Set<Food> foods = foodmap.keySet();
for (Food fo : foods) {
if (fo.getFoodName().equals(food.getFoodName())) {
int num = Integ