package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelBase.TunnelBasePageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelBase.TunnelBaseRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.domain.tunnel.TunnelFacilityEnableStatus;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelBaseDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelFacilityDO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.user.vo.user.UserRespVO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.dept.DeptDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.user.AdminUserDO;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cico.nest.lcdp.framework.mybatis.core.mapper.BaseMapperX;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * 隧道设施基础信息 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface TunnelBaseMapper extends BaseMapperX<TunnelBaseDO> {

    default PageResult<TunnelBaseDO> selectPage(TunnelBasePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TunnelBaseDO>()
                .eqIfPresent(TunnelBaseDO::getTunnelCode, reqVO.getTunnelCode())
                .likeIfPresent(TunnelBaseDO::getTunnelName, reqVO.getTunnelName())
                .eqIfPresent(TunnelBaseDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(TunnelBaseDO::getRoad, reqVO.getRoad())
                .eqIfPresent(TunnelBaseDO::getLongitude, reqVO.getLongitude())
                .eqIfPresent(TunnelBaseDO::getLatitude, reqVO.getLatitude())
                .eqIfPresent(TunnelBaseDO::getTunnelType, reqVO.getTunnelType())
                .eqIfPresent(TunnelBaseDO::getTunnelDesignType, reqVO.getTunnelDesignType())
                .eqIfPresent(TunnelBaseDO::getLength, reqVO.getLength())
                .eqIfPresent(TunnelBaseDO::getTrafficFlow, reqVO.getTrafficFlow())
                .eqIfPresent(TunnelBaseDO::getCrossNum, reqVO.getCrossNum())
                .eqIfPresent(TunnelBaseDO::getMileStart, reqVO.getMileStart())
                .eqIfPresent(TunnelBaseDO::getMileEnd, reqVO.getMileEnd())
                .eqIfPresent(TunnelBaseDO::getIsCommon, reqVO.getIsCommon())
                .eqIfPresent(TunnelBaseDO::getModel3dFlag, reqVO.getModel3dFlag())
                .eqIfPresent(TunnelBaseDO::getModel3dPath, reqVO.getModel3dPath())
                .eqIfPresent(TunnelBaseDO::getModules, reqVO.getModules())
                .eqIfPresent(TunnelBaseDO::getEnable, reqVO.getEnable())
                .eqIfPresent(TunnelBaseDO::getSort, reqVO.getSort())
                .betweenIfPresent(TunnelBaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TunnelBaseDO::getId));
    }

    /**
     * 启用
     * @param id
     */
    default void enable(String id){
        update(Wrappers.<TunnelBaseDO>lambdaUpdate()
                .set(TunnelBaseDO::getEnable, TunnelFacilityEnableStatus.enable.type)
                .eq(TunnelBaseDO::getId,id));
    }
    /**
     * 禁用
     * @param id
     */
    default void disEnable(String id){
        update(Wrappers.<TunnelBaseDO>lambdaUpdate()
                .set(TunnelBaseDO::getEnable, TunnelFacilityEnableStatus.disEnable.type)
                .eq(TunnelBaseDO::getId,id));
    }

    /**
     * 设施启用列表
     * @return 设施列表
     */
    default List<TunnelBaseDO> getEnableList(String deptId){
        return selectList(Wrappers.<TunnelBaseDO>lambdaQuery()
                .eq(TunnelBaseDO::getDeptId,deptId)
                .eq(TunnelBaseDO::getEnable, TunnelFacilityEnableStatus.enable.type));
    }

    default PageResult<TunnelBaseRespVO> selectPageV2(TunnelBasePageReqVO pageReqVO){
        MPJLambdaWrapper<TunnelBaseDO> wrapper = new MPJLambdaWrapper<TunnelBaseDO>()
                .selectAll(TunnelBaseDO.class)
                .selectAs(DeptDO::getName, TunnelBaseRespVO::getDeptName)
                .leftJoin(DeptDO.class,DeptDO::getId,TunnelBaseDO::getDeptId)
                .likeIfExists(TunnelBaseDO::getTunnelCode, pageReqVO.getTunnelCode())
                .likeIfExists(TunnelBaseDO::getTunnelName, pageReqVO.getTunnelName())
                .eq(TunnelBaseDO::getDeleted, 0)
                .orderByDesc(TunnelBaseDO::getCreateTime);
        PageResult<TunnelBaseRespVO> result = selectJoinPage(pageReqVO,TunnelBaseRespVO.class,wrapper);
        return result ;
    };
}
