package com.dcs.hnyz.service.impl;

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.domain.PlcInfo;
import com.dcs.hnyz.mapper.PlcInfoMapper;
import com.dcs.hnyz.service.IEquipmentService;
import com.dcs.hnyz.service.IPlcInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * PLC信息管理Service业务层处理
 * 
 * @author hmy
 * @date 2025-04-27
 */
@Service
public class PlcInfoServiceImpl implements IPlcInfoService 
{
    @Resource
    private PlcInfoMapper plcInfoMapper;
    @Resource
    private IEquipmentService equipmentService;

    /**
     * 查询PLC信息管理
     * 
     * @param plcId PLC信息管理主键
     * @return PLC信息管理
     */
    @Override
    public PlcInfo selectPlcInfoByPlcId(Long plcId)
    {
        return plcInfoMapper.selectById(plcId);
    }

    /**
     * 查询PLC信息管理列表
     * 
     * @param plcInfo PLC信息管理
     * @return PLC信息管理
     */
    @Override
    public List<PlcInfo> selectPlcInfoList(PlcInfo plcInfo)
    {
        return plcInfoMapper.selectPlcInfoList(plcInfo);
    }

    /**
     * 新增PLC信息管理
     * 
     * @param plcInfo PLC信息管理
     * @return 结果
     */
    @Override
    public int insertPlcInfo(PlcInfo plcInfo)
    {
        plcInfo.setCreateTime(DateUtils.getNowDate());
        return plcInfoMapper.insert(plcInfo);
    }

    /**
     * 修改PLC信息管理
     * 
     * @param plcInfo PLC信息管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePlcInfo(PlcInfo plcInfo)
    {
        int i = plcInfoMapper.updateById(plcInfo);
        if (i > 0 && plcInfo.getProtocolId() != null) {
            equipmentService.updateEquipmentByPlcId(plcInfo.getPlcId());
        }
        return i;
    }

    /**
     * 批量删除PLC信息管理
     * 
     * @param plcIds 需要删除的PLC信息管理主键
     * @return 结果
     */
    @Override
    public int deletePlcInfoByPlcIds(Long[] plcIds)
    {
        return plcInfoMapper.deleteBatchIds(Arrays.asList(plcIds));
    }

    /**
     * 删除PLC信息管理信息
     * 
     * @param plcId PLC信息管理主键
     * @return 结果
     */
    @Override
    public int deletePlcInfoByPlcId(Long plcId)
    {
        return plcInfoMapper.deleteById(plcId);
    }

    /**
     * 根据PLC ID查询PLC名称
     * @param plcId
     * @return
     */
    @Override
    public String selectPlcNameByPlcId(Long plcId) {
        return plcInfoMapper.selectById(plcId).getPlcName();
    }

    @Override
    public List<PlcInfo> getPlcList() {
        //查询启用且未删除的PLC信息
        LambdaQueryWrapper<PlcInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlcInfo::getStatus, GeneralStatus.ENABLE.getCode());
        return plcInfoMapper.selectList(wrapper);
    }

    @Override
    public int changePlcStatus(PlcInfo plcInfo) {
        //如果运行中设备正在使用该plc，不允许停用
        if (plcInfo.getStatus().equals(GeneralStatus.DISABLE.getCode())) {
            Equipment e=new Equipment();
            e.setPlcId(plcInfo.getPlcId());
            e.setStatus(GeneralStatus.ENABLE.getCode());
            List<Equipment> list = equipmentService.selectEquipmentList(e);
            if (list.size() > 0) {
                throw new CustomException("该PLC正在被使用，不允许停用");
            }
        }
        return plcInfoMapper.updateById(plcInfo);
    }
}
