package com.hzs.third.job;

import com.hzs.activity.pick.IAcRetailPickLogServiceApi;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.statis.ICuBonusStatisServiceApi;
import com.hzs.member.statis.ICuBonusVertexStatisServiceApi;
import com.hzs.report.stat.IMemberReportServiceApi;
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
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    ICuBonusStatisServiceApi iCuBonusStatisServiceApi;
    @DubboReference
    ICuBonusVertexStatisServiceApi iCuBonusVertexStatisServiceApi;
    @DubboReference
    IAcRetailPickLogServiceApi iAcRetailPickLogServiceApi;
    @DubboReference
    IMemberReportServiceApi iMemberReportServiceApi;

    /**
     * 定时统计奖金拨比
     */
    @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());
            }
        }
    }

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

    /**
     * 7天重算顶点奖金拨比统计 -- 新零售
     **/
    @XxlJob("rerun-bonus-vertex-statis")
    public void rerunCuBonusVertexStatis() {
        //查询前一天的订单和奖金
        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 = iCuBonusVertexStatisServiceApi.delStatis(startDateStr, endDateStr);
        if (delstatis.isSuccess()) {
            //重算前6天的数据
            R<Boolean> statis = iCuBonusVertexStatisServiceApi.anewStatisHistoryBonus(startDateStr, endDateStr);
            if (!statis.getData()) {
                log.info("{}-重算7天定时统计顶点奖金拨统计失败", DateUtils.currentDate());
            }
        }
    }

    /**
     * 定时清除未使用的会员编号
     */
    @XxlJob("clear-member-empty-code")
    public void clearMemberEmptyCode() {
        log.info("clear-member-empty-code 开始执行!");
        Integer dataCount = iMemberServiceApi.clearMemberEmptyCode().getData();
        log.info("clear-member-empty-code 开始结束! 共处理：{}", dataCount);
    }

    /**
     * 每月10号处理上个月提货赠送活动满足活动规则的数据
     **/
    @XxlJob("deliveryLog")
    public void deliveryLog() {
        iAcRetailPickLogServiceApi.deliveryLog();
    }

    /**
     * 统计直推会员金额业绩盒数
     **/
    @XxlJob("sta-smount-box")
    public void pushAmountBoxmethod() {
        iMemberReportServiceApi.pushAmountBoxmethod();
    }

    /**
     * 重算7天统计直推会员金额业绩盒数
     **/
    @XxlJob("repeat-sta-smount-box")
    public void repeatPushAmountBoxmethod() {
        //查询前一天的订单和奖金
        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天的数据
        iMemberReportServiceApi.repeatPushAmountBoxmethod(startDateStr, endDateStr);
    }


}
