package com.tofly.scada.handle;

import cn.hutool.core.date.DateUtil;
import com.tofly.common.constant.CalendarHelper;
import com.tofly.scada.service.ScadaHistorySynService;
import com.tofly.scada.service.impl.ScadaServiceImpl;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @author HaiQiu
 * @since 2023/3/24
 */
@Component
public class ScadaHandle {

    @Autowired
    private ScadaServiceImpl scadaService;

    @Autowired
    private ScadaHistorySynService scadaHistorySynService;


    /**
     * 每月最后28日凌晨2点执行创建下个月的数据表
     * @return
     */
    @XxlJob("createMonths")
    public ReturnT<String> createMonths(){
        scadaService.createMonths();
        return ReturnT.SUCCESS;
    }

    /**
     * 同步24小时数据，支持自定义时间添加
     * @return
     */
    @XxlJob("syncHourData")
    public ReturnT<String> syncHourData(){
        scadaService.insert24H(); //同步24小时数据
        scadaService.syncDaysData(null,null); //同步当天的数据
        return ReturnT.SUCCESS;
    }

    /**
     *  同步每天数据，每天6半同步昨天的数据
     * @return
     */

    @XxlJob("syncDayData")
    public ReturnT<String> syncDayData(){
        //同步昨天的数据，按照天汇总
        //整个日历调整时间
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        //延迟一天
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String dateStr = CalendarHelper.getFormatDate(calendar.getTime(),"yyyy-MM-dd");
        scadaService.syncDaysData(dateStr,null); //天
        scadaService.syncDataIntoMonths(null);//同步当月的数据
        scadaService.syncDataIntoYears(null); //同步当年本年
        return ReturnT.SUCCESS;
    }
    @XxlJob("syncDayDataNew")
    public ReturnT<String> syncDataByTypeData(){
        //同步昨天的数据，按照天汇总
        //整个日历调整时间
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        //延迟一天
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String dateStr = CalendarHelper.getFormatDate(calendar.getTime(),"yyyy-MM-dd");
        scadaService.syncDataByTypeData(dateStr,null,"19","1"); //天

        return ReturnT.SUCCESS;
    }

    /**
     * 同步每天的售水量
     * @return
     */
    @XxlJob("synSalesWaterDay")
    public ReturnT<String> synSalesWaterDay(){
        //同步昨天的数据，按照天汇总
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        //延迟一天
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String dateStr = CalendarHelper.getFormatDate(calendar.getTime(),"yyyy-MM-dd");
        scadaService.synSalesWater(dateStr,dateStr); //天
        return ReturnT.SUCCESS;
    }



    /**
     *每月1号凌晨1点同步更新历史数据与上个月表数据，支持自定义时间添加
     * @return
     */
    @XxlJob("syncMonthData")
    public ReturnT<String> syncMonthData(){
        //每月最后28日凌晨2点执行创建下个月的数据表
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        String date= CalendarHelper.getFormatDate(calendar.getTime(),"yyyy-MM-dd") ;
        scadaService.syncDataIntoMonths(date); //上月
        return ReturnT.SUCCESS;
    }
    @XxlJob("syncYearData")
    public ReturnT<String> syncYearData(){
        //每年同步去年的数据
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int lastYear = currentYear - 1;
        scadaService.syncDataIntoYears(lastYear);
        return ReturnT.SUCCESS;
    }



    /**
     * 	获取历史数据进行报警和监控表更新每分钟10执行一次
     * @return
     */
    @XxlJob("timingInsertData")
    public ReturnT<String> timingInsertData(){
        scadaService.timingInsertData();
        return ReturnT.SUCCESS;
    }

    /**
     * 	推送的历史数据同步到历史表
     * @return
     */
    @XxlJob("synScadaHistoryData")
    public ReturnT<String> synScadaHistoryData(){
        scadaHistorySynService.synScadaHistoryData();
        return ReturnT.SUCCESS;
    }

    /**
     * 	删除推送的历史数据中间表
     * @return
     */
    @XxlJob("delScadaHistoryData")
    public ReturnT<String> delScadaHistoryData(){
        scadaHistorySynService.delScadaHistoryData();
        return ReturnT.SUCCESS;
    }

}
