package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.UDevice;
import com.ruoyi.system.mapper.UDeviceMapper;
import com.ruoyi.system.service.IUDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UDeviceServiceImpl implements IUDeviceService {

    @Autowired
    private UDeviceMapper deviceMapper;

    @Override
    public UDevice selectById(Long deviceId, Long ownerUserId) {
        UDevice d = deviceMapper.selectById(deviceId);
        if (d == null) return null;
        if (!ownerUserId.equals(d.getOwnerUserId())) {
            throw new ServiceException("无权限访问该设备");
        }
        return d;
    }

    @Override
    public List<UDevice> selectList(Long ownerUserId, UDevice query) {
        return deviceMapper.selectListByOwner(
                ownerUserId,
                query == null ? null : query.getDeviceName(),
                query == null ? null : query.getDeviceType(),
                query == null ? null : query.getStatus()
        );
    }

    @Override
    public int addDevice(Long ownerUserId, String operator, UDevice device) {
        if (device == null || StringUtils.isEmpty(device.getDeviceSn()) || StringUtils.isEmpty(device.getDeviceName())) {
            throw new ServiceException("设备名称和SN不能为空");
        }
        UDevice old = deviceMapper.selectBySn(device.getDeviceSn());
        if (old != null) {
            throw new ServiceException("设备SN已存在");
        }
        device.setOwnerUserId(ownerUserId);
        device.setStatus(StringUtils.isEmpty(device.getStatus()) ? "0" : device.getStatus());
        device.setBindTime(device.getBindTime() == null ? new Date() : device.getBindTime());
        device.setCreateBy(operator);
        device.setUpdateBy(operator);
        return deviceMapper.insert(device);
    }

    @Override
    public int editDevice(Long ownerUserId, String operator, UDevice device) {
        if (device == null || device.getDeviceId() == null) {
            throw new ServiceException("设备ID不能为空");
        }
        UDevice db = selectById(device.getDeviceId(), ownerUserId);
        if (db == null) {
            throw new ServiceException("设备不存在");
        }

        if (StringUtils.isNotEmpty(device.getDeviceSn()) && !device.getDeviceSn().equals(db.getDeviceSn())) {
            UDevice old = deviceMapper.selectBySn(device.getDeviceSn());
            if (old != null) {
                throw new ServiceException("设备SN已存在");
            }
        }

        device.setOwnerUserId(ownerUserId);
        device.setUpdateBy(operator);
        return deviceMapper.update(device);
    }

    @Override
    public int removeDevices(Long ownerUserId, String operator, Long[] deviceIds) {
        if (deviceIds == null || deviceIds.length == 0) {
            return 0;
        }
        return deviceMapper.softDeleteByIds(deviceIds, operator, ownerUserId);
    }
}
