package com.hzs.third.job;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.member.statis.ICuBonusStatisServiceApi;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.time.temporal.ChronoUnit;
import java.util.Date;

@ConditionalOnProperty(name = "xxl-job.start", havingValue = "true")
@Component
@Slf4j
public class MemberJob {

    @DubboReference
    ICuBonusStatisServiceApi iCuBonusStatisServiceApi;

    /**
     * 定时统计奖金拨比
     */
    @XxlJob("bonus-statis")
    public void cuBonusStatis() {
        //查询前一天的订单和奖金
        Date date = DateUtils.currentDate();
        date = DateUtils.beforeDate(1, ChronoUnit.DAYS, date);
        String payTimeStr = (DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date));
        R<Boolean> statis = iCuBonusStatisServiceApi.statisHistoryBonus(payTimeStr, payTimeStr);
        if (!statis.getData()) {
            log.info("{}-定时统计奖金拨统计失败", DateUtils.currentDate());
        }

    }

    /**
     * 重算7天内奖金拨比
     **/
    @XxlJob("rerun-bonus-statis")
    public void rerunCuBonusStatis() {
        //查询前一天的订单和奖金
        Date date = DateUtils.currentDate();
        date = DateUtils.beforeDate(2, ChronoUnit.DAYS, date);
        Date endDate = DateUtils.beforeDate(5, ChronoUnit.DAYS, date);
        String endDateStr = (DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date));
        String startDateStr = (DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endDate));
        //删除前6天的数据
        R<Boolean> delstatis = iCuBonusStatisServiceApi.delStatis(startDateStr, endDateStr);
        if (delstatis.getData()) {
            //重算前6天的数据
            R<Boolean> statis = iCuBonusStatisServiceApi.statisHistoryBonus(startDateStr, endDateStr);
            if (!statis.getData()) {
                log.info("{}-重算7天定时统计奖金拨统计失败", DateUtils.currentDate());
            }
        }
    }

}
