package com.aiotagro.cattletrade.business.mapper;

import com.aiotagro.cattletrade.business.entity.Vehicle;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 车辆管理 Mapper 接口
 *
 * @author System
 * @date 2025-01-20
 */
@Mapper
public interface VehicleMapper extends BaseMapper<Vehicle> {

    /**
     * 根据车牌号查询车辆（唯一性校验）
     * @param licensePlate 车牌号
     * @return 车辆信息
     */
    @Select("SELECT * FROM vehicle WHERE license_plate = #{licensePlate} AND is_delete = 0")
    Vehicle selectByLicensePlate(@Param("licensePlate") String licensePlate);

    /**
     * 根据车牌号模糊查询车辆列表
     * @param licensePlate 车牌号（可为空）
     * @return 车辆列表
     */
    @Select("<script>" +
            "SELECT * FROM vehicle WHERE is_delete = 0 " +
            "<if test='licensePlate != null and licensePlate != \"\"'>" +
            "AND license_plate LIKE CONCAT('%', #{licensePlate}, '%') " +
            "</if>" +
            "ORDER BY create_time DESC " +
            "</script>")
    List<Vehicle> selectVehicleList(@Param("licensePlate") String licensePlate);

    /**
     * 批量根据车牌号查询车辆信息
     * 用于优化性能，避免 N+1 查询问题
     * @param licensePlates 车牌号列表
     * @return 车辆列表
     */
    @Select("<script>" +
            "SELECT * FROM vehicle WHERE is_delete = 0 " +
            "AND license_plate IN " +
            "<foreach collection='licensePlates' item='plate' open='(' separator=',' close=')'>" +
            "   #{plate}" +
            "</foreach>" +
            "</script>")
    List<Vehicle> selectByLicensePlates(@Param("licensePlates") List<String> licensePlates);
}

