package com.bysj.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bysj.beans.Cart;
import com.bysj.beans.Order;
import com.bysj.beans.OrderProduct;
import com.bysj.beans.Product;
import com.bysj.service.CartService;
import com.bysj.service.CommonService;
import com.bysj.service.OrderProductService;
import com.bysj.service.OrderService;
import com.bysj.service.ProductService;
import com.bysj.tools.CommonUtils;
import com.bysj.tools.MsgHelper;
import com.github.pagehelper.PageInfo;
//订单类
@RequestMapping("/client")
@Controller
public class OrderController extends ClientBaseController {
@Autowired
private CartService cartService;
@Autowired
private ProductService productService;
@Autowired
private OrderService orderService;
@Autowired
private OrderProductService orderProductService;
@Autowired
private CommonService<Order> statisService;
@Autowired
private CommonService<OrderProduct> statisService2;
// 我的订单
@RequestMapping(value = "order/index")
public String index(HttpServletRequest request, HttpSession session, @RequestParam Map<String, String> parameter,
Map<String, Object> map) {
getCategory(map);
if (getUser(session) == null) {
return "redirect:/client/tologin?error=timeout";
} else {
return "client/order/index";
}
}
// 获取我的订单
@RequestMapping(value = "order/getdata")
public String getData(HttpServletRequest request, HttpSession session, @Param("title") String title,
Map<String, Object> map, @RequestParam(defaultValue = "1") Integer pageIndex,
@RequestParam(defaultValue = "8") Integer pageSize) throws Exception {
// get orders
String sql = "SELECT a.*,u.username FROM t_order AS a LEFT JOIN t_user AS u ON a.userid = u.id where a.userid = "
+ getUserId(session) + " order by a.id desc ";
PageInfo<Order> pageInfo = statisService.getAllBySql(Order.class, sql, pageIndex, pageSize);
// get product by order
for (Order o : pageInfo.getList()) {
Integer totalCnt = 0;
sql = "SELECT a.*,b.title,b.price,a.number*b.price AS subprice,imagepath FROM t_order_product AS a\r\n"
+ "LEFT JOIN t_product AS b ON a.productid = b.id where orderid = " + o.getId();//订单编码
List<OrderProduct> orderProduct = statisService2.getAllBySql(OrderProduct.class, sql).getList();
o.setOrderProduct(orderProduct);
// 计算商品总数
for (OrderProduct tOrderProduct : orderProduct) {
totalCnt += tOrderProduct.getNumber();
}
o.setNumber(totalCnt);
}
map.put("list", pageInfo.getList());
map.put("pageIndex", pageIndex);
map.put("pageSize", pageSize);
map.put("itemTotal", pageInfo.getTotal());
map.put("number", (pageIndex == 0 ? pageIndex : pageIndex - 1) * pageSize);
return "client/order/getdata";
}
// 提交订单
@ResponseBody
@RequestMapping("order/addorder")
public MsgHelper addorder(HttpSession session, @RequestParam Map<String, String> parameter, Order bean) {
MsgHelper msgHelper = new MsgHelper();
if (getUser(session) == null) {
msgHelper.setCode(300);
msgHelper.setMessage("登录已过期,请重新登录!");
return msgHelper;
}
List<Cart> listCart = new ArrayList<>();
// 0.get cart product
List<Cart> list = cartService.selectList(new EntityWrapper<Cart>().eq("userid", getUserId(session)));
int failCounter = 0;
Integer sumPrice = 0;
// 1 check store
for (Cart c : list) {
// 2.1 check product store
Product product = productService.selectById(c.getProductid());
Integer stockNumber = product.getNumber();
Integer saleNumber = c.getNumber();
Integer orderNumber = saleNumber;
// 库存为0
if (stockNumber == 0) {
failCounter++;
continue;
}
// 购物车里的产品数量大于库存数量
if (saleNumber > stockNumber) {
orderNumber = stockNumber;
sumPrice += (product.getPrice() * orderNumber);
OrderProduct orderProduct = new OrderProduct();
orderProduct.setProductid(product.getId());
orderProduct.setNumber(orderNumber);
c.settOrderProduct(orderProduct);
listCart.add(c);
failCounter++;
} else {
sumPrice += (product.getPrice() * orderNumber);
OrderProduct orderProduct = new OrderProduct();
orderProduct.setProductid(product.getId());
orderProduct.setNumber(orderNumber);
c.settOrderProduct(orderProduct);
listCart.add(c);
}
}
bean.setSumprice(sumPrice);
if (failCounter == list.size()) {
msgHelper.setCode(500);
msgHelper.setMessage("抱歉,由于库存不足订单生成失败!");
return msgHelper;
}
// 2.create order ******
bean.setCreatedate(CommonUtils.getNowDateTime());
bean.setUserid(getUserId(session));
bean.setOrdername("系统自动生成");
bean.setOrdercode(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
bean.setStatus("待发货");
orderService.insert(bean);
// 3.create order-product
// Order lastOne = orderService.selectList(new
// EntityWrapper<Order>().orderDesc(Arrays.asList(new String[] { "id"
// })).last("limit 1")).get(0);
Order lastOne = orderService.selectById(bean.getId());
for (Cart c : listCart) {
OrderProduct opb = c.gettOrderProduct();
opb.setOrderid(lastOne.getId());
orderProductService.insert(opb);
// 2.2 update stock number
Product product = productService.selectById(c.getProductid());
product.setNumber(product.getNumber() - opb.getNumber());
productService.updateById(product);
}
// 4. clear cart
cartService.delete(new EntityWrapper<Cart>().eq("userid", getUserId(session)));
if (failCounter > 0) {
msgHelper.setCode(400);
msgHelper.setMessage("订单生成成功!但由于部分商品库存不足,订单中的数量小于购物的数量!");
} else {
msgHelper.setMessage("恭喜您,订单生成成功!");
}
return msgHelper;
}
// 取消订单
@ResponseBody
@RequestMapping("order/cancelorder")
public MsgHelper cancelorder(HttpSession session, @RequestParam Map<String, String> parameter, Order bean) {
MsgHelper msgHelper = new MsgHelper();
if (getUser(session) == null) {
msgHelper.setCode(300);
msgHelper.setMessage("登录已过期,请重新登录!");
} else {
Integer orderId = bean.getId();
// 1.get order-product -> delete
String sql = "SELECT a.*,b.title,b.price,a.number*b.price AS subprice,imagepath FROM t_order_product AS a\r\n"
+ "LEFT JOIN t_product AS b ON a.productid = b.id where orderid = " + orderId;
List<OrderProduct> list = statisService2.getAllBySql(OrderProduct.class, sql).getList();
for (OrderProduct c : list) {
// 1.1 update stock number
Product product = productService.selectById(c.getProductid());
Integer stockNumber = product.getNumber();
Integer orderNumber = c.getNumber();
product.setNumber(stockNumber + orderNumber);
productService.updateById(product);
// 1.2 delete order-product
orderProductService.deleteById(c.getId());
}
// 2.delete order
orderService.deleteById(orderId);
msgHelper.setMessage("订单取消成功!");
}
return msgHelper;
}
// 确认收货
@ResponseBody
@RequestMapping("order/getproduct")
public MsgHelper getproduct(HttpSession