Pu Zhibing
2024-11-28 34ec63ac94b2b61bbdbc4b6a98a577efedbafda8
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
package com.ruoyi.order.service.impl;
 
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.model.AppUser;
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.ShoppingCartService;
import com.ruoyi.order.vo.MyShoppingCartVo;
import com.ruoyi.order.vo.SetGoodsNumber;
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 lombok.Data;
import model.ShoppingCart;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
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;
    
    
    
    
    
    
    /**
     * 获取购物车列表
     * @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 = new ArrayList<>();
        //构建返回数据
        for (ShoppingCart shoppingCart : list) {
            Goods goods = data.stream().filter(s -> s.getId().equals(shoppingCart.getGoodsId())).findFirst().get();
            MyShoppingCartVo vo = new MyShoppingCartVo();
            vo.setId(shoppingCart.getId());
            vo.setHomePicture(goods.getHomePagePicture());
            vo.setName(goods.getName());
            
            //获取支付价格
            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);
            }
            //构建价格展示内容
            String sellingPrice = "";
            if(null != price.getCash() && null != price.getPoint()){
                sellingPrice = price.getCash() + "或" + price.getPoint() + "积分";
            }
            if(null != price.getCash() && null == price.getPoint()){
                sellingPrice = price.getCash() + "";
            }
            if(null == price.getCash() && null != price.getPoint()){
                sellingPrice = price.getPoint() + "积分";
            }
            vo.setSellingPrice(sellingPrice);
            vo.setEndTime(price.getEndTime());
            vo.setOriginalPrice(goods.getOriginalPrice().toString());
            vo.setNumber(shoppingCart.getNumber());
            GoodsShop goodsShop = new GoodsShop();
            goodsShop.setGoodsId(shoppingCart.getGoodsId());
            goodsShop.setShopId(shopId);
            GoodsShop goodsShop1 = goodsShopClient.getGoodsShop(goodsShop).getData();
            vo.setVerifiable(null == goodsShop1 ? false : true);
            page.add(vo);
        }
        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());
                    }
                }else{
                    price.setCash(goodsArea.getSellingPrice());
                    price.setPoint(goodsArea.getIntegral());
                }
            }else{
                price.setCash(bargainPriceDetail.getSellingPrice());
                price.setPoint(bargainPriceDetail.getIntegral());
            }
        }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.setEndTime(goodsSeckill.getEndTime());
        }
        return price;
    }
    
    
    @Data
    class Price {
        /**
         * 现金
         */
        private BigDecimal cash;
        /**
         * 积分
         */
        private Integer point;
        /**
         * 获取结束时间
         */
        private Long endTime;
    }
    
 
    @Override
    public void addGoods(ShoppingCart shoppingCart) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        shoppingCart.setAppUserId(userid);
        this.save(shoppingCart);
    }
    
    
    /**
     * 修改购物车数量
     * @param setGoodsNumber
     * @return
     */
    @Override
    public AjaxResult setGoodsNumber(SetGoodsNumber setGoodsNumber) {
        if(0 >= setGoodsNumber.getNumber()){
            return AjaxResult.error("修改数量不能小于等于0");
        }
        ShoppingCart shoppingCart = this.getById(setGoodsNumber.getId());
        if(null != shoppingCart){
            shoppingCart.setNumber(setGoodsNumber.getNumber());
            this.updateById(shoppingCart);
        }
        return AjaxResult.success();
    }
}