package com.ruoyi.rocketmq.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rocketmq.domain.RmInitialMonitorItem;
import com.ruoyi.rocketmq.mapper.RmInitialMonitorItemMapper;
import com.ruoyi.rocketmq.service.IRmInitialMonitorItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基础监控项Service业务层处理
 * 
 * @author gyt
 * @date 2025-09-10
 */
@Service
public class RmInitialMonitorItemServiceImpl implements IRmInitialMonitorItemService 
{
    @Autowired
    private RmInitialMonitorItemMapper rmInitialMonitorItemMapper;

    /**
     * 查询基础监控项
     * 
     * @param id 基础监控项主键
     * @return 基础监控项
     */
    @Override
    public RmInitialMonitorItem selectRmInitialMonitorItemById(Long id)
    {
        return rmInitialMonitorItemMapper.selectRmInitialMonitorItemById(id);
    }

    /**
     * 查询基础监控项列表
     * 
     * @param rmInitialMonitorItem 基础监控项
     * @return 基础监控项
     */
    @Override
    public List<RmInitialMonitorItem> selectRmInitialMonitorItemList(RmInitialMonitorItem rmInitialMonitorItem)
    {
        return rmInitialMonitorItemMapper.selectRmInitialMonitorItemList(rmInitialMonitorItem);
    }

    /**
     * 新增基础监控项
     * 
     * @param rmInitialMonitorItem 基础监控项
     * @return 结果
     */
    @Override
    public int insertRmInitialMonitorItem(RmInitialMonitorItem rmInitialMonitorItem)
    {
        rmInitialMonitorItem.setCreateTime(DateUtils.getNowDate());
        return rmInitialMonitorItemMapper.insertRmInitialMonitorItem(rmInitialMonitorItem);
    }

    /**
     * 修改基础监控项
     * 
     * @param rmInitialMonitorItem 基础监控项
     * @return 结果
     */
    @Override
    public int updateRmInitialMonitorItem(RmInitialMonitorItem rmInitialMonitorItem)
    {
        rmInitialMonitorItem.setUpdateTime(DateUtils.getNowDate());
        return rmInitialMonitorItemMapper.updateRmInitialMonitorItem(rmInitialMonitorItem);
    }

    /**
     * 批量删除基础监控项
     * 
     * @param ids 需要删除的基础监控项主键
     * @return 结果
     */
    @Override
    public int deleteRmInitialMonitorItemByIds(Long[] ids)
    {
        return rmInitialMonitorItemMapper.deleteRmInitialMonitorItemByIds(ids);
    }

    /**
     * 删除基础监控项信息
     * 
     * @param id 基础监控项主键
     * @return 结果
     */
    @Override
    public int deleteRmInitialMonitorItemById(Long id)
    {
        return rmInitialMonitorItemMapper.deleteRmInitialMonitorItemById(id);
    }

    /**
     * 查询所有监控标识以及监控名称
     * @param rmInitialMonitorItem
     * @return
     */
    @Override
    public Map<String, List<RmInitialMonitorItem>> selectAllMsgList(RmInitialMonitorItem rmInitialMonitorItem) {
        Map<String, List<RmInitialMonitorItem>> resultMap = new HashMap<>();
        String resourceType = rmInitialMonitorItem.getResourceType();
        String itemType = rmInitialMonitorItem.getItemType();

        List<RmInitialMonitorItem> allItems = rmInitialMonitorItemMapper.selectRmInitialMonitorItemList(rmInitialMonitorItem);

        if ("linux".equals(resourceType)) {
            if ("monitorItem".equals(itemType)) {
                // CPU信息
                resultMap.put("cpu", filterItems(allItems, "CPU"));
                // 其他系统信息
                resultMap.put("other", filterItems(allItems, "OTHER"));
            }
            else if ("autodiscoverItem".equals(itemType)) {
                // 挂载点信息
                resultMap.put("vfs", filterItems(allItems, "POINT"));
                // 网络接口信息
                resultMap.put("net", filterItems(allItems, "NET"));
                // 磁盘信息
                resultMap.put("disk", filterItems(allItems, "DISK"));
                // Docker容器信息
                resultMap.put("docker", filterItems(allItems, "DOCKER"));
            }
        }
        else if ("switch".equals(resourceType)) {
            if ("monitorItem".equals(itemType)) {
                // 系统信息
                resultMap.put("switchOther", filterItems(allItems, "SYSTEM"));
            }
            else if ("autodiscoverItem".equals(itemType)) {
                // 网络端口信息
                resultMap.put("switchNet", filterItems(allItems, "NETPORT"));
                // 光模块信息
                resultMap.put("switchModule", filterItems(allItems, "LIGHTMODULE"));
                // 风扇信息
                resultMap.put("switchFan", filterItems(allItems, "FAN"));
                // MPU信息
                resultMap.put("switchMpu", filterItems(allItems, "MPU"));
                // 电源信息
                resultMap.put("switchPwr", filterItems(allItems, "POWERSOURCE"));
            }
        }

        return resultMap;
    }

    /**
     * 通用过滤方法
     */
    private List<RmInitialMonitorItem> filterItems(List<RmInitialMonitorItem> items, String dataType) {
        return items.stream()
                .filter(item -> dataType.equals(item.getDataType()))
                .collect(Collectors.toList());
    }
}
