puzhibing
2024-03-18 65ca63cc3d36f23d285a7d944fcc0d6bac92430d
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
package com.ruoyi.order.service.impl.order;
 
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.order.domain.dto.*;
import com.ruoyi.order.domain.pojo.order.ConsumerGoods;
import com.ruoyi.order.domain.pojo.order.ServiceRecordDetail;
import com.ruoyi.order.domain.pojo.order.UserServiceRecord;
import com.ruoyi.order.domain.vo.AppConsumerPageVo;
import com.ruoyi.order.domain.vo.MerConsumerGoodsListVo;
import com.ruoyi.order.domain.vo.MerConsumerGoodsVo;
import com.ruoyi.order.domain.vo.MerMemberConsumerGoodsTotalVo;
import com.ruoyi.order.mapper.order.ConsumerGoodsMapper;
import com.ruoyi.order.service.order.ConsumerGoodsService;
import com.ruoyi.order.service.order.ServiceRecordDetailService;
import com.ruoyi.order.service.order.UserServiceRecordService;
import com.ruoyi.order.util.MsgUtils;
import com.ruoyi.system.api.constant.AppErrorConstant;
import com.ruoyi.system.api.domain.dto.AppShopGoodsGetDto;
import com.ruoyi.system.api.domain.dto.BirthdayGiftSendDto;
import com.ruoyi.system.api.domain.dto.MemberTotalChangeDto;
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.poji.member.Member;
import com.ruoyi.system.api.domain.poji.shop.Shop;
import com.ruoyi.system.api.service.RemoteGoodsService;
import com.ruoyi.system.api.service.RemoteMemberService;
import com.ruoyi.system.api.service.RemoteShopService;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务 服务实现类
 * </p>
 *
 * @author jqs
 * @since 2023-04-25
 */
@Service
public class ConsumerGoodsServiceImpl extends ServiceImpl<ConsumerGoodsMapper, ConsumerGoods> implements ConsumerGoodsService {
    
    private Logger log = LoggerFactory.getLogger(ConsumerGoodsServiceImpl.class);
 
    @Resource
    private ConsumerGoodsMapper consumerGoodsMapper;
 
    @Resource
    private UserServiceRecordService userServiceRecordService;
 
    @Resource
    private RemoteMemberService remoteMemberService;
 
    @Resource
    private RemoteGoodsService remoteGoodsService;
 
    @Resource
    private ServiceRecordDetailService serviceRecordDetailService;
 
    @Resource
    private RemoteShopService remoteShopService;
 
 
 
 
    /**
     * 获取用户服务
     * @param page
     * @param appConsumerPageDto
     * @return
     */
    @Override
    public List<AppConsumerPageVo> pageUserConsumerGoods(Page page, AppConsumerPageDto appConsumerPageDto){
        return consumerGoodsMapper.pageUserConsumerGoods(page, appConsumerPageDto);
    }
 
    /**
     * 获取会员服务商品
     * @param merConsumerGoodsDto
     * @return
     */
    @Override
    public MerConsumerGoodsVo listMerConsumerGoods(MerConsumerGoodsDto merConsumerGoodsDto){
        MerConsumerGoodsVo merConsumerGoodsVo = new MerConsumerGoodsVo();
        if(merConsumerGoodsDto.getGoodsType()==1){
            List<MerConsumerGoodsListVo> noSureList = consumerGoodsMapper.listMerNoSureConsumerGoods(merConsumerGoodsDto);
            merConsumerGoodsVo.setNoSureList(noSureList);
        }
        List<MerConsumerGoodsListVo> sureList = consumerGoodsMapper.listMerConsumerGoods(merConsumerGoodsDto);
        merConsumerGoodsVo.setSureList(sureList);
        return merConsumerGoodsVo;
    }
 
    /**
     * 确认商品次数
     * @param merSureConsumerGoodsDto
     */
    @Override
    @Transactional
    public void sureMerConsumerGoods(MerSureConsumerGoodsDto merSureConsumerGoodsDto){
        ConsumerGoods oldConsumerGoods = this.getById(merSureConsumerGoodsDto.getConsumerGoodsId());
        if(oldConsumerGoods.getSureNum()==null||oldConsumerGoods.getSureNum()<1){
            throw new ServiceException(AppErrorConstant.GOODS_SURE_FAILED);
        }
        List<MerSureConsumerGoodsListDto> merSureConsumerGoodsListDtoList = merSureConsumerGoodsDto.getMerSureConsumerGoodsListDtoList();
        if(merSureConsumerGoodsListDtoList!=null && !merSureConsumerGoodsListDtoList.isEmpty()){
            ConsumerGoods consumerGoods;
            Integer serviceCount = 0;
            Integer cycleService = 0;
            Integer serviceService = 0;
            Integer experienceService = 0;
            Goods goods;
            GoodsFile goodsFile;
            String consumerGoodsId;
            List<ConsumerGoods> consumerGoodsList = new ArrayList<>();
            int sureNum = 0;
            for(MerSureConsumerGoodsListDto merSureConsumerGoodsListDto : merSureConsumerGoodsListDtoList){
                goods = remoteGoodsService.getGoods(merSureConsumerGoodsListDto.getGoodsId()).getData();
                goodsFile = remoteGoodsService.getGoodsFile(goods.getGoodsId()).getData();
                if(goods!=null&&goods.getGoodsType()==1&&goods.getCycleNumFlag()==1){
                    for(int i=0;i<merSureConsumerGoodsListDto.getGoodsNum();i++){
                        consumerGoods = new ConsumerGoods();
                        consumerGoodsId = IdUtils.simpleUUID();
                        consumerGoods.setConsumerGoodsId(consumerGoodsId);
                        consumerGoods.setDelFlag(0);
                        consumerGoods.setServiceStatus(1);
                        consumerGoods.setShopId(merSureConsumerGoodsDto.getShopId());
                        consumerGoods.setUserId(merSureConsumerGoodsDto.getMemberUserId());
                        consumerGoods.setOrderId(oldConsumerGoods.getOrderId());
                        consumerGoods.setOrderGoodsId(oldConsumerGoods.getOrderGoodsId());
                        consumerGoods.setGoodsId(goods.getGoodsId());
                        consumerGoods.setGoodsName(goods.getGoodsName());
                        consumerGoods.setCycleNumFlag(goods.getCycleNumFlag());
                        consumerGoods.setServiceNum(goods.getServiceNum());
                        consumerGoods.setUsedNum(0);
                        consumerGoods.setCreateTime(new Date());
                        consumerGoods.setGoodsType(goods.getGoodsType());
                        consumerGoods.setGoodsTag(goods.getGoodsTags());
                        consumerGoods.setGoodsIntroduction(goods.getGoodsIntroduction());
                        consumerGoods.setGoodsPicture(goodsFile.getFileUrl());
                        consumerGoods.setGoodsNurses(goods.getGoodsNurses());
                        consumerGoods.setSourceFrom(1);
                        consumerGoodsList.add(consumerGoods);
                        //处理商品服务次数
                        switch (consumerGoods.getGoodsType()) {
                            case 1:
                                cycleService = cycleService + consumerGoods.getServiceNum();
                                break;
                            case 2:
                                serviceService = serviceService + consumerGoods.getServiceNum();
                                break;
                            case 3:
                                experienceService = experienceService + consumerGoods.getServiceNum();
                                break;
                            default:
                                break;
                        }
                        sureNum = sureNum + 1;
                    }
                }
            }
            //判断商品确认次数
            if(sureNum>oldConsumerGoods.getSureNum()){
                throw new ServiceException(AppErrorConstant.GOODS_SURE_FAILED);
            }else{
                int surpNum = oldConsumerGoods.getSureNum();
                surpNum = surpNum - sureNum;
                oldConsumerGoods.setSureNum(surpNum);
                if(surpNum==0){
                    oldConsumerGoods.setDelFlag(1);
                }
                this.saveOrUpdate(oldConsumerGoods);
            }
            this.saveBatch(consumerGoodsList);
            serviceCount = cycleService + serviceService + experienceService;
            //更新商户和会员服务统计
            MemberTotalChangeDto memberTotalChangeDto = new MemberTotalChangeDto();
            memberTotalChangeDto.setUserId(merSureConsumerGoodsDto.getMemberUserId());
            memberTotalChangeDto.setTypeService(1);
            memberTotalChangeDto.setServiceCount(serviceCount);
            log.info("更新商户和会员服务统计---》" + memberTotalChangeDto.toString());
            remoteMemberService.changeMemberTotal(memberTotalChangeDto);
        }
    }
 
    /**
     * 消费服务商品
     * @param merSureConsumerGoodsDto
     */
    @Override
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public void useMerConsumerGoods(MerSureConsumerGoodsDto merSureConsumerGoodsDto){
        List<MerSureConsumerGoodsListDto> goodsList = merSureConsumerGoodsDto.getMerSureConsumerGoodsListDtoList();
        if (goodsList != null && !goodsList.isEmpty()) {
            ConsumerGoods consumerGoods;
            Integer serviceNum;
            Integer usedNum;
            Integer useNum;
            Integer surplusNum;
            Long shopId = null;
            Long userId = null;
            String goodsNames = "";
            String goodsIds = "";
            Integer serviceType = 1;
            Integer serviceCount = 0;
            Integer cycleService = 0;
            Integer serviceService = 0;
            Integer experienceService = 0;
            //获取会员商品总数
            List<ServiceRecordDetail> serviceRecordDetailList = new ArrayList<>();
            ServiceRecordDetail serviceRecordDetail;
            List<String> consumerGoodsList = new ArrayList<>();
            List<Map<String, Object>> sendData = new ArrayList<>();
            for (MerSureConsumerGoodsListDto goodsDto : goodsList) {
                consumerGoods = this.getById(goodsDto.getConsumerGoodsId());
                serviceRecordDetail = new ServiceRecordDetail();
                serviceNum = consumerGoods.getServiceNum();
                usedNum = consumerGoods.getUsedNum();
                surplusNum = serviceNum - usedNum;
                useNum = goodsDto.getServiceNum();
                //判断使用数量是否超过剩余数量
                if (useNum > surplusNum) {
                    throw new ServiceException(AppErrorConstant.BEYOND_SURP_NUM);
                }
                consumerGoods.setUsedNum(usedNum + useNum);
                consumerGoods.setUseTime(new Date());
                //判断商品是否已使用完
                if (consumerGoods.getUsedNum().equals(serviceNum)) {
                    consumerGoods.setServiceStatus(2);
                    consumerGoods.setCompleteTime(new Date());
                }
                this.saveOrUpdate(consumerGoods);
 
                int number = consumerGoods.getServiceNum() - consumerGoods.getUsedNum();
                if(number < 3){
                    Map<String, Object> map = new HashMap<>();
                    map.put("product", consumerGoods.getGoodsName());
                    map.put("number", number);
                    sendData.add(map);
                }
 
                shopId = consumerGoods.getShopId();
                userId = consumerGoods.getUserId();
                serviceType = consumerGoods.getGoodsType();
                //拼接商品ID
                if (StringUtils.isBlank(goodsIds)) {
                    goodsIds = consumerGoods.getConsumerGoodsId();
                } else {
                    goodsIds = goodsIds + "," + consumerGoods.getConsumerGoodsId();
                }
                //拼接商品名称
                if (StringUtils.isBlank(goodsNames)) {
                    goodsNames = consumerGoods.getGoodsName();
                } else {
                    goodsNames = goodsNames + "、" + consumerGoods.getGoodsName();
                }
                //处理商品服务次数
                switch (consumerGoods.getGoodsType()) {
                    case 1:
                        cycleService = cycleService + useNum;
                        break;
                    case 2:
                        serviceService = serviceService + useNum;
                        break;
                    case 3:
                        experienceService = experienceService + useNum;
                        break;
                    default:
                        break;
                }
                serviceRecordDetail.setDelFlag(0);
                serviceRecordDetail.setConsumerGoodsId(consumerGoods.getConsumerGoodsId());
                serviceRecordDetail.setConsumerGoodsName(consumerGoods.getGoodsName());
                serviceRecordDetail.setServiceNum(consumerGoods.getServiceNum());
                serviceRecordDetail.setSurpNum(consumerGoods.getServiceNum()-consumerGoods.getUsedNum());
                if(serviceRecordDetail.getSurpNum()==0){
                    consumerGoodsList.add(consumerGoods.getGoodsName());
                }
                serviceRecordDetailList.add(serviceRecordDetail);
            }
            serviceCount = cycleService + serviceService + experienceService;
            UserServiceRecord serviceRecord = new UserServiceRecord();
            serviceRecord.setCreateTime(new Date());
            serviceRecord.setServiceType(serviceType);
            serviceRecord.setDelFlag(0);
            serviceRecord.setShopId(shopId);
            serviceRecord.setUserId(userId);
            serviceRecord.setConsumerGoodsIds(goodsIds);
            serviceRecord.setConsumerGoodsNames(goodsNames);
            userServiceRecordService.saveOrUpdate(serviceRecord);
            //服务记录详情
            for(ServiceRecordDetail serviceRecordDetail1 : serviceRecordDetailList){
                serviceRecordDetail1.setRecordId(serviceRecord.getId());
            }
            serviceRecordDetailService.saveBatch(serviceRecordDetailList);
            //更新商户和会员服务统计
            MemberTotalChangeDto memberTotalChange = new MemberTotalChangeDto();
            memberTotalChange.setUserId(merSureConsumerGoodsDto.getMemberUserId());
            memberTotalChange.setTypeService(2);
            memberTotalChange.setServiceCount(serviceCount);
            remoteMemberService.changeMemberTotal(memberTotalChange);
            if(!sendData.isEmpty()){
                Member member = remoteMemberService.getMember(userId).getData();
                R<Shop> r = remoteShopService.getShop(shopId);
                if(r.getCode() != 200){
                    throw new ServiceException(r.getMsg());
                }
                Shop shop = r.getData();
                try {
                    for (Map<String, Object> sendDatum : sendData) {
                        String product = sendDatum.get("product").toString();
                        Integer number = Integer.valueOf(sendDatum.get("number").toString());
                        String msg = "{\"name\":\"" + (StringUtils.isEmpty(member.getRealName()) ? member.getNickName() : member.getRealName()) + "\"," +
                                "\"store\":\"" + shop.getShopName() + "\",\"product\":\"" + product + "\"}";
                        if(number == 2){
                            MsgUtils.sendMsg(member.getMobile(),"SMS_464376210",msg);
                        }
                        if(number == 0){
                            MsgUtils.sendMsg(member.getMobile(),"SMS_464321234",msg);
                        }
                    }
 
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
 
    /**
     * 分页获取用户服务次数列表
     * @param page
     * @param merConsumerGoodsPageDto
     * @return
     */
    @Override
    public List<MerConsumerGoodsListVo> pageMerConsumerGoodsNum(Page page, MerConsumerGoodsPageDto merConsumerGoodsPageDto){
        List<MerConsumerGoodsListVo> merConsumerGoodsListVos = consumerGoodsMapper.pageMerConsumerGoodsNum(page, merConsumerGoodsPageDto);
        return merConsumerGoodsListVos;
    }
 
    /**
     * @description  获取用户在商户
     * @author  jqs
     * @date    2023/6/29 15:13
     * @param userId
     * @param shopId
     * @return  MerMemberConsumerGoodsTotalVo
     */
    @Override
    public MerMemberConsumerGoodsTotalVo getMemberConsumerGoodsTotalVo(Long userId, Long shopId){
        return consumerGoodsMapper.getMemberConsumerGoodsTotalVo(userId, shopId);
    }
 
    /**
     * @description  发放用户商品礼物
     * @author  jqs
     * @date    2023/7/7 18:51
     * @param giftSendDtoList
     * @return  void
     */
    @Override
    public void sendGoodsGift(List<BirthdayGiftSendDto> giftSendDtoList){
        // 使用StringJoiner拼接goodsId
        StringJoiner goodsSj = new StringJoiner(",");
        for (BirthdayGiftSendDto birthdayGiftSendDto : giftSendDtoList) {
            goodsSj.add(birthdayGiftSendDto.getGoodsId());
        }
        String goodsIds = goodsSj.toString();
        // 获取商品列表
        List<Goods> goodsList = remoteGoodsService.listGoodsByGoodsId(goodsIds).getData();
        // 初始化变量
        List<ConsumerGoods> consumerGoodsList = new ArrayList<>();
        Map<String, Goods> goodsMap = goodsList.stream()
                .collect(Collectors.toMap(Goods::getGoodsId, Function.identity()));
        ConsumerGoods consumerGoods;
        String consumerGoodsId;
        Goods goods;
        String goodsId;
        GoodsFile goodsFile;
        AppShopGoodsGetDto appShopGoodsGetDto;
        ShopGoods shopGoods = null;
        // 遍历giftSendDtoList
        for (BirthdayGiftSendDto birthdayGiftSendDto : giftSendDtoList) {
            goodsId = birthdayGiftSendDto.getGoodsId();
            goods = goodsMap.get(goodsId);
            goodsFile = remoteGoodsService.getGoodsFile(goods.getGoodsId()).getData();
            // 获取商户服务次数
            if (birthdayGiftSendDto.getGiftFrom() == 2) {
                appShopGoodsGetDto = new AppShopGoodsGetDto();
                appShopGoodsGetDto.setGoodsId(goodsId);
                appShopGoodsGetDto.setShopId(birthdayGiftSendDto.getShopId());
                shopGoods = remoteGoodsService.getShopGoods(appShopGoodsGetDto).getData();
            }
            // 根据goodsNumber创建ConsumerGoods对象并添加到列表中
            for (int i = 0; i < birthdayGiftSendDto.getGoodsNumber(); i++) {
                consumerGoods = new ConsumerGoods();
                consumerGoodsId = IdUtils.simpleUUID();
                consumerGoods.setConsumerGoodsId(consumerGoodsId);
                consumerGoods.setDelFlag(0);
                consumerGoods.setServiceStatus(1);
                consumerGoods.setShopId(birthdayGiftSendDto.getShopId());
                consumerGoods.setUserId(birthdayGiftSendDto.getUserId());
                consumerGoods.setGoodsId(goodsId);
                consumerGoods.setGoodsName(goods.getGoodsName());
                consumerGoods.setCycleNumFlag(goods.getCycleNumFlag());
                consumerGoods.setServiceNum(goods.getServiceNum());
                consumerGoods.setUsedNum(0);
                consumerGoods.setCreateTime(new Date());
                consumerGoods.setGoodsType(goods.getGoodsType());
                consumerGoods.setGoodsTag(goods.getGoodsTags());
                consumerGoods.setGoodsIntroduction(goods.getGoodsIntroduction());
                consumerGoods.setGoodsPicture(goodsFile.getFileUrl());
                consumerGoods.setGoodsNurses(goods.getGoodsNurses());
                consumerGoods.setSourceFrom(3);
                // 如果shopGoods不为空,则使用shopGoods的serviceNum
                if (shopGoods != null) {
                    consumerGoods.setServiceNum(shopGoods.getServiceNum());
                }
                consumerGoodsList.add(consumerGoods);
            }
        }
        // 批量保存consumerGoodsList
        this.saveBatchConsumerGoods(consumerGoodsList);
    }
 
 
    /**
     * @description
     * @author  jqs
     * @date    2023/7/7 19:31
     * @param consumerGoodsList
     * @return  boolean
     */
    @Override
    public boolean saveBatchConsumerGoods(List<ConsumerGoods> consumerGoodsList){
        Integer serviceCount = 0;
        Integer cycleService = 0;
        Integer serviceService = 0;
        Integer experienceService = 0;
        Long shopId = consumerGoodsList.get(0).getShopId();
        Long userId = consumerGoodsList.get(0).getUserId();
        for(ConsumerGoods consumerGoods : consumerGoodsList){
            //处理商品服务次数
            switch (consumerGoods.getGoodsType()) {
                case 1:
                    if(consumerGoods.getServiceNum()!=null){
                        cycleService = cycleService + consumerGoods.getServiceNum();
                    }
                    break;
                case 2:
                    if(consumerGoods.getServiceNum()!=null){
                        serviceService = serviceService + consumerGoods.getServiceNum();
                    }
                    break;
                case 3:
                    if(consumerGoods.getServiceNum()!=null) {
                        experienceService = experienceService + consumerGoods.getServiceNum();
                    }
                    break;
                default:
                    break;
            }
        }
        serviceCount = cycleService + serviceService + experienceService;
        //更新商户和会员服务统计
        MemberTotalChangeDto memberTotalChangeDto = new MemberTotalChangeDto();
        memberTotalChangeDto.setUserId(userId);
        memberTotalChangeDto.setTypeService(1);
        memberTotalChangeDto.setServiceCount(serviceCount);
        remoteMemberService.changeMemberTotal(memberTotalChangeDto);
        return this.saveBatch(consumerGoodsList);
    }
}