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.InitialDiskInfo;
import com.ruoyi.rocketmq.mapper.InitialDiskInfoMapper;
import com.ruoyi.rocketmq.service.IInitialDiskInfoService;
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 InitialDiskInfoServiceImpl implements IInitialDiskInfoService 
{
    @Autowired
    private InitialDiskInfoMapper initialDiskInfoMapper;

    /**
     * 查询磁盘监控信息
     * 
     * @param id 磁盘监控信息主键
     * @return 磁盘监控信息
     */
    @Override
    public InitialDiskInfo selectInitialDiskInfoById(Long id)
    {
        return initialDiskInfoMapper.selectInitialDiskInfoById(id);
    }

    /**
     * 查询磁盘监控信息列表
     * 
     * @param initialDiskInfo 磁盘监控信息
     * @return 磁盘监控信息
     */
    @Override
    public List<InitialDiskInfo> selectInitialDiskInfoList(InitialDiskInfo initialDiskInfo)
    {
        return initialDiskInfoMapper.selectInitialDiskInfoList(initialDiskInfo);
    }

    /**
     * 新增磁盘监控信息
     * 
     * @param initialDiskInfo 磁盘监控信息
     * @return 结果
     */
    @Override
    public int insertInitialDiskInfo(InitialDiskInfo initialDiskInfo)
    {
        initialDiskInfo.setCreateTime(DateUtils.getNowDate());
        return initialDiskInfoMapper.insertInitialDiskInfo(initialDiskInfo);
    }

    /**
     * 修改磁盘监控信息
     * 
     * @param initialDiskInfo 磁盘监控信息
     * @return 结果
     */
    @Override
    public int updateInitialDiskInfo(InitialDiskInfo initialDiskInfo)
    {
        initialDiskInfo.setUpdateTime(DateUtils.getNowDate());
        return initialDiskInfoMapper.updateInitialDiskInfo(initialDiskInfo);
    }

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

    /**
     * 删除磁盘监控信息信息
     * 
     * @param id 磁盘监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialDiskInfoById(Long id)
    {
        return initialDiskInfoMapper.deleteInitialDiskInfoById(id);
    }
    /**
     * 批量新增磁盘监控信息
     *
     * @param list 磁盘监控信息集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialDiskInfo(List<InitialDiskInfo> list) {
        try {
            return initialDiskInfoMapper.batchInsertInitialDiskInfo(list);
        }catch (Exception e){
            log.error("批量插入磁盘信息失败，失败数量：{}", list.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }
    /**
     * 磁盘设备/dev/sda基础信息
     * @param initialDiskInfo
     * @return
     */
    @Override
    public InitialDiskInfo getDistDetailsMsg(InitialDiskInfo initialDiskInfo) {
        InitialDiskInfo info = initialDiskInfoMapper.getDistDetailsMsgByClientId(initialDiskInfo);
        long gbUnit = 1024L * 1024 * 1024;
        info.setTotal(info.getTotal() / gbUnit);
        return initialDiskInfoMapper.getDistDetailsMsgByClientId(initialDiskInfo);
    }
    /**
     * /dev/sda读写速率(KB/s)
     * @param initialDiskInfo
     * @return
     */
    @Override
    public Map<String, Object> rwSpeedEcharts(InitialDiskInfo initialDiskInfo) {
        List<InitialDiskInfo> list = initialDiskInfoMapper.selectInitialDiskInfoList(initialDiskInfo);
        Map<String, Function<InitialDiskInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("readSpeedData", info -> info.getReadSpeed() / 1024.0);
        extractors.put("writeSpeedData", info -> info.getWriteSpeed() / 1024.0);
        return EchartsDataUtils.buildEchartsData(list, InitialDiskInfo::getCreateTime, extractors);
    }
    /**
     * /dev/sda读写次数
     * @param initialDiskInfo
     * @return
     */
    @Override
    public Map<String, Object> rwTimesEcharts(InitialDiskInfo initialDiskInfo) {
        List<InitialDiskInfo> list = initialDiskInfoMapper.selectInitialDiskInfoList(initialDiskInfo);
        Map<String, Function<InitialDiskInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("readTimesData", info -> info.getReadTimes());
        extractors.put("writeTimesData", info -> info.getWriteTimes());
        return EchartsDataUtils.buildEchartsData(list, InitialDiskInfo::getCreateTime, extractors);
    }
    /**
     * /dev/sda读写字节
     * @param initialDiskInfo
     * @return
     */
    @Override
    public Map<String, Object> rwBytesEcharts(InitialDiskInfo initialDiskInfo) {
        List<InitialDiskInfo> list = initialDiskInfoMapper.selectInitialDiskInfoList(initialDiskInfo);
        Map<String, Function<InitialDiskInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("readBytesData", info -> info.getReadBytes());
        extractors.put("writeBytesData", info -> info.getWriteBytes());
        return EchartsDataUtils.buildEchartsData(list, InitialDiskInfo::getCreateTime, extractors);
    }

    /**
     * 获取指定服务器的磁盘名称
     * @param initialDiskInfo
     * @return
     */
    @Override
    public List<Map> getAllDistName(InitialDiskInfo initialDiskInfo) {
        return initialDiskInfoMapper.getAllDistName(initialDiskInfo);
    }
}
