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.common.core.utils.TableSubUtil;
import com.tongran.rocketmq.domain.AllDiskName;
import com.tongran.rocketmq.domain.DeviceMessage;
import com.tongran.rocketmq.domain.InitialDiskInfo;
import com.tongran.rocketmq.domain.vo.PolicyTypeVo;
import com.tongran.rocketmq.mapper.AllDiskNameMapper;
import com.tongran.rocketmq.model.ProducerMode;
import com.tongran.rocketmq.producer.MessageProducer;
import com.tongran.rocketmq.service.IAllDiskNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 磁盘名称存储Service业务层处理
 * 
 * @author tongran
 * @date 2026-01-14
 */
@Service
public class AllDiskNameServiceImpl implements IAllDiskNameService 
{
    @Autowired
    private AllDiskNameMapper allDiskNameMapper;
    @Autowired
    private ProducerMode producerMode;

    /**
     * 查询磁盘名称存储
     * 
     * @param id 磁盘名称存储主键
     * @return 磁盘名称存储
     */
    @Override
    public AllDiskName selectAllDiskNameById(Long id)
    {
        return allDiskNameMapper.selectAllDiskNameById(id);
    }

    /**
     * 查询磁盘名称存储列表
     * 
     * @param allDiskName 磁盘名称存储
     * @return 磁盘名称存储
     */
    @Override
    public List<AllDiskName> selectAllDiskNameList(AllDiskName allDiskName)
    {
        return allDiskNameMapper.selectAllDiskNameList(allDiskName);
    }

    /**
     * 新增磁盘名称存储
     * 
     * @param allDiskName 磁盘名称存储
     * @return 结果
     */
    @Override
    public int insertAllDiskName(AllDiskName allDiskName)
    {
        allDiskName.setCreateTime(DateUtils.getNowDate());
        return allDiskNameMapper.insertAllDiskName(allDiskName);
    }

    /**
     * 修改磁盘名称存储
     * 
     * @param allDiskName 磁盘名称存储
     * @return 结果
     */
    @Override
    public int updateAllDiskName(AllDiskName allDiskName)
    {
        allDiskName.setUpdateTime(DateUtils.getNowDate());
        return allDiskNameMapper.updateAllDiskName(allDiskName);
    }

    /**
     * 批量删除磁盘名称存储
     * 
     * @param ids 需要删除的磁盘名称存储主键
     * @return 结果
     */
    @Override
    public int deleteAllDiskNameByIds(Long[] ids)
    {
        return allDiskNameMapper.deleteAllDiskNameByIds(ids);
    }

    /**
     * 删除磁盘名称存储信息
     * 
     * @param id 磁盘名称存储主键
     * @return 结果
     */
    @Override
    public int deleteAllDiskNameById(Long id)
    {
        return allDiskNameMapper.deleteAllDiskNameById(id);
    }

    @Override
    public int batchInsertAllDistName(List<InitialDiskInfo> dataList) {
        if(dataList == null){
            dataList = new ArrayList<>();
        }
        List<AllDiskName> nameList = new ArrayList<>();
        for (InitialDiskInfo initialDiskInfo : dataList) {
            AllDiskName allDiskName = new AllDiskName();
            allDiskName.setClientId(initialDiskInfo.getClientId());
            allDiskName.setName(initialDiskInfo.getName());
            allDiskName.setStatus(1);
            allDiskName.setCreateTime(initialDiskInfo.getCreateTime());
            allDiskName.setUpdateTime(DateUtils.getNowDate());
            nameList.add(allDiskName);
        }
        allDiskNameMapper.batchInsertAllDistName(nameList);
        return 1;
    }

    @Override
    public int issuanceOperation(AllDiskName query) {
        Long id = query.getId();
        Integer processType = query.getProcessType();
        AllDiskName allDiskName = new AllDiskName();
        allDiskName = allDiskNameMapper.selectAllDiskNameById(id);
        String clientId = allDiskName.getClientId();
        // 构建磁盘下发信息
        PolicyTypeVo policyTypeVo = new PolicyTypeVo();
        if(processType == 1){
            policyTypeVo.setDiskIopsTest(allDiskName.getName());
        }else if(processType ==2){
            AllDiskName updateDisk = new AllDiskName();
            updateDisk.setId(id);
            updateDisk.setUmountFlag(1);
            allDiskNameMapper.updateAllDiskName(updateDisk);
            policyTypeVo.setUmountDisk(allDiskName.getName());
        }
        MessageProducer messageProducer = new MessageProducer();
        String configJson = JSONObject.toJSONString(policyTypeVo);
        DeviceMessage message = new DeviceMessage();
        message.setClientId(clientId);
        message.setData(configJson);
        message.setDataType(MsgEnum.获取最新策略应答.getValue());

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

    @Override
    public List<AllDiskName> selectDiskInfoList(AllDiskName allDiskName) {
        String tableName = TableSubUtil.getTableName(DateUtils.getNowDate(), "initial_disk_info");
        allDiskName.setTableName(tableName);
        List<AllDiskName> list = allDiskNameMapper.selectDiskInfoList(allDiskName);
        return list;
    }
}
