package com.dcs.hnyz.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcs.common.exception.CustomException;
import com.dcs.hnyz.domain.vo.EquipmentFormVo;
import org.springframework.stereotype.Service;
import com.dcs.hnyz.mapper.EquipmentMapper;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.service.IEquipmentService;

import javax.annotation.Resource;

/**
 * 设备管理Service业务层处理
 * 
 * @author hmy
 * @date 2025-04-22
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService 
{
    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private FlowServiceImpl flowService;

    @Resource
    private CommunicationProtocolServiceImpl communicationProtocolService;

    @Resource
    private PlcInfoServiceImpl plcInfoService;

    /**
     * 查询设备管理
     * 
     * @param equipmentId 设备管理主键
     * @return 设备管理
     */
    @Override
    public Equipment selectEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.selectById(equipmentId);
    }

    /**
     * 查询设备管理列表
     * 
     * @param equipment 设备管理
     * @return 设备管理
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    @Override
    public List<EquipmentFormVo> selectEquipmentForm(Equipment equipment) {
        List<EquipmentFormVo> list = equipmentMapper.selectEquipmentForm(equipment);

        for (EquipmentFormVo equipmentFormVo : list) {
            //获取plc名称
            if (equipmentFormVo.getPlcId() != null) {
                equipmentFormVo.setPlcName(plcInfoService.selectPlcNameByPlcId(equipmentFormVo.getPlcId()));
            }
            //获取通信协议名称
            if (equipmentFormVo.getProtocolId() != null) {
                equipmentFormVo.setProtocolName(communicationProtocolService.selectProtocolNameByProtocolId(equipmentFormVo.getProtocolId()));
            }
            //处理flowName数据
            String flowIdStr = equipmentFormVo.getFlowIds(); // "1,2"
            if (flowIdStr != null && !flowIdStr.isEmpty()) {
                String[] flowIds = flowIdStr.split(","); // ["1", "2"]
                List<String> flowNames = new ArrayList<>();
                for (String flowId : flowIds) {
                    //通过flowId获取flowName
                    flowNames.add(flowService.selectFlowNameByFlowId(Long.valueOf(flowId)));
                }
                // 设备名拼接
                equipmentFormVo.setFlowName(String.join(",", flowNames));
            }
        }
        return list;
    }

    /**
     * 新增设备管理
     * 
     * @param equipment 设备管理
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        LambdaQueryWrapper<Equipment> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Equipment::getCode,equipment.getCode());
        Equipment d= equipmentMapper.selectOne(wrapper);
        if(d!=null){
            throw new CustomException("设备标识已存在");
        }
        return equipmentMapper.insert(equipment);
    }

    /**
     * 修改设备管理
     * 
     * @param equipment 设备管理
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        LambdaQueryWrapper<Equipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Equipment::getCode,equipment.getCode());
        wrapper.ne(Equipment::getEquipmentId,equipment.getEquipmentId());
        Equipment d= equipmentMapper.selectOne(wrapper);
        if(d!=null){
            throw new CustomException("设备标识已存在");
        }
        return equipmentMapper.updateById(equipment);
    }

    /**
     * 批量删除设备管理
     * 
     * @param equipmentIds 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentIds(Long[] equipmentIds)
    {
        return equipmentMapper.deleteBatchIds(Arrays.asList(equipmentIds));
    }

    /**
     * 删除设备管理信息
     * 
     * @param equipmentId 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.deleteById(equipmentId);
    }
}
