package com.usky.ai.service.impl;

import com.usky.ai.domain.AiDevice;
import com.usky.ai.mapper.AiDeviceMapper;
import com.usky.ai.service.AiDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AiDeviceServiceImpl implements AiDeviceService {

    private final AiDeviceMapper aiDeviceMapper;

    @Autowired
    public AiDeviceServiceImpl(AiDeviceMapper aiDeviceMapper) {
        this.aiDeviceMapper = aiDeviceMapper;
    }

    @Override
    public void addDevice(AiDevice device) {
        if (aiDeviceMapper.checkExists(device.getBoardId(), device.getBoardIp())) {
            throw new IllegalArgumentException("设备已存在， 设备Id 或 设备Ip 已存在");
        }
        aiDeviceMapper.insertDevice(device);
    }

    @Override
    public void editDevice(AiDevice device) {
        aiDeviceMapper.updateDevice(device);
    }

    @Override
    public void saveOrUpdateDevice(AiDevice device) {
        aiDeviceMapper.saveOrUpdateDevice(device);
    }

    @Override
    public void updateDeviceStatus() {
        aiDeviceMapper.updateDeviceStatus();
    }

    @Override
    public List<AiDevice> getDevices(String boardId, int pageNum, int pageSize) {
        updateDeviceStatus(); // 在查询之前更新设备的 status 状态
        int offset = (pageNum - 1) * pageSize;
        return aiDeviceMapper.getDevices(boardId, offset, pageSize);
    }

    @Override
    public long getTotalDevices(String boardId) {
        return aiDeviceMapper.getTotalDevices(boardId);
    }

    @Override
    public void deleteDevice(String boardId) {
        aiDeviceMapper.deleteDevice(boardId);
    }
}
