<?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.VehicleEextensionMapper">


    <update id="delByIds">
        update  vehicle_extension set is_del=1
        <where>
            vehicle_id=#{vehicleId} and  is_del=0
            <if test="ids!=null and ids.size()!=0">
                and id not in
                <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </update>


    <select id="selectList"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleExtensionVO">
        SELECT
        e.*,
        c.`name` as parentCataName,
        c1.`name` as cataName
        FROM vehicle_extension e
        LEFT JOIN vehicle_plat_cata c  ON e.parent_cata_id=c.id
        LEFT JOIN vehicle_plat_cata c1  ON e.cata_id=c1.id
        <where>
            e.is_del =0  and e.vehicle_id =#{vehicleId}
            <if test="parentCataId != null">
                and  e.parent_cata_id=#{parentCataId}
            </if>
        </where>
        ORDER BY e.parent_cata_id
    </select>

    <select id="selectListByParent"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleExtensionVO">
        SELECT
        e.parent_cata_id as parentCataId,
        c.`name` as parentCataName
        FROM vehicle_extension e
        LEFT JOIN vehicle_plat_cata c  ON e.parent_cata_id=c.id
        <where>
            e.is_del =0  and e.vehicle_id =#{vehicleId}
        </where>
        GROUP BY e.parent_cata_id
        ORDER BY e.parent_cata_id
    </select>



    <select id="selectListByApply"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleExtensionVO">
       SELECT
       c.id as cataId,
       c.`name` as cataName ,
       c1.id as parentCataId,
       c1.`name` as parentCataName
        FROM vehicle_plat_cata c
        LEFT JOIN vehicle_plat_cata c1 ON c.parent_id=c1.id
        WHERE c.id in
        <foreach collection="cataIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        <if test="parentCataId != null">
            and  c.parent_id=#{parentCataId}
        </if>
        ORDER BY c1.id
    </select>


    <select id="selectListByApplyParent"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleExtensionVO">
        SELECT
        c1.id as parentCataId,
        c1.`name` as parentCataName
        FROM vehicle_plat_cata c
        LEFT JOIN vehicle_plat_cata c1 ON c.parent_id=c1.id
        WHERE c.id in
        <foreach collection="cataIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and c1.state = 0
        GROUP BY c.parent_id
        ORDER BY c1.id
    </select>

    <select id="selectListByPlatCata"  parameterType="com.xxfc.platform.vehicle.pojo.dto.VehicleExtensionFindDTO" resultType="com.xxfc.platform.vehicle.pojo.vo.VehicleExtensionVO">
        SELECT
            c.id,
            c.`name` as cataName,
            c.`is_input_more` as isInputMore,
            <choose>
                <when test="vehicleId != null and vehicleId != ''">
                    IF(e.cata_id is NULL,0,1) as isSelected,
                    IF(e1.cata_id is NULL,0,1) as isParentSelected
                </when>
                <when test="cataIds != null and cataIds.size() > 0">
                    IF(c2.id is NULL,0,1) as isSelected,
                    IF(c1.parent_id is NULL,0,1) as isParentSelected
                </when>
                <otherwise>
                   0 as isSelected,
                   0 as isParentSelected
                </otherwise>
            </choose>

        FROM vehicle_plat_cata c
            <if test="vehicleId != null and vehicleId != '' ">
                LEFT JOIN (SELECT * FROM vehicle_extension WHERE
                is_del = 0 and vehicle_id=#{vehicleId}    ) e ON c.id=e.cata_id
            </if>

            <if test="vehicleId != null and vehicleId != '' ">
                LEFT JOIN (SELECT * FROM vehicle_extension WHERE
                is_del = 0 and vehicle_id=#{vehicleId} ) e1 ON c.id=e1.parent_cata_id
            </if>
            <if test="cataIds != null and cataIds.size() > 0 ">
                LEFT JOIN (select * FROM vehicle_plat_cata WHERE id in
                <foreach collection="cataIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                 GROUP BY parent_id) c1 ON c.id=c1.parent_id
            </if>

            <if test="cataIds != null and cataIds.size() > 0">
                LEFT JOIN (select * FROM vehicle_plat_cata WHERE id in
                <foreach collection="cataIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                ) c2 ON c.id=c2.id
            </if>
        <where>
             c.state = 0
            <if test="parentId != null ">
              and  c.parent_id=#{parentId}
            </if>
            <if test="goodsType != null ">
                and  c.goods_type=#{goodsType}
            </if>
        </where>
        GROUP BY c.id
        ORDER BY c.id
    </select>
</mapper>