KKSU
2023-11-27 83b56237a9e0b379b46d31ab77def13226f33548
src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
@@ -59,6 +59,11 @@
    private final MallScoreRecordMapper mallScoreRecordMapper;
    private final MallScoreAchieveReleaseMapper mallScoreAchieveReleaseMapper;
    private final MallScoreVoucherMapper mallScoreVoucherMapper;
    private final CommonService commonService;
    private final MallProductBuyRecordMapper mallProductBuyRecordMapper;
    private final MallProductBuyMapper mallProductBuyMapper;
    private final MallProductSellMapper mallProductSellMapper;
    private final MallProductSellRecordMapper mallProductSellRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -771,28 +776,28 @@
         * 每次拿重试次数大于零
         * 按ID asc排序的十条数据
         */
        List<MallMqRecord> mallMqRecords = mallMqRecordMapper.selectByStateLimitTen(2);
        if(CollUtil.isEmpty(mallMqRecords)){
            return;
        }
        DateTime dateTime = DateUtil.offsetMinute(new Date(), -5);
        for(MallMqRecord mallMqRecord : mallMqRecords){
            if(DateUtil.compare(dateTime,mallMqRecord.getCreateTime()) <= 0){
                continue;
            }
            Integer retryTimes = mallMqRecord.getRetryTimes();
            if(retryTimes <= 0){
                continue;
            }
            retryTimes = retryTimes - 1;
            mallMqRecord.setRetryTimes(retryTimes);
            mallMqRecordMapper.updateById(mallMqRecord);
            Long orderId = mallMqRecord.getOrderId();
            if(mallMqRecord.getState() == 2){
                agentProducer.sendPerkMoneyMsg(orderId);
            }
        }
//        List<MallMqRecord> mallMqRecords = mallMqRecordMapper.selectByStateLimitTen(2);
//        if(CollUtil.isEmpty(mallMqRecords)){
//            return;
//        }
//        DateTime dateTime = DateUtil.offsetMinute(new Date(), -5);
//        for(MallMqRecord mallMqRecord : mallMqRecords){
//            if(DateUtil.compare(dateTime,mallMqRecord.getCreateTime()) <= 0){
//                continue;
//            }
//            Integer retryTimes = mallMqRecord.getRetryTimes();
//            if(retryTimes <= 0){
//                continue;
//            }
//            retryTimes = retryTimes - 1;
//            mallMqRecord.setRetryTimes(retryTimes);
//            mallMqRecordMapper.updateById(mallMqRecord);
//
//            Long orderId = mallMqRecord.getOrderId();
//            if(mallMqRecord.getState() == 2){
//                agentProducer.sendPerkMoneyMsg(orderId);
//            }
//        }
    }
    @Override
@@ -819,7 +824,8 @@
        for(MallScoreRecord mallScoreRecord : mallScoreRecords){
            Long memberId = mallScoreRecord.getMemberId();
            //每日释放的绿色凭证数量 = 总数 * 释放比例
            BigDecimal scoreCnt = mallScoreRecord.getScoreCnt();
//            BigDecimal scoreCnt = mallScoreRecord.getScoreCnt();
            BigDecimal scoreCnt = mallScoreRecord.getSurplusCnt();
            BigDecimal scoreReleaseCnt = scoreCnt.multiply(roleReleasePercent).setScale(2,BigDecimal.ROUND_DOWN);
            BigDecimal surplusCnt = mallScoreRecord.getSurplusCnt();
@@ -899,8 +905,8 @@
        /**
         * 20%给合伙人、联创
         *  5% 给联创
         *  15% 给合伙人
         *  5% 给联创 10%
         *  15% 给合伙人 10%
         */
        DataDictionaryCustom roleAchievePercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                GreenScoreEnum.ROLE_ACHIEVE_PERCENT.getType(),
@@ -914,7 +920,8 @@
        List<MallMember> roleAchieveReleaseMembers = mallMemberMapper.selectByRole(3,1);
        if(CollUtil.isNotEmpty(roleAchieveReleaseMembers)){
            //联创拿走0.25
            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.25));
//            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.25));
            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.5));
            BigDecimal memberAchieveRelease = multiply.divide(new BigDecimal(roleAchieveReleaseMembers.size()), 2, BigDecimal.ROUND_DOWN);
            if(memberAchieveRelease.compareTo(BigDecimal.ZERO) > 0){
                for(MallMember mallMember : roleAchieveReleaseMembers){
@@ -930,7 +937,8 @@
        List<MallMember> roleAchieveReleaseHHR = mallMemberMapper.selectByRole(4,1);
        if(CollUtil.isNotEmpty(roleAchieveReleaseHHR)){
            //合伙人拿走0.75
            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.75));
//            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.75));
            BigDecimal multiply = roleAchieveRelease.multiply(new BigDecimal(0.5));
            BigDecimal memberAchieveReleaseHHR = multiply.divide(new BigDecimal(roleAchieveReleaseHHR.size()), 2, BigDecimal.ROUND_DOWN);
            if(memberAchieveReleaseHHR.compareTo(BigDecimal.ZERO) > 0){
                for(MallMember mallMember : roleAchieveReleaseHHR){
@@ -1086,7 +1094,7 @@
                            voucherAmountAdd,
                            MoneyFlowTypeEnum.VOUCHER_SALE.getValue(),
                            voucherNo,
                            FlowTypeEnum.VOUCHER_AMOUNT.getValue());
                            FlowTypeEnum.BALANCE.getValue());
                }
                //增加积分凭证池的凭证数量
@@ -1103,6 +1111,153 @@
        }
    }
    @Override
    public void fcmNFTExchangeMsg(String cnt) {
        //销毁数量
        BigDecimal destoryCnt = new BigDecimal(cnt);
        DataDictionaryCustom fcmDestoryCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.FCM_DESTORY_CNT.getType(),
                DataDictionaryEnum.FCM_DESTORY_CNT.getCode()
        );
        BigDecimal fcmDestoryCnt = new BigDecimal(fcmDestoryCntDic.getValue());
        BigDecimal fcmDestoryCntAdd = fcmDestoryCnt.add(destoryCnt);
         /**
         * 每次销毁10000个,价格增加0.1
          * 满足的次数 divide
         */
        BigDecimal divide = fcmDestoryCntAdd.divide(AppContants.FCM_BASIC, 0, BigDecimal.ROUND_DOWN);
        if(BigDecimal.ZERO.compareTo(divide) == 0){
            commonService.updateDataDic(
                    DataDictionaryEnum.BANK_TRANS_URL.getType(),
                    DataDictionaryEnum.BANK_TRANS_URL.getCode(),
                    fcmDestoryCntAdd.toString());
        }
        if(BigDecimal.ZERO.compareTo(divide) < 0){
            //例:累计15000 去掉10000,累计只剩5000
            BigDecimal fcmDestoryCntReal = fcmDestoryCntAdd.subtract(divide.multiply(AppContants.FCM_BASIC));
            commonService.updateDataDic(
                    DataDictionaryEnum.FCM_DESTORY_CNT.getType(),
                    DataDictionaryEnum.FCM_DESTORY_CNT.getCode(),
                    fcmDestoryCntReal.toString());
            //更新总销毁数量
            DataDictionaryCustom fcmDestoryTotalDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    DataDictionaryEnum.FCM_DESTORY_TOTAL.getType(),
                    DataDictionaryEnum.FCM_DESTORY_TOTAL.getCode()
            );
            BigDecimal fcmDestoryTotal = new BigDecimal(fcmDestoryTotalDic.getValue());
            fcmDestoryTotal = fcmDestoryTotal.add(destoryCnt);
            commonService.updateDataDic(
                    DataDictionaryEnum.FCM_DESTORY_TOTAL.getType(),
                    DataDictionaryEnum.FCM_DESTORY_TOTAL.getCode(),
                    fcmDestoryTotal.toString());
            //更新价格
            DataDictionaryCustom fcmPriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    DataDictionaryEnum.FCM_PRICE.getType(),
                    DataDictionaryEnum.FCM_PRICE.getCode()
            );
            BigDecimal fcmPrice = new BigDecimal(fcmPriceDic.getValue());
            fcmPrice = fcmPrice.add(AppContants.FCM_BASIC_ADD_PRICE.multiply(divide));
            commonService.updateDataDic(
                    DataDictionaryEnum.FCM_PRICE.getType(),
                    DataDictionaryEnum.FCM_PRICE.getCode(),
                    fcmPrice.toString());
        }
    }
    @Override
    public void fcmOrderSellInsureMsg(Long sellRecordId) {
        /**
         * 买单确认
         * 判断买单是否存在、是否已支付
         * 判断买单是否存在、是否已支付
         * 更新卖单状态
         * 更新买单状态
         */
        MallProductSellRecord mallProductSellRecord = mallProductSellRecordMapper.selectById(sellRecordId);
        if(ObjectUtil.isEmpty(mallProductSellRecord)){
            return;
        }
        Integer state = mallProductSellRecord.getState();
        if(ProductEnum.PRODUCT_MATE_STATE_CONFIRM.getValue() != state){
            return;
        }
        MallProductBuyRecord mallProductBuyRecord = mallProductBuyRecordMapper.selectById(mallProductSellRecord.getBuyRecordId());
        if(ObjectUtil.isEmpty(mallProductBuyRecord)){
            return;
        }
        Integer stateBuy = mallProductBuyRecord.getState();
        if(ProductEnum.PRODUCT_MATE_STATE_CONFIRM.getValue() != stateBuy){
            return;
        }
        mallProductSellRecord.setState(ProductEnum.PRODUCT_MATE_STATE_FINISH.getValue());
        mallProductSellRecordMapper.updateById(mallProductSellRecord);
        mallProductBuyRecord.setState(ProductEnum.PRODUCT_MATE_STATE_FINISH.getValue());
        mallProductBuyRecordMapper.updateById(mallProductBuyRecord);
        Long sellId = mallProductSellRecord.getSellId();
        //已完成的买单
        List<MallProductSellRecord> mallProductSellRecords = mallProductSellRecordMapper.selectListBySellId(sellId,ProductEnum.PRODUCT_MATE_STATE_FINISH.getValue());
        if(CollUtil.isEmpty(mallProductSellRecords)){
            return;
        }
        //实际支付总数
        BigDecimal nftCntTotal = mallProductSellRecords.stream().map(MallProductSellRecord::getNftCnt).reduce(BigDecimal.ZERO, BigDecimal::add);
        MallProductSell mallProductSell = mallProductSellMapper.selectById(sellId);
        if(nftCntTotal.compareTo(mallProductSell.getNftCnt()) < 0){
            return;
        }
        mallProductSell.setState(ProductEnum.PRODUCT_SELL_SUCCESS.getValue());
        mallProductSellMapper.updateById(mallProductSell);
        Long buyId = mallProductBuyRecord.getBuyId();
        List<MallProductBuyRecord> mallProductBuyRecords = mallProductBuyRecordMapper.selectListByBuyId(buyId,ProductEnum.PRODUCT_MATE_STATE_FINISH.getValue());
        if(CollUtil.isEmpty(mallProductBuyRecords)){
            return;
        }
        //实际支付总数
        BigDecimal nftCntTotalBuy = mallProductBuyRecords.stream().map(MallProductBuyRecord::getPickNftCnt).reduce(BigDecimal.ZERO, BigDecimal::add);
        MallProductBuy mallProductBuy = mallProductBuyMapper.selectById(buyId);
        if(nftCntTotalBuy.compareTo(mallProductBuy.getNftTotal()) < 0){
            return;
        }
        mallProductBuy.setState(ProductEnum.PRODUCT_BUY_SUCCESS.getValue());
        mallProductBuy.setPayTime(DateUtil.date());
        mallProductBuyMapper.updateById(mallProductBuy);
    }
    @Override
    public void fcmOrderBuyCancelMsg(Long buyRecordId) {
        MallProductBuyRecord mallProductBuyRecord = mallProductBuyRecordMapper.selectById(buyRecordId);
        if(ProductEnum.PRODUCT_MATE_STATE_WAIT_PAY.getValue() != mallProductBuyRecord.getState()){
            return;
        }
        Long sellRecordId = mallProductBuyRecord.getSellRecordId();
        //更新买单子表的数据
        mallProductBuyRecord.setState(ProductEnum.PRODUCT_MATE_STATE_FAIL.getValue());
        mallProductBuyRecordMapper.updateById(mallProductBuyRecord);
        //更新买单主表
        MallProductBuy mallProductBuy = mallProductBuyMapper.selectById(mallProductBuyRecord.getBuyId());
        mallProductBuy.setNftAva(mallProductBuy.getNftAva().add(mallProductBuyRecord.getPickNftCnt()));
        mallProductBuyMapper.updateById(mallProductBuy);
        //更新卖单子表的数据
        MallProductSellRecord mallProductSellRecord = mallProductSellRecordMapper.selectById(sellRecordId);
        mallProductSellRecord.setState(ProductEnum.PRODUCT_MATE_STATE_FAIL.getValue());
        mallProductSellRecordMapper.updateById(mallProductSellRecord);
        //更新卖单主表
        Long sellId = mallProductSellRecord.getSellId();
        MallProductSell mallProductSell = mallProductSellMapper.selectById(sellId);
        mallProductSell.setNftCntAva(mallProductSell.getNftCntAva().add(mallProductSellRecord.getNftCnt()));
        mallProductSellMapper.updateById(mallProductSell);
    }
    public static void main(String[] args) {
        BigDecimal divide = new BigDecimal(12000).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_DOWN);
        System.out.println(divide);
    }
    /**
     * 给用户的增加凭证数据,并且增加流水
     * @param memberAchieveRelease 释放数量