package com.dcs.hnyz.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcs.common.enums.GeneralStatus;
import com.dcs.common.exception.CustomException;
import com.dcs.common.utils.DateUtils;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.service.IEquipmentService;
import org.springframework.stereotype.Service;
import com.dcs.hnyz.mapper.CommunicationProtocolMapper;
import com.dcs.hnyz.domain.CommunicationProtocol;
import com.dcs.hnyz.service.ICommunicationProtocolService;

import javax.annotation.Resource;

/**
 * 通讯协议管理Service业务层处理
 * 
 * @author hmy
 * @date 2025-04-27
 */
@Service
public class CommunicationProtocolServiceImpl implements ICommunicationProtocolService 
{
    @Resource
    private CommunicationProtocolMapper communicationProtocolMapper;
    @Resource
    private IEquipmentService equipmentService;

    /**
     * 查询通讯协议管理
     * 
     * @param protocolId 通讯协议管理主键
     * @return 通讯协议管理
     */
    @Override
    public CommunicationProtocol selectCommunicationProtocolByProtocolId(Long protocolId)
    {
        return communicationProtocolMapper.selectById(protocolId);
    }

    /**
     * 查询通讯协议管理列表
     * 
     * @param communicationProtocol 通讯协议管理
     * @return 通讯协议管理
     */
    @Override
    public List<CommunicationProtocol> selectCommunicationProtocolList(CommunicationProtocol communicationProtocol)
    {
        return communicationProtocolMapper.selectCommunicationProtocolList(communicationProtocol);
    }

    /**
     * 新增通讯协议管理
     * 
     * @param communicationProtocol 通讯协议管理
     * @return 结果
     */
    @Override
    public int insertCommunicationProtocol(CommunicationProtocol communicationProtocol)
    {
        communicationProtocol.setCreateTime(DateUtils.getNowDate());
        return communicationProtocolMapper.insert(communicationProtocol);
    }

    /**
     * 修改通讯协议管理
     * 
     * @param communicationProtocol 通讯协议管理
     * @return 结果
     */
    @Override
    public int updateCommunicationProtocol(CommunicationProtocol communicationProtocol)
    {
        communicationProtocol.setUpdateTime(DateUtils.getNowDate());
        return communicationProtocolMapper.updateById(communicationProtocol);
    }

    /**
     * 批量删除通讯协议管理
     * 
     * @param protocolIds 需要删除的通讯协议管理主键
     * @return 结果
     */
    @Override
    public int deleteCommunicationProtocolByProtocolIds(Long[] protocolIds)
    {
        return communicationProtocolMapper.deleteBatchIds(Arrays.asList(protocolIds));
    }

    /**
     * 删除通讯协议管理信息
     * 
     * @param protocolId 通讯协议管理主键
     * @return 结果
     */
    @Override
    public int deleteCommunicationProtocolByProtocolId(Long protocolId)
    {
        return communicationProtocolMapper.deleteById(protocolId);
    }

    /**
     * 通过协议id查找协议名
     * @param protocolId
     * @return
     */
    @Override
    public String selectProtocolNameByProtocolId(Long protocolId) {
        CommunicationProtocol protocol = communicationProtocolMapper.selectById(protocolId);
        if (protocol == null) {
            return null;
        }
        return protocol.getProtocolName();
    }

    /**
     * 查询通讯协议管理列表
     * @return
     */
    @Override
    public List<CommunicationProtocol> getCommProtocolList() {
        //查找使用中的通讯协议
        LambdaQueryWrapper<CommunicationProtocol> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CommunicationProtocol::getStatus, GeneralStatus.ENABLE.getCode());
        return communicationProtocolMapper.selectList(wrapper);
    }

    @Override
    public int changeProtocolStatus(CommunicationProtocol cp) {
        //如果运行中设备正在使用该通讯协议，不允许停用
        if(cp.getStatus().equals(GeneralStatus.DISABLE.getCode())){
            Equipment e=new Equipment();
            e.setProtocolId(cp.getProtocolId());
            e.setStatus(GeneralStatus.ENABLE.getCode());
            List<Equipment> list = equipmentService.selectEquipmentList(e);
            if(list.size()>0){
                throw new CustomException("运行中设备正在使用该通讯协议，不允许停用");
            }
        }
        return communicationProtocolMapper.updateById(cp);
    }
}
