jyy
2021-03-19 c16b9ffceac828840ef7ebc4827612d17906704a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.matrix.system.shopXcx.api.action;
 
import com.matrix.biz.bean.BizUser;
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.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;
 
 
 
    @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) {
        List<ShopCartVo> cartList = shoppingCartService.findUserCartList(shoppingCart.getShopId(),ShopShoppingCart.CAR_TYPE_MICRO);
        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;
    }
 
 
    @ApiOperation(value = "批量删除", notes = "")
    @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);
        }
    }
 
    @ApiOperation(value = "查询微商城购物车数量", notes = "")
    @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());
        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.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
     */
    @PostMapping("/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
     */
    @PostMapping("/updateCartAllSelected/{shopId}/{isSelected}")
    @ResponseBody
    public AjaxResult updateCartAllSelected(@PathVariable("isSelected") Integer isSelected,
                                            @PathVariable("shopId") Long shopId) {
        BizUser user = redisUserLoginUtils.getLoginUser(BizUser.class);
        shoppingCartDao.updateAllSelected(user.getOpenId(),shopId, isSelected);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
    }
}