package cn.com.victorysoft.business.calc.task;

import cn.com.victorysoft.business.calc.service.*;
import cn.com.victorysoft.business.sys.entity.DateRange;
import cn.com.victorysoft.business.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * 数据同步任务调度
 */
@Slf4j
@Component
public class DataSyncTask {

    @Value("${data.calc.enabled}")
    private boolean calcEnabled;

    @Resource
    private DataSyncService dataSyncService;
    @Resource
    private QualityDataSyncService qualityDataSyncService;
    @Resource
    private FlushDataSyncService flushDataSyncService;
    @Resource
    private AdjustDataSyncService adjustDataSyncService;
    @Resource
    private AbnormalDataSyncService abnormalDataSyncService;
    @Resource
    private ConditionDataSyncService conditionDataSyncService;
    @Resource
    private RealtimeService realtimeService;
    @Resource
    private WellToolDataSyncService wellToolDataSyncService;
    @Resource
    private WellTubeDataSyncService wellTubeDataSyncService;

    @Scheduled(cron = "0 0 23 * * ?")
    public void syncStatic() {
        if (calcEnabled) {
            log.info("静态数据同步任务开始");
            dataSyncService.syncStaticData();
            log.info("静态数据同步任务结束");
        }
    }

    /**
     * 每月同步上月数据，汇总月度数据
     */
    @Scheduled(cron = "0 0 6 1,2 * ?")
    public void syncDaily() {
        if (calcEnabled) {
            log.info("日度注水数据同步任务开始");
            // 本月第一天
            Date firstDate = DateUtils.truncate(new Date(), Calendar.MONTH);
            // 上月最后一天
            Date endDate = DateUtils.addDays(firstDate, -1);
            // 上月第一天
            Date startDate = DateUtils.truncate(endDate, Calendar.MONTH);
            // 报错重跑
            try {
                dataSyncService.syncWellSectionDaily(startDate, endDate);
            } catch (Exception e) {
                log.error("日度注水数据同步失败", e);
                dataSyncService.syncWellSectionDaily(startDate, endDate);
            }
            log.info("日度注水数据同步任务结束");
            // 汇总月度数据
            DateRange dateRange = DateUtils.getLastMonth(new Date());
            String month = dateRange.getName();
            dataSyncService.summaryMonth(month);
        }
    }

    /**
     * 每月同步上月数据，包括井下工具和在井管柱-------------
     */
    @Scheduled(cron = "0 0 5 1 * ?")
    public void syncMonth() {
        if (calcEnabled) {
            // 本月第一天
            Date firstDate = DateUtils.truncate(new Date(), Calendar.MONTH);
            // 上月最后一天
            Date endDate = DateUtils.addDays(firstDate, -1);
            // 上月第一天
            Date startDate = DateUtils.truncate(endDate, Calendar.MONTH);
            wellToolDataSyncService.sync(startDate, endDate);
            wellTubeDataSyncService.sync(startDate, endDate);
        }
    }

    /**
     * 每天同步注水日度数据，停用，改为月初统一同步
     */
//    @Scheduled(cron = "0 0 20 * * ?")
    public void syncWaterDailyData() {
        if (calcEnabled) {
            Date date = DateUtils.truncate(new Date(), Calendar.DATE);

            log.info("日度注水数据同步任务开始");
            // 本月第一天
            Date firstDate = DateUtils.truncate(date, Calendar.MONTH);
            dataSyncService.syncWellSectionDaily(firstDate, date);
            log.info("日度注水数据同步任务结束");
        }
    }

    /**
     * 每天同步日度数据-----
     */
    @Scheduled(cron = "0 0 14,22 * * ?")
    public void syncDailyData() {
        if (calcEnabled) {
            Date date = DateUtils.truncate(new Date(), Calendar.DATE);

            log.info("水质监测数据同步任务开始");
            // 30天前
//            qualityDataSyncService.syncQualityData(DateUtils.addDays(date, -30), date);
            try {
                qualityDataSyncService.xchyfxSzjcsj(DateUtils.addDays(date, -30), date);
            }catch (Exception e) {
                log.error("水质监测数据同步任务异常", e);
            }
            log.info("水质监测同步任务结束");

            log.info("洗井数据同步任务开始");
            // 2天前
            try {
                flushDataSyncService.syncFlushRecord(DateUtils.addDays(date, -30), date);
            }catch (Exception e) {
                log.error("洗井数据同步任务异常", e);
            }
            log.info("洗井数据同步任务结束");

            log.info("异常井数据同步任务开始");
            try {
                abnormalDataSyncService.syncAbnormalWell(date);
                abnormalDataSyncService.summaryAbnormalUnit(date);
            }catch (Exception e) {
                log.error("异常井数据同步任务异常", e);
            }
            log.info("异常井数据同步任务结束");

            log.info("单位工况数据汇总任务开始");
            try {
                conditionDataSyncService.summary(date);
            }catch (Exception e) {
                log.error("单位工况数据同步任务异常", e);
            }
            log.info("单位工况数据汇总任务结束");
        }
    }

    /**
     * 每天8点同步注水泵数据
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void syncPumpData() {
        if (calcEnabled) {
            log.info("注水泵数据同步任务开始");
            realtimeService.syncPumpDeviceList();
            log.info("注水泵同步任务结束");
        }
    }

    /**
     * 26号生成测调计划，月底再生成一次----------
     */
    @Scheduled(cron = "0 0 23 26,28,29,30,31 * ?")
    public void generateAdjustPlan() {
        if (calcEnabled) {
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DATE);
            //如果不是26号，也不是最后一天，返回
            if (day != 26 && day != calendar.getActualMaximum(Calendar.DATE)) {
                return;
            }
            Date date = DateUtils.truncate(new Date(), Calendar.DATE);
            log.info("测调数据生成任务开始");
            adjustDataSyncService.generateAdjustPlan(date);
            log.info("测调数据生成任务结束");
        }
    }

    /**
     * 每天更新当前月计划的完成情况
     */
    @Scheduled(cron = "0 50 23 * * ?")
    public void finishAdjustPlan() {
        if (calcEnabled) {
            log.info("测调数据更新任务开始");
            adjustDataSyncService.finishAdjustPlan();
            log.info("测调数据更新任务结束");
        }
    }
}
