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.InitialCpuInfo;
import com.ruoyi.rocketmq.mapper.InitialCpuInfoMapper;
import com.ruoyi.rocketmq.service.IInitialCpuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

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

import static com.ruoyi.common.core.utils.EchartsDataUtils.convertMicrosecondsToSeconds;


/**
 * CPU监控信息Service业务层处理
 * 
 * @author gyt
 * @date 2025-08-25
 */
@Service
@Slf4j
public class InitialCpuInfoServiceImpl implements IInitialCpuInfoService 
{
    @Autowired
    private InitialCpuInfoMapper initialCpuInfoMapper;

    /**
     * 查询CPU监控信息
     * 
     * @param id CPU监控信息主键
     * @return CPU监控信息
     */
    @Override
    public InitialCpuInfo selectInitialCpuInfoById(Long id)
    {
        return initialCpuInfoMapper.selectInitialCpuInfoById(id);
    }

    /**
     * 查询CPU监控信息列表
     * 
     * @param initialCpuInfo CPU监控信息
     * @return CPU监控信息
     */
    @Override
    public List<InitialCpuInfo> selectInitialCpuInfoList(InitialCpuInfo initialCpuInfo)
    {
        return initialCpuInfoMapper.selectInitialCpuInfoList(initialCpuInfo);
    }

    /**
     * 新增CPU监控信息
     * 
     * @param initialCpuInfo CPU监控信息
     * @return 结果
     */
    @Override
    public int insertInitialCpuInfo(InitialCpuInfo initialCpuInfo)
    {
        initialCpuInfo.setCreateTime(DateUtils.getNowDate());
        return initialCpuInfoMapper.insertInitialCpuInfo(initialCpuInfo);
    }

    /**
     * 修改CPU监控信息
     * 
     * @param initialCpuInfo CPU监控信息
     * @return 结果
     */
    @Override
    public int updateInitialCpuInfo(InitialCpuInfo initialCpuInfo)
    {
        initialCpuInfo.setUpdateTime(DateUtils.getNowDate());
        return initialCpuInfoMapper.updateInitialCpuInfo(initialCpuInfo);
    }

    /**
     * 批量删除CPU监控信息
     * 
     * @param ids 需要删除的CPU监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialCpuInfoByIds(Long[] ids)
    {
        return initialCpuInfoMapper.deleteInitialCpuInfoByIds(ids);
    }

    /**
     * 删除CPU监控信息信息
     * 
     * @param id CPU监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialCpuInfoById(Long id)
    {
        return initialCpuInfoMapper.deleteInitialCpuInfoById(id);
    }

    /**
     * 批量新增CPU监控信息信息
     *
     * @param list CPU监控信息信息集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialCpuInfo(List<InitialCpuInfo> list) {
        try {
            return initialCpuInfoMapper.batchInsertInitialCpuInfo(list);
        }catch (Exception e){
            log.error("批量插入CPU信息失败，失败数量：{}", list.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }

    /**
     * cpu负载
     * @param initialCpuInfo
     * @return
     */
    @Override
    public Map<String, Object> cupLoadEcharts(InitialCpuInfo initialCpuInfo) {
        // 查询原始CPU监控数据并按时间排序
        List<InitialCpuInfo> list = initialCpuInfoMapper.selectInitialCpuInfoList(initialCpuInfo);

        Map<String, Function<InitialCpuInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("load1Data", InitialCpuInfo::getAvg1);
        extractors.put("load5Data", InitialCpuInfo::getAvg5);
        extractors.put("load15Data", InitialCpuInfo::getAvg15);
        extractors.put("utiData", InitialCpuInfo::getUti);

        return EchartsDataUtils.buildEchartsData(list, InitialCpuInfo::getCreateTime, extractors);
    }
    /**
     * cpu时间(s)
     * @param initialCpuInfo
     * @return
     */
    @Override
    public Map<String, Object> cpuTimeEcharts(InitialCpuInfo initialCpuInfo) {
        List<InitialCpuInfo> list = initialCpuInfoMapper.selectInitialCpuInfoList(initialCpuInfo);

        Map<String, Function<InitialCpuInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("normalData", info -> convertMicrosecondsToSeconds(info.getNormal()));
        extractors.put("idleData", info -> convertMicrosecondsToSeconds(info.getIdle()));
        extractors.put("iowaitData", InitialCpuInfo::getIowait);
        extractors.put("systemData", info -> convertMicrosecondsToSeconds(info.getSystem()));
        extractors.put("norespData", InitialCpuInfo::getNoresp);
        extractors.put("userpData", info -> convertMicrosecondsToSeconds(info.getUser()));

        return EchartsDataUtils.buildEchartsData(list,InitialCpuInfo::getCreateTime, extractors);
    }

}
