<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.custom.mapper.CarModelMapper">

    <sql id="baseCarModelColumn">
        tbcmod.id,
        tbcmod.sort,
        tbcmod.model_name,
        tbcmod.model_value,
        tbcmod.rule_mileage,
        tbcmod.rule_time,
        tbcmod.remark,
        tbcmod.creator,
        tbcmod.create_time,
        tbcmod.updater,
        tbcmod.update_time,
        tbcmod.deleted,
        tbcmod.abb_name,
        tbcmod.brand_id,
        tbcmod.tenant_id
    </sql>

    <select id="findPage" resultType="cn.iocoder.yudao.module.custom.vo.CarModelRespVO">
        SELECT
        <include refid="baseCarModelColumn"></include>
        FROM `base_car_model` tbcmod
        WHERE
            tbcmod.deleted = 0
        <if test="dto.brandId != null and dto.brandId != ''">
            AND tbcmod.brand_id = #{dto.brandId}
        </if>
        <if test="dto.modelName != null and dto.modelName != ''">
            AND tbcmod.model_name LIKE CONCAT('%',#{dto.modelName},'%')
        </if>
        <if test="dto.modelValue != null and dto.modelValue != ''">
            AND tbcmod.model_value = #{dto.modelValue}
        </if>
        <if test="dto.remark != null and dto.remark != ''">
            AND tbcmod.remark LIKE CONCAT('%',#{dto.remark},'%')
        </if>
        <if test="dto.abbName != null and dto.abbName != ''">
            AND tbcmod.abb_name LIKE CONCAT('%',#{dto.abbName},'%')
        </if>
        ORDER BY
              tbcmod.sort DESC
    </select>

    <select id="getCarModelPageByBrandId" resultType="cn.iocoder.yudao.module.custom.vo.CarModelRespVO">
        SELECT
        <include refid="baseCarModelColumn"></include>
        FROM `base_car_model` tbcmod
        WHERE
            tbcmod.deleted = 0
        <if test="dto.brandId != null and dto.brandId != ''">
            AND tbcmod.brand_id = #{dto.brandId}
        </if>
        <if test="dto.modelName != null and dto.modelName != ''">
            AND tbcmod.model_name LIKE CONCAT('%',#{dto.modelName},'%')
        </if>
        <if test="dto.modelValue != null and dto.modelValue != ''">
            AND tbcmod.model_value = #{dto.modelValue}
        </if>
        <if test="dto.remark != null and dto.remark != ''">
            AND tbcmod.remark LIKE CONCAT('%',#{dto.remark},'%')
        </if>
        <if test="dto.abbName != null and dto.abbName != ''">
            AND tbcmod.abb_name LIKE CONCAT('%',#{dto.abbName},'%')
        </if>
        ORDER BY
              tbcmod.sort DESC
    </select>

    <select id="searchModelByBrandid" resultType="cn.iocoder.yudao.module.custom.vo.CascaderOptionsVO">
        SELECT
            main.model_name AS 'label',
            main.id AS 'value',
            '1' AS 'leaf'
        FROM
            base_car_model main
        WHERE
            main.deleted = '0'
        <if test="dto.modelName != null and dto.modelName != ''">
          AND CONCAT( '', main.model_name, main.abb_name ) LIKE CONCAT('%',#{dto.modelName},'%')
        </if>
          AND main.brand_id = #{dto.brandId}
        GROUP BY
            main.id
    </select>

    <select id="searchModel" resultType="cn.iocoder.yudao.module.custom.vo.CarModelRespVO">
        SELECT
        <include refid="baseCarModelColumn"></include>
        FROM `base_car_model` tbcmod
        WHERE
            tbcmod.deleted = 0
        <if test="dto.brandId != null and dto.brandId != ''">
            AND tbcmod.brand_id = #{dto.brandId}
        </if>
        <if test="dto.modelName != null and dto.modelName != ''">
            AND tbcmod.model_name LIKE CONCAT('%',#{dto.modelName},'%')
        </if>
        <if test="dto.modelValue != null and dto.modelValue != ''">
            AND tbcmod.model_value = #{dto.modelValue}
        </if>
        <if test="dto.remark != null and dto.remark != ''">
            AND tbcmod.remark LIKE CONCAT('%',#{dto.remark},'%')
        </if>
        <if test="dto.abbName != null and dto.abbName != ''">
            AND tbcmod.abb_name LIKE CONCAT('%',#{dto.abbName},'%')
        </if>
        ORDER BY
              tbcmod.sort DESC
    </select>

</mapper>