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.InitialDockerInfo;
import com.ruoyi.rocketmq.mapper.InitialDockerInfoMapper;
import com.ruoyi.rocketmq.service.IInitialDockerInfoService;
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 InitialDockerInfoServiceImpl implements IInitialDockerInfoService 
{
    @Autowired
    private InitialDockerInfoMapper initialDockerInfoMapper;

    /**
     * 查询容器监控信息
     * 
     * @param id 容器监控信息主键
     * @return 容器监控信息
     */
    @Override
    public InitialDockerInfo selectInitialDockerInfoById(Long id)
    {
        return initialDockerInfoMapper.selectInitialDockerInfoById(id);
    }

    /**
     * 查询容器监控信息列表
     * 
     * @param initialDockerInfo 容器监控信息
     * @return 容器监控信息
     */
    @Override
    public List<InitialDockerInfo> selectInitialDockerInfoList(InitialDockerInfo initialDockerInfo)
    {
        return initialDockerInfoMapper.selectInitialDockerInfoList(initialDockerInfo);
    }

    /**
     * 新增容器监控信息
     * 
     * @param initialDockerInfo 容器监控信息
     * @return 结果
     */
    @Override
    public int insertInitialDockerInfo(InitialDockerInfo initialDockerInfo)
    {
        initialDockerInfo.setCreateTime(DateUtils.getNowDate());
        return initialDockerInfoMapper.insertInitialDockerInfo(initialDockerInfo);
    }

    /**
     * 修改容器监控信息
     * 
     * @param initialDockerInfo 容器监控信息
     * @return 结果
     */
    @Override
    public int updateInitialDockerInfo(InitialDockerInfo initialDockerInfo)
    {
        initialDockerInfo.setUpdateTime(DateUtils.getNowDate());
        return initialDockerInfoMapper.updateInitialDockerInfo(initialDockerInfo);
    }

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

    /**
     * 删除容器监控信息信息
     * 
     * @param id 容器监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialDockerInfoById(Long id)
    {
        return initialDockerInfoMapper.deleteInitialDockerInfoById(id);
    }

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

    /**
     * 容器基础信息
     * @param initialDockerInfo
     * @return
     */
    @Override
    public InitialDockerInfo getDockerDetailsMsg(InitialDockerInfo initialDockerInfo) {
        return initialDockerInfoMapper.getDockerDetailsMsg(initialDockerInfo);
    }

    /**
     * cou利用率
     * @param initialDockerInfo
     * @return
     */
    @Override
    public Map<String, Object> cpuUtilEcharts(InitialDockerInfo initialDockerInfo) {
        List<InitialDockerInfo> list = initialDockerInfoMapper.selectInitialDockerInfoList(initialDockerInfo);
        Map<String, Function<InitialDockerInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("cpuUtilData", info -> info.getCpuUtil());
        return EchartsDataUtils.buildEchartsData(list, InitialDockerInfo::getCreateTime, extractors);
    }
    /**
     * 容器的内存利用率
     * @param initialDockerInfo
     * @return
     */
    @Override
    public Map<String, Object> memUtilEcharts(InitialDockerInfo initialDockerInfo) {
        List<InitialDockerInfo> list = initialDockerInfoMapper.selectInitialDockerInfoList(initialDockerInfo);
        Map<String, Function<InitialDockerInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("memUtilData", info -> info.getMemUtil());
        return EchartsDataUtils.buildEchartsData(list, InitialDockerInfo::getCreateTime, extractors);
    }
    /**
     * 容器的网络速率(KB/s)
     * @param initialDockerInfo
     * @return
     */
    @Override
    public Map<String, Object> netSpeedEcharts(InitialDockerInfo initialDockerInfo) {
        List<InitialDockerInfo> list = initialDockerInfoMapper.selectInitialDockerInfoList(initialDockerInfo);
        Map<String, Function<InitialDockerInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("netInSpeedData", info -> info.getNetInSpeed());
        extractors.put("netOutSpeedData", info -> info.getNetOutSpeed());
        return EchartsDataUtils.buildEchartsData(list, InitialDockerInfo::getCreateTime, extractors);
    }

    /**
     * 获取指定服务器的容器id
     * @param initialDockerInfo
     * @return
     */
    @Override
    public List<Map> getAllDockerId(InitialDockerInfo initialDockerInfo) {
        return initialDockerInfoMapper.getAllDockerId(initialDockerInfo);
    }
}
