Pu Zhibing
2025-02-08 d94776ba25ffcd19cdd7970048ed88d9212bd394
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package com.ruoyi.other.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.constant.TokenConstants;
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.api.vo.GetGoodsBargainPrice;
import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
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.*;
import com.ruoyi.other.util.GeodesyUtil;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.system.api.model.LoginUser;
import org.jetbrains.annotations.NotNull;
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.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * <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 GoodsShopService goodsShopService;
    @Resource
    private GoodsAppUserService goodsAppUserService;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private GoodsAreaService goodsAreaService;
    @Resource
    private RemoteOrderGoodsClient remoteOrderGoodsClient;
    @Resource
    private SeckillActivityInfoService seckillActivityInfoService;
    @Resource
    private GoodsSeckillService goodsSeckillService;
    @Resource
    private GoodsBargainPriceService goodsBargainPriceService;
    @Resource
    private GoodsBargainPriceDetailService goodsBargainPriceDetailService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private GoodsEvaluateService goodsEvaluateService;
 
 
 
 
 
    @Override
    public PageInfo<GoodsVO> goodsList(Goods search) {
        Integer vipId = 0;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        }
        List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(), vipId);
        for (GoodsVO goods : list) {
            Price price = getPrice(vipId, goods.getGoodsId(), null, 1, provinceCode, cityCode, districtCode);
            if(null != price){
                goods.setCashPayment(price.getCashPayment() ? 1 : 0);
                goods.setPointPayment(price.getPointPayment() ? 1 : 0);
                goods.setSellingPrice(price.getCash());
                goods.setIntegral(price.getPoint());
            }
            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, Integer shopId, String longitude, String latitude) {
        if (goodsId == null || goodsId <= 0) {
            throw new NullPointerException("商品ID不能为空");
        }
 
        Integer vipId = 0;
        String provinceCode = null;
        String cityCode = null;
        String districtCode = null;
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        }
 
        Goods goods = this.getById(goodsId);
        if(null == goods || goods.getDelFlag() == 1){
            throw new RuntimeException("商品不存在");
        }
        if(goods.getStatus() == 1){
            throw new RuntimeException("商品已被下架");
        }
        if(!goods.getCommodityAuthority().contains("-1") && !goods.getCommodityAuthority().contains(vipId.toString())){
            throw new RuntimeException("权限不足");
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyBeanProp(goodsVO, goods);
        goodsVO.setGoodsId(goods.getId());
        goodsVO.setGoodsName(goods.getName());
        Price price = getPrice(vipId, goods.getId(), shopId, 1, provinceCode, cityCode, districtCode);
        if(null != price){
            goodsVO.setPointPayment(price.getPointPayment() ? 1 : 0);
            goodsVO.setCashPayment(price.getCashPayment() ? 1 : 0);
            goodsVO.setSellingPrice(price.getCash());
            goodsVO.setIntegral(price.getPoint());
        }
 
        if(goods.getType() == 1){
            if(goods.getAppointStore() == 2){
                List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
                        .in(Shop::getDelFlag, 0));
                goodsVO.setShopList(shopList);
            }else{
                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);
                }
            }
        }else{
            List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
                    .in(Shop::getDelFlag, 0));
            goodsVO.setShopList(shopList);
        }
        if(StringUtils.isNotEmpty(longitude) && StringUtils.isNotEmpty(latitude)){
            List<Shop> shopList = goodsVO.getShopList();
            for (Shop shop : shopList) {
                Double wgs84 = GeodesyUtil.getDistance(longitude + "," + latitude, shop.getLongitude() + "," + shop.getLatitude()).get("WGS84");
                shop.setDistance(wgs84);
            }
            shopList.sort(new Comparator<Shop>() {
                @Override
                public int compare(Shop o1, Shop o2) {
                    return o1.getDistance().compareTo(o2.getDistance());
                }
            });
            goodsVO.setShopList(shopList);
        }
        Integer integer = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(integer);
        GoodsEvaluate goodsEvaluate = goodsEvaluateService.lambdaQuery().eq(GoodsEvaluate::getGoodsId, goodsId).ne(GoodsEvaluate::getComment, "")
                .orderByDesc(GoodsEvaluate::getGrade).eq(GoodsEvaluate::getStatus, 2).isNotNull(GoodsEvaluate::getComment)
                .eq(GoodsEvaluate::getDelFlag, 0).last(" limit 0, 1").one();
        if (null != goodsEvaluate) {
            AppUser appUserById = appUserClient.getAppUserById(goodsEvaluate.getAppUserId());
            if(null != appUserById){
                goodsEvaluate.setUserName(appUserById.getName());
                goodsEvaluate.setAvatar(appUserById.getAvatar());
                goodsEvaluate.setIdStr(String.valueOf(goodsEvaluate.getId()));
            }
        }
        goodsVO.setGoodsEvaluate(goodsEvaluate);
        return goodsVO;
    }
 
 
    @Override
    public List<Goods> getGoodsListByShopId(PageInfo<Goods> pageInfo, Integer shopId) {
        Integer vipId = 0;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        }
        List<Goods> goods = goodsMapper.selectListByShopId(pageInfo, shopId, vipId);
        for (Goods good : goods) {
            Price price = getPrice(vipId, good.getId(), shopId, 1, provinceCode, cityCode, districtCode);
            if(null != price){
                good.setCashPayment(price.getCashPayment() ? 1 : 0);
                good.setPointPayment(price.getPointPayment() ? 1 : 0);
                good.setSellingPrice(price.getCash());
                good.setIntegral(price.getPoint());
            }
            Integer data = orderClient.getGoodsSaleNum(good.getId(), 1).getData();
            good.setSaleNum(data);
        }
        return goods;
    }
 
    @Override
    public IPage<Goods> getManageGoodsList(Page<Goods> page, Goods goods) {
        IPage<Goods> goodsIPage = goodsMapper.selectManageGoodsList(page, goods);
        goodsIPage.getRecords().forEach(goods1 -> {
            Integer data = orderClient.getGoodsSaleNum(goods1.getId(), 1).getData();
            goods1.setSaleNum(data);
        });
        return goodsIPage;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoods(Goods goods) {
        goods.setSaleNum(0);
        goods.setStatus(GoodsStatus.DOWN.getCode());
        goodsMapper.insert(goods);
 
        // 指定门店
        Integer appointStore = goods.getAppointStore();
        if (null != appointStore && appointStore == 1){
            List<GoodsShop> goodsShopList = goods.getGoodsShopList();
            if (CollectionUtils.isEmpty(goodsShopList)){
                throw new NullPointerException("请选择指定门店");
            }
            saveGoodsShopList(goodsShopList, goods.getId());
        }
 
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
        if (!CollectionUtils.isEmpty(goodsAppUserList)){
            saveGoodsAppUserList(goodsAppUserList, goods.getId());
        }
 
        // 会员价格配置
        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
        saveGoodsVipList(goodsVipList, goods.getId());
 
        // 特殊地区售价设置
        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
        if (!CollectionUtils.isEmpty(goodsAreaList)){
            saveGoodsAreaList(goodsAreaList, goods.getId());
        }
 
    }
 
    @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()));
        // 指定门店
        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
        saveGoodsShopList(goodsShopList, goods.getId());
 
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
        saveGoodsAppUserList(goodsAppUserList, goods.getId());
 
        // 会员价格配置
        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
        saveGoodsVipList(goodsVipList, goods.getId());
 
        // 特殊地区售价设置
        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
        saveGoodsAreaList(goodsAreaList, goods.getId());
    }
 
 
 
 
 
    private void saveGoodsAreaList(List<GoodsArea> goodsAreaList, Integer id) {
        goodsAreaService.remove(new LambdaQueryWrapper<GoodsArea>()
                .eq(GoodsArea::getGoodsId, id));
        if (!CollectionUtils.isEmpty(goodsAreaList)){
            for (GoodsArea goodsArea : goodsAreaList) {
                List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
                List<GoodsArea> goodsAreaList2 = new ArrayList<>();
                for (GoodsArea area : goodsAreaList1) {
                    area.setId(null);
                    area.setGoodsId(id);
                    area.setProvince(goodsArea.getProvince());
                    area.setCity(goodsArea.getCity());
                    area.setDistricts(goodsArea.getDistricts());
                    area.setProvinceCode(goodsArea.getProvinceCode());
                    area.setCityCode(goodsArea.getCityCode());
                    area.setDistrictsCode(goodsArea.getDistrictsCode());
//                    if(null != area.getSellingPrice() || null != area.getIntegral()){
//                        goodsAreaList2.add(area);
//                    }
                    goodsAreaList2.add(area);
                }
                goodsAreaService.saveBatch(goodsAreaList2);
            }
        }
    }
 
    private void saveGoodsVipList(List<GoodsVip> goodsVipList, Integer id) {
        goodsVipService.remove(new LambdaQueryWrapper<GoodsVip>()
                .eq(GoodsVip::getGoodsId, id));
        goodsVipList.forEach(item -> {
            item.setId(null);
            item.setGoodsId(id);
        });
        goodsVipService.saveBatch(goodsVipList);
    }
 
    private void saveGoodsAppUserList(List<GoodsAppUser> goodsAppUserList, Integer id) {
        goodsAppUserService.remove(new LambdaQueryWrapper<GoodsAppUser>()
                .eq(GoodsAppUser::getGoodsId, id));
        if (!CollectionUtils.isEmpty(goodsAppUserList)){
            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
                goodsAppUser.setId(null);
                goodsAppUser.setGoodsId(id);
            }
        }
        goodsAppUserService.saveBatch(goodsAppUserList);
    }
 
    private void saveGoodsShopList(List<GoodsShop> goodsShopList, Integer goodsId) {
        goodsShopService.remove(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
        if (!CollectionUtils.isEmpty(goodsShopList)){
            for (GoodsShop goodsShop : goodsShopList) {
                goodsShop.setGoodsId(goodsId);
                goodsShop.setId(null);
            }
        }
        goodsShopService.saveBatch(goodsShopList);
    }
 
    @Override
    public Goods getManageGoodsDetail(Long goodsId) {
        Goods goods = getById(goodsId);
        if (goods == null){
            return null;
        }
        // 指定门店
        List<GoodsShop> goodsShops = goodsShopService.list(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
 
        for (GoodsShop goodsShop : goodsShops) {
            Shop shop = shopMapper.selectById(goodsShop.getShopId());
            if(null != shop){
                goodsShop.setShopName(shop.getName());
                goodsShop.setOwnerName(shop.getShopManager());
                goodsShop.setPhone(shop.getPhone());
                goodsShop.setAddress(shop.getAddress());
            }
        }
        goods.setGoodsShopList(goodsShops);
 
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goodsAppUserService.list(new LambdaQueryWrapper<GoodsAppUser>()
                .eq(GoodsAppUser::getGoodsId, goodsId));
        List<Long> userIds = goodsAppUserList.stream().map(GoodsAppUser::getAppUserId).collect(Collectors.toList());
        List<AppUser> appUsers = appUserClient.listByIds(userIds);
        for (GoodsAppUser goodsAppUser : goodsAppUserList) {
            appUsers.stream().filter(u -> u.getId().equals(goodsAppUser.getAppUserId())).findFirst().ifPresent( u -> {
                goodsAppUser.setAppUserIdStr(goodsAppUser.getAppUserId().toString());
                goodsAppUser.setUserName(u.getName());
                goodsAppUser.setPhone(u.getPhone());
                VipSetting vipSetting = vipSettingService.getById(u.getVipId());
                if (vipSetting != null){
                    goodsAppUser.setVipName(vipSetting.getVipName());
                    goodsAppUser.setVipId(u.getVipId());
                }
            });
        }
 
        goods.setGoodsAppUserList(goodsAppUserList);
 
        // 会员价格
        List<GoodsVip> goodsVipList = goodsVipService.list(new LambdaQueryWrapper<GoodsVip>()
                .eq(GoodsVip::getGoodsId, goodsId));
        List<VipSetting> vipSettings = getVipSettings(goodsVipList.stream().map(GoodsVip::getVip));
        goodsVipList.forEach(goodsArea -> vipSettings.stream()
                .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
                .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
 
        goods.setGoodsVipList(goodsVipList);
 
        // 特殊地区售价设置
        List<GoodsArea> goodsAreaList = goodsAreaService.list(new LambdaQueryWrapper<GoodsArea>()
                .eq(GoodsArea::getGoodsId, goodsId));
 
        Map<String, List<GoodsArea>> listMap = goodsAreaList.stream()
                .collect(Collectors.groupingBy(goodsArea ->
                        goodsArea.getProvinceCode() + goodsArea.getCityCode() + goodsArea.getDistrictsCode()));
 
        List<GoodsArea> goodsAreaList1 = new ArrayList<>();
        listMap.forEach((s, goodsAreas) -> {
 
            List<VipSetting> vipSettings1 = getVipSettings(goodsAreas.stream().map(GoodsArea::getVip));
            goodsAreas.forEach(goodsArea -> vipSettings1.stream()
                    .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
                    .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
 
            GoodsArea goodsArea1 = getGoodsArea(goodsAreas);
            goodsAreaList1.add(goodsArea1);
        });
 
        goods.setGoodsAreaList(goodsAreaList1);
        return goods;
    }
 
    private List<VipSetting> getVipSettings(Stream<Integer> goodsVipList) {
        List<Integer> vipIds = goodsVipList.collect(Collectors.toList());
        if (CollectionUtils.isEmpty(vipIds)){
            return new ArrayList<>();
        }
        return vipSettingService.listByIds(vipIds);
    }
 
    @NotNull
    private static GoodsArea getGoodsArea(List<GoodsArea> goodsAreas) {
        GoodsArea goodsArea = goodsAreas.get(0);
        GoodsArea goodsArea1 = new GoodsArea();
        goodsArea1.setProvinceCode(goodsArea.getProvinceCode());
        goodsArea1.setCityCode(goodsArea.getCityCode());
        goodsArea1.setDistrictsCode(goodsArea.getDistrictsCode());
        goodsArea1.setProvince(goodsArea.getProvince());
        goodsArea1.setCity(goodsArea.getCity());
        goodsArea1.setDistricts(goodsArea.getDistricts());
        goodsArea1.setGoodsAreaList(goodsAreas);
        return goodsArea1;
    }
 
 
    /**
     * 根据商品的价格配置体系获取商品当前的价格
     * @param vip
     * @param goodsId
     * @param type          1普通商品,2秒杀商品
     * @param shopId
     * @param provinceCode
     * @param cityCode
     * @param districtCode
     * @return
     */
    public Price getPrice(Integer vip, Integer goodsId, Integer shopId, Integer type, String provinceCode, String cityCode, String districtCode){
        //获取支付价格
        //秒杀活动>门店特价>地区价格>会员价格
        //判断是否有秒杀活动
        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 and FIND_IN_SET(" + vip + ", vip_ids) 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()).eq(GoodsSeckill::getVip, vip));
        }
        //没有秒杀活动或者添加的普通商品则不使用秒杀活动价格
        if(null == goodsSeckill || type == 1){
            //没有秒杀价,则判断门店特价
            GoodsBargainPriceDetail bargainPriceDetail = null;
            if (shopId != null){
                GoodsBargainPrice priceServiceOne = goodsBargainPriceService.getOne(new LambdaQueryWrapper<GoodsBargainPrice>().eq(GoodsBargainPrice::getGoodsId, goodsId).eq(GoodsBargainPrice::getShopId, shopId)
                        .eq(GoodsBargainPrice::getDelFlag, 0).eq(GoodsBargainPrice::getAuditStatus, 1).last(" order by create_time desc limit 0, 1"));
                if(null != priceServiceOne){
                    bargainPriceDetail = goodsBargainPriceDetailService.getOne(new LambdaQueryWrapper<GoodsBargainPriceDetail>().eq(GoodsBargainPriceDetail::getGoodsBargainPriceId, priceServiceOne.getId()).eq(GoodsBargainPriceDetail::getVip, vip));
                }
            }
            if(null == bargainPriceDetail){
                //没有门店特价,判断地区价格配置
                LambdaQueryWrapper<GoodsArea> queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                if(StringUtils.isNotEmpty(provinceCode)){
                    queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                }
                if(StringUtils.isNotEmpty(cityCode)){
                    queryWrapper.eq(GoodsArea::getCityCode, cityCode);
                }
                if(StringUtils.isNotEmpty(districtCode)){
                    queryWrapper.eq(GoodsArea::getDistrictsCode, districtCode);
                }
                GoodsArea goodsArea = goodsAreaService.getOne(queryWrapper);
                if(goodsArea == null){
                    queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                    if(StringUtils.isNotEmpty(provinceCode)){
                        queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                    }
                    if(StringUtils.isNotEmpty(cityCode)){
                        queryWrapper.eq(GoodsArea::getCityCode, cityCode);
                    }
                    goodsArea = goodsAreaService.getOne(queryWrapper);
                }
                if(goodsArea == null){
                    queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                    if(StringUtils.isNotEmpty(provinceCode)){
                        queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                    }
                    goodsArea = goodsAreaService.getOne(queryWrapper);
                }
                if(null == goodsArea || (null == goodsArea.getCashPayment() && null == goodsArea.getPointPayment())){
                    //没有地区价格,则使用会员价格
                    GoodsVip goodsVip = goodsVipService.getOne(new LambdaQueryWrapper<GoodsVip>().eq(GoodsVip::getGoodsId, goodsId).eq(GoodsVip::getVip, vip));
                    if(null == goodsVip || (null == goodsVip.getCashPayment() && null == goodsVip.getPointPayment())){
                        //没有配置价格,直接使用原始基础价格
                        return null;
                    }else{
                        //构建价格数据
                        if(goodsVip.getCashPayment() == 1 && goodsVip.getPointPayment() == 1){
                            price.setCash(goodsVip.getSellingPrice());
                            price.setPoint(goodsVip.getIntegral());
                        }
                        if(goodsVip.getCashPayment() == 1 && goodsVip.getPointPayment() == 0){
                            price.setCash(goodsVip.getSellingPrice());
                        }
                        if(goodsVip.getCashPayment() == 0 && goodsVip.getPointPayment() == 1){
                            price.setPoint(goodsVip.getIntegral());
                        }
                        price.setCashPayment(goodsVip.getCashPayment() == 1);
                        price.setPointPayment(goodsVip.getPointPayment() == 1);
                    }
                }else{
                    //构建价格数据
                    if(goodsArea.getCashPayment() == 1 && goodsArea.getPointPayment() == 1){
                        price.setCash(goodsArea.getSellingPrice());
                        price.setPoint(goodsArea.getIntegral());
                    }
                    if(goodsArea.getCashPayment() == 1 && goodsArea.getPointPayment() == 0){
                        price.setCash(goodsArea.getSellingPrice());
                    }
                    if(goodsArea.getCashPayment() == 0 && goodsArea.getPointPayment() == 1){
                        price.setPoint(goodsArea.getIntegral());
                    }
                    price.setCashPayment(goodsArea.getCashPayment() == 1);
                    price.setPointPayment(goodsArea.getPointPayment() == 1);
                }
            }else{
                price.setCash(bargainPriceDetail.getSellingPrice());
                price.setPoint(bargainPriceDetail.getIntegral());
                price.setCashPayment(bargainPriceDetail.getSellingPrice() != null);
                price.setPointPayment(bargainPriceDetail.getIntegral() != null);
            }
        }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);
            price.setPointPayment(goodsSeckill.getPointPayment() == 1);
        }
        return price;
    }
 
}