package com.zcxk.xxljob.jobs;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import com.zcxk.xxljob.service.MeterReadRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author linqingwei
 * @date 2021-08-11 10:57
 */
@Slf4j
@Component
public class MeterReadJob {

    @Autowired
    private MeterReadRecordService meterReadRecordService;

    /*
     * @description 生成明天未抄记录
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("meterReadJobHandler")
    public ReturnT<String> meterReadJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin Meter Read Job.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            // more执行明天的未抄记录
            map.put("readDay",getTomorrowDay());
        }

        // 分片参数
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        if (shardingVO == null) {
            shardingVO = new ShardingUtil.ShardingVO(0,1);
        }
        XxlJobLogger.log("分片参数：当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());

        // 业务逻辑
        meterReadRecordService.executeCreateMeterUnReadRecord(map,shardingVO.getIndex(), shardingVO.getTotal());

        XxlJobLogger.log("XXL-JOB,end Meter Read Job.");
        return ReturnT.SUCCESS;
    }

    protected  int getYesterdayDay(){
        Date tomorrow = DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH,  -1);
        int tomorrowDay = Integer.parseInt(DateUtil.format(tomorrow, "yyyyMMdd"));
        return tomorrowDay;
    }

    protected  int getTomorrowDay(){
        Date tomorrow = DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH,  1);
        int tomorrowDay = Integer.parseInt(DateUtil.format(tomorrow, "yyyyMMdd"));
        return tomorrowDay;
    }


    /**
     * @description 同步昨天抄表记录到MySQL
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("syncMeterReadJobHandler")
    public ReturnT<String> syncMeterReadJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin syncMeterReadJobHandler.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            // more执行明天的未抄记录
            map.put("readDay",getYesterdayDay());
        }



        // 业务逻辑
        meterReadRecordService.executeSyncMeterReadRecord(map);

        XxlJobLogger.log("XXL-JOB,end syncMeterReadJobHandler.");
        return ReturnT.SUCCESS;
    }

    /*
     * @description 生成今天抄表率
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("todayReadRateJobHandler")
    public ReturnT<String> todayReadRateJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin todayReadRateJobHandler.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            map.put("readDay",getTomorrowDay());
        }

        // 业务逻辑
        meterReadRecordService.todayReadRateJobHandler(map);

        XxlJobLogger.log("XXL-JOB,end todayReadRateJobHandler. ");
        return ReturnT.SUCCESS;
    }


    /*
     * @description 统计昨天抄表率
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("yesterdayReadRateJobHandler")
    public ReturnT<String> yesterdayReadRateJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin yesterdayReadRateJobHandler.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            map.put("readDay",getYesterdayDay());
        }

        // 业务逻辑
        meterReadRecordService.yesterdayReadRateJobHandler(map);

        XxlJobLogger.log("XXL-JOB,end yesterdayReadRateJobHandler. ");
        return ReturnT.SUCCESS;
    }


    /*
     * @description 统计近7天抄表率
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("recent7daysReadRateJobHandler")
    public ReturnT<String> recent7daysReadRateJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin recent7daysReadRateJobHandler.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            map.put("readDay",getYesterdayDay());
        }

        // 业务逻辑
        meterReadRecordService.recent7daysReadRateJobHandler(map);


        XxlJobLogger.log("XXL-JOB,end recent7daysReadRateJobHandler. ");
        return ReturnT.SUCCESS;
    }

    /*
     * @description 统计上月抄表率
     * @param param
     * @return
     * @author linqingwei
     **/
    @XxlJob("lastMonthReadRateJobHandler")
    public ReturnT<String> lastMonthReadRateJobHandler(String param) {
        XxlJobLogger.log("XXL-JOB,begin lastMonthReadRateJobHandler.Param = {}",param);


        Map<String,Object> map = null ;
        if(StringUtils.isNotBlank(param)){
            map = JSON.parseObject(param, HashMap.class);
        }
        else{
            map = new HashMap<>(10);
            map.put("readDay",getYesterdayDay());
        }

        // 业务逻辑
        meterReadRecordService.lastMonthReadRateJobHandler(map);

        XxlJobLogger.log("XXL-JOB,end lastMonthReadRateJobHandler");
        return ReturnT.SUCCESS;
    }

}
