package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelPassage.*;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelPassageService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelPassageMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelPassageDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.pojo.PageParam;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import static com.cico.nest.lcdp.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
import static com.cico.nest.lcdp.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 隧道横通道 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class TunnelPassageServiceImpl implements TunnelPassageService {

    @Resource
    private TunnelPassageMapper tunnelPassageMapper;

    @Override
    public String createTunnelPassage(TunnelPassageSaveReqVO createReqVO) {
        // 插入
        TunnelPassageDO tunnelPassage = BeanUtils.toBean(createReqVO, TunnelPassageDO.class);
        tunnelPassageMapper.insert(tunnelPassage);
        // 返回
        return tunnelPassage.getId();
    }

    @Override
    public void updateTunnelPassage(TunnelPassageSaveReqVO updateReqVO) {
        // 校验存在
        validateTunnelPassageExists(updateReqVO.getId());
        // 更新
        TunnelPassageDO updateObj = BeanUtils.toBean(updateReqVO, TunnelPassageDO.class);
        tunnelPassageMapper.updateById(updateObj);
    }

    @Override
    public void deleteTunnelPassage(String id) {
        // 校验存在
        validateTunnelPassageExists(id);
        // 删除
        tunnelPassageMapper.deleteById(id);
    }

    private void validateTunnelPassageExists(String id) {
        if (tunnelPassageMapper.selectById(id) == null) {
            throw exception(INTERNAL_SERVER_ERROR.getCode(), "隧道横通道不存在");
        }
    }

    @Override
    public TunnelPassageDO getTunnelPassage(String id) {
        return tunnelPassageMapper.selectById(id);
    }

    @Override
    public PageResult<TunnelPassageDO> getTunnelPassagePage(TunnelPassagePageReqVO pageReqVO) {
        return tunnelPassageMapper.selectPage(pageReqVO);
    }

}
