/** 查看购物车 */ public String myShoppingCart() { HttpServletRequest request = ServletActionContext.getRequest(); // 获取用户ID Users user = (Users) request.getSession().getAttribute("users"); String userId = user.getId(); // 购物车商品列表map Map<Commodity, Integer> cartmap = (Map<Commodity, Integer>) request.getSession().getAttribute("cartmap"); // 根据userid查询购物车表的commodity String hql = "from ShoppingCart s where s.usersId = ? "; List<ShoppingCart> list = commonService.findListByHQL(hql, userId); // 购物车中的商品数量 int count = 0; // 购物车商品总价 float money = 0; if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { ShoppingCart shoppingCart = list.get(i); commodity = commonService.findById(shoppingCart.getCommodityId(), new Commodity()); FileSource fileSource = commonService.findById(commodity.getPicId(), new FileSource()); commodity.setFileSource(fileSource); cartmap.put(commodity, shoppingCart.getCount()); money += commodity.getPrice() * shoppingCart.getCount(); count += shoppingCart.getCount(); } } request.setAttribute("count", count); request.setAttribute("money", money); request.setAttribute("cartmap", cartmap); return "myShoppingCart"; }
/** 加入购物车 */ public String addToShoppingCart() { HttpServletRequest request = ServletActionContext.getRequest(); Timestamp creTime = Utils.date2Stamp(new Date()); // 获取用户ID Users user = (Users) request.getSession().getAttribute("users"); String userId = user.getId(); // 获取页面传的商品ID String id = request.getParameter("commodity.id"); System.out.println(id); Map<Commodity, Integer> cartmap = (Map<Commodity, Integer>) request.getSession().getAttribute("cartmap"); // 根据userid查询购物车表有无该用户对应的commodityID String hql = "from ShoppingCart s where s.usersId = ? "; List<ShoppingCart> list = commonService.findListByHQL(hql, userId); // 购物车商品总价 int money = 0; // 购物车商品总数 int count = 0; boolean flag = false; // 记录查询时是否有对应的commodityId对应上 for (int i = 0; i < list.size(); i++) { ShoppingCart shoppingCart = list.get(i); if (id.equals(shoppingCart.getCommodityId().toString())) { // 更新shoppingcart表的count值 shoppingCart.setCount(shoppingCart.getCount() + 1); commonService.saveOrUpdate(shoppingCart); flag = true; } commodity = commonService.findById(shoppingCart.getCommodityId(), new Commodity()); FileSource fileSource = commonService.findById(commodity.getPicId(), new FileSource()); commodity.setFileSource(fileSource); cartmap.put(commodity, shoppingCart.getCount()); // 更新购物车商品总数 count = count + shoppingCart.getCount(); // 更新购物车商品总价 money = (int) (money + shoppingCart.getCount() * commodity.getPrice()); } // 设置属性并保存新的shoppingcart实体 if (flag == false) { shoppingCart.setCommodityId(id); shoppingCart.setCreateTime(creTime); shoppingCart.setUpdateTime(creTime); shoppingCart.setId(Utils.getUUID()); shoppingCart.setUsersId(userId); shoppingCart.setCount(1); commonService.save(shoppingCart); // 取得新增的商品信息并放入cartmap commodity = commonService.findById(shoppingCart.getCommodityId(), new Commodity()); FileSource fileSource = commonService.findById(commodity.getPicId(), new FileSource()); commodity.setFileSource(fileSource); cartmap.put(commodity, shoppingCart.getCount()); // 更新购物车商品总数 count = count + shoppingCart.getCount(); // 更新购物车商品总价 money = (int) (money + shoppingCart.getCount() * commodity.getPrice()); } request.setAttribute("cartmap", cartmap); request.setAttribute("money", money); request.setAttribute("count", count); return "addToShoppingCart"; }