package com.aucma.base.service.impl;

import com.aucma.base.domain.BaseDeviceParam;
import com.aucma.base.domain.BaseDeviceParamVal;
import com.aucma.base.domain.ProcessAlert;
import com.aucma.base.mapper.BaseDeviceParamMapper;
import com.aucma.base.mapper.BaseDeviceParamValMapper;
import com.aucma.base.mapper.ProcessAlertMapper;
import com.aucma.base.service.IProcessAlertService;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 工艺预警Service业务层处理
 *
 * @author Yinq
 * @date 2025-12-30
 */
@Service
public class ProcessAlertServiceImpl implements IProcessAlertService {
    private static final Logger log = LoggerFactory.getLogger(ProcessAlertServiceImpl.class);
    
    @Autowired
    private ProcessAlertMapper processAlertMapper;
    
    @Autowired
    private BaseDeviceParamMapper baseDeviceParamMapper;
    
    @Autowired
    private BaseDeviceParamValMapper baseDeviceParamValMapper;

    @Override
    public ProcessAlert selectProcessAlertByAlertId(Long alertId) {
        return processAlertMapper.selectProcessAlertByAlertId(alertId);
    }

    @Override
    public List<ProcessAlert> selectProcessAlertList(ProcessAlert processAlert) {
        return processAlertMapper.selectProcessAlertList(processAlert);
    }

    @Override
    public int insertProcessAlert(ProcessAlert processAlert) {
        processAlert.setCreateTime(DateUtils.getNowDate());
        if (processAlert.getAlertTime() == null) {
            processAlert.setAlertTime(DateUtils.getNowDate());
        }
        if (processAlert.getAlertStatus() == null || processAlert.getAlertStatus().isEmpty()) {
            processAlert.setAlertStatus("0");
        }
        if (processAlert.getIsFlag() == null || processAlert.getIsFlag().isEmpty()) {
            processAlert.setIsFlag("1");
        }
        return processAlertMapper.insertProcessAlert(processAlert);
    }

    @Override
    public int updateProcessAlert(ProcessAlert processAlert) {
        processAlert.setUpdateTime(DateUtils.getNowDate());
        return processAlertMapper.updateProcessAlert(processAlert);
    }

    @Override
    public int deleteProcessAlertByAlertIds(Long[] alertIds) {
        return processAlertMapper.deleteProcessAlertByAlertIds(alertIds);
    }

    @Override
    public int deleteProcessAlertByAlertId(Long alertId) {
        return processAlertMapper.deleteProcessAlertByAlertId(alertId);
    }
    
    @Override
    public int checkThresholdAlerts() {
        int alertCount = 0;
        try {
            // 查询所有启用预警的参数
            BaseDeviceParam queryParam = new BaseDeviceParam();
            queryParam.setAlertEnabled("1");
            queryParam.setIsFlag(1L);
            List<BaseDeviceParam> paramList = baseDeviceParamMapper.selectBaseDeviceParamList(queryParam);
            
            if (paramList == null || paramList.isEmpty()) {
                log.info("没有启用预警的参数配置");
                return 0;
            }
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            Date now = DateUtils.getNowDate();
            
            for (BaseDeviceParam param : paramList) {
                if (param.getUpperLimit() == null && param.getLowerLimit() == null) {
                    continue;
                }
                
                // 查询该参数最新的采集值
                BaseDeviceParamVal valQuery = new BaseDeviceParamVal();
                valQuery.setParamCode(param.getParamCode());
                valQuery.setDeviceCode(param.getDeviceCode());
                List<BaseDeviceParamVal> valList = baseDeviceParamValMapper.selectBaseDeviceParamValList(valQuery);
                
                if (valList == null || valList.isEmpty()) {
                    continue;
                }
                
                // 取最新一条记录
                BaseDeviceParamVal latestVal = valList.get(0);
                String paramValueStr = latestVal.getParamValue();
                
                if (paramValueStr == null || paramValueStr.isEmpty()) {
                    continue;
                }
                
                try {
                    double paramValue = Double.parseDouble(paramValueStr);
                    String alertType = null;
                    String thresholdValue = null;
                    
                    // 检查是否超上限
                    if (param.getUpperLimit() != null && paramValue > param.getUpperLimit()) {
                        alertType = "超上限";
                        thresholdValue = String.valueOf(param.getUpperLimit());
                    }
                    // 检查是否低于下限
                    else if (param.getLowerLimit() != null && paramValue < param.getLowerLimit()) {
                        alertType = "低于下限";
                        thresholdValue = String.valueOf(param.getLowerLimit());
                    }
                    
                    if (alertType != null) {
                        // 检查是否已存在未处理的预警（去重）
                        ProcessAlert existingAlert = processAlertMapper.selectUnhandledAlert(
                            param.getDeviceCode(), 
                            param.getParamCode()
                        );
                        
                        if (existingAlert != null) {
                            // 已存在未处理的预警，更新预警值和时间
                            existingAlert.setAlertValue(paramValueStr);
                            existingAlert.setAlertTime(latestVal.getCollectTime() != null ? latestVal.getCollectTime() : now);
                            existingAlert.setAlertContent(param.getParamName() + alertType + "，当前值：" + paramValueStr + "，阈值：" + thresholdValue);
                            updateProcessAlert(existingAlert);
                            log.info("更新未处理预警: {}", existingAlert.getAlertContent());
                        } else {
                            // 查询最新预警（包含已处理）
                            ProcessAlert latestAlert = processAlertMapper.selectLatestAlert(
                                param.getDeviceCode(), 
                                param.getParamCode()
                            );
                            
                            // 如果存在已处理的预警，检查预警冷却时间（5分钟）
                            if (latestAlert != null && "2".equals(latestAlert.getAlertStatus())) {
                                long timeDiff = now.getTime() - latestAlert.getAlertTime().getTime();
                                long cooldownMinutes = 5; // 预警冷却时间5分钟
                                
                                if (timeDiff < cooldownMinutes * 60 * 1000) {
                                    // 还在冷却期内，不生成新预警
                                    log.debug("预警冷却期内，跳过: deviceCode={}, paramCode={}", 
                                        param.getDeviceCode(), param.getParamCode());
                                    continue;
                                }
                            }
                            
                            // 生成新的预警记录
                            ProcessAlert alert = new ProcessAlert();
                            alert.setAlertCode("ALERT-" + sdf.format(now) + "-" + (alertCount + 1));
                            alert.setAlertType("参数超限");
                            alert.setAlertLevel(param.getAlertLevel() != null ? param.getAlertLevel() : "1");
                            alert.setDeviceCode(param.getDeviceCode());
                            alert.setDeviceName(param.getDeviceName());
                            alert.setParamCode(param.getParamCode());
                            alert.setParamName(param.getParamName());
                            alert.setAlertContent(param.getParamName() + alertType + "，当前值：" + paramValueStr + "，阈值：" + thresholdValue);
                            alert.setAlertValue(paramValueStr);
                            alert.setThresholdValue(thresholdValue);
                            alert.setAlertTime(latestVal.getCollectTime() != null ? latestVal.getCollectTime() : now);
                            alert.setAlertStatus("0");
                            alert.setIsFlag("1");
                            alert.setCreateTime(now);
                            
                            insertProcessAlert(alert);
                            alertCount++;
                            log.info("生成新预警: {}", alert.getAlertContent());
                        }
                    }
                } catch (NumberFormatException e) {
                    log.warn("参数值无法转换为数字: paramCode={}, value={}", param.getParamCode(), paramValueStr);
                }
            }
        } catch (Exception e) {
            log.error("检查阈值预警时发生错误", e);
        }
        
        return alertCount;
    }

    @Override
    public int batchMarkAsProcessed(Long[] alertIds, String handleUser) {
        if (alertIds == null || alertIds.length == 0) {
            return 0;
        }
        
        int count = 0;
        Date now = DateUtils.getNowDate();
        
        for (Long alertId : alertIds) {
            try {
                ProcessAlert alert = processAlertMapper.selectProcessAlertByAlertId(alertId);
                if (alert != null && !"2".equals(alert.getAlertStatus())) {
                    alert.setAlertStatus("2"); // 已处理
                    alert.setHandleUser(handleUser);
                    alert.setHandleTime(now);
                    alert.setHandleResult("批量标记为已处理");
                    processAlertMapper.updateProcessAlert(alert);
                    count++;
                    log.info("批量标记预警已处理: alertId={}, alertCode={}", alertId, alert.getAlertCode());
                }
            } catch (Exception e) {
                log.error("标记预警已处理失败: alertId={}", alertId, e);
            }
        }
        
        return count;
    }
}
