package com.tongran.rocketmq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tongran.common.core.enums.MsgEnum;
import com.tongran.common.core.utils.DateUtils;
import com.tongran.rocketmq.domain.DeviceMessage;
import com.tongran.rocketmq.domain.RmTcpdumpConfig;
import com.tongran.rocketmq.domain.vo.PolicyTypeVo;
import com.tongran.rocketmq.mapper.RmTcpdumpConfigMapper;
import com.tongran.rocketmq.model.ProducerMode;
import com.tongran.rocketmq.producer.MessageProducer;
import com.tongran.rocketmq.service.IRmTcpdumpConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * tcpdump探测策略Service业务层处理
 * 
 * @author tongran
 * @date 2026-01-21
 */
@Service
public class RmTcpdumpConfigServiceImpl implements IRmTcpdumpConfigService 
{
    @Autowired
    private RmTcpdumpConfigMapper rmTcpdumpConfigMapper;
    @Autowired
    private ProducerMode producerMode;

    /**
     * 查询tcpdump探测策略
     * 
     * @param id tcpdump探测策略主键
     * @return tcpdump探测策略
     */
    @Override
    public RmTcpdumpConfig selectRmTcpdumpConfigById(Long id)
    {
        return rmTcpdumpConfigMapper.selectRmTcpdumpConfigById(id);
    }

    /**
     * 查询tcpdump探测策略列表
     * 
     * @param rmTcpdumpConfig tcpdump探测策略
     * @return tcpdump探测策略
     */
    @Override
    public List<RmTcpdumpConfig> selectRmTcpdumpConfigList(RmTcpdumpConfig rmTcpdumpConfig)
    {
        return rmTcpdumpConfigMapper.selectRmTcpdumpConfigList(rmTcpdumpConfig);
    }

    /**
     * 新增tcpdump探测策略
     * 
     * @param rmTcpdumpConfig tcpdump探测策略
     * @return 结果
     */
    @Override
    public int insertRmTcpdumpConfig(RmTcpdumpConfig rmTcpdumpConfig)
    {
        rmTcpdumpConfig.setCreateTime(DateUtils.getNowDate());
        rmTcpdumpConfigMapper.insertRmTcpdumpConfig(rmTcpdumpConfig);
        // 下发消息给agent
        PolicyTypeVo policyTypeVo = new PolicyTypeVo();
        if(rmTcpdumpConfig.getDetectFlag() == 1 && rmTcpdumpConfig.getDetectTimes() != null){
            policyTypeVo.setTcpdumpTimes(rmTcpdumpConfig.getDetectTimes());
        }else{
            policyTypeVo.setTcpdumpTimes("");
        }
        MessageProducer messageProducer = new MessageProducer();
        String configJson = JSONObject.toJSONString(policyTypeVo);
        DeviceMessage message = new DeviceMessage();
        message.setClientId(rmTcpdumpConfig.getClientId());
        message.setData(configJson);
        message.setDataType(MsgEnum.获取最新策略应答.getValue());

        messageProducer.sendAsyncProducerMessage(
                producerMode.getAgentTopic(),
                "",
                "",
                JSONObject.toJSONString(message)
        );
        return 1;
    }

    /**
     * 修改tcpdump探测策略
     * 
     * @param rmTcpdumpConfig tcpdump探测策略
     * @return 结果
     */
    @Override
    public int updateRmTcpdumpConfig(RmTcpdumpConfig rmTcpdumpConfig)
    {
        rmTcpdumpConfig.setUpdateTime(DateUtils.getNowDate());
        return rmTcpdumpConfigMapper.updateRmTcpdumpConfig(rmTcpdumpConfig);
    }

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

    /**
     * 删除tcpdump探测策略信息
     * 
     * @param id tcpdump探测策略主键
     * @return 结果
     */
    @Override
    public int deleteRmTcpdumpConfigById(Long id)
    {
        return rmTcpdumpConfigMapper.deleteRmTcpdumpConfigById(id);
    }

    @Override
    public RmTcpdumpConfig selectRmTcpdumpConfigByClientId(String clientId) {
        return rmTcpdumpConfigMapper.selectRmTcpdumpConfigByClientId(clientId);
    }
}
