huliguo
2025-04-23 f2070facdb5715e7349df69cfe257289c680d292
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
package com.ruoyi.other.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.order.feignClient.OrderClient;
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
import com.ruoyi.order.vo.Price;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.dto.AddGoodsDTO;
import com.ruoyi.other.enums.GoodsStatus;
import com.ruoyi.other.mapper.GoodsMapper;
import com.ruoyi.other.mapper.GoodsShopMapper;
import com.ruoyi.other.mapper.ShopMapper;
import com.ruoyi.other.service.*;
import com.ruoyi.other.util.GeodesyUtil;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.other.vo.NearbyShopVO;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.feignClient.SysConfigClient;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
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 AppUserClient appUserClient;
    @Resource
    private GoodsShopMapper goodsShopMapper;
    @Resource
    private GoodsShopService goodsShopService;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private RemoteOrderGoodsClient remoteOrderGoodsClient;
    @Resource
    private SeckillActivityInfoService seckillActivityInfoService;
    @Resource
    private GoodsSeckillService goodsSeckillService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private SysConfigClient  sysConfigClient;
    @Resource
    private ShopService shopService;
 
 
    /**
     * 热门商品列表
     * @param search
     * @return
     */
 
    @Override
    public PageInfo<GoodsVO> goodsList(Goods search) {
        List<Long> shopIds=null;
        if (null!=search.getLongitude()&&null!=search.getLatitude()){
            //获取最近十家店的id
            List<NearbyShopVO> nearbyShopVOS = shopService.nearbyShopList(search.getLongitude(), search.getLatitude(), new Shop());
            shopIds = nearbyShopVOS.subList(0, 10).stream().map(NearbyShopVO::getId).collect(Collectors.toList());
        }
        //查找满足条件的商品  分类、名称、附近十家店
        List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(),shopIds);
        //去重
        Map<Integer, GoodsVO> uniqueGoodsMap = new LinkedHashMap<>();
        for (GoodsVO goods : list) {
            uniqueGoodsMap.putIfAbsent(goods.getGoodsId(), goods);
        }
        list = new ArrayList<>(uniqueGoodsMap.values());
        for (GoodsVO goods : list) {
 
            //计算所需价格和积分
            Price price = getPrice( goods.getGoodsId());
            if(null != price){
                //秒杀活动
                goods.setSellingPrice(price.getCash());
                goods.setIntegral(price.getPoint());
                goods.setStartTime(price.getStartTime());
                goods.setEndTime(price.getEndTime());
                goods.setPurchaseLimit(price.getPurchaseLimit());
            }
            //已售数量
            Integer data = orderClient.getGoodsSaleNum(goods.getGoodsId(), 1).getData();
            goods.setSaleNum(data);
        }
        //手动排序
        if(StringUtils.isNotEmpty(search.getOrderByColumn())){
            if("tgs.selling_price".equals(search.getOrderByColumn())){//价格排序
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        if(null != o1.getSellingPrice() && null != o2.getSellingPrice()){
                            return o1.getSellingPrice().compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null == o1.getSellingPrice() && null != o2.getSellingPrice()){
                            return BigDecimal.ZERO.compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null != o1.getSellingPrice() && null == o2.getSellingPrice()){
                            return o1.getSellingPrice().compareTo(BigDecimal.ZERO) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        return 0;
                    }
                });
            }
            if("integral".equals(search.getOrderByColumn())){//积分排序
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        if(null != o1.getIntegral() && null != o2.getIntegral()){
                            return o1.getIntegral().compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null == o1.getIntegral() && null != o2.getIntegral()){
                            return Integer.valueOf(0).compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null != o1.getIntegral() && null == o2.getIntegral()){
                            return o1.getIntegral().compareTo(Integer.valueOf(0)) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        return 0;
                    }
                });
            }
            if("sale_num".equals(search.getOrderByColumn())){//销量
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        return o1.getSaleNum().compareTo(o2.getSaleNum()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                    }
                });
            }
        }
        // 手动处理分页
        PageInfo<GoodsVO> pageInfo = new PageInfo<>();
        pageInfo.setSize(search.getPageSize());
        pageInfo.setTotal(list.size());
 
        int pageSize = search.getPageSize();
        int pageCurr = search.getPageCurr();
 
        // 计算起始索引
        int pageNum = (pageCurr - 1) * pageSize;
 
        // 检查 pageNum 是否合法
        if (pageNum < 0 || pageNum >= list.size()) {
            // 如果 pageNum 不合法,返回空列表
            pageInfo.setRecords(new ArrayList<>());
            return pageInfo;
        }
 
        // 计算结束索引
        int endIndex = Math.min(pageNum + pageSize, list.size());
 
        // 获取分页数据
        List<GoodsVO> pageList = list.subList(pageNum, endIndex);
 
        // 设置分页结果
        pageInfo.setRecords(pageList);
        return pageInfo;
    }
 
    @Override
    public GoodsVO goodsDetail(Long goodsId) {
        if (goodsId == null || goodsId <= 0) {
            throw new NullPointerException("商品ID不能为空");
        }
 
        Goods goods = this.getById(goodsId);
 
        if(null == goods || goods.getDelFlag() == 1){
            throw new RuntimeException("商品不存在");
        }
        if(goods.getStatus() == 1){
            throw new RuntimeException("商品已被下架");
        }
 
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyBeanProp(goodsVO, goods);
        goodsVO.setGoodsId(goods.getId());
        goodsVO.setGoodsName(goods.getName());
 
        //计算所需价格和积分
        Price price = getPrice( goods.getId());
        if(null != price){
            //秒杀活动
            goodsVO.setSellingPrice(price.getCash());
            goodsVO.setIntegral(price.getPoint());
            goodsVO.setStartTime(price.getStartTime());
            goodsVO.setEndTime(price.getEndTime());
            goodsVO.setPurchaseLimit(price.getPurchaseLimit());
        }
        //已售数量
        Integer data = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(data);
        //一个商品对应一个门店
        //查找门店
        GoodsShop goodsShop = goodsShopMapper.selectOne(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
        Shop shop1 = shopMapper.selectById(goodsShop.getShopId());
 
        goodsVO.setShop(shop1);
        //已售数量
        Integer integer = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(integer);
 
        return goodsVO;
 
    }
 
 
    @Override
    public List<GoodsVO> getGoodsListByShopId(PageInfo<GoodsVO> pageInfo, Integer shopId) {
        //查询该门店商品
        List<GoodsVO> goods = goodsMapper.selectListByShopId(pageInfo, shopId);
        if (goods == null) {
            return null;
        }
        for (GoodsVO good : goods) {
            //价格
            Price price = getPrice( good.getGoodsId());
            if(null != price){
                //秒杀活动
                good.setSellingPrice(price.getCash());
                good.setIntegral(price.getPoint());
                good.setPurchaseLimit(price.getPurchaseLimit());
                good.setStartTime(price.getStartTime());
                good.setEndTime(price.getEndTime());
            }
            Integer data = orderClient.getGoodsSaleNum(good.getGoodsId(), 1).getData();
            good.setSaleNum(data);
        }
        return goods;
    }
 
    @Override
    public R getManageGoodsList(Integer pageNum,Integer pageSize,  Goods goods) {
        PageInfo<Goods> pageInfo = new PageInfo<>(pageNum,pageSize);
        List<Goods> list = goodsMapper.selectManageGoodsList(pageInfo, goods);
        pageInfo.setRecords(list);
        return R.ok(pageInfo);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoods(Goods goods) {
 
        goodsMapper.insert(goods);
 
        //保存商品门店关系
        GoodsShop goodsShop = new GoodsShop();
        goodsShop.setGoodsId(goods.getId());
        goodsShop.setShopId(goods.getShopId());
        Shop shop = shopMapper.selectById(goods.getShopId());
        goodsShop.setShopName(shop.getName());
        goodsShop.setOwnerName(shop.getShopManager());
        goodsShop.setPhone(shop.getPhone());
        goodsShop.setAddress(shop.getAddress());
        goodsShopService.save(goodsShop);
 
        //判断是否参加秒杀活动
 
 
    }
 
    @Override
    public void updateManageGoods(Goods goods) {
        this.updateById(goods);
        //修改个别字段
        this.update(new LambdaUpdateWrapper<Goods>().eq(Goods::getId, goods.getId()).set(Goods::getSellingPrice, goods.getSellingPrice())
                .set(Goods::getIntegral, goods.getIntegral()));
        // 保存门店
        saveGoodsShop(goods.getShopId(),goods.getId());
    }
 
 
 
 
 
 
 
 
    private void saveGoodsShop(Integer shopId, Integer goodsId) {
        goodsShopService.remove(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
 
        GoodsShop goodsShop = new GoodsShop();
        goodsShop.setGoodsId(goodsId);
        goodsShop.setShopId(shopId);
        goodsShop.setId(null);
 
        goodsShopService.save(goodsShop);
    }
 
    @Override
    public Goods getManageGoodsDetail(Long goodsId) {
        Goods goods = getById(goodsId);
        if (goods == null){
            return null;
        }
        // 获取指定门店
        GoodsShop goodsShop = goodsShopService.getOne(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
        goods.setShopId(goodsShop.getShopId());
        Shop shop = shopService.getById(goodsShop.getShopId());
        if(null != shop){
            goodsShop.setShopName(shop.getName());
            goodsShop.setPhone(shop.getPhone());
            goodsShop.setAddress(shop.getAddress());
            goodsShop.setOwnerName(shop.getShopManager());
        }
        goods.setGoodsShop(goodsShop);
        return goods;
    }
 
 
 
 
    /**
     * 获取商品当前的价格,就是判断是否在秒杀活动中
     */
    public Price getPrice( Integer goodsId ){
        //判断是否有在秒杀活动时间中
        Price price = new Price();
        SeckillActivityInfo one = seckillActivityInfoService.getOne(new LambdaQueryWrapper<SeckillActivityInfo>().eq(SeckillActivityInfo::getGoodId, goodsId)
                .eq(SeckillActivityInfo::getIsShelves, 1).eq(SeckillActivityInfo::getDelFlag, 0)
                .last(" and now() between start_time and end_time order by create_time desc limit 0, 1"));
        GoodsSeckill goodsSeckill = null;//秒杀配置
        if(null != one){
            //有秒杀活动,查看秒杀价格
            goodsSeckill = goodsSeckillService.getOne(new LambdaQueryWrapper<GoodsSeckill>().eq(GoodsSeckill::getSeckillActivityInfoId, one.getId()));
        }
        //没有秒杀活动或者添加的普通商品则使用秒杀活动价格
        if(null == goodsSeckill ){
           return null;
        }
        //秒杀活动价格
        price.setCash(goodsSeckill.getSellingPrice());
        //计算对应积分
        price.setPoint(getPoint(price.getCash()));
        price.setStartTime(one.getStartTime());
        price.setEndTime(one.getEndTime());
        price.setPurchaseLimit(one.getMaxNum());
        return price;
    }
 
    /**
     * 获取现金对应积分
     */
    public Integer getPoint(BigDecimal cash){
        if (cash == null || cash.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("金额不能为null或负数");
        }
        // 获取积分兑换比例配置
        R<SysConfig> info = sysConfigClient.getInfo(6L);
        if (info == null || info.getData() == null) {
            throw new RuntimeException("获取积分兑换比例配置失败");
        }
        String configValue = info.getData().getConfigValue();
        if (StringUtils.isBlank(configValue)) {
            throw new RuntimeException("积分兑换比例配置值为空");
        }
        try {
            // 使用BigDecimal处理比例,避免精度问题
            BigDecimal ratio = new BigDecimal(configValue.trim());
            if (ratio.compareTo(BigDecimal.ZERO) <= 0) {
                throw new RuntimeException("积分兑换比例必须大于0");
            }
 
            // 计算积分并四舍五入取整
            return cash.multiply(ratio).intValue();
 
        } catch (NumberFormatException e) {
            throw new RuntimeException("积分兑换比例配置值格式错误", e);
        } catch (ArithmeticException e) {
            throw new RuntimeException("积分计算结果溢出", e);
        }
 
    }
 
}