package com.ruoyi.rocketmq.service.impl;

import com.ruoyi.common.core.utils.ConvertOtherTypeUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.EchartsDataUtils;
import com.ruoyi.common.core.utils.SpeedUtils;
import com.ruoyi.rocketmq.domain.InitialSwitchInfo;
import com.ruoyi.rocketmq.mapper.InitialSwitchInfoMapper;
import com.ruoyi.rocketmq.service.IInitialSwitchInfoService;
import com.ruoyi.system.api.RemoteRevenueConfigService;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
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 InitialSwitchInfoServiceImpl implements IInitialSwitchInfoService 
{
    @Autowired
    private InitialSwitchInfoMapper initialSwitchInfoMapper;
    @Autowired
    private RemoteRevenueConfigService remoteRevenueConfigService;

    /**
     * 查询交换机流量监控信息
     * 
     * @param id 交换机流量监控信息主键
     * @return 交换机流量监控信息
     */
    @Override
    public InitialSwitchInfo selectInitialSwitchInfoById(Long id)
    {
        return initialSwitchInfoMapper.selectInitialSwitchInfoById(id);
    }

    /**
     * 查询交换机流量监控信息列表
     * 
     * @param initialSwitchInfo 交换机流量监控信息
     * @return 交换机流量监控信息
     */
    @Override
    public List<InitialSwitchInfo> selectInitialSwitchInfoList(InitialSwitchInfo initialSwitchInfo)
    {
        return initialSwitchInfoMapper.selectInitialSwitchInfoList(initialSwitchInfo);
    }

    /**
     * 新增交换机流量监控信息
     * 
     * @param initialSwitchInfo 交换机流量监控信息
     * @return 结果
     */
    @Override
    public int insertInitialSwitchInfo(InitialSwitchInfo initialSwitchInfo)
    {
        initialSwitchInfo.setCreateTime(DateUtils.getNowDate());
        return initialSwitchInfoMapper.insertInitialSwitchInfo(initialSwitchInfo);
    }

    /**
     * 修改交换机流量监控信息
     * 
     * @param initialSwitchInfo 交换机流量监控信息
     * @return 结果
     */
    @Override
    public int updateInitialSwitchInfo(InitialSwitchInfo initialSwitchInfo)
    {
        initialSwitchInfo.setUpdateTime(DateUtils.getNowDate());
        return initialSwitchInfoMapper.updateInitialSwitchInfo(initialSwitchInfo);
    }

    /**
     * 批量删除交换机流量监控信息
     * 
     * @param ids 需要删除的交换机流量监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchInfoByIds(Long[] ids)
    {
        return initialSwitchInfoMapper.deleteInitialSwitchInfoByIds(ids);
    }

    /**
     * 删除交换机流量监控信息信息
     * 
     * @param id 交换机流量监控信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchInfoById(Long id)
    {
        return initialSwitchInfoMapper.deleteInitialSwitchInfoById(id);
    }

    /**
     * 批量新增交换机流量监控信息
     *
     * @param list 交换机流量监控信息集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public int batchInsertInitialSwitchInfo(List<InitialSwitchInfo> list) {
        try {
            return initialSwitchInfoMapper.batchInsertInitialSwitchInfo(list);
        }catch (Exception e){
            log.error("批量插入交换机信息失败，失败数量：{}", list.size(), e);
            throw new RuntimeException("批量保存失败",e);
        }
    }

    /**
     * 获取交换机网络端口的基础信息
     * @param initialSwitchInfo
     * @return
     */
    @Override
    public InitialSwitchInfo getSwitchNetDetailsMsg(InitialSwitchInfo initialSwitchInfo) {
        InitialSwitchInfo info = initialSwitchInfoMapper.getSwitchNetDetailsMsg(initialSwitchInfo);
        if(info.getType()!=null){
            info.setType(ConvertOtherTypeUtil.getInterfaceTypeName(Integer.parseInt(info.getType())));
        }
        return info;
    }

    /**
     * 获取交换机网口网络速率
     * @param initialSwitchInfo
     * @return
     */
    @Override
    public Map<String, Object> switchNetSpeedEcharts(InitialSwitchInfo initialSwitchInfo) {
        List<InitialSwitchInfo> list = initialSwitchInfoMapper.selectInitialSwitchInfoList(initialSwitchInfo);
        try {
            String unit = SpeedUtils.calculateUnit(list, "inSpeed", "outSpeed");
            BigDecimal divisor = SpeedUtils.getDivisor(unit);
            Map<String, Function<InitialSwitchInfo, ?>> extractors = new LinkedHashMap<>();
            extractors.put("netInSpeedData", info ->
                    info != null && info.getInSpeed() != null ?
                            info.getInSpeed().divide(divisor, 0, RoundingMode.HALF_UP) :
                            0);

            extractors.put("netOutSpeedData", info ->
                    info != null && info.getOutSpeed() != null ?
                            info.getOutSpeed().divide(divisor, 0, RoundingMode.HALF_UP) :
                            0);
            Map<String, Object> resultMap = EchartsDataUtils.buildEchartsData(list, InitialSwitchInfo::getCreateTime, extractors);
            resultMap.put("unit", unit);
            return resultMap;
        } catch (Exception e){
            e.printStackTrace();
        }
        return new HashMap<>();
    }

    /**
     * 丢包数
     * @param initialSwitchInfo
     * @return
     */
    @Override
    public Map<String, Object> switchNetDiscardsEcharts(InitialSwitchInfo initialSwitchInfo) {
        List<InitialSwitchInfo> list = initialSwitchInfoMapper.selectInitialSwitchInfoList(initialSwitchInfo);
        Map<String, Function<InitialSwitchInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("netInDiscardsData", info -> info.getIfInDiscards());
        extractors.put("netOutDiscardsData", info -> info.getIfOutDiscards());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchInfo::getCreateTime, extractors);
    }

    /**
     * 流量总数
     * @param initialSwitchInfo
     * @return
     */
    @Override
    public Map<String, Object> switchNetTotalEcharts(InitialSwitchInfo initialSwitchInfo) {
        List<InitialSwitchInfo> list = initialSwitchInfoMapper.selectInitialSwitchInfoList(initialSwitchInfo);
        Map<String, Function<InitialSwitchInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("netInTotalData", info -> info.getInBytes().multiply(new BigDecimal(8)));
        extractors.put("netOutTotalData", info -> info.getOutBytes().multiply(new BigDecimal(8)));
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchInfo::getCreateTime, extractors);
    }

    /**
     * 错误包数
     * @param initialSwitchInfo
     * @return
     */
    @Override
    public Map<String, Object> switchNetErrDiscardsEcharts(InitialSwitchInfo initialSwitchInfo) {
        List<InitialSwitchInfo> list = initialSwitchInfoMapper.selectInitialSwitchInfoList(initialSwitchInfo);
        Map<String, Function<InitialSwitchInfo, ?>> extractors = new LinkedHashMap<>();
        extractors.put("netInErrDiscardsData", info -> info.getIfInErrors());
        extractors.put("netOutErrDiscardsData", info -> info.getIfOutErrors());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchInfo::getCreateTime, extractors);
    }
}
