package com.iailab.module.shasteel.job.task;

import com.iailab.module.model.api.mcs.McsApi;
import com.iailab.module.model.api.mcs.dto.AlarmMessageRespDTO;
import com.iailab.module.model.api.mcs.dto.ScheduleSuggestRespDTO;
import com.iailab.module.model.api.mcs.dto.StScheduleSchemeDTO;
import com.iailab.module.model.api.mdk.MdkApi;
import com.iailab.module.model.api.mdk.dto.MdkScheduleReqDTO;
import com.iailab.module.model.api.mdk.dto.MdkScheduleRespDTO;
import com.iailab.module.model.enums.CommonConstant;
import com.iailab.module.model.enums.ScheduleTriggerMethodEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 蒸汽调度
 * 定时触发
 * 30 0/2 * * * ?
 *
 * 参考点压力信号	pressure_flag
 * 锦丰调度建议	jinfengAdvice
 * 大新调度建议	daxinAdvice
 * 大新预警	daxinWarning
 * 锦丰预警	jinfengWarning
 * 转炉一预警	zhuanlu1Warning
 * 转炉二预警	zhuanlu2Warning
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年01月08日
 */
@Component("runScheduleSteamTask")
public class RunScheduleSteamTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private McsApi mcsApi;

    @Autowired
    private MdkApi mdkApi;

    @Override
    public void run(String params) {
        logger.info("RunScheduleSteamTask,参数为:{}", params);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            Date scheduleTime = calendar.getTime();
            if (StringUtils.isEmpty(params)) {
                logger.info("参数为空");
                return;
            }

            // 根据条件查找方案
            List<StScheduleSchemeDTO> schemeList = mcsApi.listScheduleScheme(ScheduleTriggerMethodEnum.JOB.getCode(), params);
            if (CollectionUtils.isEmpty(schemeList)) {
                logger.info("方案为空");
                return;
            }
            StScheduleSchemeDTO scheme = schemeList.get(0);
            MdkScheduleReqDTO dto = new MdkScheduleReqDTO();
            dto.setScheduleTime(scheduleTime);
            dto.setScheduleCode(scheme.getCode());
            MdkScheduleRespDTO mdkScheduleRespDTO = mdkApi.doSchedule(dto);
            if (!CommonConstant.MDK_STATUS_100.equals(mdkScheduleRespDTO.getStatusCode())) {
                logger.info("调度结果异常,STATUS=" + mdkScheduleRespDTO.getStatusCode());
                return;
            }

            Object jinfengAdvice = mdkScheduleRespDTO.getResult().get("jinfengAdvice");
            saveScheduleSuggest("锦丰调度建议", jinfengAdvice, scheme.getScheduleObj(), scheduleTime);

            Object daxinAdvice = mdkScheduleRespDTO.getResult().get("daxinAdvice");
            saveScheduleSuggest("大新调度建议", daxinAdvice, scheme.getScheduleObj(), scheduleTime);

            Object daxinWarning = mdkScheduleRespDTO.getResult().get("daxinWarning");
            saveAlarmMessage("大新预警", daxinWarning, scheme.getScheduleObj(), scheduleTime);

            Object jinfengWarning = mdkScheduleRespDTO.getResult().get("jinfengWarning");
            saveAlarmMessage("锦丰预警", jinfengWarning, scheme.getScheduleObj(), scheduleTime);

            Object zhuanlu1Warning = mdkScheduleRespDTO.getResult().get("zhuanlu1Warning");
            saveAlarmMessage("转炉一预警", zhuanlu1Warning, scheme.getScheduleObj(), scheduleTime);

            Object zhuanlu2Warning = mdkScheduleRespDTO.getResult().get("zhuanlu2Warning");
            saveAlarmMessage("转炉二预警", zhuanlu2Warning, scheme.getScheduleObj(), scheduleTime);

            logger.info(params + "调度方案执行完成");
        } catch (Exception ex) {
            logger.error("RunScheduleSteamTask运行异常");
            ex.printStackTrace();
        }
        logger.info("RunScheduleSteamTask运行完成");
    }

    private void saveScheduleSuggest(String title, Object content, String scheduleObj, Date scheduleTime) {
        if (content == null || StringUtils.isBlank(content.toString()) || "0".equals(content.toString())) {
            logger.info(title + "content为空");
            return;
        }
        ScheduleSuggestRespDTO suggest = new ScheduleSuggestRespDTO();
        suggest.setTitle(title);
        suggest.setContent(content.toString());
        suggest.setScheduleObj(scheduleObj);
        suggest.setScheduleTime(scheduleTime);
        suggest.setSort(0);
        mcsApi.createScheduleSuggest(suggest);
    }

    private void saveAlarmMessage(String title, Object content, String alarmObj, Date scheduleTime) {
        if (content == null || StringUtils.isBlank(content.toString()) || "0".equals(content.toString())) {
            logger.info(title + "content为空");
            return;
        }
        AlarmMessageRespDTO alarm = new AlarmMessageRespDTO();
        alarm.setTitle(title);
        alarm.setContent(content.toString());
        alarm.setAlarmObj(alarmObj);
        alarm.setAlarmType("");
        alarm.setAlarmTime(scheduleTime);
        mcsApi.createAlarmMessage(alarm);
    }
}