xiaoyong931011
2022-09-16 8c167a91ac251d3c35f10aa831c3f28d6729047e
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallTeamLeaderServiceImpl.java
@@ -2,28 +2,30 @@
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.ShareCodeUtil;
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.mall.conversion.MallLeaderAchieveConversion;
import cc.mrbird.febs.mall.conversion.MallOrderInfoConversion;
import cc.mrbird.febs.mall.conversion.MallTeamLeaderConversion;
import cc.mrbird.febs.mall.dto.*;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IAdminMallOrderService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IApiMallTeamLeaderService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.mall.vo.*;
import cc.mrbird.febs.pay.model.OrderStateDto;
import cc.mrbird.febs.pay.service.IXcxPayService;
import cc.mrbird.febs.pay.util.WechatConfigure;
import cc.mrbird.febs.pay.util.WeixinServiceUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -35,10 +37,7 @@
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Slf4j
@Service
@@ -61,6 +60,10 @@
    private final IApiMallMemberWalletService memberWalletService;
    private final MallLeaderAchieveMapper mallLeaderAchieveMapper;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final MallGoodsMapper mallGoodsMapper;
    private final MallGoodsSkuMapper mallGoodsSkuMapper;
    private final MallElectronicFenceMapper mallElectronicFenceMapper;
    private final IMallElectronicFenceServiceImpl iMallElectronicFenceService;
    @Override
    @Transactional
@@ -79,15 +82,53 @@
        mallTeamLeader.setMemberId(memberId);
        mallTeamLeader.setState(MallTeamLeader.STATE_ING);
        mallTeamLeader.setUniqueCode(member.getInviteId());
        //申请团长默认关闭返利,返利比例为0
        mallTeamLeader.setProfitSwitch(2);
        mallTeamLeader.setBonusPercent("0");
        this.baseMapper.insert(mallTeamLeader);
        return new FebsResponse().success().message("申请成功");
    }
    @Override
    public List<ApiLeaderListVo> findLeaderListInPage(ApiLeaderListDto apiLeaderListDto) {
        Page<MallTeamLeader> page = new Page<>(apiLeaderListDto.getPageNum(), apiLeaderListDto.getPageSize());
        IPage<MallTeamLeader> mallTeamLeaderIPage = this.baseMapper.selectApiLeaderListInPage(page, apiLeaderListDto);
        return MallTeamLeaderConversion.INSTANCE.entitysToVos(mallTeamLeaderIPage.getRecords());
    public FebsResponse findLeaderListInPage(ApiLeaderListDto apiLeaderListDto) {
        //根据当前位置的经纬度查询在所属配送区域内的团长
        List<MallElectronicFence> mallElectronicFences = mallElectronicFenceMapper.selectAllOnlineLeader();
        List<ApiLeaderListVo> apiLeaderListVos = new ArrayList<>();
        if(CollUtil.isEmpty(mallElectronicFences)){
            return new FebsResponse().success().data(apiLeaderListVos);
        }
        StringBuffer fenceIdStringBuffer = new StringBuffer();
        for(MallElectronicFence mallElectronicFence : mallElectronicFences){
            fenceIdStringBuffer.append(mallElectronicFence.getGid()).append(",");
        }
        String fenceIdStr = fenceIdStringBuffer.substring(0, fenceIdStringBuffer.length() - 1);
        //{"data":{"count":1,"results":[{"gfid":800899,"gfname":"87053658_fence","in":0}]},"errcode":10000,"errdetail":null,"errmsg":"OK"}
        String resultsStr = iMallElectronicFenceService.fenceToLocation(mallElectronicFences.get(0).getGKey(),
                mallElectronicFences.get(0).getServiceId(),
                apiLeaderListDto.getLongitude() + "," + apiLeaderListDto.getLatitude(),
                fenceIdStr);
        if("fail".equals(resultsStr)){
            return new FebsResponse().success().data(apiLeaderListVos);
        }
        JSONArray objects = JSONUtil.parseArray(resultsStr);
        for(Object obj : objects){
            //{"gfid":800899,"gfname":"87053658_fence","in":0}
            JSONObject jsonObject = JSONUtil.parseObj(obj);
            String gfid = jsonObject.get("gfid").toString();
            String in = jsonObject.get("in").toString();
            String gfname = jsonObject.get("gfname").toString();
            //指定坐标是否在围栏中
            //1:在,
            //0:不在;
            if("1".equals(in)){
                MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectByFenceIdAndFenceNameAndLonAndLat(apiLeaderListDto.getLongitude(), apiLeaderListDto.getLatitude(),gfid,gfname);
                ApiLeaderListVo apiLeaderListVo = MallTeamLeaderConversion.INSTANCE.entityToApiVo(mallTeamLeader);
                apiLeaderListVos.add(apiLeaderListVo);
            }
        }
        return new FebsResponse().success().data(apiLeaderListVos);
    }
    @Override
@@ -162,17 +203,17 @@
            mallOrderInfoMapper.updateById(mallOrderInfo);
            OrderStateDto orderStateDto = new OrderStateDto();
            orderStateDto.setOrderNo(mallOrderInfo.getOrderNo());
            String productNames = getProductNames(mallOrderInfo.getMemberId(), mallOrderInfo.getId());
            orderStateDto.setGoodsName(StrUtil.sub(productNames,0,10)+"...");
            orderStateDto.setOrderState("已送达");
            orderStateDto.setTakeCode(mallOrderInfo.getTakeCode());
            orderStateDto.setGoodsName(StrUtil.sub(productNames,0,20)+"...");
            String takeUniqueCode = mallOrderInfo.getTakeUniqueCode();
            MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectLeaderByUniqueCode(takeUniqueCode);
            orderStateDto.setAddressArea(StrUtil.sub(mallTeamLeader.getAddressArea(),0,10)+"...");
            orderStateDto.setAddressArea(StrUtil.sub(mallTeamLeader.getAddressArea(),0,20)+"...");
            orderStateDto.setDetailAddress(StrUtil.sub(mallTeamLeader.getDetailAddress(),0,20)+"...");
            orderStateDto.setLeaderPhone(mallTeamLeader.getPhone());
            orderStateDto.setOpenId(mallMemberMapper.selectById(mallOrderInfo.getMemberId()).getOpenId());
            DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.WX_TEMPLATE_ID_ONE.getType(), DataDictionaryEnum.WX_TEMPLATE_ID_ONE.getCode());
            DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.WX_TEMPLATE_ID_THREE.getType(), DataDictionaryEnum.WX_TEMPLATE_ID_THREE.getCode());
            orderStateDto.setTemplateId(dataDictionaryCustom.getValue());
            orderStateDto.setRemark("果蔬等生鲜,请尽快取货");
            iXcxPayService.pushOrderToAddress(orderStateDto);
        }
        return new FebsResponse().success().message("确认成功");
@@ -230,7 +271,6 @@
        //经纬度定位
        }else if(longitude != 0 && latitude != 0){
            mallTeamLeader = this.baseMapper.selectLeaderByLonAndLat(longitude,latitude);
        //直接进入,默认选择列表第一个
        }else{
            List<MallTeamLeader> mallTeamLeaderList = this.baseMapper.getMallTeamLeaderList();
@@ -249,6 +289,9 @@
            apiLeaderInfoVo.setTownship(mallTeamLeader.getTownship());
            apiLeaderInfoVo.setDetailAddress(mallTeamLeader.getDetailAddress());
            apiLeaderInfoVo.setAddressArea(mallTeamLeader.getAddressArea());
        }
        if(ObjectUtil.isEmpty(apiLeaderInfoVo)){
            return new FebsResponse().success();
        }
        return new FebsResponse().success().data(apiLeaderInfoVo);
    }
@@ -323,6 +366,16 @@
                //更新订单详情
                mallOrderItem.setState(3);
                mallOrderItemMapper.updateById(mallOrderItem);
                //更新库存信息
                MallGoods mallGoods = mallGoodsMapper.selectById(mallOrderItem.getGoodsId());
                mallGoods.setStock(mallGoods.getStock() + mallOrderItem.getCnt());
                mallGoods.setVolume(mallGoods.getVolume() - mallOrderItem.getCnt());
                mallGoodsMapper.updateById(mallGoods);
                MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(mallOrderItem.getSkuId());
                mallGoodsSku.setStock(mallGoodsSku.getStock() + mallOrderItem.getCnt());
                mallGoodsSku.setSkuVolume(mallGoodsSku.getSkuVolume() - mallOrderItem.getCnt());
                mallGoodsSkuMapper.updateById(mallGoodsSku);
                //更新退款订单
                mallRefundEntity.setState(1);
                mallRefundEntity.setUpdatedTime(DateUtil.date());
@@ -354,6 +407,17 @@
                //更新订单详情
                mallOrderItem.setState(3);
                mallOrderItemMapper.updateById(mallOrderItem);
                //更新库存信息
                MallGoods mallGoods = mallGoodsMapper.selectById(mallOrderItem.getGoodsId());
                mallGoods.setStock(mallGoods.getStock() + mallOrderItem.getCnt());
                mallGoods.setVolume(mallGoods.getVolume() - mallOrderItem.getCnt());
                mallGoodsMapper.updateById(mallGoods);
                MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(mallOrderItem.getSkuId());
                mallGoodsSku.setStock(mallGoodsSku.getStock() + mallOrderItem.getCnt());
                mallGoodsSku.setSkuVolume(mallGoodsSku.getSkuVolume() - mallOrderItem.getCnt());
                mallGoodsSkuMapper.updateById(mallGoodsSku);
                //更新退款订单
                mallRefundEntity.setState(1);
                mallRefundEntity.setUpdatedTime(DateUtil.date());
@@ -367,6 +431,11 @@
                    mallOrderInfoMapper.updateById(mallOrderRefund);
                }
            }else{
                //更新订单详情
                mallOrderItem.setState(1);
                mallOrderItemMapper.updateById(mallOrderItem);
                mallRefundEntity.setState(2);
                mallRefundMapper.updateById(mallRefundEntity);
                return new FebsResponse().fail().message("退款失败,请联系客服人员");
@@ -405,10 +474,52 @@
                null,null,
                DateUtil.date());
        apiLeaderProfitVo.setMonthProfit(monthProfit);
        BigDecimal waitProfit = mallLeaderAchieveMapper.selectProfitByStateAndDateTimeAndUniqueCode(1,null,loginUser.getInviteId()).setScale(2,BigDecimal.ROUND_DOWN);
        apiLeaderProfitVo.setWaitProfit(waitProfit);
        String inviteId = loginUser.getInviteId();
        MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectLeaderByUniqueCode(inviteId);
        Integer profitSwitch = mallTeamLeader.getProfitSwitch() == null ? 2:mallTeamLeader.getProfitSwitch();
        if(2 == profitSwitch){
            apiLeaderProfitVo.setWaitProfit(BigDecimal.ZERO);
        }else{
            BigDecimal waitProfit = mallLeaderAchieveMapper.selectProfitByStateAndDateTimeAndUniqueCode(1,null,loginUser.getInviteId()).setScale(2,BigDecimal.ROUND_DOWN);
            apiLeaderProfitVo.setWaitProfit(waitProfit);
        }
        return new FebsResponse().success().data(apiLeaderProfitVo);
    }
    @Override
    public List<ApiOrderProfitVo> findLeaderProfitListInPage(ApiOrderProfitDto apiOrderProfitDto) {
        MallMember member = LoginUserUtil.getLoginUser();
        IPage<MallLeaderAchieve> page = new Page<>(apiOrderProfitDto.getPageNum(), apiOrderProfitDto.getPageSize());
        apiOrderProfitDto.setInvitedId(member.getInviteId());
        IPage<MallLeaderAchieve> mallLeaderAchieves = mallLeaderAchieveMapper.selectApiLeaderProfitListInPage(page, apiOrderProfitDto);
        return MallLeaderAchieveConversion.INSTANCE.entitysToVos(mallLeaderAchieves.getRecords());
    }
    @Override
    public FebsResponse findSomeLeaderListInPage(ApiLeaderListDto apiLeaderListDto) {
        Page<MallTeamLeader> page = new Page<>(apiLeaderListDto.getPageNum(), apiLeaderListDto.getPageSize());
        IPage<MallTeamLeader> mallTeamLeaderIPage = this.baseMapper.selectApiLeaderListInPage(page, apiLeaderListDto);
        List<ApiLeaderListVo> apiLeaderListVos = new ArrayList<>();
        DataDictionaryCustom rangeSwitchDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANGE_SWITCH.getType(), DataDictionaryEnum.RANGE_SWITCH.getCode());
        if (ObjectUtil.isNotNull(rangeSwitchDic)) {
            int rangeSwitch = Integer.parseInt(rangeSwitchDic.getValue());
            if(1 == rangeSwitch){
                DataDictionaryCustom rangeSizeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANGE_SIZE.getType(), DataDictionaryEnum.RANGE_SIZE.getCode());
                double rangeSize = Double.parseDouble(rangeSizeDic.getValue())*1000;
                for(MallTeamLeader mallTeamLeader : mallTeamLeaderIPage.getRecords()){
                    Double distance = mallTeamLeader.getDistance();
                    if(distance <= rangeSize){
                        ApiLeaderListVo apiLeaderListVo = MallTeamLeaderConversion.INSTANCE.entityToApiVo(mallTeamLeader);
                        apiLeaderListVos.add(apiLeaderListVo);
                    }
                }
            }else{
                apiLeaderListVos = MallTeamLeaderConversion.INSTANCE.entitysToVos(mallTeamLeaderIPage.getRecords());
            }
        }
        return new FebsResponse().success().data(apiLeaderListVos);
    }
}