package com.ruoyi.framework.task;

import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.common.domain.TCommonfile;
import com.ruoyi.project.common.service.ITCommonfileService;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.reliability.domain.TPreventivemaintenance;
import com.ruoyi.project.reliability.service.ITPreventivemaintenanceService;
import com.ruoyi.project.system.domain.TAlarmPrincipal;
import com.ruoyi.project.system.domain.TAlarmhistory;
import com.ruoyi.project.system.domain.TAlarmtype;
import com.ruoyi.project.system.service.ITAlarmPrincipalService;
import com.ruoyi.project.system.service.ITAlarmhistoryService;
import com.ruoyi.project.system.service.ITAlarmtypeService;
import com.ruoyi.project.training.domain.TTrainingrecords;
import com.ruoyi.project.training.domain.TWorklicense;
import com.ruoyi.project.training.service.ITTrainingrecordsService;
import com.ruoyi.project.training.service.ITWorklicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 培训成绩是否过期预警
 *
 * @author ruoyi
 */
@Component
public class TrainingRecordTask extends BaseController
{
    /**
     * 注入发送邮件的接口
     */
    @Autowired
    private IMailService mailService;

    /**
     * 注入报警类型的接口
     */
    @Autowired
    private ITAlarmtypeService alarmtypeService;

    /**
     * 注入报警管理负责人的接口
     */
    @Autowired
    private ITAlarmPrincipalService alarmPrincipalService;

    /**
     * 注入报警记录管理的接口
     */
    @Autowired
    private ITAlarmhistoryService alarmhistoryService;

    /**
     * 注入通用文件的接口
     */
    @Autowired
    private ITCommonfileService commonfileService;

    /**
     * 注入培训成绩的接口
     */
    @Autowired
    private ITTrainingrecordsService trainingrecordsService;

    /**
     * 注入上岗证培训的接口
     */
    @Autowired
    private ITWorklicenseService tWorklicenseService;

    /**
     * 培训日期预警
     */
    public void TrainingDateTask()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] today = sdf.format(new Date()).split("-");
        TAlarmtype alarmtype = alarmtypeService.selectTAlarmtypeById(Long.parseLong("10000"));
        //判断报警是否开启
        if (alarmtype.getIsOpen() == 1) {
            List<TTrainingrecords> tTrainingrecords = trainingrecordsService.selectList(new TTrainingrecords());
            for (TTrainingrecords t : tTrainingrecords) {
                TTrainingrecords trainingrecords = new TTrainingrecords();
                trainingrecords.setId(t.getId());
                if (t.getClasses().equals("18")) {
                    //当班值为W时
                    trainingrecords.setCraft("3");
                    trainingrecords.setPostCard("3");
                    TCommonfile commonfile = new TCommonfile();
                    commonfile.setpId(t.getId());
                    //TDS
                    commonfile.setpType("trainingrecords-tds");
                    List<TCommonfile> commonfilesTDS = commonfileService.selectAllFileList(commonfile);
                    if (commonfilesTDS.size() > 0) {
                        ChangeStatus(today, commonfilesTDS, "trainingrecords-tds", t.getId());
                    }else {
                        trainingrecords.setTds("2");
                        trainingrecordsService.updateTTrainingrecords(trainingrecords);
                    }
                    trainingrecordsService.updateTTrainingrecords(trainingrecords);
                }else {
                    TWorklicense worklicense = tWorklicenseService.selectTWorklicenseByEmployeeid(t.getEmployeeid());
                    if (worklicense != null) {
                        if (worklicense.getBoiler().equals("1") || worklicense.getCracking().equals("1") || worklicense.getHotarea().equals("1") || worklicense.getColdarea().equals("1")) {
                            //该人员存在上岗证
                            TCommonfile postcardFile = new TCommonfile();
                            postcardFile.setpId(worklicense.getId());
                            int m = 0;
                            if (worklicense.getCracking().equals("1")) {
                                postcardFile.setpType("trainingrecords-worklicense压缩");
                                List<TCommonfile> commonfilesCracking = commonfileService.selectAllFileList(postcardFile);
                                if (commonfilesCracking.size() == 0) {
                                    m = 1;
                                }
                            }
                            if (worklicense.getBoiler().equals("1")) {
                                postcardFile.setpType("trainingrecords-worklicense裂解");
                                List<TCommonfile> commonfilesBoiler = commonfileService.selectAllFileList(postcardFile);
                                if (commonfilesBoiler.size() == 0) {
                                    m = 1;
                                }
                            }
                            if (worklicense.getHotarea().equals("1")) {
                                postcardFile.setpType("trainingrecords-worklicense分离");
                                List<TCommonfile> commonfilesHotarea = commonfileService.selectAllFileList(postcardFile);
                                if (commonfilesHotarea.size() == 0) {
                                    m = 1;
                                }
                            }
                            if (worklicense.getColdarea().equals("1")) {
                                postcardFile.setpType("trainingrecords-worklicensePGU/AEU");
                                List<TCommonfile> commonfilesColdarea = commonfileService.selectAllFileList(postcardFile);
                                if (commonfilesColdarea.size() == 0) {
                                    m = 1;
                                }
                            }
                            if (m == 1) {
                                trainingrecords.setPostCard("2");
                            }else {
                                trainingrecords.setPostCard("1");
                            }
                            trainingrecordsService.updateTTrainingrecords(trainingrecords);
                        }else {
                            trainingrecords.setPostCard("2");
                            trainingrecordsService.updateTTrainingrecords(trainingrecords);
                        }
                    }
                    TCommonfile commonfile = new TCommonfile();
                    commonfile.setpId(t.getId());
                    //TDS
                    commonfile.setpType("trainingrecords-tds");
                    List<TCommonfile> commonfilesTDS = commonfileService.selectAllFileList(commonfile);
                    if (commonfilesTDS.size() > 0) {
                        ChangeStatus(today, commonfilesTDS, "trainingrecords-tds", t.getId());
                    }else {
                        trainingrecords.setTds("2");
                        trainingrecordsService.updateTTrainingrecords(trainingrecords);
                    }
                    //工艺培训
                    commonfile.setpType("trainingrecords-craft");
                    List<TCommonfile> commonfilesCraft = commonfileService.selectAllFileList(commonfile);
                    if (commonfilesCraft.size() > 0) {
                        ChangeStatus(today, commonfilesCraft, "trainingrecords-craft", t.getId());
                    }else {
                        trainingrecords.setCraft("2");
                        trainingrecordsService.updateTTrainingrecords(trainingrecords);
                    }
                }
            }
        }
    }

    /**
     * 根据是否过期修改状态
     */
    public void ChangeStatus(String[] today, List<TCommonfile> commonfiles, String type, Long trainingRecordId)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //0为未过期，1为过期
        int isChange = 0;
        /*for (TCommonfile c : commonfiles) {*/
        TCommonfile c = commonfiles.get(0);
            if (c.getpDate() != null) {
                String[] trainingDate = sdf.format(c.getpDate()).split("-");
                int year = Integer.parseInt(today[0]) - Integer.parseInt(trainingDate[0]);
                int day = Integer.parseInt(today[2]) - Integer.parseInt(trainingDate[2]);
                if (today[1].equals("12")) {
                    if (year > 4) {
                        isChange = 1;
                    }else if (year == 4){
                        if (trainingDate[1].equals("1")) {
                            if (day >= 0) {
                                isChange = 1;
                            }
                        }
                    }
                }else {
                    if (year == 5) {
                        int m = 12 - Integer.parseInt(trainingDate[1]) + Integer.parseInt(today[1]);
                        if (m == 12) {
                            isChange = 1;
                        }else if (m == 11){
                            if (day >= 0) {
                                isChange = 1;
                            }
                        }
                    }else if (year > 5) {
                        isChange = 1;
                    }
                }
            }else {
                isChange = 1;
            }
        /*}*/
        TTrainingrecords trainingrecords = new TTrainingrecords();
        trainingrecords.setId(trainingRecordId);
        if (isChange == 1) {
            if (type.equals("trainingrecords-postCard")) {
                trainingrecords.setPostCard("2");
            }else if (type.equals("trainingrecords-tds")) {
                trainingrecords.setTds("2");
            }else if (type.equals("trainingrecords-craft")) {
                trainingrecords.setCraft("2");
            }
        }else {
            if (type.equals("trainingrecords-postCard")) {
                trainingrecords.setPostCard("1");
            }else if (type.equals("trainingrecords-tds")) {
                trainingrecords.setTds("1");
            }else if (type.equals("trainingrecords-craft")) {
                trainingrecords.setCraft("1");
            }
        }
        trainingrecordsService.updateTTrainingrecords(trainingrecords);
    }
}
