use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartController method checkCart.
@LoginRequired(loginSuccess = false)
@RequestMapping("/checkCart")
public String checkCart(String isChecked, String skuId, HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
String memberId = (String) request.getAttribute("memberId");
String nickname = (String) request.getAttribute("nickname");
// 调用服务,修改状态
cartService.checkCart(skuId, memberId, isChecked);
// 将最新的数据从缓存总查出来,渲染给内嵌页
List<OmsCartItem> omsCartItems = cartService.carList(memberId);
modelMap.put("cartList", omsCartItems);
BigDecimal totalAmount = getTotalAmount(omsCartItems);
modelMap.put("totalAmount", totalAmount.toString());
return "cartListInner";
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartController method cartList.
@LoginRequired(loginSuccess = false)
@RequestMapping("/cartList")
public String cartList(String skuId, Integer quantity, HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
List<OmsCartItem> omsCartItems = new ArrayList<>();
String userId = (String) request.getAttribute("memberId");
String nickname = (String) request.getAttribute("nickname");
if (StringUtils.isNotBlank(userId)) {
// 已经登录 查询db
omsCartItems = cartService.carList(userId);
} else {
// 没有登陆 查询cookie
String cartListCookie = CookieUtil.getCookieValue(request, "cartListCookie", true);
omsCartItems = JSON.parseArray(cartListCookie, OmsCartItem.class);
for (OmsCartItem omsCartItem : omsCartItems) {
omsCartItem.setTotalPrice(omsCartItem.getPrice().multiply(BigDecimal.valueOf(omsCartItem.getQuantity())).toString());
}
}
modelMap.put("cartList", omsCartItems);
BigDecimal totalAmount = getTotalAmount(omsCartItems);
modelMap.put("totalAmount", totalAmount.toString());
return "cartList";
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartController method addToCart.
@LoginRequired(loginSuccess = false)
@RequestMapping("/addToCart")
public String addToCart(String skuId, int quantity, HttpServletRequest request, HttpServletResponse response) {
// 调用商品服务查询商品信息
PmsSkuInfo pmsSkuInfo = skuService.getSkuById(skuId);
// 将商品信息封装成购物车信息
OmsCartItem omsCartItem = new OmsCartItem();
omsCartItem.setCreateDate(new Date());
omsCartItem.setDeleteStatus(0);
omsCartItem.setModifyDate(new Date());
omsCartItem.setPrice(BigDecimal.valueOf(pmsSkuInfo.getPrice()));
omsCartItem.setProductAttr("");
omsCartItem.setProductBrand("");
omsCartItem.setProductCategoryId(pmsSkuInfo.getCatalog3Id());
omsCartItem.setProductId(Long.valueOf(pmsSkuInfo.getSpuId()));
omsCartItem.setProductName(pmsSkuInfo.getSkuName());
omsCartItem.setProductPic(pmsSkuInfo.getSkuDefaultImg());
omsCartItem.setProductSkuCode("1111111111");
omsCartItem.setProductSkuId(Long.valueOf(skuId));
omsCartItem.setQuantity(quantity);
// 判断用户是否登录
String memberId = (String) request.getAttribute("memberId");
String nickname = (String) request.getAttribute("nickname");
if (StringUtils.isBlank(memberId)) {
// 用户未登录
List<OmsCartItem> omsCartItems = new ArrayList<>();
// 判断用户本地是由有cookie信息
String cartListCookie = CookieUtil.getCookieValue(request, "cartListCookie", true);
if (cartListCookie != null) {
// cookie不为空
omsCartItems = JSON.parseArray(cartListCookie, OmsCartItem.class);
boolean exist = if_cart_exist(omsCartItems, omsCartItem);
// 判断当点添加的商品在我们的购物车是否存在
if (exist) {
// 之前添加过,更新操作
for (OmsCartItem cartItem : omsCartItems) {
String productSkuId = cartItem.getProductSkuId().toString();
if (productSkuId.equals(omsCartItem.getProductSkuId())) {
cartItem.setQuantity(cartItem.getQuantity() + omsCartItem.getQuantity());
cartItem.setPrice(cartItem.getPrice().add(omsCartItem.getPrice()));
}
}
} else {
// 之前没有添加过
omsCartItems.add(omsCartItem);
}
} else {
// cookie为空
omsCartItems.add(omsCartItem);
}
CookieUtil.setCookie(request, response, "cartListCookie", JSON.toJSONString(omsCartItems), 60 * 60 * 72, true);
} else {
// 用户已登录
// 在db中查询购物车数据
OmsCartItem omsCartItemFromDB = cartService.getCartExistByUser(memberId, skuId);
if (omsCartItemFromDB == null) {
// 购物车中没有该商品
omsCartItem.setMemberId(Long.valueOf(memberId));
cartService.addCartIterm(omsCartItem);
} else {
// 购物车中有该商品 直接更新数据库
omsCartItemFromDB.setQuantity(omsCartItem.getQuantity());
cartService.updateCarItem(omsCartItemFromDB);
}
// 同步缓存
cartService.flushCache(memberId);
}
return "redirect:/success.html";
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartController method if_cart_exist.
private boolean if_cart_exist(List<OmsCartItem> omsCartItems, OmsCartItem omsCartItem) {
boolean exist = false;
for (OmsCartItem cartItem : omsCartItems) {
String cartItemSkuId = cartItem.getProductSkuId().toString();
String productSkuId = omsCartItem.getProductSkuId().toString();
if (cartItemSkuId.equals(productSkuId)) {
exist = true;
}
}
return exist;
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class OrderController method toTrade.
@LoginRequired(loginSuccess = true)
@RequestMapping("/toTrade")
public String toTrade(HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
String memberId = (String) request.getAttribute("memberId");
String nickname = (String) request.getAttribute("nickname");
// 收件人地址列表
List<MemberReceiveAddress> receiveAddressesByMemberId = userService.getReceiveAddressByMemberId(memberId);
modelMap.put("userAddressList", receiveAddressesByMemberId);
// 将购物车集合转换为页面计算 清单集合
List<OmsCartItem> omsCartItems = cartService.carList(memberId);
List<OmsOrderItem> omsOrderItems = new ArrayList<>();
for (OmsCartItem omsCartItem : omsCartItems) {
if (omsCartItem.getIsChecked().equals("1")) {
// 每循环一个购物车对象,封装一个商品的详情到OmsOrderItem
OmsOrderItem omsOrderItem = new OmsOrderItem();
omsOrderItem.setProductName(omsCartItem.getProductName());
omsOrderItem.setProductPrice(omsCartItem.getPrice());
omsOrderItem.setProductQuantity(omsCartItem.getQuantity());
omsOrderItem.setProductPic(omsCartItem.getProductPic());
omsOrderItems.add(omsOrderItem);
}
}
modelMap.put("omsOrderItems", omsOrderItems);
BigDecimal totalAmount = getTotalAmount(omsOrderItems);
modelMap.put("totalAmount", totalAmount.toString());
// 生成交易码,为了在提交订单时做交易码的校验
String tradeCode = orderService.genTradeCode(memberId);
modelMap.put("tradeCode", tradeCode);
return "trade";
}
Aggregations