mitao
2025-01-17 afa0dbb4f54e7244835dd67ec33c3e545f122f71
ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/order/ConsumerGoodsServiceImpl.java
@@ -1,10 +1,47 @@
package com.ruoyi.order.service.impl.order;
import com.ruoyi.system.domain.pojo.order.ConsumerGoods;
import com.ruoyi.system.mapper.order.ConsumerGoodsMapper;
import com.ruoyi.system.service.order.ConsumerGoodsService;
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>
@@ -16,5 +53,437 @@
 */
@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);
    }
}