package com.ruoyi.framework.task;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.thread.furnance.FurnancePressureThread;
import com.ruoyi.common.thread.furnance.FurnanceTemperatureThread;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.production.controller.TFurnancePressureController;
import com.ruoyi.project.production.controller.TFurnanceTemperatureController;
import com.ruoyi.project.production.controller.vo.FurnancePressureFvpVO;
import com.ruoyi.project.production.controller.vo.FurnanceTemperatureVO;
import com.ruoyi.project.production.domain.TFurnancePressure;
import com.ruoyi.project.production.domain.TFurnanceTemperature;
import com.ruoyi.project.production.service.ITFurnancePressureService;
import com.ruoyi.project.production.service.ITFurnanceTemperatureService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 定时任务 - 裂解炉管测温测压
 *
 * @author Wang Zi Wen
 * @email wangggziwen@163.com
 * @date 2024/05/16 14:51:21
 */
@Component("furnanceTask")
public class FurnanceTask extends BaseController {

    @Autowired
    private ITFurnancePressureService pressureService;

    @Autowired
    private ITFurnanceTemperatureService temperatureService;

    @Autowired
    private TFurnancePressureController pressureController;

    @Autowired
    private TFurnanceTemperatureController temperatureController;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    @Autowired
    private IMailService mailService;

    @Autowired
    private ITStaffmgrService staffmgrService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITFurnanceTemperatureService tFurnanceTemperatureService;

    @Autowired
    private ITFurnancePressureService tFurnancePressureService;

    /**
     * 每日巡检数据生成
     */
    public void genData() throws ParseException {
        TFurnancePressure pressure = new TFurnancePressure();
        TFurnanceTemperature temperature = new TFurnanceTemperature();
        Date recordTime = new Date();
        Date date = new Date();
        date.setYear(recordTime.getYear());
        date.setMonth(recordTime.getMonth());
        date.setDate(recordTime.getDate());
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse(sdf.format(date));
        pressure.setRecordTime(parse);
        temperature.setRecordTime(parse);
        List<TFurnancePressure> tFurnancePressures = tFurnancePressureService.selectTFurnancePressureByDate(pressure);
        List<TFurnanceTemperature> tFurnanceTemperatures = tFurnanceTemperatureService.selectTFurnanceTemperatureByDate2(temperature);
        if (tFurnancePressures.size() == 0) {
            this.genPressureData();
        }
        if (tFurnanceTemperatures.size() == 0) {
            this.genTemperatureData();
        }
    }

    /**
     * 生成测压数据
     */
    public void genPressureData() {
        // yyyy-MM-dd 08:00:00
        Date am = new Date();
        am.setHours(8);
        am.setMinutes(0);
        am.setSeconds(0);
        // yyyy-MM-dd 20:00:00
        Date pm = new Date();
        pm.setHours(20);
        pm.setMinutes(0);
        pm.setSeconds(0);
        // furnanceName array
        String[] furnanceNames = {"H109", "H110", "H111", "H112", "H113", "H114", "H115", "H116", "H117", "H118", "H130"};
        // 遍历生成数据
        for (String furnanceName : furnanceNames) {
            // 测压 08:00:00
            TFurnancePressure pressure1 = new TFurnancePressure();
            pressure1.setRecordTime(am);
            pressure1.setDeptId(103L);
            pressure1.setFurnanceName(furnanceName);
            // 测压 20:00:00
            TFurnancePressure pressure2 = new TFurnancePressure();
            pressure2.setRecordTime(pm);
            pressure2.setDeptId(103L);
            pressure2.setFurnanceName(furnanceName);
            // 默认赋值=0
//            switch (furnanceName) {
//                case "H109":
//                    this.setZeroPressure(pressure1, 16, 15);
//                    this.setZeroPressure(pressure2, 16, 15);
//                    break;
//                case "H110":
//                case "H111":
//                case "H112":
//                case "H113":
//                case "H114":
//                case "H115":
//                case "H116":
//                case "H117":
//                case "H118":
//                    this.setZeroPressure(pressure1, 8, 11);
//                    this.setZeroPressure(pressure2, 8, 11);
//                    break;
//                case "H130":
//                    this.setZeroPressure(pressure1, 4, 7);
//                    this.setZeroPressure(pressure2, 4, 7);
//                    break;
//            }
            // 新增
            pressureService.insertTFurnancePressure(pressure1);
            pressureService.insertTFurnancePressure(pressure2);
        }
    }

    public String getZeroString(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append("0");
            if (i != length - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public void setZeroPressure(TFurnancePressure pressure, int passNum, int length) {
        String zeroString = this.getZeroString(length);
        pressure.setPass1(zeroString);
        pressure.setPass2(zeroString);
        pressure.setPass3(zeroString);
        pressure.setPass4(zeroString);
        if (passNum == 8 || passNum == 16) {
            pressure.setPass5(zeroString);
            pressure.setPass6(zeroString);
            pressure.setPass7(zeroString);
            pressure.setPass8(zeroString);
        }
        if (passNum == 16) {
            pressure.setPass9(zeroString);
            pressure.setPass10(zeroString);
            pressure.setPass11(zeroString);
            pressure.setPass12(zeroString);
            pressure.setPass13(zeroString);
            pressure.setPass14(zeroString);
            pressure.setPass15(zeroString);
            pressure.setPass16(zeroString);
        }
    }

    public void setZeroTemperature(TFurnanceTemperature temperature, int passNum, int length) {
        String zeroString = this.getZeroString(length);
        temperature.setPass1(zeroString);
        temperature.setPass2(zeroString);
        temperature.setPass3(zeroString);
        temperature.setPass4(zeroString);
        temperature.setPass5(zeroString);
        temperature.setPass6(zeroString);
        temperature.setPass7(zeroString);
        temperature.setPass8(zeroString);
        if (passNum == 12 || passNum == 16) {
            temperature.setPass9(zeroString);
            temperature.setPass10(zeroString);
            temperature.setPass11(zeroString);
            temperature.setPass12(zeroString);
        }
        if (passNum == 16) {
            temperature.setPass13(zeroString);
            temperature.setPass14(zeroString);
            temperature.setPass15(zeroString);
            temperature.setPass16(zeroString);
        }
    }

    /**
     * 生成测温数据
     */
    public void genTemperatureData() {
        // yyyy-MM-dd 08:00:00
        Date am = new Date();
        am.setHours(8);
        am.setMinutes(0);
        am.setSeconds(0);
        // yyyy-MM-dd 20:00:00
        Date pm = new Date();
        pm.setHours(20);
        pm.setMinutes(0);
        pm.setSeconds(0);
        // furnanceName array
        String[] furnanceNames = {"H109", "H110", "H111", "H112", "H113", "H114", "H115", "H116", "H117", "H118", "H130"};
        // 遍历生成数据
        for (String furnanceName : furnanceNames) {
            // 测温 08:00:00
            TFurnanceTemperature temperature = new TFurnanceTemperature();
            temperature.setRecordTime(am);
            temperature.setDeptId(103L);
            temperature.setFurnanceName(furnanceName);
            // 默认赋值=0
//            switch (furnanceName) {
//                case "H109":
//                    this.setZeroTemperature(temperature, 16, 14);
//                    break;
//                case "H110":
//                case "H111":
//                case "H112":
//                case "H113":
//                case "H114":
//                case "H115":
//                case "H116":
//                case "H117":
//                case "H118":
//                    this.setZeroTemperature(temperature, 8, 10);
//                    break;
//                case "H130":
//                    this.setZeroTemperature(temperature, 12, 3);
//                    break;
//            }
            // 新增
            temperatureService.insertTFurnanceTemperature(temperature);
        }
    }

    /**
     * FVP邮件提醒
     */
    public void alertFvp() throws ParseException, PinyinException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        Date parse = sdf.parse(sdf.format(date));
        TFurnancePressure pressure = new TFurnancePressure();
        pressure.setRecordTime(parse);
        AjaxResult fvp = pressureController.fvp(pressure);
        Object obj = fvp.get("data");
        if (obj != null) {
            List<FurnancePressureFvpVO> data = (List<FurnancePressureFvpVO>) obj;
            boolean flag = false;
            for (FurnancePressureFvpVO datum : data) {
                double pass1Ratio = datum.getPass1Ratio();
                double pass2Ratio = datum.getPass2Ratio();
                double pass3Ratio = datum.getPass3Ratio();
                double pass4Ratio = datum.getPass4Ratio();
                double pass5Ratio = datum.getPass5Ratio();
                double pass6Ratio = datum.getPass6Ratio();
                double pass7Ratio = datum.getPass7Ratio();
                double pass8Ratio = datum.getPass8Ratio();
                double allPassMaxRatio = datum.getAllPassMaxRatio();
                if (Double.compare(pass1Ratio, 0.9) >= 0 || Double.compare(pass2Ratio, 0.9) >= 0
                        || Double.compare(pass3Ratio, 0.9) >= 0 || Double.compare(pass4Ratio, 0.9) >= 0
                        || Double.compare(pass5Ratio, 0.9) >= 0 || Double.compare(pass6Ratio, 0.9) >= 0
                        || Double.compare(pass7Ratio, 0.9) >= 0 || Double.compare(pass8Ratio, 0.9) >= 0
                        || Double.compare(allPassMaxRatio, 0.9) >= 0) {
                    flag = true;
                }
            }
            if (flag) {
                List<SysDictData> furnanceRecipientDict = iSysDictTypeService.selectDictDataByType("FURNANCE_RECIPIENT");
                for (SysDictData sysDictData : furnanceRecipientDict) {
                    String dictValue = sysDictData.getDictValue();
                    TStaffmgr staffmgr = staffmgrService.selectTStaffmgrByStaffId(dictValue);
                    SysUser sysUser = userService.selectUserByStaffId(dictValue);
                    FurnancePressureThread mailThread = new FurnancePressureThread(mailService, staffmgr, sysUser.getUserName(), "");
                    Thread thread = new Thread(mailThread);
                    thread.start();
                }
            }
        }
    }

    /**
     * COIL邮件提醒
     */
    public void alertCoil() throws ParseException, PinyinException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date.setDate(1);
        Date parse = sdf.parse(sdf.format(date));
        TFurnanceTemperature temperature = new TFurnanceTemperature();
        temperature.setRecordTime(parse);
        AjaxResult coil = temperatureController.coil(temperature);
        Object obj = coil.get("data");
        if (obj != null) {
            List<FurnanceTemperatureVO> data = (List<FurnanceTemperatureVO>) obj;
            boolean flag = false;
            for (FurnanceTemperatureVO datum : data) {
                String h109Out = datum.getH109Out();
                String h110Out = datum.getH110Out();
                String h111Out = datum.getH111Out();
                String h112Out = datum.getH112Out();
                String h113Out = datum.getH113Out();
                String h114Out = datum.getH114Out();
                String h115Out = datum.getH115Out();
                String h116Out = datum.getH116Out();
                String h117Out = datum.getH117Out();
                String h118Out = datum.getH118Out();
                String h130Out = datum.getH130Out();
                if (this.compare1080(h109Out)) { flag = true; }
                if (this.compare1080(h110Out)) { flag = true; }
                if (this.compare1080(h111Out)) { flag = true; }
                if (this.compare1080(h112Out)) { flag = true; }
                if (this.compare1080(h113Out)) { flag = true; }
                if (this.compare1080(h114Out)) { flag = true; }
                if (this.compare1080(h115Out)) { flag = true; }
                if (this.compare1080(h116Out)) { flag = true; }
                if (this.compare1080(h117Out)) { flag = true; }
                if (this.compare1080(h118Out)) { flag = true; }
                if (this.compare1080(h130Out)) { flag = true; }
            }
            if (flag) {
                List<SysDictData> furnanceRecipientDict = iSysDictTypeService.selectDictDataByType("FURNANCE_RECIPIENT");
                for (SysDictData sysDictData : furnanceRecipientDict) {
                    String dictValue = sysDictData.getDictValue();
                    TStaffmgr staffmgr = staffmgrService.selectTStaffmgrByStaffId(dictValue);
                    SysUser sysUser = userService.selectUserByStaffId(dictValue);
                    FurnanceTemperatureThread mailThread = new FurnanceTemperatureThread(mailService, staffmgr, sysUser.getUserName(), "");
                    Thread thread = new Thread(mailThread);
                    thread.start();
                }
            }
        }
    }

    private boolean compare1080(String string) {
        boolean flag = false;
        if (StringUtils.isNotNull(string) && StringUtils.isNotEmpty(string)) {
            String[] split = string.split(",");
            for (int i = 0; i < split.length; i++) {
                int element = Integer.parseInt(split[i]);
                if (element > 1080) {
                    flag = true;
                }
            }
        }
        return flag;
    }

}
