package com.ruoyi.rocketmq.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.EchartsDataUtils;
import com.ruoyi.rocketmq.domain.InitialSwitchMpuInfo;
import com.ruoyi.rocketmq.mapper.InitialSwitchMpuInfoMapper;
import com.ruoyi.rocketmq.service.IInitialSwitchMpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * MPU信息Service业务层处理
 * 
 * @author gyt
 * @date 2025-09-22
 */
@Service
public class InitialSwitchMpuInfoServiceImpl implements IInitialSwitchMpuInfoService 
{
    @Autowired
    private InitialSwitchMpuInfoMapper initialSwitchMpuInfoMapper;

    /**
     * 查询MPU信息
     * 
     * @param id MPU信息主键
     * @return MPU信息
     */
    @Override
    public InitialSwitchMpuInfo selectInitialSwitchMpuInfoById(Long id)
    {
        return initialSwitchMpuInfoMapper.selectInitialSwitchMpuInfoById(id);
    }

    /**
     * 查询MPU信息列表
     * 
     * @param initialSwitchMpuInfo MPU信息
     * @return MPU信息
     */
    @Override
    public List<InitialSwitchMpuInfo> selectInitialSwitchMpuInfoList(InitialSwitchMpuInfo initialSwitchMpuInfo)
    {
        return initialSwitchMpuInfoMapper.selectInitialSwitchMpuInfoList(initialSwitchMpuInfo);
    }

    /**
     * 新增MPU信息
     * 
     * @param initialSwitchMpuInfo MPU信息
     * @return 结果
     */
    @Override
    public int insertInitialSwitchMpuInfo(InitialSwitchMpuInfo initialSwitchMpuInfo)
    {
        return initialSwitchMpuInfoMapper.insertInitialSwitchMpuInfo(initialSwitchMpuInfo);
    }

    /**
     * 修改MPU信息
     * 
     * @param initialSwitchMpuInfo MPU信息
     * @return 结果
     */
    @Override
    public int updateInitialSwitchMpuInfo(InitialSwitchMpuInfo initialSwitchMpuInfo)
    {
        initialSwitchMpuInfo.setUpdateTime(DateUtils.getNowDate());
        return initialSwitchMpuInfoMapper.updateInitialSwitchMpuInfo(initialSwitchMpuInfo);
    }

    /**
     * 批量删除MPU信息
     * 
     * @param ids 需要删除的MPU信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchMpuInfoByIds(Long[] ids)
    {
        return initialSwitchMpuInfoMapper.deleteInitialSwitchMpuInfoByIds(ids);
    }

    /**
     * 删除MPU信息信息
     * 
     * @param id MPU信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchMpuInfoById(Long id)
    {
        return initialSwitchMpuInfoMapper.deleteInitialSwitchMpuInfoById(id);
    }

    /**
     * 批量新增mpu信息
     * @param list
     * @return
     */
    @Override
    public int insertBatchInitialSwitchMpuInfo(List<InitialSwitchMpuInfo> list) {
        return initialSwitchMpuInfoMapper.insertBatchInitialSwitchMpuInfo(list);
    }

    /**
     * 指定交换机的mpu名称
     * @param initialSwitchMpuInfo
     * @return
     */
    @Override
    public List<Map> getAllMpuName(InitialSwitchMpuInfo initialSwitchMpuInfo) {
        return initialSwitchMpuInfoMapper.getAllMpuName(initialSwitchMpuInfo);
    }

    /**
     * 获取mpu基础信息
     * @param initialSwitchMpuInfo
     * @return
     */
    @Override
    public List<InitialSwitchMpuInfo> getMpuMsg(InitialSwitchMpuInfo initialSwitchMpuInfo) {
        return initialSwitchMpuInfoMapper.getMpuMsg(initialSwitchMpuInfo);
    }

    /**
     * mpu的cpu利用率
     * @param initialSwitchMpuInfo
     * @return
     */
    @Override
    public Map<String, Object> getMpuCpuUse(InitialSwitchMpuInfo initialSwitchMpuInfo) {
        List<InitialSwitchMpuInfo> list = initialSwitchMpuInfoMapper.selectInitialSwitchMpuInfoList(initialSwitchMpuInfo);
        Map<String, Function<InitialSwitchMpuInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("cpuUsage", info -> info.getMpuEntityCpuUsage());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchMpuInfo::getCreateTime, extractors);
    }

    /**
     * mpu的内存使用率
     * @param initialSwitchMpuInfo
     * @return
     */
    @Override
    public Map<String, Object> getMpuMemUse(InitialSwitchMpuInfo initialSwitchMpuInfo) {
        List<InitialSwitchMpuInfo> list = initialSwitchMpuInfoMapper.selectInitialSwitchMpuInfoList(initialSwitchMpuInfo);
        Map<String, Function<InitialSwitchMpuInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("memUsage", info -> info.getMpuEntityMemUsage());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchMpuInfo::getCreateTime, extractors);
    }

    /**
     * mpu的温度
     * @param initialSwitchMpuInfo
     * @return
     */
    @Override
    public Map<String, Object> getMpuTemperature(InitialSwitchMpuInfo initialSwitchMpuInfo) {
        List<InitialSwitchMpuInfo> list = initialSwitchMpuInfoMapper.selectInitialSwitchMpuInfoList(initialSwitchMpuInfo);
        Map<String, Function<InitialSwitchMpuInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("temperature", info -> info.getMpuEntityTemperature());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchMpuInfo::getCreateTime, extractors);
    }
}
