KKSU
2025-02-07 05d3159f2a95904a9669e09bbbbb00b3ba2f5530
src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java
@@ -1,29 +1,23 @@
package cc.mrbird.febs.mall.quartz;
import cc.mrbird.febs.common.enumerates.AgentLevelEnum;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.utils.MallUtils;
import cc.mrbird.febs.mall.entity.MallMember;
import cc.mrbird.febs.mall.entity.MallMoneyFlow;
import cc.mrbird.febs.mall.mapper.MallMemberMapper;
import cc.mrbird.febs.mall.mapper.MallMoneyFlowMapper;
import cc.mrbird.febs.mall.service.IApiMallMemberService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IMemberProfitService;
import cc.mrbird.febs.mall.service.impl.CommonService;
import cc.mrbird.febs.common.enumerates.YesOrNoOrIngEnum;
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;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
/**
@@ -32,46 +26,164 @@
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "system", name = "job", havingValue = "true")
public class ProfitJob {
    @Autowired
    private IMemberProfitService memberProfitService;
    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;
    /**
     * 代理分红
     * 一分钟运行一次
     *      结束活动
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void profitJob() {
        memberProfitService.agentProfit(null);
    }
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void stopActivity() {
        DateTime date = DateUtil.date();
        List<MallActivity> mallActivities =
                mallActivityMapper.selectList(
                        new LambdaQueryWrapper<MallActivity>()
                                .eq(MallActivity::getState, YesOrNoOrIngEnum.ING.getValue())
                                .le(MallActivity::getEndTime, date)
                );
        if(CollUtil.isEmpty(mallActivities)){
            return;
        }
    @Scheduled(cron = "0 30 1 * * ?")
    public void storeAndDirectorJob() {
        memberProfitService.storeAndDirectorProfit(null);
        mallActivities.forEach(mallActivity -> {
                mallActivity.setState(YesOrNoOrIngEnum.NO.getValue());
                mallActivityMapper.updateById(mallActivity);
        });
    }
    /**
     * 感恩奖
     * 一分钟运行一次
     *      开始活动
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void thankfulJob() {
        memberProfitService.thankfulProfit(null);
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void startActivity() {
        DateTime date = DateUtil.date();
        List<MallActivity> mallActivities =
                mallActivityMapper.selectList(
                        new LambdaQueryWrapper<MallActivity>()
                                .eq(MallActivity::getState, YesOrNoOrIngEnum.YES.getValue())
                                .le(MallActivity::getStartTime, date)
                                .ge(MallActivity::getEndTime, date)
                );
        if(CollUtil.isEmpty(mallActivities)){
            return;
        }
        mallActivities.forEach(mallActivity -> {
                mallActivity.setState(YesOrNoOrIngEnum.ING.getValue());
                mallActivityMapper.updateById(mallActivity);
        });
    }
    /**
     * 静态分红
     * 一分钟运行一次
     *      查询退款记录,去验证是否已经退款成功
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void staticProfitJob() {
        memberProfitService.staticProfit(null);
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void refundJob() {
        LambdaQueryWrapper<MallRefundEntity> mallOrderRefundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mallOrderRefundLambdaQueryWrapper.eq(MallRefundEntity::getState, 3);
        mallOrderRefundLambdaQueryWrapper.eq(MallRefundEntity::getType, 1);
        List<MallRefundEntity> mallRefundEntities = mallRefundMapper.selectList(mallOrderRefundLambdaQueryWrapper);
        if(CollUtil.isEmpty(mallRefundEntities)){
            return;
        }
        mallRefundEntities.forEach(mallRefundEntity -> {
            processRefund(mallRefundEntity);
        });
    }
    /**
     * 排名奖 每月1号
     */
    @Scheduled(cron = "0 30 0 1 * ?")
    public void rankJob() {
        memberProfitService.rankProfit();
    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);
    }
}