package com.ruoyi.app.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.MonitorMapper;
import com.ruoyi.app.model.Monitor;
import com.ruoyi.app.service.IMonitorService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 监控设备Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class MonitorServiceImpl implements IMonitorService 
{
    @Autowired
    private MonitorMapper monitorMapper;

    /**
     * 查询监控设备
     * 
     * @param id 监控设备主键
     * @return 监控设备
     */
    @Override
    public Monitor selectMonitorById(Long id)
    {
        return monitorMapper.selectMonitorById(id);
    }

    /**
     * 查询监控设备列表
     * 
     * @param monitor 监控设备
     * @return 监控设备
     */
    @Override
    public List<Monitor> selectMonitorList(Monitor monitor)
    {
        return monitorMapper.selectMonitorList(monitor);
    }

    /**
     * 校验待设备名称是否唯一
     *
     * @param monitor 设备
     * @return 结果
     */
    @Override
    public boolean checkMonitorNameUnique(Monitor monitor)
    {
        Long regionId = StringUtils.isNull(monitor.getId()) ? -1L : monitor.getId();
        Monitor info = monitorMapper.checkMonitorNameUnique(monitor.getMonitorName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != regionId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 新增监控设备
     * 
     * @param monitor 监控设备
     * @return 结果
     */
    @Override
    public int insertMonitor(Monitor monitor)
    {
        monitor.setCreateTime(DateUtils.getNowDate());
        return monitorMapper.insertMonitor(monitor);
    }

    /**
     * 同步监控设备
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int syncMonitor(List<Monitor> monitorList)
    {
        //先清空所有设备
        monitorMapper.deleteAllMonitor();
        return monitorMapper.insertMonitorBatch(monitorList);
    }

    /**
     * 修改监控设备
     * 
     * @param monitor 监控设备
     * @return 结果
     */
    @Override
    public int updateMonitor(Monitor monitor)
    {
        monitor.setUpdateTime(DateUtils.getNowDate());
        return monitorMapper.updateMonitor(monitor);
    }

    /**
     * 批量删除监控设备
     * 
     * @param ids 需要删除的监控设备主键
     * @return 结果
     */
    @Override
    public int deleteMonitorByIds(Long[] ids)
    {
        return monitorMapper.deleteMonitorByIds(ids);
    }

    /**
     * 删除监控设备信息
     * 
     * @param id 监控设备主键
     * @return 结果
     */
    @Override
    public int deleteMonitorById(Long id)
    {
        return monitorMapper.deleteMonitorById(id);
    }
}
