package com.ruoyi.mtragent.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.security.utils.SecurityUtils;
import com.ruoyi.mtragent.domain.RmMtrPolicyConfig;
import com.ruoyi.mtragent.mapper.RmMtrPolicyConfigMapper;
import com.ruoyi.mtragent.service.IRmMtrPolicyConfigService;
import com.ruoyi.system.api.RemoteRocketMqService;
import com.ruoyi.system.api.domain.RmNetworkInterfaceRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

/**
 * mtr探测策略配置Service业务层处理
 * 
 * @author gyt
 * @date 2025-11-18
 */
@Service
public class RmMtrPolicyConfigServiceImpl implements IRmMtrPolicyConfigService 
{
    @Autowired
    private RmMtrPolicyConfigMapper rmMtrPolicyConfigMapper;
    @Autowired
    private RemoteRocketMqService remoteRocketMqService;

    /**
     * 查询mtr探测策略配置
     * 
     * @param id mtr探测策略配置主键
     * @return mtr探测策略配置
     */
    @Override
    public RmMtrPolicyConfig selectRmMtrPolicyConfigById(Long id)
    {
        RmMtrPolicyConfig mtrPolicyConfig = rmMtrPolicyConfigMapper.selectRmMtrPolicyConfigById(id);
        mtrPolicyConfig.setServerGroup(mtrPolicyConfig.getServerGroup().replace("\n", ";"));
        return mtrPolicyConfig;
    }

    /**
     * 查询mtr探测策略配置列表
     * 
     * @param rmMtrPolicyConfig mtr探测策略配置
     * @return mtr探测策略配置
     */
    @Override
    public List<RmMtrPolicyConfig> selectRmMtrPolicyConfigList(RmMtrPolicyConfig rmMtrPolicyConfig)
    {
        List<RmMtrPolicyConfig> list = rmMtrPolicyConfigMapper.selectRmMtrPolicyConfigList(rmMtrPolicyConfig);
        for (RmMtrPolicyConfig mtrPolicyConfig : list) {
            mtrPolicyConfig.setServerGroup(mtrPolicyConfig.getServerGroup().replace("\n", ";"));
        }
        return list;
    }

    /**
     * 辅助方法，给ip集合赋值
     * @param mtrPolicyConfig
     */
    public void setServerip(RmMtrPolicyConfig mtrPolicyConfig){
        StringJoiner resultJoiner = new StringJoiner(";");
        String serverGroupStr = mtrPolicyConfig.getServerGroup().replace("\n",",");
        RmNetworkInterfaceRemote query = new RmNetworkInterfaceRemote();
        query.setClientIds(serverGroupStr);
        R<List<RmNetworkInterfaceRemote>> networkList = remoteRocketMqService.getNetworkInterfaceList(query, SecurityConstants.INNER);
        if(networkList != null && networkList.getData() != null){
            List<RmNetworkInterfaceRemote> rmNetworkInterfaceRemoteList = networkList.getData();
            if(!rmNetworkInterfaceRemoteList.isEmpty()){
                for (RmNetworkInterfaceRemote rmNetworkInterfaceRemote : rmNetworkInterfaceRemoteList) {
                    String publicIp = rmNetworkInterfaceRemote.getPublicIp();
                    resultJoiner.add(publicIp);
                }
                mtrPolicyConfig.setServeripGroup(resultJoiner.toString());
            }
        }
    }

    /**
     * 新增mtr探测策略配置
     * 
     * @param rmMtrPolicyConfig mtr探测策略配置
     * @return 结果
     */
    @Override
    public int insertRmMtrPolicyConfig(RmMtrPolicyConfig rmMtrPolicyConfig)
    {
        rmMtrPolicyConfig.setCreateTime(DateUtils.getNowDate());
        rmMtrPolicyConfig.setUpdateTime(DateUtils.getNowDate());
        rmMtrPolicyConfig.setCreateBy(SecurityUtils.getUsername());
        // 给ip赋值
        setServerip(rmMtrPolicyConfig);
        int rows = rmMtrPolicyConfigMapper.insertRmMtrPolicyConfig(rmMtrPolicyConfig);
        return rows;
    }

    /**
     * 修改mtr探测策略配置
     * 
     * @param rmMtrPolicyConfig mtr探测策略配置
     * @return 结果
     */
    @Override
    public int updateRmMtrPolicyConfig(RmMtrPolicyConfig rmMtrPolicyConfig)
    {
        rmMtrPolicyConfig.setUpdateTime(DateUtils.getNowDate());
        return rmMtrPolicyConfigMapper.updateRmMtrPolicyConfig(rmMtrPolicyConfig);
    }

    /**
     * 批量删除mtr探测策略配置
     * 
     * @param ids 需要删除的mtr探测策略配置主键
     * @return 结果
     */
    @Override
    public int deleteRmMtrPolicyConfigByIds(Long[] ids)
    {
        return rmMtrPolicyConfigMapper.deleteRmMtrPolicyConfigByIds(ids);
    }

    /**
     * 删除mtr探测策略配置信息
     * 
     * @param id mtr探测策略配置主键
     * @return 结果
     */
    @Override
    public int deleteRmMtrPolicyConfigById(Long id)
    {
        return rmMtrPolicyConfigMapper.deleteRmMtrPolicyConfigById(id);
    }
}
