KKSU
2024-01-12 8b74444e85757e588b234ef992a205271688795e
src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
@@ -1,9 +1,6 @@
package cc.mrbird.febs.mall.service.impl;
import cc.mrbird.febs.common.enumerates.AgentLevelEnum;
import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.dto.ScorePoorDto;
@@ -54,6 +51,7 @@
    private final MallAchieveRecordMapper mallAchieveRecordMapper;
    private final RedisUtils redisUtils;
    private final MallMemberWalletMapper mallMemberWalletMapper;
    private final IApiMallMemberWalletService iApiMallMemberWalletService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -383,56 +381,100 @@
    }
    /**
     * 拿推荐人的收益(代理,直推,排名)的10%加权平分给下面直推
     * 每日自动分发套餐的百分比到余额
     */
    @Override
    public void thankfulProfit(Date date) {
        log.info("######==感恩奖==start==####");
        List<MallMember> mallMembers = mallMemberMapper.selectMemberListHasChild();
        if (CollUtil.isEmpty(mallMembers)) {
        List<MallAchieveRecord> mallAchieveRecordList = mallAchieveRecordMapper.selectListByCostAmountAndIsNorMal(BigDecimal.ZERO,2);
        if(CollUtil.isEmpty(mallAchieveRecordList)){
            return;
        }
        if (date == null) {
            date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        DataDictionaryCustom yiPositionDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.YI_POSITION.getType(),
                DataDictionaryEnum.YI_POSITION.getCode()
        );
        BigDecimal yiPosition = new BigDecimal(yiPositionDic.getValue());
        DataDictionaryCustom yiPositionPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.YI_POSITION_PERCENT.getType(),
                DataDictionaryEnum.YI_POSITION_PERCENT.getCode()
        );
        BigDecimal yiPositionPercent = new BigDecimal(yiPositionPercentDic.getValue()).multiply(new BigDecimal("0.01"));
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.THANKFUL_BONUS.getType(), DataDictionaryEnum.THANKFUL_BONUS.getCode());
        // 感恩奖比例
        BigDecimal prop = new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        DataDictionaryCustom erPositionDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.ER_POSITION.getType(),
                DataDictionaryEnum.ER_POSITION.getCode()
        );
        BigDecimal erPosition = new BigDecimal(erPositionDic.getValue());
        DataDictionaryCustom erPositionPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.ER_POSITION_PERCENT.getType(),
                DataDictionaryEnum.ER_POSITION_PERCENT.getCode()
        );
        BigDecimal erPositionPercent = new BigDecimal(erPositionPercentDic.getValue()).multiply(new BigDecimal("0.01"));
        for (MallMember mallMember : mallMembers) {
            BigDecimal income = mallMoneyFlowMapper.selectThankfulCommission(date, mallMember.getId());
            List<MallMember> children = mallMemberMapper.selectMemberDirectForHasLevel(mallMember.getInviteId());
            if (CollUtil.isEmpty(children)) {
        DataDictionaryCustom sanPositionDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SAN_POSITION.getType(),
                DataDictionaryEnum.SAN_POSITION.getCode()
        );
        BigDecimal sanPosition = new BigDecimal(sanPositionDic.getValue());
        DataDictionaryCustom sanPositionPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SAN_POSITION_PERCENT.getType(),
                DataDictionaryEnum.SAN_POSITION_PERCENT.getCode()
        );
        BigDecimal sanPositionPercent = new BigDecimal(sanPositionPercentDic.getValue()).multiply(new BigDecimal("0.01"));
        DataDictionaryCustom siPositionDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SI_POSITION.getType(),
                DataDictionaryEnum.SI_POSITION.getCode()
        );
        BigDecimal siPosition = new BigDecimal(siPositionDic.getValue());
        DataDictionaryCustom siPositionPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SI_POSITION_PERCENT.getType(),
                DataDictionaryEnum.SI_POSITION_PERCENT.getCode()
        );
        BigDecimal siPositionPercent = new BigDecimal(siPositionPercentDic.getValue()).multiply(new BigDecimal("0.01"));
        for(MallAchieveRecord mallAchieveRecord : mallAchieveRecordList){
            Long orderId = mallAchieveRecord.getOrderId();
            MallOrderInfo mallOrderInfo = mallOrderInfoMapper.selectById(orderId);
            if(OrderStatusEnum.FINISH.getValue() != mallOrderInfo.getStatus()){
                continue;
            }
            BigDecimal reduceProfit = income.multiply(prop);
            if (reduceProfit.compareTo(BigDecimal.ZERO) < 1) {
            BigDecimal percent = BigDecimal.ZERO;//比例
            BigDecimal amount = mallAchieveRecord.getAmount();//总业绩
            BigDecimal costAmount = mallAchieveRecord.getCostAmount();//剩余业绩
            if(amount.compareTo(yiPosition) < 0){
                continue;
            }else if(amount.compareTo(yiPosition) >= 0 && amount.compareTo(erPosition) < 0){
                percent = yiPositionPercent;
            }else if(amount.compareTo(erPosition) >= 0 && amount.compareTo(sanPosition) < 0){
                percent = erPositionPercent;
            }else if(amount.compareTo(sanPosition) >= 0 && amount.compareTo(siPosition) < 0){
                percent = sanPositionPercent;
            }else{
                percent = siPositionPercent;
            }
            BigDecimal perk = amount.multiply(percent).setScale(2,BigDecimal.ROUND_DOWN);//实际发送的余额
            if(BigDecimal.ZERO.compareTo(perk) >= 0){
                continue;
            }
            int reduce = walletService.reduce(reduceProfit, mallMember.getId(), "commission");
            if (reduce == 2) {
                continue;
            if(perk.compareTo(costAmount) >= 0){
                perk = costAmount;
            }
            moneyFlowService.addMoneyFlow(mallMember.getId(), reduceProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            costAmount = costAmount.subtract(perk);
            mallAchieveRecord.setCostAmount(costAmount);
            mallAchieveRecordMapper.updateById(mallAchieveRecord);
            //增加
            iApiMallMemberWalletService.add(perk,mallAchieveRecord.getMemberId(),"balance");
            //增加一个流水记录
            moneyFlowService.addMoneyFlow(
                    mallAchieveRecord.getMemberId(),
                    perk,
                    MoneyFlowTypeEnum.TC_TO_BALANCE.getValue(),
                    mallOrderInfo.getOrderNo(),
                    mallAchieveRecord.getMemberId(),
                    FlowTypeEnum.BALANCE.getValue(),
                    AppContants.IS_RETURN_YES);
            for (MallMember child : children) {
                BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN);
                if (preProfit.compareTo(BigDecimal.ZERO) < 1) {
                    continue;
                }
//                int reduce1 = walletService.reduce(preProfit, child.getId(), "score");
//                if (reduce1 == 2) {
//                    continue;
//                }
//                walletService.add(preProfit, child.getId(), "commission");
//                moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null);
            }
        }
        log.info("######==感恩奖==end==####");
    }
@@ -500,6 +542,13 @@
    @Override
    public void scorePool() {
        log.info("#####==积分池==start==######");
        String redisKey = "scorePool";
//        List<MallOrderInfo> mallOrderInfos = mallOrderInfoMapper.selectList(new QueryWrapper<>());
//        if(CollUtil.isEmpty(mallOrderInfos)){
//            redisUtils.set(redisKey,0,0L);
//            return;
//        }
        /**
         * 后一天2点,结算前一天的所有消费
         * 积分池增加每日消费的10%
@@ -517,8 +566,6 @@
        Date date = DateUtil.offsetDay(new Date(), -1);
        BigDecimal totalAmount = mallOrderInfoMapper.selectTotalAmountByPayDoneAndDataString(1, date);
        BigDecimal multiplyTotalAmount = totalAmount.multiply(everydayRatio);
        String redisKey = "scorePool";
        String scorePoolRedis = redisUtils.getString(redisKey);
        if(StrUtil.isBlank(scorePoolRedis)){
            redisUtils.set(redisKey,0,0L);
@@ -569,4 +616,68 @@
                +";人数:"+cnt);
    }
    @Override
    public void fireJob(Object o) {
        List<MallMemberWallet> mallMemberWallets = mallMemberWalletMapper.seleListByScore();
        if(CollUtil.isEmpty(mallMemberWallets)){
            return;
        }
        DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.FIRE_SCORE_PERCENT.getType(),
                DataDictionaryEnum.FIRE_SCORE_PERCENT.getCode()
        );
        BigDecimal fireScorePercent = new BigDecimal(dataDictionaryCustom.getValue()).multiply(new BigDecimal("0.01"));
        BigDecimal totalReduce = BigDecimal.ZERO;
        for(MallMemberWallet mallMemberWallet : mallMemberWallets){
            BigDecimal score = mallMemberWallet.getScore();
            BigDecimal multiply = score.multiply(fireScorePercent).setScale(2,BigDecimal.ROUND_DOWN);//每次需要燃烧的数量
            if(BigDecimal.ZERO.compareTo(multiply) >= 0){
                continue;
            }
            iApiMallMemberWalletService.reduce(multiply,mallMemberWallet.getMemberId(),"score");
            //增加一个流水记录
            moneyFlowService.addMoneyFlow(
                    mallMemberWallet.getMemberId(),
                    multiply.negate(),
                    MoneyFlowTypeEnum.FIRE_SCORE.getValue(),
                    null,
                    mallMemberWallet.getMemberId(),
                    FlowTypeEnum.SCORE.getValue(),
                    AppContants.IS_RETURN_YES);
            totalReduce = totalReduce.add(multiply);
        }
        if(BigDecimal.ZERO.compareTo(totalReduce) >= 0){
            return;
        }
        /**
         * H金劵减少,更新价格
         */
        DataDictionaryCustom poolCashDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                PerkEnum.POOL_CASH.getType(),
                PerkEnum.POOL_CASH.getCode());
        //当前现金池数量
        BigDecimal poolCash = new BigDecimal(poolCashDic.getValue());
        DataDictionaryCustom poolScoreDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                PerkEnum.POOL_SCORE.getType(),
                PerkEnum.POOL_SCORE.getCode());
        //当前H金劵池数量
        BigDecimal poolScore = new BigDecimal(poolScoreDic.getValue());
        poolScore = poolScore.subtract(totalReduce).setScale(2,BigDecimal.ROUND_DOWN);
        dataDictionaryCustomMapper.updateDicValueByTypeAndCode(
                PerkEnum.POOL_SCORE.getType(),
                PerkEnum.POOL_SCORE.getCode(),
                poolScore.toString()
        );
        if(poolScore.compareTo(BigDecimal.ZERO) > 0 && poolCash.compareTo(BigDecimal.ZERO) > 0){
            BigDecimal divide = poolCash.divide(poolScore, 8, BigDecimal.ROUND_DOWN);
            dataDictionaryCustomMapper.updateDicValueByTypeAndCode(
                    PerkEnum.POOL_SCORE_PRICE.getType(),
                    PerkEnum.POOL_SCORE_PRICE.getCode(),
                    divide.toString()
            );
        }
    }
}