package com.fs.his.task;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.service.ICompanyService;
import com.fs.course.service.BalanceRollbackErrorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: 公司余额同步定时任务 (红包余额)
 * @author: Xgb
 * @createDate: 2025/10/22
 * @version: 1.0
 */
@Component("companyBalanceTask")
@Slf4j
public class CompanyBalanceTask {


    @Autowired
    private BalanceRollbackErrorService balanceRollbackErrorService;

    @Autowired
    private ICompanyService companyService;


    /**
     * @Description: 每10分钟从缓存获取余额同步到公司账户中
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/10/22 10:56
     */
    public void syncCompanyBalance() {
        companyService.syncCompanyBalance();
    }

    /**
     * @Description: 定时回滚公司余额数据
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/10/22 11:48
     */
    public void processBatchRollbackByCompanyId() {
        balanceRollbackErrorService.processBatchRollbackByCompanyId();
    }

    /**
     * @Description: spring启动执行 initCompanyBalance 查询余额报存到缓存中
     * 如果新增公司可以重启admin或者定时配置一下这个方法手动执行一次
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/10/22 11:52
     */
    public void initCompanyBalance() {
        balanceRollbackErrorService.initCompanyBalance();
    }

    /**
     * @Description: 优化成回滚前查询记录，一笔一笔回滚
     * @Param: 每天0点执行一次
     * @Return:
     * @Author xgb
     * @Date 2025/11/7 9:48
     */
    public void rollbackRedPacketMoney(String time) throws Exception {
        // 默认是前两天时间
        String createSTime;
        String createETime;
        if (StringUtils.isNotBlank(time)) {
            Date date = DateUtils.parseDate(time);
            createSTime = time+" 00:00:00";
            createETime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.addDays(date, 1))+" 00:00:00";
        } else {
            createSTime = DateUtils.parseDateToStr( DateUtils.YYYY_MM_DD,DateUtils.addDays(new Date(), -2))+" 00:00:00";
            createETime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.addDays(new Date(), -1))+" 00:00:00";
        }

        // 这个地方真加的是company money字段 xgb
        companyService.rollbackRedPacketMoney(createSTime, createETime);
    }

    /**
     * @Description: 每天晚上0点定时获取获取公司余额，记录到数据库中
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/11/4 10:43
     */
    public void recordRedPacketBalance() {
        companyService.recordRedPacketBalance();
    }



    /**
     * @Description: 更具批次号查询转账结果
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/12/25 10:31
     */
    public void checkMchTransferStatus(String outBatchNo,String companyIdStr) {
        Long companyId=Long.parseLong(companyIdStr);
        String result=companyService.checkMchTransferStatus(outBatchNo,companyId);
        log.info("查询商户转账结果：{}",result);
    }



    /**
     * @Description: 更具批次号查询转账结果
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/12/25 10:31
     */
    public void checkMchTransferStatusByBatchID(String batchId,String companyIdStr) {
        Long companyId=Long.parseLong(companyIdStr);
        R result=companyService.checkMchTransferStatusByBatchID(batchId,companyId);
        log.info("查询商户转账结果：{}",result);
    }




}
