package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.EchartsDataUtils;
import com.ruoyi.common.core.utils.SpeedUtils;
import com.ruoyi.system.domain.RmMonitorConfig;
import com.ruoyi.system.domain.RmMonitorConfigDetails;
import com.ruoyi.system.mapper.RmMonitorConfigDetailsMapper;
import com.ruoyi.system.mapper.RmMonitorConfigMapper;
import com.ruoyi.system.service.IRmMonitorConfigDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;

/**
 * 监控看板详情Service业务层处理
 * 
 * @author gyt
 * @date 2025-11-14
 */
@Service
public class RmMonitorConfigDetailsServiceImpl implements IRmMonitorConfigDetailsService 
{
    @Autowired
    private RmMonitorConfigDetailsMapper rmMonitorConfigDetailsMapper;
    @Autowired
    private RmMonitorConfigMapper rmMonitorConfigMapper;

    /**
     * 查询监控看板详情
     * 
     * @param id 监控看板详情主键
     * @return 监控看板详情
     */
    @Override
    public RmMonitorConfigDetails selectRmMonitorConfigDetailsById(Long id)
    {
        return rmMonitorConfigDetailsMapper.selectRmMonitorConfigDetailsById(id);
    }

    /**
     * 查询监控看板详情列表
     * 
     * @param rmMonitorConfigDetails 监控看板详情
     * @return 监控看板详情
     */
    @Override
    public List<RmMonitorConfigDetails> selectRmMonitorConfigDetailsList(RmMonitorConfigDetails rmMonitorConfigDetails)
    {
        return rmMonitorConfigDetailsMapper.selectRmMonitorConfigDetailsList(rmMonitorConfigDetails);
    }

    /**
     * 新增监控看板详情
     * 
     * @param rmMonitorConfigDetails 监控看板详情
     * @return 结果
     */
    @Override
    public int insertRmMonitorConfigDetails(RmMonitorConfigDetails rmMonitorConfigDetails)
    {
        rmMonitorConfigDetails.setCreateTime(DateUtils.getNowDate());
        return rmMonitorConfigDetailsMapper.insertRmMonitorConfigDetails(rmMonitorConfigDetails);
    }

    /**
     * 修改监控看板详情
     * 
     * @param rmMonitorConfigDetails 监控看板详情
     * @return 结果
     */
    @Override
    public int updateRmMonitorConfigDetails(RmMonitorConfigDetails rmMonitorConfigDetails)
    {
        rmMonitorConfigDetails.setUpdateTime(DateUtils.getNowDate());
        return rmMonitorConfigDetailsMapper.updateRmMonitorConfigDetails(rmMonitorConfigDetails);
    }

    /**
     * 批量删除监控看板详情
     * 
     * @param ids 需要删除的监控看板详情主键
     * @return 结果
     */
    @Override
    public int deleteRmMonitorConfigDetailsByIds(Long[] ids)
    {
        return rmMonitorConfigDetailsMapper.deleteRmMonitorConfigDetailsByIds(ids);
    }

    /**
     * 删除监控看板详情信息
     * 
     * @param id 监控看板详情主键
     * @return 结果
     */
    @Override
    public int deleteRmMonitorConfigDetailsById(Long id)
    {
        return rmMonitorConfigDetailsMapper.deleteRmMonitorConfigDetailsById(id);
    }

    /**
     * 监控看板-详情视图 出入流量
     * @param queryParam
     * @return
     */
    @Override
    public List<Map<String, Object>> getTrafficByMonitorView(RmMonitorConfigDetails queryParam) {
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        if(queryParam.getMonitorId() != null){
            // 根据id查询配置信息
            RmMonitorConfig rmMonitorConfig = rmMonitorConfigMapper.selectRmMonitorConfigById(queryParam.getMonitorId());
            Map<String, Object> map = processViewData(queryParam);
            map.put("rmMonitorConfig", rmMonitorConfig);
            resultMapList.add(map);
        }else{
            // 查询所有配置
            List<RmMonitorConfig> rmMonitorConfigs = rmMonitorConfigMapper.selectRmMonitorConfigList(new RmMonitorConfig());
            if (rmMonitorConfigs != null && !rmMonitorConfigs.isEmpty()) {
                for (RmMonitorConfig rmMonitorConfig : rmMonitorConfigs) {
                    RmMonitorConfigDetails rmMonitorConfigDetails = new RmMonitorConfigDetails();
                    BeanUtils.copyProperties(queryParam, rmMonitorConfigDetails);
                    rmMonitorConfigDetails.setMonitorId(rmMonitorConfig.getId());
                    Map<String, Object> map = processViewData(rmMonitorConfigDetails);
                    map.put("rmMonitorConfig", rmMonitorConfig);
                    resultMapList.add(map);
                }
            }
        }
        return resultMapList;
    }

    public Map<String, Object> processViewData(RmMonitorConfigDetails queryParam){
        // 查询概览配置内容
        RmMonitorConfig rmMonitorConfig = rmMonitorConfigMapper.selectRmMonitorConfigById(queryParam.getMonitorId());
        if(rmMonitorConfig != null){
            // 拿到开始时间
            String startTime = rmMonitorConfig.getMonitorStartTime();
            queryParam.setStartTime(startTime);
            if(queryParam.getEndTime() == null){
                queryParam.setEndTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            }
            List<RmMonitorConfigDetails> list = rmMonitorConfigDetailsMapper.selectRmMonitorConfigDetailsList(queryParam);
            if(list == null){
                list = new ArrayList<>();
            }
            try {
                String unit = SpeedUtils.calculateUnit(list, "inSpeed", "outSpeed");
                if(queryParam.getUnit() != null){
                    unit = queryParam.getUnit();
                }
                BigDecimal divisor = SpeedUtils.getDivisor(unit);
                Map<String, Function<RmMonitorConfigDetails, ?>> extractors = new LinkedHashMap<>();

                extractors.put("netOutSpeedData", info ->
                        info != null && info.getOutSpeed() != null ?
                                info.getOutSpeed().divide(divisor, 2, RoundingMode.HALF_UP) :
                                0);
                extractors.put("netInSpeedData", info ->
                        info != null && info.getInSpeed() != null ?
                                info.getInSpeed().divide(divisor, 2, RoundingMode.HALF_UP) :
                                0);
                extractors.put("deployDevice", info ->
                        info != null && info.getDeployDevice() != null ?
                                info.getDeployDevice() :
                                null);
                Map<String, Object> resultMap = EchartsDataUtils.buildEchartsDataAutoPadding(
                        list, RmMonitorConfigDetails::getCreateTime, extractors, queryParam.getStartTime(), queryParam.getEndTime()
                );
                resultMap.put("unit", unit);
                return resultMap;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return new HashMap<>();
    }

    @Override
    public int batchInsertRmMonitorConfigDetails(List<RmMonitorConfigDetails> batchInsertList) {
        return rmMonitorConfigDetailsMapper.batchInsertRmMonitorConfigDetails(batchInsertList);
    }
}
