package com.ruoyi.rocketmq.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rocketmq.domain.RmAlarmRecord;
import com.ruoyi.rocketmq.mapper.RmAlarmRecordMapper;
import com.ruoyi.rocketmq.service.IRmAlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 告警记录Service业务层处理
 * 
 * @author gyt
 * @date 2025-09-12
 */
@Service
public class RmAlarmRecordServiceImpl implements IRmAlarmRecordService 
{
    @Autowired
    private RmAlarmRecordMapper rmAlarmRecordMapper;

    /**
     * 查询告警记录
     * 
     * @param id 告警记录主键
     * @return 告警记录
     */
    @Override
    public RmAlarmRecord selectRmAlarmRecordById(Long id)
    {
        return rmAlarmRecordMapper.selectRmAlarmRecordById(id);
    }

    /**
     * 查询告警记录列表
     * 
     * @param rmAlarmRecord 告警记录
     * @return 告警记录
     */
    @Override
    public List<RmAlarmRecord> selectRmAlarmRecordList(RmAlarmRecord rmAlarmRecord)
    {
        return rmAlarmRecordMapper.selectRmAlarmRecordList(rmAlarmRecord);
    }

    /**
     * 新增告警记录
     * 
     * @param rmAlarmRecord 告警记录
     * @return 结果
     */
    @Override
    public int insertRmAlarmRecord(RmAlarmRecord rmAlarmRecord)
    {
        rmAlarmRecord.setCreateTime(DateUtils.getNowDate());
        return rmAlarmRecordMapper.insertRmAlarmRecord(rmAlarmRecord);
    }

    /**
     * 修改告警记录
     * 
     * @param rmAlarmRecord 告警记录
     * @return 结果
     */
    @Override
    public int updateRmAlarmRecord(RmAlarmRecord rmAlarmRecord)
    {
        rmAlarmRecord.setUpdateTime(DateUtils.getNowDate());
        return rmAlarmRecordMapper.updateRmAlarmRecord(rmAlarmRecord);
    }

    /**
     * 批量删除告警记录
     * 
     * @param ids 需要删除的告警记录主键
     * @return 结果
     */
    @Override
    public int deleteRmAlarmRecordByIds(Long[] ids)
    {
        return rmAlarmRecordMapper.deleteRmAlarmRecordByIds(ids);
    }

    /**
     * 删除告警记录信息
     * 
     * @param id 告警记录主键
     * @return 结果
     */
    @Override
    public int deleteRmAlarmRecordById(Long id)
    {
        return rmAlarmRecordMapper.deleteRmAlarmRecordById(id);
    }

    /**
     * 资源告警处理情况
     * @return
     */
    @Override
    public Map alarmHandlingStatus() {
        // 1. 获取告警列表
        RmAlarmRecord rmAlarmRecord = new RmAlarmRecord();
        List<RmAlarmRecord> alarmList = rmAlarmRecordMapper.selectRmAlarmRecordList(rmAlarmRecord);

        // 2. 计算统计数据
        BigDecimal total = BigDecimal.valueOf(alarmList.size());
        long processAlarmCount = alarmList.stream().filter(alarm -> "1".equals(alarm.getStatus())).count();
        long noProcessAlarmCount = alarmList.stream().filter(alarm -> "0".equals(alarm.getStatus())).count();

        // 3. 计算处理率（安全处理除零情况）
        BigDecimal processAlarmRate = total.compareTo(BigDecimal.ZERO) == 0
                ? BigDecimal.ZERO
                : BigDecimal.valueOf(processAlarmCount)
                .divide(total, 2, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(100));

        // 4. 构建返回结果
        Map<String, Object> result = new HashMap<>(4);
        result.put("total", total);
        result.put("processAlarmCount", processAlarmCount);
        result.put("noProcessAlarmCount", noProcessAlarmCount);
        result.put("processAlarmRate", processAlarmRate);

        return result;
    }
}
