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.InitialSwitchOpticalModule;
import com.ruoyi.rocketmq.mapper.InitialSwitchOpticalModuleMapper;
import com.ruoyi.rocketmq.service.IInitialSwitchOpticalModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 光模块信息Service业务层处理
 * 
 * @author gyt
 * @date 2025-09-22
 */
@Service
public class InitialSwitchOpticalModuleServiceImpl implements IInitialSwitchOpticalModuleService 
{
    @Autowired
    private InitialSwitchOpticalModuleMapper initialSwitchOpticalModuleMapper;

    /**
     * 查询光模块信息
     * 
     * @param id 光模块信息主键
     * @return 光模块信息
     */
    @Override
    public InitialSwitchOpticalModule selectInitialSwitchOpticalModuleById(Long id)
    {
        return initialSwitchOpticalModuleMapper.selectInitialSwitchOpticalModuleById(id);
    }

    /**
     * 查询光模块信息列表
     * 
     * @param initialSwitchOpticalModule 光模块信息
     * @return 光模块信息
     */
    @Override
    public List<InitialSwitchOpticalModule> selectInitialSwitchOpticalModuleList(InitialSwitchOpticalModule initialSwitchOpticalModule)
    {
        return initialSwitchOpticalModuleMapper.selectInitialSwitchOpticalModuleList(initialSwitchOpticalModule);
    }

    /**
     * 新增光模块信息
     * 
     * @param initialSwitchOpticalModule 光模块信息
     * @return 结果
     */
    @Override
    public int insertInitialSwitchOpticalModule(InitialSwitchOpticalModule initialSwitchOpticalModule)
    {
        return initialSwitchOpticalModuleMapper.insertInitialSwitchOpticalModule(initialSwitchOpticalModule);
    }

    /**
     * 修改光模块信息
     * 
     * @param initialSwitchOpticalModule 光模块信息
     * @return 结果
     */
    @Override
    public int updateInitialSwitchOpticalModule(InitialSwitchOpticalModule initialSwitchOpticalModule)
    {
        initialSwitchOpticalModule.setUpdateTime(DateUtils.getNowDate());
        return initialSwitchOpticalModuleMapper.updateInitialSwitchOpticalModule(initialSwitchOpticalModule);
    }

    /**
     * 批量删除光模块信息
     * 
     * @param ids 需要删除的光模块信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchOpticalModuleByIds(Long[] ids)
    {
        return initialSwitchOpticalModuleMapper.deleteInitialSwitchOpticalModuleByIds(ids);
    }

    /**
     * 删除光模块信息信息
     * 
     * @param id 光模块信息主键
     * @return 结果
     */
    @Override
    public int deleteInitialSwitchOpticalModuleById(Long id)
    {
        return initialSwitchOpticalModuleMapper.deleteInitialSwitchOpticalModuleById(id);
    }

    /**
     * 批量新增光模块信息
     * @param list
     */
    @Override
    public int batchInitialSwitchOpticalModule(List<InitialSwitchOpticalModule> list) {
        return initialSwitchOpticalModuleMapper.batchInitialSwitchOpticalModule(list);
    }

    /**
     * 光模块基础信息
     * @param initialSwitchOpticalModule
     * @return
     */
    @Override
    public List<InitialSwitchOpticalModule> switchOpticalModuleMsg(InitialSwitchOpticalModule initialSwitchOpticalModule) {
        return initialSwitchOpticalModuleMapper.switchOpticalModuleMsg(initialSwitchOpticalModule);
    }

    /**
     * 光模块名称查询
     * @param initialSwitchOpticalModule
     * @return
     */
    @Override
    public List<Map> getAllModuleName(InitialSwitchOpticalModule initialSwitchOpticalModule) {
        return initialSwitchOpticalModuleMapper.getAllModuleName(initialSwitchOpticalModule);
    }

    /**
     * 光衰阈值
     * @param initialSwitchOpticalModule
     * @return
     */
    @Override
    public Map<String, Object> opticalModuleLowThreshold(InitialSwitchOpticalModule initialSwitchOpticalModule) {
        List<InitialSwitchOpticalModule> list = initialSwitchOpticalModuleMapper.selectInitialSwitchOpticalModuleList(initialSwitchOpticalModule);
        Map<String, Function<InitialSwitchOpticalModule, ?>> extractors = new LinkedHashMap<>();
        extractors.put("TxLowThreshold", info -> info.getHwEntityOpticalTxLowThreshold());
        extractors.put("RxLowThreshold", info -> info.getHwEntityOpticalRxLowThreshold());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchOpticalModule::getCreateTime, extractors);
    }

    /**
     * 功率
     * @param initialSwitchOpticalModule
     * @return
     */
    @Override
    public Map<String, Object> opticalModulePower(InitialSwitchOpticalModule initialSwitchOpticalModule) {
        List<InitialSwitchOpticalModule> list = initialSwitchOpticalModuleMapper.selectInitialSwitchOpticalModuleList(initialSwitchOpticalModule);
        Map<String, Function<InitialSwitchOpticalModule, ?>> extractors = new LinkedHashMap<>();
        extractors.put("TxPower", info -> info.getHwEntityOpticalTxPower());
        extractors.put("RxPower", info -> info.getHwEntityOpticalRxPower());
        return EchartsDataUtils.buildEchartsData(list, InitialSwitchOpticalModule::getCreateTime, extractors);
    }
}
