package cn.iocoder.byzs.module.ai.service.virtualdevice;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.byzs.module.ai.controller.admin.virtualdevice.vo.*;
import cn.iocoder.byzs.module.ai.dal.dataobject.virtualdevice.VirtualDeviceDO;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

import cn.iocoder.byzs.module.ai.dal.mysql.virtualdevice.VirtualDeviceMapper;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.byzs.module.ai.enums.ErrorCodeConstants.*;

/**
 * AI-虚拟实验室 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class VirtualDeviceServiceImpl implements VirtualDeviceService {

    @Resource
    private VirtualDeviceMapper virtualDeviceMapper;

    @Override
    public Integer createVirtualDevice(VirtualDeviceSaveReqVO createReqVO) {
        // 插入
        VirtualDeviceDO virtualDevice = BeanUtils.toBean(createReqVO, VirtualDeviceDO.class);
        virtualDeviceMapper.insert(virtualDevice);
        // 返回
        return virtualDevice.getId();
    }

    @Override
    public void updateVirtualDevice(VirtualDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateVirtualDeviceExists(updateReqVO.getId());
        // 更新
        VirtualDeviceDO updateObj = BeanUtils.toBean(updateReqVO, VirtualDeviceDO.class);
        virtualDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteVirtualDevice(Integer id) {
        // 校验存在
        validateVirtualDeviceExists(id);
        // 删除
        virtualDeviceMapper.deleteById(id);
    }

    @Override
        public void deleteVirtualDeviceListByIds(List<Integer> ids) {
        // 校验存在
        validateVirtualDeviceExists(ids);
        // 删除
        virtualDeviceMapper.deleteByIds(ids);
        }

    private void validateVirtualDeviceExists(List<Integer> ids) {
        List<VirtualDeviceDO> list = virtualDeviceMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(VIRTUAL_DEVICE_NOT_EXISTS);
        }
    }

    private void validateVirtualDeviceExists(Integer id) {
        if (virtualDeviceMapper.selectById(id) == null) {
            throw exception(VIRTUAL_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public VirtualDeviceDO getVirtualDevice(Integer id) {
        return virtualDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<VirtualDeviceDO> getVirtualDevicePage(VirtualDevicePageReqVO pageReqVO) {
        return virtualDeviceMapper.selectPage(pageReqVO);
    }

}