package com.hzs.third.job;

import com.hzs.bonus.settle.IMemberSettleBonusApi;
import com.hzs.common.core.utils.DateUtils;
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;

/**
 * 每日奖金计算定时任务
 */
@Component
@Slf4j
@ConditionalOnProperty(name = "xxl-job.start", havingValue = "true")
public class BonusSettleJob {

    @DubboReference
    private IMemberSettleBonusApi iMemberSettleBonusApi;

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus")
    public void autoSettleBonusEveryday() {
        Date startDate = DateUtils.beforeDate(7, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus1")
    public void autoSettleBonusEveryday1() {
        Date startDate = DateUtils.beforeDate(6, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus2")
    public void autoSettleBonusEveryday2() {
        Date startDate = DateUtils.beforeDate(5, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus3")
    public void autoSettleBonusEveryday3() {
        Date startDate = DateUtils.beforeDate(4, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus4")
    public void autoSettleBonusEveryday4() {
        Date startDate = DateUtils.beforeDate(3, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus5")
    public void autoSettleBonusEveryday5() {
        Date startDate = DateUtils.beforeDate(2, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /**
     * 奖金每天自动结算,计算前一天奖金
     */
    @XxlJob("autoSettleBonus6")
    public void autoSettleBonusEveryday6() {
        Date startDate = DateUtils.beforeDate(1, ChronoUnit.DAYS, DateUtils.currentDate());
        log.info("开始进行奖金结算");
        iMemberSettleBonusApi.autoCalculateBonus(startDate);
    }

    /*
     *  自动公布奖金
     **/
    @XxlJob("autoPublishBonus")
    public void autoPublishBonusEveryday() {
        log.info("开始进行奖金公布");
        iMemberSettleBonusApi.autoPublishBonus();
        log.info("奖金公布完成");
    }

    /*
     *  自动发放奖金
     **/
    @XxlJob("autoGrantBonus")
    public void autoGrantBonusEveryday() {
        log.info("开始进行奖金发放");
        iMemberSettleBonusApi.autoGrantBonus();
        log.info("奖金发放完成");
    }

    /*
     *  自动结算每日业绩，每月业绩，用于统计分析
     **/
//    @XxlJob("autoSettleMember")
    public void autoSettleMemberAchieveEveryday() {
        log.info("开始进行会员业绩计算");
        iMemberSettleBonusApi.insertCuMemberAchieveByEveryDay();
    }

}
