KKSU
2025-02-07 6539b325b5ce95d1fafa864c75a32279c954167f
src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java
@@ -1,8 +1,13 @@
package cc.mrbird.febs.mall.quartz;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.enumerates.YesOrNoOrIngEnum;
import cc.mrbird.febs.mall.entity.MallActivity;
import cc.mrbird.febs.mall.mapper.MallActivityMapper;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.pay.model.RefundStatus;
import cc.mrbird.febs.pay.util.FiuuRefundUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
@@ -26,6 +31,27 @@
    @Autowired
    private MallActivityMapper mallActivityMapper;
    @Autowired
    private MallRefundMapper mallRefundMapper;
    @Autowired
    private MallOrderInfoMapper mallOrderInfoMapper;
    @Autowired
    private MallOrderItemMapper mallOrderItemMapper;
    @Autowired
    private MallGoodsSkuMapper mallGoodsSkuMapper;
    @Autowired
    private MallGoodsMapper mallGoodsMapper;
    @Autowired
    private FiuuRefundUtil fiuuRefundUtil;
    @Autowired
    private IMallMoneyFlowService mallMoneyFlowService;
    /**
     * 一分钟运行一次
@@ -74,4 +100,90 @@
        });
    }
    /**
     * 一分钟运行一次
     *      查询退款记录,去验证是否已经退款成功
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void refundJob() {
        LambdaQueryWrapper<MallRefundEntity> mallOrderRefundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mallOrderRefundLambdaQueryWrapper.eq(MallRefundEntity::getStatus, 3);
        mallOrderRefundLambdaQueryWrapper.eq(MallRefundEntity::getType, 1);
        List<MallRefundEntity> mallRefundEntities = mallRefundMapper.selectList(mallOrderRefundLambdaQueryWrapper);
        if(CollUtil.isEmpty(mallRefundEntities)){
            return;
        }
        mallRefundEntities.forEach(mallRefundEntity -> {
            processRefund(mallRefundEntity);
        });
    }
    private void processRefund(MallRefundEntity mallRefundEntity) {
        try {
            MallOrderInfo mallOrderInfo = mallOrderInfoMapper.selectById(mallRefundEntity.getOrderId());
            MallOrderItem mallOrderItem = mallOrderItemMapper.selectById(mallRefundEntity.getItemId());
            MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(mallOrderItem.getSkuId());
            String txnId = mallOrderInfo.getPayOrderNo();
            RefundStatus status = fiuuRefundUtil.pollRefundStatus(txnId);
            switch (status.getStatus()) {
                case "success":
                    updateOnSuccess(mallRefundEntity, mallOrderInfo, mallOrderItem, mallGoodsSku);
                    break;
                case "rejected":
                    updateOnRejected(mallRefundEntity, mallOrderItem);
                    break;
                default:
                    log.warn("未知状态:{}", status.getStatus());
            }
        } catch (Exception e) {
            log.error("处理退款失败: {}", e.getMessage(), e);
        }
    }
    private void updateOnSuccess(MallRefundEntity mallRefundEntity, MallOrderInfo mallOrderInfo, MallOrderItem mallOrderItem, MallGoodsSku mallGoodsSku) {
        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.setStock(mallGoodsSku.getStock() + mallOrderItem.getCnt());
        mallGoodsSku.setSkuVolume(mallGoodsSku.getSkuVolume() - mallOrderItem.getCnt());
        mallGoodsSkuMapper.updateById(mallGoodsSku);
        mallRefundEntity.setState(1);
        mallRefundEntity.setUpdatedTime(DateUtil.date());
        mallRefundMapper.updateById(mallRefundEntity);
        mallMoneyFlowService.addMoneyFlow(
                mallOrderInfo.getMemberId(),
                mallRefundEntity.getAmount(),
                MoneyFlowTypeEnum.WECHAT_REFUND.getValue(),
                mallOrderInfo.getOrderNo(),
                FlowTypeEnum.WECHAT.getValue(),
                "FIUU退款",
                2);
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByNotInStateAndOrderId(3, mallRefundEntity.getOrderId());
        if (CollUtil.isEmpty(mallOrderItemList)) {
            MallOrderInfo mallOrderRefund = mallOrderInfoMapper.selectById(mallRefundEntity.getOrderId());
            mallOrderRefund.setStatus(6);
            mallOrderInfoMapper.updateById(mallOrderRefund);
        }
    }
    private void updateOnRejected(MallRefundEntity mallRefundEntity, MallOrderItem mallOrderItem) {
        mallOrderItem.setState(1);
        mallOrderItemMapper.updateById(mallOrderItem);
        mallRefundEntity.setState(2);
        mallRefundMapper.updateById(mallRefundEntity);
    }
}