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.InitialMemoryInfo;
import com.ruoyi.rocketmq.mapper.InitialMemoryInfoMapper;
import com.ruoyi.rocketmq.service.IInitialMemoryInfoService;
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 InitialMemoryInfoServiceImpl implements IInitialMemoryInfoService 
{
    @Autowired
    private InitialMemoryInfoMapper initialMemoryInfoMapper;

    /**
     * 查询内存监控信息
     * 
     * @param id 内存监控信息主键
     * @return 内存监控信息
     */
    @Override
    public InitialMemoryInfo selectInitialMemoryInfoById(Long id)
    {
        return initialMemoryInfoMapper.selectInitialMemoryInfoById(id);
    }

    /**
     * 查询内存监控信息列表
     * 
     * @param initialMemoryInfo 内存监控信息
     * @return 内存监控信息
     */
    @Override
    public List<InitialMemoryInfo> selectInitialMemoryInfoList(InitialMemoryInfo initialMemoryInfo)
    {
        return initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
    }

    /**
     * 新增内存监控信息
     * 
     * @param initialMemoryInfo 内存监控信息
     * @return 结果
     */
    @Override
    public int insertInitialMemoryInfo(InitialMemoryInfo initialMemoryInfo)
    {
        initialMemoryInfo.setCreateTime(DateUtils.getNowDate());
        return initialMemoryInfoMapper.insertInitialMemoryInfo(initialMemoryInfo);
    }

    /**
     * 修改内存监控信息
     * 
     * @param initialMemoryInfo 内存监控信息
     * @return 结果
     */
    @Override
    public int updateInitialMemoryInfo(InitialMemoryInfo initialMemoryInfo)
    {
        initialMemoryInfo.setUpdateTime(DateUtils.getNowDate());
        return initialMemoryInfoMapper.updateInitialMemoryInfo(initialMemoryInfo);
    }

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

    /**
     * 删除内存监控信息信息
     * 
     * @param id 内存监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialMemoryInfoById(Long id)
    {
        return initialMemoryInfoMapper.deleteInitialMemoryInfoById(id);
    }

    /**
     * 批量新增内存监控信息
     *
     * @param list 内存监控信息集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialMemoryInfo(List<InitialMemoryInfo> list) {
        try {
            return initialMemoryInfoMapper.batchInsertInitialMemoryInfo(list);
        }catch (Exception e){
            log.error("批量插入内存信息失败，失败数量：{}", list.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }
    /**
     * 交换卷/文件的可用空间（字节）
     * @param initialMemoryInfo
     * @return
     */
    @Override
    public Map<String, Object> swapSizeFreeEcharts(InitialMemoryInfo initialMemoryInfo) {
        List<InitialMemoryInfo> list = initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
        Map<String, Function<InitialMemoryInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("swapSizeFreeData", InitialMemoryInfo::getSwapSizeFree);
        return EchartsDataUtils.buildEchartsData(list, InitialMemoryInfo::getCreateTime, extractors);
    }

    /**
     * 内存利用率
     * @param initialMemoryInfo
     * @return
     */
    @Override
    public Map<String, Object> untilzationEcharts(InitialMemoryInfo initialMemoryInfo) {
        List<InitialMemoryInfo> list = initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
        Map<String, Function<InitialMemoryInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("untilzationData", InitialMemoryInfo::getUntilzation);
        return EchartsDataUtils.buildEchartsData(list, InitialMemoryInfo::getCreateTime, extractors);
    }

    /**
     * 可用内存（KB）
     * @param initialMemoryInfo
     * @return
     */
    @Override
    public Map<String, Object> availableMemoryEcharts(InitialMemoryInfo initialMemoryInfo) {
        List<InitialMemoryInfo> list = initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
        Map<String, Function<InitialMemoryInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("availableData", info -> info.getAvailable() / 1024.0);
        return EchartsDataUtils.buildEchartsData(list, InitialMemoryInfo::getCreateTime, extractors);
    }

    /**
     * 可用交换空间百分比（%）
     * @param initialMemoryInfo
     * @return
     */
    @Override
    public Map<String, Object> swapSizePercentEcharts(InitialMemoryInfo initialMemoryInfo) {
        List<InitialMemoryInfo> list = initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
        Map<String, Function<InitialMemoryInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("swapSizePercentData", InitialMemoryInfo::getSwapSizePercent);
        return EchartsDataUtils.buildEchartsData(list, InitialMemoryInfo::getCreateTime, extractors);
    }

    /**
     * 可用内存百分比（%）
     * @param initialMemoryInfo
     * @return
     */
    @Override
    public Map<String, Object> percentEcharts(InitialMemoryInfo initialMemoryInfo) {
        List<InitialMemoryInfo> list = initialMemoryInfoMapper.selectInitialMemoryInfoList(initialMemoryInfo);
        Map<String, Function<InitialMemoryInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("percentData", InitialMemoryInfo::getPercent);
        return EchartsDataUtils.buildEchartsData(list, InitialMemoryInfo::getCreateTime, extractors);
    }
}
