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.InitialMountPointInfo;
import com.ruoyi.rocketmq.mapper.InitialMountPointInfoMapper;
import com.ruoyi.rocketmq.service.IInitialMountPointInfoService;
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 InitialMountPointInfoServiceImpl implements IInitialMountPointInfoService 
{
    @Autowired
    private InitialMountPointInfoMapper initialMountPointInfoMapper;

    /**
     * 查询挂载点监控信息
     * 
     * @param id 挂载点监控信息主键
     * @return 挂载点监控信息
     */
    @Override
    public InitialMountPointInfo selectInitialMountPointInfoById(Long id)
    {
        return initialMountPointInfoMapper.selectInitialMountPointInfoById(id);
    }

    /**
     * 查询挂载点监控信息列表
     * 
     * @param initialMountPointInfo 挂载点监控信息
     * @return 挂载点监控信息
     */
    @Override
    public List<InitialMountPointInfo> selectInitialMountPointInfoList(InitialMountPointInfo initialMountPointInfo)
    {
        return initialMountPointInfoMapper.selectInitialMountPointInfoList(initialMountPointInfo);
    }

    /**
     * 新增挂载点监控信息
     * 
     * @param initialMountPointInfo 挂载点监控信息
     * @return 结果
     */
    @Override
    public int insertInitialMountPointInfo(InitialMountPointInfo initialMountPointInfo)
    {
        initialMountPointInfo.setCreateTime(DateUtils.getNowDate());
        return initialMountPointInfoMapper.insertInitialMountPointInfo(initialMountPointInfo);
    }

    /**
     * 修改挂载点监控信息
     * 
     * @param initialMountPointInfo 挂载点监控信息
     * @return 结果
     */
    @Override
    public int updateInitialMountPointInfo(InitialMountPointInfo initialMountPointInfo)
    {
        initialMountPointInfo.setUpdateTime(DateUtils.getNowDate());
        return initialMountPointInfoMapper.updateInitialMountPointInfo(initialMountPointInfo);
    }

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

    /**
     * 删除挂载点监控信息信息
     * 
     * @param id 挂载点监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialMountPointInfoById(Long id)
    {
        return initialMountPointInfoMapper.deleteInitialMountPointInfoById(id);
    }
    /**
     * 批量新增挂载点监控信息
     *
     * @param list 挂载点监控信息集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialMountPointInfo(List<InitialMountPointInfo> list) {
        try {
            return initialMountPointInfoMapper.batchInsertInitialMountPointInfo(list);
        }catch (Exception e){
            log.error("批量插入挂载点信息失败，失败数量：{}", list.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }

    @Override
    public InitialMountPointInfo pointDetailsMsg(InitialMountPointInfo initialMountPointInfo) {
        return initialMountPointInfoMapper.pointDetailsMsg(initialMountPointInfo.getClientId());
    }

    /**
     * 获取挂载点监控信息的空间图形
     * @param initialMountPointInfo
     * @return
     */
    @Override
    public Map<String, Object> spaceEcharts(InitialMountPointInfo initialMountPointInfo) {
        List<InitialMountPointInfo> list = initialMountPointInfoMapper.selectInitialMountPointInfoList(initialMountPointInfo);
        Map<String, Function<InitialMountPointInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("vfsFreeData", info -> info.getVfsFree() / 1024.0);
        extractors.put("vfsTotalData", info -> info.getVfsTotal() / 1024.0);
        return EchartsDataUtils.buildEchartsData(list,InitialMountPointInfo::getCreateTime, extractors);
    }

    /**
     * 获取挂载文件/的空间利用率
     * @param initialMountPointInfo
     * @return
     */
    @Override
    public Map<String, Object> spaceRateEcharts(InitialMountPointInfo initialMountPointInfo) {
        List<InitialMountPointInfo> list = initialMountPointInfoMapper.selectInitialMountPointInfoList(initialMountPointInfo);
        Map<String, Function<InitialMountPointInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("vfsUtilData", InitialMountPointInfo::getVfsUtil);
        return EchartsDataUtils.buildEchartsData(list,InitialMountPointInfo::getCreateTime, extractors);
    }

    /**
     * 获取指定服务器的挂载文件系统名
     * @param initialMountPointInfo
     * @return
     */
    @Override
    public List<Map> getAllMountName(InitialMountPointInfo initialMountPointInfo) {
        return initialMountPointInfoMapper.getAllMountName(initialMountPointInfo);
    }
}
