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

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelDevice.*;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelDeviceService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelDeviceMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelHoleMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelDeviceDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelHoleV2DO;
import com.cico.nest.lcdp.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.StringUtils;
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 TunnelDeviceServiceImpl implements TunnelDeviceService {

    @Resource
    private TunnelDeviceMapper tunnelDeviceMapper;
    @Resource
    private TunnelHoleMapper tunnelHoleMapper;

    @Override
    public String createTunnelDevice(TunnelDeviceSaveReqVO createReqVO) {
        // 插入
        TunnelDeviceDO tunnelDevice = BeanUtils.toBean(createReqVO, TunnelDeviceDO.class);
        String id = StringUtils.isNotEmpty(createReqVO.getId()) ? createReqVO.getId() : IdUtil.getSnowflakeNextIdStr();
        tunnelDevice.setId(id);
        //查询洞的上下行方向
        if (createReqVO.getHoleId() != null) {
            TunnelHoleV2DO existsHole = tunnelHoleMapper.selectById(createReqVO.getHoleId());
            tunnelDevice.setDirectionUpdown(existsHole.getDirectionUpdown());
        }
        tunnelDeviceMapper.insert(tunnelDevice);
        // 返回
        return tunnelDevice.getId();
    }

    @Override
    public void updateTunnelDevice(TunnelDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateTunnelDeviceExists(updateReqVO.getId());
        // 更新
        TunnelDeviceDO updateObj = BeanUtils.toBean(updateReqVO, TunnelDeviceDO.class);
        //查询洞的上下行方向
        if (updateReqVO.getHoleId() != null) {
            TunnelHoleV2DO existsHole = tunnelHoleMapper.selectById(updateReqVO.getHoleId());
            updateObj.setDirectionUpdown(existsHole.getDirectionUpdown());
        }
        tunnelDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteTunnelDevice(String id) {
        // 校验存在
        validateTunnelDeviceExists(id);
        // 删除
        tunnelDeviceMapper.deleteById(id);
    }

    private void validateTunnelDeviceExists(String id) {
        if (tunnelDeviceMapper.selectById(id) == null) {
            throw exception(INTERNAL_SERVER_ERROR.getCode(), "隧道设备不存在");
        }
    }

    @Override
    public TunnelDeviceDO getTunnelDevice(String id) {
        return tunnelDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<TunnelDeviceDO> getTunnelDevicePage(TunnelDevicePageReqVO pageReqVO) {
        return tunnelDeviceMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<TunnelDeviceRespVO> getTunnelDevicePageV2(TunnelDevicePageReqVO pageReqVO) {
        return tunnelDeviceMapper.selectPageV2(pageReqVO);
    }

    @Override
    public List<TunnelDeviceDO> listByHole(TunnelDeviceRespVO param) {
        LambdaQueryWrapperX<TunnelDeviceDO> query = new LambdaQueryWrapperX<TunnelDeviceDO>()
                .eqIfPresent(TunnelDeviceDO::getDeleted,0)
                .eqIfPresent(TunnelDeviceDO::getHoleId,param.getHoleId())
                .eqIfPresent(TunnelDeviceDO::getDeviceType,param.getDeviceType())
                .eqIfPresent(TunnelDeviceDO::getTunnelCode,param.getTunnelCode());
        return tunnelDeviceMapper.selectList(query);
    }
}
