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

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelFacilityPageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelFacilitySaveReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelFacilityService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelFacilityMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelFacilityDO;
import com.cico.nest.lcdp.framework.common.context.LcdpGlobalContext;
import com.cico.nest.lcdp.framework.common.exception.ServiceException;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;

import static com.cico.nest.lcdp.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;

/**
 * 隧道设施 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class TunnelFacilityServiceImpl implements TunnelFacilityService {

    @Resource
    private TunnelFacilityMapper tunnelFacilityMapper;

    @Override
    public String createTunnelFacility(TunnelFacilitySaveReqVO createReqVO) {
        // 插入
        TunnelFacilityDO tunnelFacility = TunnelFacilitySaveReqVO.convert2DO(createReqVO);
        tunnelFacility.setCreateBy(LcdpGlobalContext.getLoginUserId());
        tunnelFacility.setCreateTime(new Date());
        tunnelFacilityMapper.insert(tunnelFacility);
        // 返回
        return tunnelFacility.getId();
    }

    @Override
    public void updateTunnelFacility(TunnelFacilitySaveReqVO updateReqVO) {
        // 校验存在
        validateTunnelFacilityExists(updateReqVO.getId());
        // 更新
        TunnelFacilityDO updateObj = BeanUtils.toBean(updateReqVO, TunnelFacilityDO.class);
        updateObj.setUpdateBy(LcdpGlobalContext.getLoginUserId());
        updateObj.setUpdateTime(new Date());
        tunnelFacilityMapper.updateById(updateObj);
    }

    @Override
    public void deleteTunnelFacility(String id) {
        // 校验存在
        validateTunnelFacilityExists(id);
        // 删除
        tunnelFacilityMapper.deleteById(id);
    }

    private void validateTunnelFacilityExists(String id) {
        if (tunnelFacilityMapper.selectById(id) == null) {
            throw new ServiceException(INTERNAL_SERVER_ERROR.getCode(), "隧道设施不存在");
        }
    }

    @Override
    public TunnelFacilityDO getTunnelFacility(String id) {
        return tunnelFacilityMapper.selectById(id);
    }

    @Override
    public PageResult<TunnelFacilityDO> getTunnelFacilityPage(TunnelFacilityPageReqVO pageReqVO) {
        return tunnelFacilityMapper.selectPage(pageReqVO);
    }

    @Override
    public void enable(String id) {
        tunnelFacilityMapper.enable(id);
    }

    @Override
    public boolean existEnable() {
        return tunnelFacilityMapper.existEnable();
    }

    @Override
    public void disEnable(String id) {
        tunnelFacilityMapper.disEnable(id);
    }

    @Override
    public List<TunnelFacilityDO> getEnableList() {
        return tunnelFacilityMapper.getEnableList();
    }

}
