package org.dromara.ems.record.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.ems.base.domain.EmsAlarmActionStep;
import org.dromara.ems.base.service.IEmsAlarmActionStepService;
import org.dromara.ems.record.domain.EmsRecordAlarmRule;
import org.dromara.ems.record.mapper.EmsRecordAlarmRuleMapper;
import org.dromara.ems.record.service.IEmsRecordAlarmRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 异常告警规则Service业务层处理
 * 
 * @author Yinq
 * @date 2024-05-15
 */
@Service
@RequiredArgsConstructor
public class EmsRecordAlarmRuleServiceImpl implements IEmsRecordAlarmRuleService 
{
    
    private final EmsRecordAlarmRuleMapper emsRecordAlarmRuleMapper;

    
    private final IEmsAlarmActionStepService emsAlarmActionStepService;

    /**
     * 查询异常告警规则
     * 
     * @param objId 异常告警规则主键
     * @return 异常告警规则
     */
    @Override
    public EmsRecordAlarmRule selectEmsRecordAlarmRuleByObjId(Long objId)
    {
        return emsRecordAlarmRuleMapper.selectEmsRecordAlarmRuleByObjId(objId);
    }

    /**
     * 查询异常告警规则列表
     * 
     * @param emsRecordAlarmRule 异常告警规则
     * @return 异常告警规则
     */
    @Override
    public List<EmsRecordAlarmRule> selectEmsRecordAlarmRuleList(EmsRecordAlarmRule emsRecordAlarmRule)
    {
        return emsRecordAlarmRuleMapper.selectEmsRecordAlarmRuleList(emsRecordAlarmRule);
    }

    /**
     * 新增异常告警规则
     * 
     * @param emsRecordAlarmRule 异常告警规则
     * @return 结果
     */
    @Override
    public int insertEmsRecordAlarmRule(EmsRecordAlarmRule emsRecordAlarmRule)
    {
        emsRecordAlarmRule.setCreateTime(DateUtils.getNowDate());
        return emsRecordAlarmRuleMapper.insertEmsRecordAlarmRule(emsRecordAlarmRule);
    }

    /**
     * 修改异常告警规则
     * 
     * @param emsRecordAlarmRule 异常告警规则
     * @return 结果
     */
    @Override
    public int updateEmsRecordAlarmRule(EmsRecordAlarmRule emsRecordAlarmRule)
    {
        emsRecordAlarmRule.setUpdateTime(DateUtils.getNowDate());
        return emsRecordAlarmRuleMapper.updateEmsRecordAlarmRule(emsRecordAlarmRule);
    }

    /**
     * 批量删除异常告警规则
     * 
     * @param objIds 需要删除的异常告警规则主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEmsRecordAlarmRuleByObjIds(Long[] objIds)
    {
        try {
            int result = 0;
            for (Long objId : objIds) {
                result += deleteEmsRecordAlarmRuleByObjId(objId);
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException("批量删除异常规则失败: " + e.getMessage(), e);
        }
    }

    /**
     * 删除异常告警规则信息（级联删除关联的措施步骤和图片）
     * 
     * @param objId 异常告警规则主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEmsRecordAlarmRuleByObjId(Long objId)
    {
        try {
            // 1. 查询要删除的异常规则信息
            EmsRecordAlarmRule alarmRule = emsRecordAlarmRuleMapper.selectEmsRecordAlarmRuleByObjId(objId);
            if (alarmRule == null) {
                throw new RuntimeException("异常规则不存在，ID: " + objId);
            }
            
            // 2. 删除该规则关联的所有措施步骤（会级联删除图片文件）
            List<EmsAlarmActionStep> actionSteps = emsAlarmActionStepService.selectEmsAlarmActionStepByRuleObjId(objId.toString());
            
            if (actionSteps != null && !actionSteps.isEmpty()) {
                String[] stepIds = actionSteps.stream()
                    .map(EmsAlarmActionStep::getObjId)
                    .toArray(String[]::new);
                
                // 级联删除措施步骤（会自动删除关联的图片文件）
                int deletedSteps = emsAlarmActionStepService.deleteEmsAlarmActionStepByObjIds(stepIds);
                System.out.println("删除异常规则 " + alarmRule.getRuleName() + " 关联的 " + deletedSteps + " 个措施步骤");
            }
            
            // 3. 最后删除异常规则本身
            int result = emsRecordAlarmRuleMapper.deleteEmsRecordAlarmRuleByObjId(objId);
            
            if (result > 0) {
                System.out.println("成功删除异常规则: " + alarmRule.getRuleName() + " (ID: " + objId + ")");
            }
            
            return result;
        } catch (Exception e) {
            throw new RuntimeException("删除异常规则失败: " + e.getMessage(), e);
        }
    }

    /**
     * 查询异常告警规则总数
     *
     * @return 告警规则总数
     */
    @Override
    public int getEmsRecordAlarmRuleTotalCount()
    {
    	return emsRecordAlarmRuleMapper.getEmsRecordAlarmRuleTotalCount();
    }
}
