package com.ningdatech.carapi.car.vehicle.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.car.vehicle.model.NdVehicleBaseInfoJoin;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
public interface VehicleBaseInfoJoinMapper extends BaseMapper<NdVehicleBaseInfoJoin> {

    String QUERY_SQL = "SELECT b.*, c.name as company_name,o.name as operator_name, " +
            "null as equipment_id FROM nd_vehicle_base_info AS b " +
            "LEFT JOIN nd_company_info AS c ON b.company_id = c.id " +
            "LEFT JOIN nd_operator_info AS o ON b.operator_id = o.id ";
    String WRAPPER_SQL = "SELECT * from ( " + QUERY_SQL + " ) AS q ${ew.customSqlSegment}";


    String QUERY_SQL_WITH_NO_COMPANY = "SELECT b.*, c.name as company_name,o.name as operator_name, " +
            "null as equipment_id FROM nd_vehicle_base_info AS b " +
            "LEFT JOIN nd_company_info AS c ON b.company_id = c.id " +
            "LEFT JOIN nd_operator_info AS o ON b.operator_id = o.id where b.company_id is not null ";
    String WRAPPER_NO_COMPANY_SQL = "SELECT * from ( " + QUERY_SQL_WITH_NO_COMPANY  + ") AS q ${ew.customSqlSegment}";

    String QUERY_SQL_WITH_EQUIPMENT = "SELECT" +
            " b.*," +
            " c.NAME AS company_name," +
            " o.NAME AS operator_name," +
            " null as equipment_id" +
            " FROM " +
            " nd_vehicle_base_info AS b" +
            " LEFT JOIN nd_company_info AS c ON b.company_id = c.id" +
            " LEFT JOIN nd_operator_info AS o ON b.operator_id = o.id"
             + " where b.id in (select vehicle_id from nd_vehicle_equipment_info where equipment_function = 'video') ";
    String WRAPPER_SQL_WITH_EQUIPMENT = "SELECT * from ( " + QUERY_SQL_WITH_EQUIPMENT + " ) AS q ${ew.customSqlSegment}";

    String QUERY_SQL_WITH_NOT_EQUIPMENT = "SELECT" +
            " b.*," +
            " c.NAME AS company_name," +
            " o.NAME AS operator_name," +
            " null as equipment_id" +
            " FROM " +
            " nd_vehicle_base_info AS b" +
            " LEFT JOIN nd_company_info AS c ON b.company_id = c.id" +
            " LEFT JOIN nd_operator_info AS o ON b.operator_id = o.id" +
            " where b.car_plate not in (select car_plate from nd_vehicle_equipment_info where equipment_function = 'video') ";
    String WRAPPER_SQL_WITH_NOT_EQUIPMENT = "SELECT * from ( " + QUERY_SQL_WITH_NOT_EQUIPMENT + " ) AS q ${ew.customSqlSegment}";

    @Select(WRAPPER_SQL)
    Page<NdVehicleBaseInfoJoin> pageJoin(Page page, @Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_SQL)
    List<NdVehicleBaseInfoJoin> listJoin(@Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_SQL)
    NdVehicleBaseInfoJoin oneJoin(@Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_SQL_WITH_EQUIPMENT)
    Page<NdVehicleBaseInfoJoin> pageJoinWithEquipment(Page<NdVehicleBaseInfoJoin> page,@Param("ew")  LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_SQL_WITH_EQUIPMENT)
    NdVehicleBaseInfoJoin oneJoinWithEquipment(@Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_SQL_WITH_NOT_EQUIPMENT)
    Page<NdVehicleBaseInfoJoin> pageJoinWithNotEquipment(Page<NdVehicleBaseInfoJoin> page,@Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);

    @Select(WRAPPER_NO_COMPANY_SQL)
    List<NdVehicleBaseInfoJoin> listCarJoin(@Param("ew") LambdaQueryWrapper<NdVehicleBaseInfoJoin> wrapper);
}
