jiangqs
2023-06-27 046d7ac5bbef4cbb6c6b64f3589e2722d13a07a5
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
package com.ruoyi.goods.service.impl.goods;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.CodeFactoryUtil;
import com.ruoyi.goods.domain.dto.*;
import com.ruoyi.goods.domain.pojo.goods.GoodsTotal;
import com.ruoyi.goods.domain.vo.*;
import com.ruoyi.goods.mapper.goods.GoodsMapper;
import com.ruoyi.goods.service.activity.ActivityGoodsService;
import com.ruoyi.goods.service.goods.GoodsFileService;
import com.ruoyi.goods.service.goods.GoodsService;
import com.ruoyi.goods.service.goods.GoodsTotalService;
import com.ruoyi.goods.service.goods.ShopGoodsService;
import com.ruoyi.system.api.constant.AppErrorConstant;
import com.ruoyi.system.api.domain.poji.activity.ActivityGoods;
import com.ruoyi.system.api.domain.poji.config.SysClassification;
import com.ruoyi.system.api.domain.poji.goods.Goods;
import com.ruoyi.system.api.domain.poji.goods.GoodsFile;
import com.ruoyi.system.api.domain.poji.goods.ShopGoods;
import com.ruoyi.system.api.domain.vo.MgtSelectGoodsPageVo;
import com.ruoyi.system.api.service.RemoteConfigService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author jqs
 * @since 2023-04-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
 
    @Resource
    private GoodsMapper goodsMapper;
 
    @Resource
    private ActivityGoodsService activityGoodsService;
 
    @Resource
    private ShopGoodsService shopGoodsService;
 
    @Resource
    private GoodsFileService goodsFileService;
 
    @Resource
    private RemoteConfigService remoteConfigService;
 
    @Resource
    private GoodsTotalService goodsTotalService;
 
    
 
    /**
    * @description 获取推荐商品列表
    * @param page shopId
    * @return List<AppSimpleGoodsVo>
    * @author jqs34
    * @date 2023/6/4 16:27
    */
    @Override
    public List<AppSimpleGoodsVo> pageRecommendGoods(Page page, Long shopId){
        List<AppSimpleGoodsVo> appSimpleGoodsVoList = goodsMapper.pageRecommendGoods(page,shopId);
        return appSimpleGoodsVoList;
    }
 
    /**
     * 获取商城商品列表
     * @param page
     * @param appShopGoodsPageDto
     * @return
     */
    @Override
    public List<AppSimpleGoodsVo> pageShopGoods(Page page, AppShopGoodsPageDto appShopGoodsPageDto){
        List<AppSimpleGoodsVo> appSimpleGoodsVoList = goodsMapper.pageShopGoods(page,appShopGoodsPageDto);
        return appSimpleGoodsVoList;
    }
 
    /**
     * 获取搜索商品列表
     * @param page
     * @param appSearchGoodsPageDto
     * @return
     */
    @Override
    public List<AppSimpleGoodsVo> pageSearchGoods(Page page, AppSearchGoodsPageDto appSearchGoodsPageDto){
        List<AppSimpleGoodsVo> appSimpleGoodsVoList = goodsMapper.pageSearchGoods(page,appSearchGoodsPageDto);
        return appSimpleGoodsVoList;
    }
 
    /**
     * 获取搜索商品列表
     * @param page
     * @param appSearchGoodsPageDto
     * @return
     */
    @Override
    public List<AppSimpleActivityGoodsVo> pageSearchActivityGoods(Page page, AppSearchGoodsPageDto appSearchGoodsPageDto){
        List<AppSimpleActivityGoodsVo> appSimpleGoodsVoList = goodsMapper.pageSearchActivityGoods(page,appSearchGoodsPageDto);
        int activityNumber;
        int salesNumber;
        BigDecimal salesPercent;
        for(AppSimpleActivityGoodsVo appSimpleActivityGoodsVo : appSimpleGoodsVoList){
            activityNumber = appSimpleActivityGoodsVo.getActivityNumber();
            salesNumber = appSimpleActivityGoodsVo.getSalesNumber();
            salesPercent = BigDecimal.ZERO;
            if(activityNumber != 0){
                salesPercent = BigDecimal.valueOf(salesNumber).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(activityNumber), 2, BigDecimal.ROUND_HALF_UP);
            }
            appSimpleActivityGoodsVo.setSalesPercent(salesPercent);
        }
        return appSimpleGoodsVoList;
    }
 
    /**
     * 获取商品详情
     * @param appGoodsInfoGetDto
     * @return
     */
    @Override
    public AppGoodsInfoVo getGoodsInfo(AppGoodsInfoGetDto appGoodsInfoGetDto){
        AppGoodsInfoVo appGoodsInfoVo = new AppGoodsInfoVo();
        Goods goods = this.getById(appGoodsInfoGetDto.getId());
        if(goods.getGoodsStatus()==-1){
            throw new ServiceException(AppErrorConstant.GOODS_DELETED);
        }
        BeanUtils.copyProperties(goods, appGoodsInfoVo);
        //商户定制价格
        ShopGoods shopGoods = shopGoodsService.getByShopIdAndGoodsId(appGoodsInfoGetDto.getShopId(),goods.getGoodsId());
        if(shopGoods!=null){
            appGoodsInfoVo.setSalesPrice(shopGoods.getSalesPrice());
            appGoodsInfoVo.setSalesNum(shopGoods.getServiceNum());
        }
        //活动商品判断
        if(appGoodsInfoGetDto.getActivityFlag()==1){
            ActivityGoods activityGoods = activityGoodsService.getByGoodsId(goods.getGoodsId());
            //原价取较大值
            if(shopGoods!=null&&goods.getSalesPrice().compareTo(shopGoods.getSalesPrice())>0){
                appGoodsInfoVo.setSalesPrice(goods.getSalesPrice());
            }
            appGoodsInfoVo.setActivityFlag(1);
            appGoodsInfoVo.setActivityPrice(activityGoods.getActivityPrice());
            appGoodsInfoVo.setSalesNum(activityGoods.getSalesNumber());
            appGoodsInfoVo.setActivityDeadline(activityGoods.getActivityDeadline());
            //售罄判断
            if(activityGoods.getActivityNumber().equals(activityGoods.getSalesNumber())){
                appGoodsInfoVo.setSellOutFlag(1);
            }else{
                appGoodsInfoVo.setSellOutFlag(0);
            }
        }else{
            appGoodsInfoVo.setActivityFlag(0);
        }
        //商品图片
        List<GoodsFile> goodsFileList = goodsFileService.listByGoodsId(goods.getGoodsId());
        List<String> bannerList = new ArrayList<>();
        if(goodsFileList!=null&&!goodsFileList.isEmpty()){
            for(GoodsFile goodsFile : goodsFileList){
                if(goodsFile.getFileType()==2){
                    appGoodsInfoVo.setGoodsVideo(goodsFile.getFileUrl());
                }else if(goodsFile.getFileType()==3){
                    bannerList.add(goodsFile.getFileUrl());
                }
            }
            if(!bannerList.isEmpty()){
                appGoodsInfoVo.setGoodsBanners(String.join(",", bannerList));
            }
        }
        return appGoodsInfoVo;
    }
 
    /**
     * 分页商户商品列表
     * @param merGoodsPageDto
     * @return
     */
    @Override
    public List<MerGoodsPageVo> pageMerShopGoods(Page page,MerGoodsPageDto merGoodsPageDto){
        List<MerGoodsPageVo> merGoodsPageVoList = goodsMapper.pageMerShopGoods(page,merGoodsPageDto);
        return merGoodsPageVoList;
 
    }
 
 
    /**
     * 修改商户商品
     * @param merShopGoodsEditDto
     */
    @Override
    public void editMerShopGoods(MerShopGoodsEditDto merShopGoodsEditDto){
        Goods goods = this.getById(merShopGoodsEditDto.getGoodsId());
        if(merShopGoodsEditDto.getSalePrice().compareTo(goods.getMininumPrice())<0){
            throw new ServiceException(AppErrorConstant.SALESPRICE_MIN+goods.getMininumPrice().toString()+"元");
        }
        //商户定制价格
        ShopGoods shopGoods = shopGoodsService.getByShopIdAndGoodsId(merShopGoodsEditDto.getShopId(),goods.getGoodsId());
        if(shopGoods!=null){
            shopGoods.setSalesPrice(merShopGoodsEditDto.getSalePrice());
            shopGoods.setServiceNum(merShopGoodsEditDto.getServiceNum());
            shopGoods.setUpdateTime(new Date());
            shopGoods.setUpdateUserId(merShopGoodsEditDto.getUserId());
        }else{
            shopGoods = new ShopGoods();
            shopGoods.setDelFlag(0);
            shopGoods.setShopId(merShopGoodsEditDto.getShopId());
            shopGoods.setGoodsId(merShopGoodsEditDto.getGoodsId());
            shopGoods.setSalesPrice(merShopGoodsEditDto.getSalePrice());
            shopGoods.setServiceNum(merShopGoodsEditDto.getServiceNum());
            shopGoods.setUpdateTime(new Date());
            shopGoods.setUpdateUserId(merShopGoodsEditDto.getUserId());
        }
        shopGoodsService.saveOrUpdate(shopGoods);
    }
 
    /**
     * 平台分页获取商品
     * @param page
     * @param mgtGoodsPageDto
     * @return
     */
    @Override
    public List<MgtGoodsPageVo> pageMgtGoods(Page page, MgtGoodsPageDto mgtGoodsPageDto) {
        // 调用goodsMapper的方法获取MgtGoodsPageVo列表
        List<MgtGoodsPageVo> mgtGoodsPageVoList = goodsMapper.pageMgtGoods(page, mgtGoodsPageDto);
        // 如果列表不为空
        if (mgtGoodsPageVoList != null && !mgtGoodsPageVoList.isEmpty()) {
            // 获取所有商品分类的ID
            List<Long> goodsClassIds = mgtGoodsPageVoList.stream()
                    .map(MgtGoodsPageVo::getGoodsClass)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            // 通过远程服务获取商品分类
            Map<Long, SysClassification> sysClassificationMap = remoteConfigService.getSysClassificationList(goodsClassIds).getData();
            // 遍历所有商品
            mgtGoodsPageVoList.forEach(mgtGoodsPageVo -> {
                // 获取商品分类
                SysClassification sysClassification = sysClassificationMap.get(Long.valueOf(mgtGoodsPageVo.getGoodsClass()));
                // 如果商品分类不为空,则设置商品分类名称
                if (sysClassification != null) {
                    mgtGoodsPageVo.setGoodsClass(sysClassification.getClassName());
                }
            });
        }
        // 返回处理后的商品列表
        return mgtGoodsPageVoList;
    }
 
    /**
     *
     * @return
     */
    @Override
    public MgtGoodsTotalVo getMgtGoodsTotal(){
        return goodsMapper.getMgtGoodsTotal();
    }
 
    /**
    * @description: editMgtGoods
    * @param: mgtGoodsEditDto
    * @return: void
    * @author jqs34
    * @date: 2023/6/4 15:37
    */
    @Override
    public void editMgtGoods(MgtGoodsEditDto mgtGoodsEditDto) {
        Goods goods;
        Boolean newGoods = false;
        String goodsId = mgtGoodsEditDto.getGoodsId();
        Goods goodsSame = this.getOne(new LambdaQueryWrapper<Goods>().eq(Goods::getDelFlag,0).eq(Goods::getGoodsName,mgtGoodsEditDto.getGoodsName()));
        // 判断是否有商品ID 没有则新建
        if (StringUtils.isNotBlank(goodsId)) {
            if(goodsSame!=null&&!Objects.equals(goodsSame.getGoodsId(), goodsId)){
                throw new ServiceException(AppErrorConstant.GOODS_DOUBLE);
            }
            goods = this.getById(goodsId);
            if(goods.getGoodsStatus()==-1){
                throw new ServiceException(AppErrorConstant.GOODS_DELETED);
            }
        } else {
            if(goodsSame!=null){
                throw new ServiceException(AppErrorConstant.GOODS_DOUBLE);
            }
            goods = new Goods();
            goodsId = IdUtils.simpleUUID();
            goods.setGoodsId(goodsId);
            goods.setDelFlag(0);
            goods.setGoodsNo(CodeFactoryUtil.getGoodsNo());
            goods.setCreateTime(new Date());
            goods.setCreateUserId(mgtGoodsEditDto.getUserId());
            newGoods = true;
        }
        goods.setUpdateTime(new Date());
        goods.setUpdateUserId(mgtGoodsEditDto.getUserId());
        goods.setGoodsStatus(mgtGoodsEditDto.getGoodsStatus());
        goods.setGoodsType(mgtGoodsEditDto.getGoodsType());
        goods.setGoodsClassId(mgtGoodsEditDto.getGoodsClassId());
        goods.setGoodsName(mgtGoodsEditDto.getGoodsName());
        goods.setCycleNumFlag(mgtGoodsEditDto.getCycleNumFlag());
        goods.setServiceNum(mgtGoodsEditDto.getServiceNum());
        goods.setGoodsIntroduction(mgtGoodsEditDto.getGoodsIntroduction());
        goods.setSalesPrice(mgtGoodsEditDto.getSalesPrice());
        goods.setMininumPrice(mgtGoodsEditDto.getMininumPrice());
        if (mgtGoodsEditDto.getSubscription() != null && mgtGoodsEditDto.getSubscription().compareTo(BigDecimal.valueOf(0)) > 0) {
            goods.setSubscriptionFlag(1);
        } else {
            goods.setSubscriptionFlag(0);
        }
        goods.setSubscription(mgtGoodsEditDto.getSubscription());
        goods.setGoodsDetail(mgtGoodsEditDto.getGoodsDetail());
        goods.setRecommendFlag(mgtGoodsEditDto.getRecommendFlag());
        goods.setGoodsNurses(mgtGoodsEditDto.getGoodsNurses());
        goods.setGoodsTags(mgtGoodsEditDto.getGoodsNurses());
        this.saveOrUpdate(goods);
        //商品图片视频处理
        final String goodsIdFinal = goodsId;
        saveGoodsFiles(goodsIdFinal, mgtGoodsEditDto.getGoodsPicture(), 1);
        saveGoodsFiles(goodsIdFinal, mgtGoodsEditDto.getGoodsVideo(), 2);
        saveGoodsFiles(goodsIdFinal, mgtGoodsEditDto.getGoodsBanners(), 3);
        //如果是新建商品创建商品统计
        if (newGoods) {
            GoodsTotal goodsTotal = new GoodsTotal();
            goodsTotal.setGoodsId(goodsId);
            goodsTotal.setBuyCount(0);
            goodsTotal.setBuyNumCount(0);
            goodsTotal.setBuyUserCount(0);
            goodsTotal.setBuyMoneyTotal(new BigDecimal("0.00"));
            goodsTotalService.saveOrUpdate(goodsTotal);
        }
    }
 
    /**
    * @description: 处理商品文件
    * @param: goodsId fileUrls fileType
    * @return: void
    * @author jqs34
    * @date: 2023/6/4 16:25
    */
     private void saveGoodsFiles(String goodsId, String fileUrls, int fileType) {
         if (StringUtils.isNotEmpty(fileUrls)) {
             List<GoodsFile> files = Arrays.stream(fileUrls.split(","))
                     .map(str -> {
                         GoodsFile goodsFile = new GoodsFile();
                         goodsFile.setDelFlag(0);
                         goodsFile.setGoodsId(goodsId);
                         goodsFile.setFileType(fileType);
                         goodsFile.setFileUrl(str);
                         return goodsFile;
                     })
                     .collect(Collectors.toList());
             // 保存文件列表
             goodsFileService.saveBatch(files);
         }
     }
 
    /**
     * @description: 上下架
     * @author jqs
     * @date   2023/6/5 17:21
     * @version 1.0
     */
    @Override
    public void upDownMgtGoods(MgtGoodsUpDownDto mgtGoodsUpDownDto){
        Goods goods = this.getById(mgtGoodsUpDownDto.getGoodsId());
        if(goods.getGoodsStatus()==-1){
            throw new ServiceException(AppErrorConstant.GOODS_DELETED);
        }
        goods.setGoodsStatus(mgtGoodsUpDownDto.getGoodsStatus());
        goods.setUpdateTime(new Date());
        goods.setUpdateUserId(mgtGoodsUpDownDto.getUserId());
        this.saveOrUpdate(goods);
    }
 
    /**
     * @description  推荐商品
     * @author  jqs
     * @date    2023/6/5 17:32
     * @param mgtGoodsRecommendDto
     * @return  void
     */
    @Override
    public void recommendMgtGoods(MgtGoodsRecommendDto mgtGoodsRecommendDto){
        Goods goods = this.getById(mgtGoodsRecommendDto.getGoodsId());
        if(goods.getGoodsStatus()==-1){
            throw new ServiceException(AppErrorConstant.GOODS_DELETED);
        }
        goods.setRecommendFlag(mgtGoodsRecommendDto.getRecommendFlag());
        goods.setUpdateTime(new Date());
        goods.setUpdateUserId(mgtGoodsRecommendDto.getUserId());
        this.saveOrUpdate(goods);
    }
 
    /**
     * @description  删除商品
     * @author  jqs
     * @date    2023/6/5 17:36
     * @param goodsId
     * @return  void
     */
    @Override
    public void deleteMgtGoods(String goodsId,Long userId){
        Goods goods = this.getById(goodsId);
        goods.setDelFlag(1);
        goods.setGoodsStatus(-1);
        goods.setUpdateTime(new Date());
        goods.setUpdateUserId(userId);
        this.saveOrUpdate(goods);
    }
 
    /**
     * @description  删除商品分类
     * @author  jqs
     * @date    2023/6/8 10:31
     * @param classId
     * @return  void
     */
    @Override
    public void deleteGoodsClass(Long classId){
        goodsMapper.deleteGoodsClass(classId);
    }
 
    /**
     * @description  删除商品标签
     * @author  jqs
     * @date    2023/6/8 17:02
     * @param goodsTag
     * @return  void
     */
    @Override
    public void deleteGoodsTag(String goodsTag){
        goodsMapper.deleteGoodsTag(goodsTag);
    }
 
    /**
     * @description  通过id获取商品列表
     * @author  jqs
     * @date    2023/6/13 15:55
     * @param goodsIds
     * @return  List<Goods>
     */
    @Override
    public List<Goods> listGoodsByGoodsId(String goodsIds){
        return goodsMapper.listGoodsByGoodsId(goodsIds);
    }
 
    /**
     * @description  分页获取商品选择列表
     * @author  jqs
     * @date    2023/6/13 16:33
     * @param page
     * @param mgtSelectGoodsPageDto
     * @return  List<MgtSelectGoodsPageVo>
     */
    @Override
    public List<MgtSelectGoodsPageVo> pageMgtSelectGoods(Page page, MgtSelectGoodsPageDto mgtSelectGoodsPageDto){
        return goodsMapper.pageMgtSelectGoods(page, mgtSelectGoodsPageDto);
    }
}