<?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="com.xxfc.platform.vehicle.mapper.VehicleApplyMapper">


    <select id="selectList"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleApplyVo" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehicleApplyFindDTO">
        SELECT
        a.*,
        i.`name` as companyName,
        b.cn_name as brandName,
        c.`name` categoryName
        FROM vehicle_apply a
        LEFT JOIN branch_company i on a.subordinate_branch=i.id
        LEFT JOIN vehicle_brand b ON a.brand_id=b.id
        LEFT JOIN vehicle_category c ON a.category_id=c.id
        <where>
              a.is_del = 0
            <if test="name != null and  name != ''">
                AND ( a.`name` like concat('%',#{name},'%') or i.`name` like concat('%',#{name},'%') )
            </if>
            <if test="formType != null ">
                AND  a.`form_type`= #{formType}
            </if>
            <if test="branchId != null and branchId > 0">
                AND  a.`subordinate_branch`= #{branchId}
            </if>
            <if test="id != null and id > 0 ">
                AND  a.`id`= #{id}
            </if>
            <if test="applyStatus != null ">
                AND  a.`apply_status`= #{applyStatus}
            </if>
            <if test="goodsType != null ">
                AND  a.`goods_type`= #{goodsType}
            </if>
            <if test="dataCompanyIds != null and dataCompanyIds.size > 0">
                and i.id in
                <foreach collection="dataCompanyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size > 0">
                and i.company_id  in
                <foreach collection="dataCorporationIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
       order by a.upd_time DESC
    </select>

    <select id="selectListByApp"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleApplyVo" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehicleApplyFindDTO">
        SELECT
        v.*,
        b.cn_name as brandName,
        c.`name` categoryName
        FROM
        (SELECT v.* FROM
        (SELECT * FROM vehicle_apply
        WHERE (vehicle_id is NOT NULL and vehicle_id != '' ) and apply_status != 1
        ORDER BY id desc) v
        GROUP BY v.vehicle_id
        UNION ALL
        SELECT * FROM vehicle_apply WHERE apply_status != 1 and (vehicle_id is  NULL OR  vehicle_id = '' )) v
        LEFT JOIN vehicle_brand b ON v.brand_id=b.id
        LEFT JOIN vehicle_category c ON v.category_id=c.id
        LEFT JOIN branch_company i on v.subordinate_branch=i.id
        <where>
            v.is_del = 0
            <if test="dataCompanyIds != null and dataCompanyIds.size > 0">
                and i.id in
                <foreach collection="dataCompanyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size > 0">
                and i.company_id  in
                <foreach collection="dataCorporationIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by v.crt_time DESC
    </select>
</mapper>