1.
phpcjl
2024-12-10 071153064e6771e377ee148712be012dba9e13c5
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
package com.ruoyi.other.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.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
import com.ruoyi.order.vo.Price;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.enums.GoodsStatus;
import com.ruoyi.other.mapper.GoodsAreaMapper;
import com.ruoyi.other.mapper.GoodsMapper;
import com.ruoyi.other.mapper.GoodsShopMapper;
import com.ruoyi.other.mapper.ShopMapper;
import com.ruoyi.other.service.GoodsService;
import com.ruoyi.other.service.GoodsVipService;
import com.ruoyi.other.service.VipSettingService;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private TokenService tokenService;
    @Resource
    private VipSettingService vipSettingService;
    @Resource
    private GoodsAreaMapper goodsAreaMapper;
    @Resource
    private GoodsVipService goodsVipService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private GoodsShopMapper goodsShopMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private RemoteOrderGoodsClient remoteOrderGoodsClient;
 
    @Override
    public List<GoodsVO> goodsList(Goods search) {
        List<Goods> goodsList = this.list(new LambdaQueryWrapper<Goods>()
                .eq(Goods::getStatus, GoodsStatus.UP.getCode())
                .eq(Objects.nonNull(search.getGoodsCategoryId()), Goods::getGoodsCategoryId, search.getGoodsCategoryId())
                .like(StringUtils.isNotEmpty(search.getName()), Goods::getName, search.getName()));
 
        List<GoodsVO> result = new ArrayList<>();
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        for (Goods goods : goodsList) {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyBeanProp(goodsVO, goods);
            goodsVO.setGoodsId(goods.getId());
            goodsVO.setGoodsName(goods.getName());
            R<Price> r = remoteOrderGoodsClient.getGoodsPrice(loginUserApplet.getUserid(), goods.getId(), null);
            if (R.isSuccess(r)){
                Price price = r.getData();
                goodsVO.setSellingPrice(price.getCash());
                goodsVO.setIntegral(price.getPoint());
            }
            result.add(goodsVO);
        }
        return result;
    }
 
    @Override
    public GoodsVO goodsDetail(Long goodsId) {
        if (goodsId == null || goodsId <= 0) {
            throw new NullPointerException("商品ID不能为空");
        }
 
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        AppUser appUser = appUserClient.getAppUserById(loginUserApplet.getUserid());
        BigDecimal sellingPrice;
        Integer integral;
 
        GoodsArea goodsArea = goodsAreaMapper.selectOne(new LambdaQueryWrapper<GoodsArea>()
                .eq(GoodsArea::getGoodsId, goodsId)
                .eq(GoodsArea::getProvinceCode, appUser.getProvinceCode())
                .eq(StringUtils.isNotEmpty(appUser.getCityCode()), GoodsArea::getCityCode, appUser.getCityCode())
                .eq(StringUtils.isNotEmpty(appUser.getDistrictCode()), GoodsArea::getDistrictsCode, appUser.getDistrictCode()));
 
        if (Objects.nonNull(goodsArea)){
            sellingPrice = goodsArea.getSellingPrice();
            integral = goodsArea.getIntegral();
        }else {
            VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
            GoodsVip goodsVip = goodsVipService.getOne(new LambdaQueryWrapper<GoodsVip>()
                    .eq(GoodsVip::getVip, vipSetting.getId())
                    .eq(GoodsVip::getGoodsId, goodsId));
 
            sellingPrice = goodsVip.getSellingPrice();
            integral = goodsVip.getIntegral();
        }
 
        Goods goods = this.getById(goodsId);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyBeanProp(goodsVO, goods);
        goodsVO.setGoodsId(goods.getId());
        goodsVO.setGoodsName(goods.getName());
        goodsVO.setSellingPrice(sellingPrice);
        goodsVO.setIntegral(integral);
 
        List<GoodsShop> goodsShopList = goodsShopMapper.selectList(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
        if (!CollectionUtils.isEmpty(goodsShopList)){
            List<Integer> shopIds = goodsShopList.stream().map(GoodsShop::getShopId).collect(Collectors.toList());
            List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
                    .in(Shop::getId, shopIds));
 
            goodsVO.setShopList(shopList);
        }
        return goodsVO;
    }
 
 
    @Override
    public List<Goods> getGoodsListByShopId(Integer shopId) {
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
        return goodsMapper.selectListByShopId(shopId, vipSetting.getId());
    }
 
}