package cn.iocoder.yudao.module.safe.service.server;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.safe.controller.admin.server.vo.*;
import cn.iocoder.yudao.module.safe.dal.dataobject.server.SafeServerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.safe.dal.mysql.server.SafeServerMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.safe.enums.ErrorCodeConstants.*;

/**
 * 安防后端设备 Service 实现类
 *
 * @author caoyang
 */
@Service
@Validated
public class SafeServerServiceImpl implements SafeServerService {

    @Resource
    private SafeServerMapper serverMapper;

    @Override
    public Long createServer(AppSafeServerSaveReqVO createReqVO) {
        // 插入
        SafeServerDO server = BeanUtils.toBean(createReqVO, SafeServerDO.class);
        serverMapper.insert(server);
        // 返回
        return server.getId();
    }

    @Override
    public void updateServer(AppSafeServerSaveReqVO updateReqVO) {
        // 校验存在
        validateServerExists(updateReqVO.getId());
        // 更新
        SafeServerDO updateObj = BeanUtils.toBean(updateReqVO, SafeServerDO.class);
        serverMapper.updateById(updateObj);
    }

    @Override
    public void deleteServer(Long id) {
        // 校验存在
        validateServerExists(id);
        // 删除
        serverMapper.deleteById(id);
    }

    private void validateServerExists(Long id) {
        if (serverMapper.selectById(id) == null) {
            throw exception(SERVER_NOT_EXISTS);
        }
    }

    @Override
    public SafeServerDO getServer(Long id) {
        return serverMapper.selectById(id);
    }

    @Override
    public PageResult<SafeServerDO> getServerPage(AppSafeServerPageReqVO pageReqVO) {
        return serverMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SafeServerDO> getAllServer() {
        return serverMapper.selectList();
    }

}
