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.domain.InitialMemoryInfo;
import com.ruoyi.rocketmq.domain.InitialSystemInfo;
import com.ruoyi.rocketmq.domain.vo.SystemMsgVo;
import com.ruoyi.rocketmq.mapper.InitialCpuInfoMapper;
import com.ruoyi.rocketmq.mapper.InitialMemoryInfoMapper;
import com.ruoyi.rocketmq.mapper.InitialSystemInfoMapper;
import com.ruoyi.rocketmq.service.IInitialSystemInfoService;
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;

/**
 * 系统监控信息Service业务层处理
 * 
 * @author gyt
 * @date 2025-08-25
 */
@Service
@Slf4j
public class InitialSystemInfoServiceImpl implements IInitialSystemInfoService 
{
    @Autowired
    private InitialSystemInfoMapper initialSystemInfoMapper;
    @Autowired
    private InitialMemoryInfoMapper initialMemoryInfoMapper;
    @Autowired
    private InitialCpuInfoMapper initialCpuInfoMapper;

    /**
     * 查询系统监控信息
     * 
     * @param id 系统监控信息主键
     * @return 系统监控信息
     */
    @Override
    public InitialSystemInfo selectInitialSystemInfoById(Long id)
    {
        return initialSystemInfoMapper.selectInitialSystemInfoById(id);
    }

    /**
     * 查询系统监控信息列表
     * 
     * @param initialSystemInfo 系统监控信息
     * @return 系统监控信息
     */
    @Override
    public List<InitialSystemInfo> selectInitialSystemInfoList(InitialSystemInfo initialSystemInfo)
    {
        return initialSystemInfoMapper.selectInitialSystemInfoList(initialSystemInfo);
    }

    /**
     * 新增系统监控信息
     * 
     * @param initialSystemInfo 系统监控信息
     * @return 结果
     */
    @Override
    public int insertInitialSystemInfo(InitialSystemInfo initialSystemInfo)
    {
        initialSystemInfo.setCreateTime(DateUtils.getNowDate());
        return initialSystemInfoMapper.insertInitialSystemInfo(initialSystemInfo);
    }

    /**
     * 修改系统监控信息
     * 
     * @param initialSystemInfo 系统监控信息
     * @return 结果
     */
    @Override
    public int updateInitialSystemInfo(InitialSystemInfo initialSystemInfo)
    {
        initialSystemInfo.setUpdateTime(DateUtils.getNowDate());
        return initialSystemInfoMapper.updateInitialSystemInfo(initialSystemInfo);
    }

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

    /**
     * 删除系统监控信息信息
     * 
     * @param id 系统监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSystemInfoById(Long id)
    {
        return initialSystemInfoMapper.deleteInitialSystemInfoById(id);
    }

    /**
     * 批量新增系统监控信息
     *
     * @param initialSystemInfo 系统监控信息集合
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialSystemInfo(List<InitialSystemInfo> initialSystemInfo)
    {
        try {
            return initialSystemInfoMapper.batchInsertInitialSystemInfo(initialSystemInfo);
        }catch (Exception e){
            log.error("批量插入挂载点信息失败，失败数量：{}", initialSystemInfo.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }
    /**
     * 系统进程（个）
     * @param initialSystemInfo
     * @return
     */
    @Override
    public Map<String, Object> systemProcessEcharts(InitialSystemInfo initialSystemInfo) {
        List<InitialSystemInfo> list = initialSystemInfoMapper.selectInitialSystemInfoList(initialSystemInfo);

        Map<String, Function<InitialSystemInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("runProcNumData", InitialSystemInfo::getRunProcNum);
        extractors.put("procNumData", InitialSystemInfo::getProcNum);

        return EchartsDataUtils.buildEchartsData(list,InitialSystemInfo::getCreateTime, extractors);
    }
    /**
     * 登录用户数（个）
     * @param initialSystemInfo
     * @return
     */
    @Override
    public Map<String, Object> systemUserNumEcharts(InitialSystemInfo initialSystemInfo) {
        List<InitialSystemInfo> list = initialSystemInfoMapper.selectInitialSystemInfoList(initialSystemInfo);

        Map<String, Function<InitialSystemInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("usersNumData", InitialSystemInfo::getUsersNum);

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

    /**
     * 系统基础信息
     * @param initialSystemInfo
     * @return
     */
    @Override
    public SystemMsgVo getSystemDetailsMsg(InitialSystemInfo initialSystemInfo) {
        // 获取总内存信息
        InitialMemoryInfo memoryInfo = initialMemoryInfoMapper.getMemoryInfoByClientId(initialSystemInfo.getClientId());
        // 获取cpu数量
        InitialCpuInfo cpuInfo = initialCpuInfoMapper.getCpuInfoByClientId(initialSystemInfo.getClientId());
        // 获取系统信息
        SystemMsgVo systemMsgVo = initialSystemInfoMapper.getSystemMsgByClientId(initialSystemInfo.getClientId());
        systemMsgVo.setNum(cpuInfo.getNum());
        systemMsgVo.setMemoryTotal(memoryInfo.getTotal());
        return systemMsgVo;
    }
}
