package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.system.api.RemoteRocketMqService;
import com.ruoyi.system.api.domain.RmMonitorPolicyRemote;
import com.ruoyi.system.domain.AllInterfaceName;
import com.ruoyi.system.domain.RmSwitchInterfaceInfo;
import com.ruoyi.system.domain.RmSwitchManagement;
import com.ruoyi.system.mapper.AllInterfaceNameMapper;
import com.ruoyi.system.mapper.RmSwitchInterfaceInfoMapper;
import com.ruoyi.system.mapper.RmSwitchManagementMapper;
import com.ruoyi.system.service.IRmSwitchManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 交换机管理Service业务层处理
 * 
 * @author gyt
 * @date 2025-10-10
 */
@Service
public class RmSwitchManagementServiceImpl implements IRmSwitchManagementService 
{
    @Autowired
    private RmSwitchManagementMapper rmSwitchManagementMapper;
    @Autowired
    private RmSwitchInterfaceInfoMapper rmSwitchInterfaceInfoMapper;
    @Autowired
    private RemoteRocketMqService remoteRocketMqService;
    @Autowired
    private AllInterfaceNameMapper allInterfaceNameMapper;

    /**
     * 查询交换机管理
     * 
     * @param id 交换机管理主键
     * @return 交换机管理
     */
    @Override
    public RmSwitchManagement selectRmSwitchManagementById(Long id)
    {
        RmSwitchManagement rmSwitchManagement = rmSwitchManagementMapper.selectRmSwitchManagementById(id);
        if(rmSwitchManagement != null){
            // 查询端口备注信息
            RmSwitchInterfaceInfo queryParam = new RmSwitchInterfaceInfo();
            queryParam.setClientId(rmSwitchManagement.getClientId());
            List<RmSwitchInterfaceInfo> interfaceInfos = rmSwitchInterfaceInfoMapper.selectRmSwitchInterfaceInfoList(queryParam);
            rmSwitchManagement.setSwitchInterfaceInfoList(interfaceInfos);
        }
        return rmSwitchManagement;
    }

    /**
     * 查询交换机管理列表
     * 
     * @param rmSwitchManagement 交换机管理
     * @return 交换机管理
     */
    @Override
    public List<RmSwitchManagement> selectRmSwitchManagementList(RmSwitchManagement rmSwitchManagement)
    {
        return rmSwitchManagementMapper.selectRmSwitchManagementList(rmSwitchManagement);
    }

    /**
     * 新增交换机管理
     * 
     * @param rmSwitchManagement 交换机管理
     * @return 结果
     */
    @Override
    public int insertRmSwitchManagement(RmSwitchManagement rmSwitchManagement) {
        // 初始化交换机信息
        rmSwitchManagement.setCreateTime(DateUtils.getNowDate());
        String clientId= IdUtils.simpleUUID();
        rmSwitchManagement.setClientId(clientId);
        rmSwitchManagementMapper.insertRmSwitchManagement(rmSwitchManagement);

        // 查询并更新优先级为0的交换机策略
        R<List<RmMonitorPolicyRemote>> policyMsgR = remoteRocketMqService.getPolicyMsgInner(
                new RmMonitorPolicyRemote() {{
                    setPriority("0");
                    setResourceType("switch");
                }},
                SecurityConstants.INNER
        );

        if (policyMsgR.getData() != null && !policyMsgR.getData().isEmpty()) {
            RmMonitorPolicyRemote policyMsg = policyMsgR.getData().get(0);

            // 构建设备信息字符串
            String newDeviceList = StringUtils.isBlank(policyMsg.getDeployDevice())
                    ? clientId
                    : policyMsg.getDeployDevice() + "\n" + clientId;

            // 更新策略设备列表
            remoteRocketMqService.updatePolicyMsgInner(
                    new RmMonitorPolicyRemote() {{
                        setId(policyMsg.getId());
                        setDeployDevice(newDeviceList);
                        setUpdateTime(DateUtils.getNowDate());
                    }},
                    SecurityConstants.INNER
            );

            // 下发更新后的策略
            remoteRocketMqService.innerIssueSwitchPolicy(policyMsg.getId(), SecurityConstants.INNER);
        }

        return 1;
    }

    /**
     * 修改交换机管理
     * 
     * @param rmSwitchManagement 交换机管理
     * @return 结果
     */
    @Override
    public int updateRmSwitchManagement(RmSwitchManagement rmSwitchManagement)
    {
        rmSwitchManagement.setUpdateTime(DateUtils.getNowDate());
        return rmSwitchManagementMapper.updateRmSwitchManagement(rmSwitchManagement);
    }

    /**
     * 批量删除交换机管理
     * 
     * @param ids 需要删除的交换机管理主键
     * @return 结果
     */
    @Override
    public int deleteRmSwitchManagementByIds(Long[] ids)
    {
        return rmSwitchManagementMapper.deleteRmSwitchManagementByIds(ids);
    }

    /**
     * 删除交换机管理信息
     * 
     * @param id 交换机管理主键
     * @return 结果
     */
    @Override
    public int deleteRmSwitchManagementById(Long id)
    {
        return rmSwitchManagementMapper.deleteRmSwitchManagementById(id);
    }

    /**
     * 交换机信息树形结构
     * @return
     */
    @Override
    public List<RmSwitchManagement> getAllSwitchNameTree() {
        List<RmSwitchManagement> list = rmSwitchManagementMapper.selectRmSwitchManagementList(new RmSwitchManagement());
        // 构建树形结构结果
        for (RmSwitchManagement switchManagement : list) {
            switchManagement.setValue(switchManagement.getId());
            switchManagement.setLabel(switchManagement.getSwitchName());
            // 该交换机的所有接口信息
            String clientId = switchManagement.getClientId();
            String switchName = switchManagement.getSwitchName();
            AllInterfaceName allInterfaceName = new AllInterfaceName();
            allInterfaceName.setClientId(clientId);
            List<AllInterfaceName> interfaceNameList = allInterfaceNameMapper.selectAllInterfaceNameList(allInterfaceName);
            for (AllInterfaceName interfaceName : interfaceNameList) {
                String valueStr = String.join(";",
                        interfaceName.getId().toString(),
                        clientId,
                        interfaceName.getInterfaceName(),
                        switchName
                );
                interfaceName.setValue(valueStr);
                interfaceName.setLabel(interfaceName.getInterfaceName());
                // 查询别名
                RmSwitchInterfaceInfo query = new RmSwitchInterfaceInfo();
                query.setInterfaceName(interfaceName.getInterfaceName());
                query.setClientId(clientId);
                List<RmSwitchInterfaceInfo> interfaceInfos = rmSwitchInterfaceInfoMapper.selectRmSwitchInterfaceInfoList(query);
                String otherName = "";
                if(!interfaceInfos.isEmpty()){
                    otherName = interfaceInfos.get(0).getInterfaceRemark();
                    interfaceName.setOtherName(otherName);
                }
                String labelStr = String.join("    ",interfaceName.getInterfaceName(), otherName);
                interfaceName.setLabel(labelStr);
            }
            switchManagement.setChildren(interfaceNameList);
        }
        return list;
    }
}
