package com.hzs.third.job;

import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
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.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 memberServiceApi;
    @DubboReference
    ICuBonusStatisServiceApi iCuBonusStatisServiceApi;
    @DubboReference
    ICuBonusVertexStatisServiceApi iCuBonusVertexStatisServiceApi;

    /**
     * 定时统计奖金拨比
     */
    @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> stats = iCuBonusStatisServiceApi.statsHistoryBonus(payTimeStr, payTimeStr);
        if (!stats.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> deltas = iCuBonusStatisServiceApi.delStats(startDateStr, endDateStr);
        if (deltas.getData()) {
            //重算前6天的数据
            R<Boolean> stats = iCuBonusStatisServiceApi.statsHistoryBonus(startDateStr, endDateStr);
            if (!stats.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> stats = iCuBonusVertexStatisServiceApi.anewStatsHistoryBonus(payTimeStr, payTimeStr);
        if (!stats.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<?> deltas = iCuBonusVertexStatisServiceApi.delStats(startDateStr, endDateStr);
        if (deltas.isSuccess()) {
            //重算前6天的数据
            R<Boolean> stats = iCuBonusVertexStatisServiceApi.anewStatsHistoryBonus(startDateStr, endDateStr);
            if (!stats.getData()) {
                log.info("{}-重算7天定时统计顶点奖金拨统计失败", DateUtils.currentDate());
            }
        }
    }

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

    /**
     * 处理会员续约状态
     */
    @XxlJob("member-expire-data")
    public void memberExpireData() {
        R<?> resultR = memberServiceApi.handleExpireDate(MagicNumberConstants.PK_ADMIN, CountryConstants.CHINA_COUNTRY);
        if (!resultR.isSuccess()) {
            log.error("处理会员续约状态出现异常，{}", resultR.getMsg());
        }
    }

}
