package com.matrix.system.shopXcx.api.action; import com.matrix.system.hive.bean.SysVipInfo; import com.matrix.component.redis.RedisUserLoginUtils; import com.matrix.core.constance.SystemErrorCode; import com.matrix.core.constance.SystemMessageCode; import com.matrix.core.exception.GlobleException; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.tools.StringUtils; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.common.interceptor.HostInterceptor; import com.matrix.system.shopXcx.api.service.OrderCouponGroupService; import com.matrix.system.shopXcx.api.service.ShoppingCartService; import com.matrix.system.shopXcx.api.service.WxShopCouponService; import com.matrix.system.shopXcx.api.vo.ShopCartBillVo; import com.matrix.system.shopXcx.api.vo.ShopCartVo; import com.matrix.system.shopXcx.bean.ShopShoppingCart; import com.matrix.system.shopXcx.bean.ShopSku; import com.matrix.system.shopXcx.dao.ShopShoppingCartDao; import com.matrix.system.shopXcx.dao.ShopSkuDao; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author jiangyouyao * @description 购物车 * @date 2019-06-12 19:15 */ @CrossOrigin(origins = "*", maxAge = 3600) @Api(tags = "购物车类") @RestController @RequestMapping(value = "wxapi/ShoppingCart") public class WxShoppingCartAction { @Autowired private ShopShoppingCartDao shoppingCartDao; @Autowired private RedisUserLoginUtils redisUserLoginUtils; @Autowired WxShopCouponService shopCouponService; @Autowired ShoppingCartService shoppingCartService; @Autowired ShopSkuDao skuDao; @Autowired OrderCouponGroupService orderCouponGroupService; @ApiOperation(value = "根据ID删除购物车", notes = "") @PostMapping("/deleteByCartId/{cartId}") @ResponseBody public AjaxResult deleteByCartId(@PathVariable("cartId") Integer cartId) { int i = shoppingCartDao.deleteById(cartId); if (i == 0) { return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败"); } return new AjaxResult(AjaxResult.STATUS_SUCCESS, "删除成功"); } @ApiOperation(value = "根据用户ID查询购物车 没有分页", notes = "") @PostMapping("/findShoppingCart") @ResponseBody public AjaxResult getShoppingCartByUserId(@RequestBody ShopShoppingCart shoppingCart) { SysVipInfo loginUser = redisUserLoginUtils.getLoginUser(SysVipInfo.class); List list = shoppingCartDao.selectByCartUserId(shoppingCart.getShopId(),loginUser.getId()); List shopCartVoList = shoppingCartService.buildShopCart(list); AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, shopCartVoList, shopCartVoList.size()); ShopCartBillVo shopCartBill = shoppingCartService.buildShopCartBillVo(shopCartVoList); result.putInMap("billTotal", shopCartBill.getBillTotal().stripTrailingZeros().toPlainString()); result.putInMap("selectCount", shopCartBill.getSelectCount()); result.putInMap("cartCount", shopCartBill.getCartCount()); result.putInMap("billCouponTotal", shopCartBill.getBillCouponTotal().stripTrailingZeros().toPlainString()); result.putInMap("srcTotal", shopCartBill.getSrcTotal().stripTrailingZeros().toPlainString()); return result; } @ApiOperation(value = "批量删除", notes = "") @PostMapping(value = "/delShoppingCart/{keys}") public @ResponseBody AjaxResult del(@PathVariable("keys") String keys) { List ids = StringUtils.strToCollToString(keys, ","); int i = shoppingCartDao.deleteByIds(ids); if (i > 0) { return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i); } else { throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL); } } @ApiOperation(value = "查询微商城购物车数量", notes = "") @PostMapping(value = "/getUserCartCount/{shopId}") public @ResponseBody AjaxResult getUserCartCount(@PathVariable("shopId") Long shopId) { SysVipInfo loginUser = redisUserLoginUtils.getLoginUser(SysVipInfo.class); Integer userCartCount = shoppingCartDao.selectUserCartCount(shopId, loginUser.getId()); if (userCartCount == null) { userCartCount = 0; } AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "请求成功"); result.putInMap("userCartCount", userCartCount); return result; } /** * 接收保存购物车数据 */ @PostMapping(value = "/saveShoppingCart") public @ResponseBody AjaxResult saveShoppingCart(@RequestBody ShopShoppingCart shoppingCart) { //查询到sku设置对应的产品id ShopSku sku = skuDao.selectById(shoppingCart.getCartSkuId()); if (sku == null) { return new AjaxResult(AjaxResult.STATUS_FAIL, "无效的产品id"); } shoppingCart.setCartProductId(sku.getpId()); SysVipInfo loginUser = redisUserLoginUtils.getLoginUser(SysVipInfo.class); shoppingCart.setCreateBy(loginUser.getOpenId()); shoppingCart.setUpdateBy(loginUser.getOpenId()); shoppingCart.setCartUserId(loginUser.getId()); shoppingCart.setCompanyId(HostInterceptor.getCompanyId()); ShopShoppingCart shoppCartLimt = shoppingCartDao.selectBuyLimit(shoppingCart.getCartProductId(), shoppingCart.getCartSkuId()); // Integer buyLimitNum = 0; Integer stock = 0; if (null != shoppCartLimt) { // buyLimitNum = shoppCart.getBuyLimit(); stock = shoppCartLimt.getStock(); } // 如果加入购物车的数量超出商品限购数量则不让其加入 /* * if(shoppingCart.getCartNumber() > buyLimitNum){ return new * AjaxResult(AjaxResult.STATUS_FAIL, "该产品加入购物车的数量超出商品限购数量"); } */ // 如果加入购物车的数量超出商品库存数量则不让其加入 if (shoppingCart.getCartNumber() > stock) { return new AjaxResult(AjaxResult.STATUS_FAIL, "您来晚了商品已经卖完了"); } // 如果同一个用户添加相同规格的产品,只做数量上的增加 Integer cartProductId = shoppingCart.getCartProductId(); Integer cartSkuId = shoppingCart.getCartSkuId(); Integer cartNumber = shoppingCart.getCartNumber(); ShopShoppingCart shopShoppingCart = new ShopShoppingCart(); shopShoppingCart.setCartProductId(cartProductId); shopShoppingCart.setCartSkuId(cartSkuId); shopShoppingCart.setCartUserId(loginUser.getId()); shopShoppingCart.setShopId(shoppingCart.getShopId()); List shopShoppingCarts = shoppingCartDao.selectByModel(shopShoppingCart); int i = 0; if (CollectionUtils.isNotEmpty(shopShoppingCarts)) { Map modifyMap = new HashMap<>(); for (ShopShoppingCart shopCart : shopShoppingCarts) { Integer cartNumber1 = shopCart.getCartNumber(); Integer total = cartNumber + cartNumber1; // 再次判断加入购物车的数量是否超出商品限购数量 /* * if(total > buyLimitNum ){ return new * AjaxResult(AjaxResult.STATUS_FAIL, "该产品加入购物车的数量超出商品限购数量"); } */ if (total > stock) { return new AjaxResult(AjaxResult.STATUS_FAIL, "该产品加入购物车的数量超出商品库存数量"); } else { modifyMap.put("cartNumber", total); modifyMap.put("isSelected", 1); modifyMap.put("cartId", shopCart.getCartId()); shoppingCartDao.updateByMap(modifyMap); i = i + 1; } } } else { shoppingCart.setIsSelected(1); i = shoppingCartDao.insert(shoppingCart); } if (i == 0) { return new AjaxResult(AjaxResult.STATUS_FAIL, "保存失败"); } int userCartCount = shoppingCartDao.selectUserCartCount(shoppingCart.getShopId(), loginUser.getId()); AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "保存成功"); result.putInMap("userCartCount", userCartCount); return result; } /** * 根据ID增减购物车 * * @param * @return */ @PostMapping("/updateCarNumberByCartId") @ResponseBody public AjaxResult updateCarNumberByCartId(@RequestBody ShopShoppingCart shoppingCart) { ShopShoppingCart shopRefundRecord = shoppingCartDao.selectById(shoppingCart.getCartId()); ShopShoppingCart shoppCart = shoppingCartDao.selectBuyLimit(shopRefundRecord.getCartProductId(), shopRefundRecord.getCartSkuId()); Integer stock = 0; if (null != shoppCart) { stock = shoppCart.getStock(); } Map modifyMap = new HashMap<>(); if (null != shoppingCart.getIncreaseAndDecrease() && AppConstance.IS_INCREASE.equals(shoppingCart.getIncreaseAndDecrease())) { modifyMap.put("cartNumber", shopRefundRecord.getCartNumber() + 1); modifyMap.put("cartId", shoppingCart.getCartId()); // 如果加入购物车的数量超出商品库存数量则不让其加入 if (shopRefundRecord.getCartNumber() + 1 > stock) { return new AjaxResult(AjaxResult.STATUS_FAIL, "您来晚了商品已经卖完了"); } shoppingCartDao.updateByMap(modifyMap); } if (null != shoppingCart.getIncreaseAndDecrease() && AppConstance.IS_DECREASE.equals(shoppingCart.getIncreaseAndDecrease())) { if ((shopRefundRecord.getCartNumber() - 1) > 0) { modifyMap.put("cartNumber", shopRefundRecord.getCartNumber() - 1); modifyMap.put("cartId", shoppingCart.getCartId()); shoppingCartDao.updateByMap(modifyMap); } } if (null != shoppingCart.getModifyCartNumber()) { modifyMap.put("cartNumber", shoppingCart.getModifyCartNumber()); modifyMap.put("cartId", shoppingCart.getCartId()); // 如果加入购物车的数量超出商品库存数量则不让其加入 if (shoppingCart.getModifyCartNumber() > stock) { return new AjaxResult(AjaxResult.STATUS_FAIL, "您来晚了商品已经卖完了"); } shoppingCartDao.updateByMap(modifyMap); } return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功"); } /** * 修改购物车产品选中状态 * * @param * @return */ @PostMapping("/updateCartSelected") @ResponseBody public AjaxResult updateCartSelected(@RequestBody ShopShoppingCart shoppingCart) { Map modifyMap = new HashMap<>(); modifyMap.put("isSelected", shoppingCart.getIsSelected()); modifyMap.put("cartId", shoppingCart.getCartId()); shoppingCartDao.updateByMap(modifyMap); return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功"); } /** * 修改购物车产品选中状态 * * @param * @return */ @PostMapping("/updateCartAllSelected/{shopId}/{isSelected}") @ResponseBody public AjaxResult updateCartAllSelected(@PathVariable("isSelected") Integer isSelected, @PathVariable("shopId") Long shopId) { SysVipInfo user = redisUserLoginUtils.getLoginUser(SysVipInfo.class); shoppingCartDao.updateAllSelected(user.getOpenId(), shopId, isSelected); return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功"); } }