package com.jeequan.jeepay.pay.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.utils.HolidayUtil;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.db.entity.SettleInfo;
import com.jeequan.jeepay.service.impl.PayOrderService;
import com.jeequan.jeepay.service.impl.SettleInfoService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * TODO
 * 商户结算数据定时任务统计
 * @author crystal
 * @date 2023/12/8 9:57
 */
@Component
@Slf4j
public class MchSettleInfoTask {

    @Autowired private PayOrderService payOrderService;
    @Autowired private SettleInfoService settleInfoService;

    @XxlJob("mchSettleInfoTaskHandler")
    public void start() {
        log.info("【处理商户结算数据】定时任务开始");
//        Date startDateTime = DateUtil.parse("2024-03-20 00:00:00", DatePattern.NORM_DATETIME_PATTERN);
//        Date endDateTime = DateUtil.parse("2024-03-20 23:59:59", DatePattern.NORM_DATETIME_PATTERN);
        Date startDateTime = DateUtil.beginOfDay(DateUtil.yesterday());
        Date endDateTime = DateUtil.endOfDay(DateUtil.yesterday());
        List<PayOrder> list = payOrderService.listMchSuccTradeData(CS.SETTLEMENT_TYPE.D1,startDateTime,endDateTime);
        if(!list.isEmpty()){
            List<SettleInfo> infos = new ArrayList<>();
            list.forEach(item -> {
                //计算退款金额
                Long totalRefundAmount = payOrderService.getNumberData(item, startDateTime, endDateTime);
                SettleInfo info = new SettleInfo(item,totalRefundAmount,startDateTime,endDateTime);
//                info.setSettleDate("2023-03-21");
                infos.add(info);
            });
            if(!infos.isEmpty()){
                settleInfoService.saveBatch(infos);
            }
        }
        //判断是否是节假日 如果是节假日 则不处理 T1的结算记录
//        List<String> settleDate = Arrays.asList("2024-03-20","2024-03-20");
        List<String> settleDate = HolidayUtil.getSettleDate(new Date());
        if(!settleDate.isEmpty()){
            Date startDate = DateUtil.beginOfDay(DateUtil.parseDate(settleDate.get(0)));
            Date endDate = DateUtil.endOfDay(DateUtil.parseDate(settleDate.get(1)));
            List<PayOrder> t1list = payOrderService.listMchSuccTradeData(CS.SETTLEMENT_TYPE.T1,startDate,endDate);
            List<SettleInfo> t1Infos = new ArrayList<>();
            t1list.forEach(item -> {
                //计算退款金额
                Long totalRefundAmount = payOrderService.getNumberData(item, startDate,endDate);
                SettleInfo info = new SettleInfo(item ,totalRefundAmount,startDate,endDate);
//                info.setSettleDate("2023-03-21");
                t1Infos.add(info);
            });
            if(!t1Infos.isEmpty()){
                settleInfoService.saveBatch(t1Infos);
            }
        }
        log.info("【处理商户结算数据】定时任务结束");
    }
}
