Pu Zhibing
2024-11-29 2ccdf86ac2599562ca994bc4047ded2d925e8bae
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.ruoyi.order.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.UserAddressClient;
import com.ruoyi.account.api.feignClient.UserCouponClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserAddress;
import com.ruoyi.account.api.vo.CouponInfoVo;
import com.ruoyi.account.api.vo.PaymentUserCoupon;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.mapper.ShoppingCartMapper;
import com.ruoyi.order.service.OrderGoodService;
import com.ruoyi.order.service.OrderService;
import com.ruoyi.order.service.ShoppingCartService;
import com.ruoyi.order.vo.*;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.api.feignClient.*;
import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import model.Order;
import model.OrderGood;
import model.ShoppingCart;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    
    @Resource
    private TokenService tokenService;
    
    @Resource
    private GoodsClient goodsClient;
    
    @Resource
    private GoodsShopClient goodsShopClient;
    
    @Resource
    private AppUserClient appUserClient;
    
    @Resource
    private GoodsAreaClient goodsAreaClient;
    
    @Resource
    private GoodsVipClient goodsVipClient;
    
    @Resource
    private SeckillActivityInfoClient seckillActivityInfoClient;
    
    @Resource
    private GoodsBargainPriceClient goodsBargainPriceClient;
    
    @Resource
    private OrderService orderService;
    
    @Resource
    private OrderGoodService orderGoodService;
    
    @Resource
    private ShopClient shopClient;
    
    @Resource
    private OrderActivityInfoClient orderActivityInfoClient;
    
    @Resource
    private BaseSettingClient baseSettingClient;
    
    @Resource
    private UserAddressClient userAddressClient;
    
    @Resource
    private UserCouponClient userCouponClient;
    
    @Resource
    private SystemConfigClient systemConfigClient;
    
    
    
    
    
    
    /**
     * 获取购物车列表
     * @param type
     * @param shopId
     * @return
     */
    @Override
    public List<MyShoppingCartVo> getMyShoppingCart(Integer type, Integer shopId) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        AppUser appUser = appUserClient.getAppUserById(userid);
        //获取对应类型的商品数据
        List<Goods> data = goodsClient.getGoodsByType(type).getData();
        if(null == data){
            throw new RuntimeException("根据类型(1=服务商品,2=单品商品)获取商品数据失败");
        }
        List<Long> goodsIds = data.stream().map(Goods::getId).collect(Collectors.toList());
        //查询符合商品类型的商品数据
        List<ShoppingCart> list = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getAppUserId, userid).in(ShoppingCart::getGoodsId, goodsIds));
        //构建返回数据
        List<MyShoppingCartVo> page = buildDetail(appUser, shopId, list, null);
        return page;
    }
    
    
    /**
     * 获取支付价格
     * @param appUser
     * @param goodsId
     * @param shopId
     * @return
     */
    public Price getPrice(AppUser appUser, Integer goodsId, Integer shopId){
        //获取支付价格
        //秒杀活动>门店特价>地区价格>会员价格
        //判断是否有秒杀活动
        Price price = new Price();
        GetSeckillActivityInfo info = new GetSeckillActivityInfo();
        info.setGoodsId(goodsId);
        info.setVip(appUser.getVipId());
        GoodsSeckill goodsSeckill = seckillActivityInfoClient.getSeckillActivityInfo(info).getData();
        if(null == goodsSeckill){
            //没有秒杀价,则判断门店特价
            GetGoodsBargainPrice goodsBargainPrice = new GetGoodsBargainPrice();
            goodsBargainPrice.setGoodsId(goodsId);
            goodsBargainPrice.setVip(appUser.getVipId());
            goodsBargainPrice.setShopId(shopId);
            GoodsBargainPriceDetail bargainPriceDetail = goodsBargainPriceClient.getGoodsBargainPrice(goodsBargainPrice).getData();
            if(null == bargainPriceDetail){
                //没有门店特价,判断地区价格配置
                GoodsArea area = new GoodsArea();
                area.setDistrictsCode(appUser.getDistrictCode());
                area.setCityCode(appUser.getCityCode());
                area.setProvinceCode(appUser.getProvinceCode());
                area.setVip(appUser.getVipId());
                GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
                if(null == goodsArea){
                    //没有地区价格,则使用会员价格
                    GoodsVip goodsVip = goodsVipClient.getGoodsVip(appUser.getVipId()).getData();
                    if(null == goodsVip){
                        //没有配置价格,直接使用原始基础价格
                        return null;
                    }else{
                        price.setCash(goodsVip.getSellingPrice());
                        price.setPoint(goodsVip.getIntegral());
                        price.setCashPayment(goodsVip.getCashPayment() == 1 ? true : false);
                        price.setPointPayment(goodsVip.getPointPayment() == 1 ? true : false);
                        price.setEarnSpendingPoints(goodsVip.getEarnSpendingPoints());
                    }
                }else{
                    price.setCash(goodsArea.getSellingPrice());
                    price.setPoint(goodsArea.getIntegral());
                    price.setCashPayment(goodsArea.getCashPayment() == 1 ? true : false);
                    price.setPointPayment(goodsArea.getPointPayment() == 1 ? true : false);
                    price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
                }
            }else{
                price.setCash(bargainPriceDetail.getSellingPrice());
                price.setPoint(bargainPriceDetail.getIntegral());
                price.setCashPayment(bargainPriceDetail.getSellingPrice() != null ? true : false);
                price.setPointPayment(bargainPriceDetail.getIntegral() != null ? true : false);
                //门店特价,消费积分使用会员等级的消费积分
                GoodsArea area = new GoodsArea();
                area.setDistrictsCode(appUser.getDistrictCode());
                area.setCityCode(appUser.getCityCode());
                area.setProvinceCode(appUser.getProvinceCode());
                area.setVip(appUser.getVipId());
                GoodsArea goodsArea = goodsAreaClient.getGoodsArea(area).getData();
                price.setEarnSpendingPoints(goodsArea.getEarnSpendingPoints());
            }
        }else{
            //构建价格数据
            if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 1){
                price.setCash(goodsSeckill.getSellingPrice());
                price.setPoint(goodsSeckill.getIntegral());
            }
            if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 0){
                price.setCash(goodsSeckill.getSellingPrice());
            }
            if(goodsSeckill.getCashPayment() == 0 && goodsSeckill.getPointPayment() == 1){
                price.setPoint(goodsSeckill.getIntegral());
            }
            price.setCashPayment(goodsSeckill.getCashPayment() == 1 ? true : false);
            price.setPointPayment(goodsSeckill.getPointPayment() == 1 ? true : false);
            price.setEndTime(goodsSeckill.getEndTime());
            price.setEarnSpendingPoints(goodsSeckill.getEarnSpendingPoints());
        }
        return price;
    }
    
    
    @Data
    class Price {
        /**
         * 现金
         */
        private BigDecimal cash;
        /**
         * 积分
         */
        private Integer point;
        /**
         * 获取结束时间
         */
        private Long endTime;
        /**
         * 现金支付
         */
        private Boolean cashPayment;
        /**
         * 积分支付
         */
        private Boolean pointPayment;
        /**
         * 可获得消费积分
         */
        private Integer earnSpendingPoints;
    }
    
 
    @Override
    public void addGoods(ShoppingCart shoppingCart) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        shoppingCart.setAppUserId(userid);
        this.save(shoppingCart);
    }
    
    
    /**
     * 修改购物车数量
     * @param setGoodsNumber
     * @return
     */
    @Override
    public R setGoodsNumber(SetGoodsNumber setGoodsNumber) {
        if(0 >= setGoodsNumber.getNumber()){
            return R.fail("修改数量不能小于等于0");
        }
        ShoppingCart shoppingCart = this.getById(setGoodsNumber.getId());
        Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
        if(null != goods.getPurchaseLimit() && -1 != goods.getPurchaseLimit() && goods.getPurchaseLimit() < setGoodsNumber.getNumber()){
            return R.fail("修改数量不能大于限购数量");
        }
        if(null != shoppingCart){
            shoppingCart.setNumber(setGoodsNumber.getNumber());
            this.updateById(shoppingCart);
        }
        return R.ok();
    }
    
    
    /**
     * 确认购物车订单
     * @param confirmOrder
     * @return
     */
    @Override
    public ConfirmOrderVo confirmOrder(ConfirmOrder confirmOrder) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        AppUser appUser = appUserClient.getAppUserById(userid);
        Integer shopId = confirmOrder.getShopId();
        Shop shop = shopClient.getShopById(shopId).getData();
        String goodsJson = confirmOrder.getGoodsJson();
        List<Long> ids = new ArrayList<>();
        JSONArray objects = JSON.parseArray(goodsJson);
        for (int i = 0; i < objects.size(); i++) {
            Long id = objects.getJSONObject(i).getLong("id");
            ids.add(id);
        }
        List<ShoppingCart> list = this.listByIds(ids);
        ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
        //构建商品明细列表
        List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
        confirmOrderVo.setGoodsList(goodsList);
        confirmOrderVo.setShopId(confirmOrder.getShopId());
        confirmOrderVo.setShopName(shop.getName());
        //现金支付
        if(confirmOrder.getPaymentType() == 1){
            BigDecimal bigDecimal = goodsList.stream().map(MyShoppingCartVo::getCash).reduce(BigDecimal::add).get();
            confirmOrderVo.setOrderMoney(bigDecimal);
        }else{
            int sum = goodsList.stream().mapToInt(MyShoppingCartVo::getPoint).sum();
            confirmOrderVo.setOrderPoint(sum);
        }
        //查询当前是否有订单活动
        OrderActivityInfo orderActivityInfo = orderActivityInfoClient.getNowOrderActivityInfo(appUser.getVipId()).getData();
        BigDecimal orderMoney = confirmOrderVo.getOrderMoney();
        BigDecimal paymentMoney = orderMoney;
        //满XX才打折,只有现金才能优惠
        if(null != orderActivityInfo && confirmOrder.getPaymentType() == 1 && orderActivityInfo.getConditionAmount().compareTo(orderMoney) <= 0){
            confirmOrderVo.setActivityName(orderActivityInfo.getActivityName());
            paymentMoney = orderActivityInfo.getDiscount().divide(new BigDecimal(10)).multiply(orderMoney);
            confirmOrderVo.setDiscountAmount(orderMoney.subtract(paymentMoney).setScale(2, RoundingMode.HALF_EVEN));
        }
        BaseSetting baseSetting = baseSettingClient.getBaseSetting(4).getData();
        confirmOrderVo.setUseSimultaneously(baseSetting.getContent().equals("1") ? true : false);
        int earnPoint = goodsList.stream().mapToInt(MyShoppingCartVo::getEarnSpendingPoints).sum();
        confirmOrderVo.setEarnPoint(earnPoint);
        //支付金额,订单金额-订单优惠
        confirmOrderVo.setPayMoney(paymentMoney);
        confirmOrderVo.setResidualPoint(appUser.getLavePoint().intValue());
        //获取默认收货地址
        UserAddress userAddress = userAddressClient.getDefaultUserAddress(userid).getData();
        confirmOrderVo.setUserAddress(userAddress);
        confirmOrderVo.setPaymentType(confirmOrder.getPaymentType());
        //获取用户优惠券,用户全部优惠券,不能使用的需要标识出来置灰展示
        PaymentUserCoupon paymentUserCoupon = new PaymentUserCoupon();
        paymentUserCoupon.setUserId(userid);
        paymentUserCoupon.setOrderMoney(orderMoney);
        paymentUserCoupon.setType(confirmOrder.getType());
        List<CouponInfoVo> data = userCouponClient.getPaymentUserCoupon(paymentUserCoupon).getData();
        confirmOrderVo.setCoupon(data);
        //获取快递策略
        SystemConfig systemConfig = systemConfigClient.getSystemConfig(3).getData();
        JSONObject jsonObject = JSON.parseObject(systemConfig.getContent());
        confirmOrderVo.setExpressFee(jsonObject.getBigDecimal("expressFee"));
        List<Integer> vip = jsonObject.getJSONArray("vip").toList(Integer.class);
        //包邮条件(所有会员或者满足条件的会员)
        if(vip.get(0) == -1 || vip.contains(appUser.getVipId())){
            if(confirmOrder.getPaymentType() == 1){
                //现金支付,支付金额满足包邮条件
                BigDecimal cash = jsonObject.getBigDecimal("cash");
                if(confirmOrderVo.getPayMoney().compareTo(cash) >= 0){
                    confirmOrderVo.setExpressFee(BigDecimal.ZERO);
                }
            }else{
                //积分支付,支付积分是否满足包邮条件
                Integer point = jsonObject.getInteger("point");
                if(confirmOrderVo.getOrderPoint().compareTo(point) >= 0){
                    confirmOrderVo.setExpressFee(BigDecimal.ZERO);
                }
            }
        }
        return confirmOrderVo;
    }
    
    
    /**
     * 构建购物车商品列表
     * @param appUser
     * @param shopId
     * @param list
     * @param objects
     * @return
     */
    private List<MyShoppingCartVo> buildDetail(AppUser appUser, Integer shopId, List<ShoppingCart> list, JSONArray objects){
        List<MyShoppingCartVo> page = new ArrayList<>();
        for (ShoppingCart shoppingCart : list) {
            Goods goods = goodsClient.getGoodsById(shoppingCart.getGoodsId()).getData();
            MyShoppingCartVo vo = new MyShoppingCartVo();
            vo.setId(shoppingCart.getId());
            vo.setHomePicture(goods.getHomePagePicture());
            vo.setName(goods.getName());
            int num = shoppingCart.getNumber();
            if(null != objects){
                for (int i = 0; i < objects.size(); i++) {
                    Long id = objects.getJSONObject(i).getLong("id");
                    if(id.equals(shoppingCart.getId())){
                        num = objects.getJSONObject(i).getInteger("num");
                        break;
                    }
                }
            }
            //获取支付价格
            Price price = getPrice(appUser, shoppingCart.getGoodsId(), shopId);
            if(null == price){
                //使用商品的基础价格
                price.setCash(1 == goods.getCashPayment() ? goods.getSellingPrice() : null);
                price.setPoint(1 == goods.getPointPayment() ? goods.getIntegral() : null);
                price.setCashPayment(goods.getCashPayment() == 1 ? true : false);
                price.setPointPayment(goods.getPointPayment() == 1 ? true : false);
            }
            vo.setCash(price.getCash());
            vo.setPoint(price.getPoint());
            vo.setCashPayment(price.getCashPayment());
            vo.setPointPayment(price.getPointPayment());
            vo.setEndTime(price.getEndTime());
            vo.setOriginalPrice(goods.getOriginalPrice().toString());
            vo.setNumber(num);
            GoodsShop goodsShop = new GoodsShop();
            goodsShop.setGoodsId(shoppingCart.getGoodsId());
            goodsShop.setShopId(shopId);
            GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
            vo.setVerifiable(null == goodsShop1 ? false : true);
            //判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
            if(null == goods.getPurchaseLimit() || -1 == goods.getPurchaseLimit()){
                vo.setPurchaseLimit(false);
            }else{
                List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getAppUserId, appUser.getId()).eq(Order::getDelFlag, 0).in(Order::getOrderStatus, Arrays.asList(4, 8)));
                List<Long> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
                int sum = 0;
                if(orderIds.size() > 0){
                    List<OrderGood> orderGoodList = orderGoodService.list(new LambdaQueryWrapper<OrderGood>().in(OrderGood::getOrderId, orderIds)
                            .eq(OrderGood::getGoodsId, shoppingCart.getGoodsId()).eq(OrderGood::getDelFlag, 0));
                    sum = orderGoodList.stream().mapToInt(OrderGood::getNum).sum();
                }
                vo.setPurchaseLimit((num + sum) > goods.getPurchaseLimit() ? true : false);
            }
            vo.setEarnSpendingPoints(price.getEarnSpendingPoints());
            vo.setDistributionMode(goods.getDistributionMode());
            page.add(vo);
        }
        return page;
    }
    
    
    /**
     * 购物车支付操作
     * @param shoppingCartPayment
     * @return
     */
    @Override
    public R shoppingCartPayment(ShoppingCartPayment shoppingCartPayment) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        AppUser appUser = appUserClient.getAppUserById(userid);
        Integer shopId = shoppingCartPayment.getShopId();
        Shop shop = shopClient.getShopById(shopId).getData();
        String goodsJson = shoppingCartPayment.getGoodsJson();
        List<Long> ids = new ArrayList<>();
        JSONArray objects = JSON.parseArray(goodsJson);
        for (int i = 0; i < objects.size(); i++) {
            Long id = objects.getJSONObject(i).getLong("id");
            ids.add(id);
        }
        List<ShoppingCart> list = this.listByIds(ids);
        ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
        //构建商品明细列表
        List<MyShoppingCartVo> goodsList = buildDetail(appUser, shopId, list, objects);
        
        
        
        
        return null;
    }
}