<?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.qaup.system.mapper.SysVehicleTypeMapper">
    
    <resultMap type="SysVehicleType" id="SysVehicleTypeResult">
        <result property="id"              column="id"              />
        <result property="typeCode"        column="type_code"       />
        <result property="typeName"        column="type_name"       />
        <result property="displayNameCn"   column="display_name_cn" />
        <result property="displayNameEn"   column="display_name_en" />
        <result property="pathLevel"       column="path_level"      />
        <result property="parentCode"      column="parent_code"     />
        <result property="fullPath"        column="full_path"       />
        <result property="isLeaf"          column="is_leaf"         />
        <result property="sortOrder"       column="sort_order"      />
        <result property="enabled"         column="enabled"         />
        <result property="description"     column="description"     />
        <result property="attributes"      column="attributes"      />
        <result property="createBy"        column="create_by"       />
        <result property="createTime"      column="create_time"     />
        <result property="updateBy"        column="update_by"       />
        <result property="updateTime"      column="update_time"     />
        <result property="version"         column="version"         />
    </resultMap>

    <sql id="selectSysVehicleTypeVo">
        select id, type_code, type_name, display_name_cn, display_name_en, path_level, parent_code, 
               full_path, is_leaf, sort_order, enabled, description, attributes, 
               create_by, create_time, update_by, update_time, version 
        from sys_vehicle_type
    </sql>

    <select id="selectSysVehicleTypeList" parameterType="SysVehicleType" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        <where>  
            <if test="typeCode != null  and typeCode != ''"> and type_code = #{typeCode}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="displayNameCn != null  and displayNameCn != ''"> and display_name_cn like concat('%', #{displayNameCn}, '%')</if>
            <if test="pathLevel != null "> and path_level = #{pathLevel}</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="isLeaf != null "> and is_leaf = #{isLeaf}</if>
            <if test="enabled != null "> and enabled = #{enabled}</if>
        </where>
        order by path_level, sort_order, type_code
    </select>
    
    <select id="selectSysVehicleTypeById" parameterType="Long" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where id = #{id}
    </select>

    <select id="selectSysVehicleTypeByCode" parameterType="String" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where type_code = #{typeCode} and enabled = true
    </select>

    <select id="selectChildTypesByParentCode" parameterType="String" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where parent_code = #{parentCode} and enabled = true
        order by sort_order, type_code
    </select>

    <select id="selectSysVehicleTypeByLevel" parameterType="Integer" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where path_level = #{pathLevel} and enabled = true
        order by sort_order, type_code
    </select>

    <select id="selectTopLevelTypes" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where path_level = 1 and enabled = true
        order by sort_order, type_code
    </select>

    <select id="selectLeafTypes" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where is_leaf = true and enabled = true
        order by path_level, sort_order, type_code
    </select>

    <select id="selectTypesByTopLevel" parameterType="String" resultMap="SysVehicleTypeResult">
        <include refid="selectSysVehicleTypeVo"/>
        where (type_code = #{topLevelCode} or type_code like concat(#{topLevelCode}, '.%')) 
          and enabled = true
        order by path_level, sort_order, type_code
    </select>

    <select id="checkTypeCodeExists" parameterType="String" resultType="int">
        select count(1) from sys_vehicle_type where type_code = #{typeCode}
    </select>

    <select id="countChildTypes" parameterType="String" resultType="int">
        select count(1) from sys_vehicle_type where parent_code = #{parentCode} and enabled = true
    </select>
        
    <insert id="insertSysVehicleType" parameterType="SysVehicleType" useGeneratedKeys="true" keyProperty="id">
        insert into sys_vehicle_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">type_code,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="displayNameCn != null and displayNameCn != ''">display_name_cn,</if>
            <if test="displayNameEn != null">display_name_en,</if>
            <if test="pathLevel != null">path_level,</if>
            <if test="parentCode != null">parent_code,</if>
            <if test="fullPath != null">full_path,</if>
            <if test="isLeaf != null">is_leaf,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="enabled != null">enabled,</if>
            <if test="description != null">description,</if>
            <if test="attributes != null">attributes,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="version != null">version,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">#{typeCode},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="displayNameCn != null and displayNameCn != ''">#{displayNameCn},</if>
            <if test="displayNameEn != null">#{displayNameEn},</if>
            <if test="pathLevel != null">#{pathLevel},</if>
            <if test="parentCode != null">#{parentCode},</if>
            <if test="fullPath != null">#{fullPath},</if>
            <if test="isLeaf != null">#{isLeaf},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="description != null">#{description},</if>
            <if test="attributes != null">#{attributes},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="version != null">#{version},</if>
        </trim>
    </insert>

    <update id="updateSysVehicleType" parameterType="SysVehicleType">
        update sys_vehicle_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="displayNameCn != null and displayNameCn != ''">display_name_cn = #{displayNameCn},</if>
            <if test="displayNameEn != null">display_name_en = #{displayNameEn},</if>
            <if test="pathLevel != null">path_level = #{pathLevel},</if>
            <if test="parentCode != null">parent_code = #{parentCode},</if>
            <if test="fullPath != null">full_path = #{fullPath},</if>
            <if test="isLeaf != null">is_leaf = #{isLeaf},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="description != null">description = #{description},</if>
            <if test="attributes != null">attributes = #{attributes},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            version = version + 1,
        </trim>
        where id = #{id} and version = #{version}
    </update>

    <delete id="deleteSysVehicleTypeById" parameterType="Long">
        delete from sys_vehicle_type where id = #{id}
    </delete>

    <delete id="deleteSysVehicleTypeByCode" parameterType="String">
        delete from sys_vehicle_type where type_code = #{typeCode}
    </delete>

    <delete id="deleteSysVehicleTypeByIds" parameterType="String">
        delete from sys_vehicle_type where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>