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

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelHole.*;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelHoleService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelHoleMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelHoleV2DO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;

import java.util.List;

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 TunnelHoleServiceImpl implements TunnelHoleService {

    @Resource
    private TunnelHoleMapper tunnelHoleMapper;

    @Override
    public String createTunnelHole(TunnelHoleSaveReqVO createReqVO) {
        // 插入
        TunnelHoleV2DO tunnelHole = BeanUtils.toBean(createReqVO, TunnelHoleV2DO.class);
        tunnelHoleMapper.insert(tunnelHole);
        // 返回
        return tunnelHole.getId();
    }

    @Override
    public void updateTunnelHole(TunnelHoleSaveReqVO updateReqVO) {
        // 校验存在
        validateTunnelHoleExists(updateReqVO.getId());
        // 更新
        TunnelHoleV2DO updateObj = BeanUtils.toBean(updateReqVO, TunnelHoleV2DO.class);
        tunnelHoleMapper.updateById(updateObj);
    }

    @Override
    public void deleteTunnelHole(String id) {
        // 校验存在
        validateTunnelHoleExists(id);
        // 删除
        tunnelHoleMapper.deleteById(id);
    }

    private void validateTunnelHoleExists(String id) {
        if (tunnelHoleMapper.selectById(id) == null) {
            throw exception(INTERNAL_SERVER_ERROR.getCode(), "隧道左右洞不存在");
        }
    }

    @Override
    public TunnelHoleV2DO getTunnelHole(String id) {
        return tunnelHoleMapper.selectById(id);
    }

    @Override
    public PageResult<TunnelHoleV2DO> getTunnelHolePage(TunnelHolePageReqVO pageReqVO) {
        return tunnelHoleMapper.selectPage(pageReqVO);
    }

    @Override
    public TunnelHoleV2DO getTunnelSfHoleId(String sfHoleId) {
        return tunnelHoleMapper.selectOne(TunnelHoleV2DO::getSfHoleId, sfHoleId);
    }
    @Override
    public List<TunnelHoleV2DO> getTunnelBaseId(String baseTunnelId) {
        return tunnelHoleMapper.selectList(TunnelHoleV2DO::getTunnelId, baseTunnelId);
    }
}
