| package com.matrix.system.shopXcx.api.action; | 
|   | 
| 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.biz.bean.BizUser; | 
| import com.matrix.component.redis.RedisUserLoginUtils; | 
| import com.matrix.system.common.constance.AppConstance; | 
|   | 
| 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 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 org.apache.commons.collections.CollectionUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Controller; | 
| 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) | 
| @Controller | 
| @RequestMapping(value = "wxapi/ShoppingCart") | 
| public class WxShoppingCartAction { | 
|     @Autowired | 
|     private ShopShoppingCartDao shoppingCartDao; | 
|     @Autowired | 
|     private RedisUserLoginUtils redisUserLoginUtils; | 
|   | 
|     @Autowired | 
|     WxShopCouponService shopCouponService; | 
|   | 
|     @Autowired | 
|     ShoppingCartService shoppingCartService; | 
|   | 
|     @Autowired | 
|     ShopSkuDao skuDao; | 
|   | 
|   | 
|     /** | 
|      * 根据ID删除购物车 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @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, "删除成功"); | 
|     } | 
|   | 
|     /** | 
|      * 根据用户ID查询购物车 没有分页 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @PostMapping("/findShoppingCart") | 
|     @ResponseBody | 
|     public AjaxResult getShoppingCartByUserId(@RequestBody ShopShoppingCart shoppingCart) { | 
|         List<ShopCartVo> cartList = shoppingCartService.findUserCartList(shoppingCart.getShopId()); | 
|         AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, cartList, cartList.size()); | 
|         ShopCartBillVo shopCartBill = shoppingCartService.buildShopCartBillVo(cartList); | 
|         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; | 
|     } | 
|   | 
|     /** | 
|      * 批量删除 | 
|      */ | 
|     @PostMapping(value = "/delShoppingCart/{keys}") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult del(@PathVariable("keys") String keys) { | 
|         List<String> 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); | 
|         } | 
|     } | 
|   | 
|     @PostMapping(value = "/getUserCartCount/{shopId}") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult getUserCartCount(@PathVariable("shopId") Long shopId) { | 
|         BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); | 
|         Integer userCartCount = shoppingCartDao.selectUserCartCount(shopId, loginUser.getOpenId()); | 
|         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()); | 
|   | 
|         BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); | 
|         shoppingCart.setCreateBy(loginUser.getOpenId()); | 
|         shoppingCart.setUpdateBy(loginUser.getOpenId()); | 
|         shoppingCart.setCartUserId(loginUser.getOpenId()); | 
|         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.getOpenId()); | 
|         shopShoppingCart.setShopId(shoppingCart.getShopId()); | 
|         List<ShopShoppingCart> shopShoppingCarts = shoppingCartDao.selectByModel(shopShoppingCart); | 
|         int i = 0; | 
|         if (CollectionUtils.isNotEmpty(shopShoppingCarts)) { | 
|             Map<String, Object> 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.getOpenId()); | 
|         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<String, Object> 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 | 
|      */ | 
|     @RequestMapping("/updateCartSelected") | 
|     @ResponseBody | 
|     public AjaxResult updateCartSelected(@RequestBody ShopShoppingCart shoppingCart) { | 
|         Map<String, Object> modifyMap = new HashMap<>(); | 
|         modifyMap.put("isSelected", shoppingCart.getIsSelected()); | 
|         modifyMap.put("cartId", shoppingCart.getCartId()); | 
|         shoppingCartDao.updateByMap(modifyMap); | 
|         return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功"); | 
|     } | 
|   | 
|     /** | 
|      * 修改购物车产品选中状态 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @RequestMapping("/updateCartAllSelected/{shopId}/{isSelected}") | 
|     @ResponseBody | 
|     public AjaxResult updateCartAllSelected(@PathVariable("isSelected") Integer isSelected, | 
|                                             @PathVariable("shopId") Long shopId) { | 
|         Map<String, Object> modifyMap = new HashMap<>(); | 
|         BizUser user = redisUserLoginUtils.getLoginUser(BizUser.class); | 
|         shoppingCartDao.updateAllSelected(user.getOpenId(),shopId, isSelected); | 
|         return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功"); | 
|     } | 
| } |