jiangqs
2023-07-08 c71055635df3d75e5dc838a9b66036c591913a13
ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/order/ConsumerGoodsServiceImpl.java
@@ -5,6 +5,7 @@
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.uuid.IdUtils;
import com.ruoyi.order.domain.dto.*;
import com.ruoyi.order.domain.pojo.order.ConsumerGoods;
import com.ruoyi.order.domain.pojo.order.UserServiceRecord;
@@ -16,15 +17,22 @@
import com.ruoyi.order.service.order.ConsumerGoodsService;
import com.ruoyi.order.service.order.UserServiceRecordService;
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.dto.ShopTotalChangeDto;
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.service.RemoteGoodsService;
import com.ruoyi.system.api.service.RemoteMemberService;
import com.ruoyi.system.api.service.RemoteShopService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * <p>
@@ -48,6 +56,9 @@
    @Resource
    private RemoteShopService remoteShopService;
    @Resource
    private RemoteGoodsService remoteGoodsService;
    /**
     * 获取用户服务
@@ -147,7 +158,7 @@
    }
    /**
     * 确认商品次数
     * 消费服务商品
     * @param merSureConsumerGoodsDto
     */
    @Override
@@ -286,4 +297,143 @@
    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.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 cyclePerson = 0;
        Integer serviceService = 0;
        Integer servicePerson = 0;
        Integer experienceService = 0;
        Integer experiencePerson = 0;
        Long shopId = consumerGoodsList.get(0).getShopId();
        Long userId = consumerGoodsList.get(0).getUserId();
        MerMemberConsumerGoodsTotalVo memberConsumerGoodsTotalVo = this.getMemberConsumerGoodsTotalVo(userId, shopId);
        for(ConsumerGoods consumerGoods : consumerGoodsList){
            //处理商品服务次数
            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;
            }
        }
        //判断是否加人
        if (cycleService > 0 && memberConsumerGoodsTotalVo.getCycleOrder() == 0) {
            cyclePerson = 1;
        }
        if (serviceService > 0 && memberConsumerGoodsTotalVo.getServiceOrder() == 0) {
            servicePerson = 1;
        }
        if (experienceService > 0 && memberConsumerGoodsTotalVo.getExperienceOrder() == 0) {
            experiencePerson = 1;
        }
        serviceCount = cycleService + serviceService + experienceService;
        //更新商户和会员服务统计
        MemberTotalChangeDto memberTotalChangeDto = new MemberTotalChangeDto();
        memberTotalChangeDto.setUserId(userId);
        memberTotalChangeDto.setTypeService(1);
        memberTotalChangeDto.setServiceCount(serviceCount);
        remoteMemberService.changeMemberTotal(memberTotalChangeDto);
        ShopTotalChangeDto shopTotalChangeDto = new ShopTotalChangeDto();
        shopTotalChangeDto.setShopId(shopId);
        shopTotalChangeDto.setTypeCycleService(1);
        shopTotalChangeDto.setCycleService(cycleService);
        shopTotalChangeDto.setCyclePerson(cyclePerson);
        shopTotalChangeDto.setTypeServiceService(1);
        shopTotalChangeDto.setServiceService(serviceService);
        shopTotalChangeDto.setServicePerson(servicePerson);
        shopTotalChangeDto.setTypeExperienceService(1);
        shopTotalChangeDto.setExperienceService(experienceService);
        shopTotalChangeDto.setExperiencePerson(experiencePerson);
        remoteShopService.changeShopTotal(shopTotalChangeDto);
        return this.saveBatch(consumerGoodsList);
    }
}