package com.ruoyi.web.v2.timer;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.framework.datasource.DynamicDataSourceService;
import com.ruoyi.web.primary.entity.DatabaseMetadata;
import com.ruoyi.web.primary.service.IDatabaseMetadataService;
import com.ruoyi.web.v2.entity.JsStoreTransportInfo;
import com.ruoyi.web.v2.mapper.JsStoreTransportInfoMapper;
import com.ruoyi.web.v2.model.JsCheckDevice;
import com.ruoyi.web.v2.model.JsCheckInstrument;
import com.ruoyi.web.v2.model.JsMaintenanceWarning;
import com.ruoyi.web.v2.mapper.JsCheckDeviceMapper;
import com.ruoyi.web.v2.mapper.JsCheckInstrumentMapper;
import com.ruoyi.web.v2.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);
                }
            }
        }
    }
    @Autowired
    private DynamicDataSourceService dynamicDataSourceService;
    @Autowired
    private IDatabaseMetadataService databaseMetadataService;
    @Autowired
    private JsStoreTransportInfoMapper storeTransportInfoMapper;
    @Scheduled(cron = "0 1 0 * * ? ")
//    @Scheduled(cron = "3 * * * * ? ")
    private void saveZhuanYunKuChun()  {
        dynamicDataSourceService.switchToMaster();
        List<DatabaseMetadata> list = databaseMetadataService.list();
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        System.out.println("start====1");
        for (DatabaseMetadata databaseMetadata : list) {
            dynamicDataSourceService.switchDataSource(databaseMetadata.getDbName());
            Long integer = storeTransportInfoMapper.selectCount(new QueryWrapper<JsStoreTransportInfo>().ge("instock_date", dateTime));
            if (integer   == 0){
                System.out.println(databaseMetadata.getDbName());
                try {
                JsStoreTransportInfo jsStoreTransportInfo = storeTransportInfoMapper.selectOne(new QueryWrapper<JsStoreTransportInfo>().orderByDesc("id").last("limit 1"));
                JsStoreTransportInfo jsStoreTransportInfo1 = new JsStoreTransportInfo();
                if (ObjectUtil.isNotEmpty(jsStoreTransportInfo)){
                    jsStoreTransportInfo1.setStockNum(jsStoreTransportInfo.getStockNum());
                }
                jsStoreTransportInfo1.setInstockDate(new Date());

                 storeTransportInfoMapper.insert(jsStoreTransportInfo1);
                }catch (Exception e){
                    System.out.println(e);
                }

            }

        }



    }
    public Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }
}
