package org.jeecg.modules.largeScreen.task;

import org.jeecg.modules.itdmAlarm.entity.ItdmAlarm;
import org.jeecg.modules.itdmAlarm.mapper.ItdmAlarmMapper;
import org.jeecg.modules.itdmAlarm.service.IItdmAlarmService;
import org.jeecg.modules.itdmAlarmTotal.entity.ItdmAlarmTotal;
import org.jeecg.modules.itdmAlarmTotal.service.IItdmAlarmTotalService;
import org.jeecg.modules.itdmDataHistory.mapper.ItdmWenxiangMapper;
import org.jeecg.modules.itdmWenxiangHistory.mapper.YCDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component // 把此类托管给 Spring，不能省略
public class TaskUtils {

    @Autowired
    @SuppressWarnings("all")
    private IItdmAlarmTotalService alarmTotalService;
    @Autowired
    @SuppressWarnings("all")
    private IItdmAlarmService alarmService;
    @Autowired
    @SuppressWarnings("all")
    private ItdmAlarmMapper alarmMapper;
    @Autowired
    @SuppressWarnings("all")
    private ItdmWenxiangMapper wenxiangMapper;
    @Autowired
    @SuppressWarnings("all")
    private YCDataMapper ycDataMapper;


    // 添加定时任务
//    @Scheduled(cron = "59 59 23 0 0 5") // cron 表达式，每周五 23:59:59 执行
    @Scheduled(cron = "0 0 12 * * ?") // cron 表达式，每天中午12:00 执行
    public void doTask(){
        System.out.println("我是定时任务————更新本月报警总数");

        SimpleDateFormat f1 = new SimpleDateFormat("yyyy");
        SimpleDateFormat f2 = new SimpleDateFormat("MM");
        int year = Integer.parseInt(f1.format(new Date()));
        int month = Integer.parseInt(f2.format(new Date()));

        List<ItdmAlarm> itdmAlarmList = alarmMapper.selectByYearMouthDay(year,month);

        Map<String, Object> alarmTotalMap = new HashMap<>();
        alarmTotalMap.put("alarm_year",year);
        alarmTotalMap.put("alarm_month",month);
        List<ItdmAlarmTotal> list = alarmTotalService.listByMap(alarmTotalMap);
        if(!list.isEmpty()){ //存在，修改
            ItdmAlarmTotal alarmTotal = list.get(0);
            alarmTotal.setAlarmTotal(itdmAlarmList.size());
            alarmTotalService.updateById(alarmTotal);
        }else{ //不存在，新增
            ItdmAlarmTotal alarmTotal = new ItdmAlarmTotal();
            alarmTotal.setAlarmYear(year);
            alarmTotal.setAlarmMonth(month);
            alarmTotal.setAlarmTotal(itdmAlarmList.size());
            alarmTotalService.save(alarmTotal);
        }


    }

    @Scheduled(cron = "0 0 0 1 * ?") // cron 表达式，每月1号0点 执行
    public void doTask1(){
        System.out.println("我是定时任务————更新上个月报警总数");

        SimpleDateFormat f1 = new SimpleDateFormat("yyyy");
        SimpleDateFormat f2 = new SimpleDateFormat("MM");
        int year = Integer.parseInt(f1.format(new Date()));
        int month = Integer.parseInt(f2.format(new Date()));

        int lastMonth = month; //上个月
        int lastYear = year; //上个月所在年份
        if(month==1){
            lastMonth=12;
            lastYear = year-1;
        }else{
            lastMonth=month-1;
            lastYear = year;
        }

        List<ItdmAlarm> itdmAlarmList1 = alarmMapper.selectByYearMouthDay(year,month);

        Map<String, Object> alarmTotalMap1 = new HashMap<>();
        alarmTotalMap1.put("alarm_year",lastYear);
        alarmTotalMap1.put("alarm_month",lastMonth);
        List<ItdmAlarmTotal> list1 = alarmTotalService.listByMap(alarmTotalMap1);
        if(!list1.isEmpty()){ //存在，修改
            ItdmAlarmTotal alarmTotal = list1.get(0);
            alarmTotal.setAlarmTotal(itdmAlarmList1.size());
            alarmTotalService.updateById(alarmTotal);
        }else{ //不存在，新增
            ItdmAlarmTotal alarmTotal = new ItdmAlarmTotal();
            alarmTotal.setAlarmYear(lastYear);
            alarmTotal.setAlarmMonth(lastMonth);
            alarmTotal.setAlarmTotal(itdmAlarmList1.size());
            alarmTotalService.save(alarmTotal);
        }
    }

    //删除所有温箱设备一年之前的数据
    @Scheduled(cron = "30 30 13 * * 7") // cron 表达式，每周日13点30分30秒 执行
    public void deleteData(){
        System.out.println("我是定时任务————删除所有温箱设备一年之前的数据");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); //当前时间
//        calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
        calendar.add(Calendar.MONTH, -3);//当前时间减去三个月，即只保留最近三个月的数据
        Date deleteDay = calendar.getTime();
        System.out.println("删除原温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        wenxiangMapper.deleteByTime(deleteDay);


        System.out.println("删除硬采0.5温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTime05(deleteDay);
        System.out.println("删除硬采1温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTime1(deleteDay);
        System.out.println("删除硬采8温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTime8(deleteDay);
        System.out.println("删除硬采16温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTime16(deleteDay);
        System.out.println("删除硬采48温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTime48(deleteDay);
        System.out.println("删除硬采霉菌温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTimeMJ(deleteDay);
        System.out.println("删除硬采三综合温箱历史数据表该时间之前的所有数据:"+ deleteDay);
        ycDataMapper.deleteYCDataByTimeSZH(deleteDay);
        System.out.println("删除完成："+ deleteDay);
    }


}

