@RequestMapping(value = "/toCart", method = RequestMethod.GET) public String toCar(Model m, @CookieValue("MyLogin") String myLogin) { int customerId = Integer.parseInt(myLogin.split(",")[0]); ////////////////////////////////// CartQueryModel qm = new CartQueryModel(); qm.getPage().setPageSize(10); qm.setCustomerUuid(customerId); Page<CartModel> page = cartService.queryByPage(qm); m.addAttribute("carts", page.getResult()); return "cart/myCart"; }
@RequestMapping(value = "/order", method = RequestMethod.GET) public String order(@CookieValue("MyLogin") String myLogin) { int customerId = Integer.parseInt(myLogin.split(",")[0]); ////////////////////////////////// CartQueryModel qm = new CartQueryModel(); qm.getPage().setPageSize(50); qm.setCustomerUuid(customerId); Page<CartModel> page = cartService.queryByPage(qm); // 计算价格 float total = 0; for (CartModel cm : page.getResult()) { total += 10 * cm.getBuyNum(); } OrderModel om = new OrderModel(); om.setCustomerUuid(customerId); om.setOrderTime(DateFormatUtil.long2string(System.currentTimeMillis())); om.setSaveMoney(0.0F); om.setTotalMoney(total); om.setState(1); // 新增订单 int orderId = orderService.create(om); //////////////////////////////////////////////// for (CartModel cm : page.getResult()) { OrderDetailModel odm = new OrderDetailModel(); odm.setGoodsUuid(cm.getGoodsUuid()); odm.setOrderUuid(orderId); odm.setOrderNum(cm.getBuyNum()); odm.setPrice(10.0F); odm.setMoney(odm.getPrice() * odm.getOrderNum()); odm.setSaveMoney(0.0F); // 新增订单详情 orderDetailService.create(odm); //////////////////////////////////////// // 更新库存 StoreModel sm = new StoreModel(); sm.setGoodsUuid(cm.getGoodsUuid()); sm.setStoreNum(cm.getBuyNum()); storeService.updateByGoodsId(sm); ///////////////////////////////////////// // 清空购物车 cartService.delete(cm.getUuid()); } return "success"; }
@RequestMapping(value = "/addToCart/{uuid}", method = RequestMethod.GET) public String addToCar( Model m, @PathVariable("uuid") int uuid, @CookieValue("MyLogin") String myLogin) { int customerId = Integer.parseInt(myLogin.split(",")[0]); CartModel cm = new CartModel(); cm.setCustomerUuid(customerId); cm.setGoodsUuid(uuid); cm.setBuyNum(1); // 新增记录 cartService.create(cm); ////////////////////////////////// CartQueryModel qm = new CartQueryModel(); qm.getPage().setPageSize(10); qm.setCustomerUuid(customerId); Page<CartModel> page = cartService.queryByPage(qm); m.addAttribute("carts", page.getResult()); return "cart/myCart"; }