package com.ruoyi.web.v2.v1.timer;


import com.ruoyi.web.v2.v1.model.JsCheckDevice;
import com.ruoyi.web.v2.v1.model.JsCheckInstrument;
import com.ruoyi.web.v2.v1.model.JsMaintenanceWarning;
import com.ruoyi.web.v2.v1.mapper.JsCheckDeviceMapper;
import com.ruoyi.web.v2.v1.mapper.JsCheckInstrumentMapper;
import com.ruoyi.web.v2.v1.mapper.JsMaintenanceWarningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Configuration
@EnableScheduling
public class WarningTimer {

    @Autowired
    private JsCheckDeviceMapper checkDeviceMapper;
    @Autowired
    private JsMaintenanceWarningMapper warningMapper;
    @Autowired
    private JsCheckInstrumentMapper instrumentMapper;

    //
    @Scheduled(cron = "0 0 0 * * ? ")
    private void getShenChan() throws ParseException {
        List<JsCheckDevice> jsCheckDevices = checkDeviceMapper.selectList(null);
        if (jsCheckDevices.size() != 0) {
            for (JsCheckDevice jsCheckDevice : jsCheckDevices) {
                String investmentTime = jsCheckDevice.getInvestmentTime();
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
                Date date = ft.parse(investmentTime);
                Date addDays = addDays(date, jsCheckDevice.getDays());
                String format = ft.format(addDays);
                Date now = new Date();
                if (ft.format(now).equals(format)) {
                    JsMaintenanceWarning warning = new JsMaintenanceWarning();
                    warning.setMaintenanceTime(format);
                    warning.setDeviceNum(jsCheckDevice.getDeviceNum());
                    warning.setContent("设备"+jsCheckDevice.getDeviceNum()+"需要维护");
                    warning.setIsHandle(0);
                    warningMapper.insert(warning);
                }
            }
        }

        List<JsCheckInstrument> jsCheckInstruments = instrumentMapper.selectList(null);
        if (jsCheckInstruments.size() != 0) {
            for (JsCheckInstrument jsCheckDevice : jsCheckInstruments) {
                String investmentTime = jsCheckDevice.getInvestmentTime();
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
                Date date = ft.parse(investmentTime);
                Date addDays = addDays(date, jsCheckDevice.getDays());
                String format = ft.format(addDays);
                Date now = new Date();
                if (ft.format(now).equals(format)) {
                    JsMaintenanceWarning warning = new JsMaintenanceWarning();
                    warning.setMaintenanceTime(format);
                    warning.setDeviceNum(jsCheckDevice.getDeviceNum());
                    warning.setContent("检验设备"+jsCheckDevice.getDeviceNum()+"需要维护");
                    warning.setIsHandle(0);
                    warningMapper.insert(warning);
                }
            }
        }
    }
    public Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }
}
