phpcjl
2024-11-28 e81d5ff655e0c5efc85d2e4b3803bc604baf59f1
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
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.security.service.TokenService;
import com.ruoyi.order.mapper.ShoppingCartMapper;
import com.ruoyi.order.service.ShoppingCartService;
import com.ruoyi.order.vo.MyShoppingCartVo;
import com.ruoyi.other.api.domain.Goods;
import com.ruoyi.other.api.domain.GoodsArea;
import com.ruoyi.other.api.domain.GoodsShop;
import com.ruoyi.other.api.domain.GoodsVip;
import com.ruoyi.other.api.feignClient.GoodsAreaClient;
import com.ruoyi.other.api.feignClient.GoodsClient;
import com.ruoyi.other.api.feignClient.GoodsShopClient;
import com.ruoyi.other.api.feignClient.GoodsVipClient;
import model.ShoppingCart;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
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;
    
    
    
    
    
    
    /**
     * 获取购物车列表
     * @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());
            
            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();
            }
            
            
            vo.setSellingPrice(goods);
            vo.setOriginalPrice(goods.getOriginalPrice().toString());
            vo.setNumber(shoppingCart.getNumber());
            vo.setEndTime();
            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;
    }
 
    @Override
    public void addGoods(ShoppingCart shoppingCart) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        shoppingCart.setAppUserId(userid);
        this.save(shoppingCart);
    }
}