<?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.ruoyi.system.mapper.AssetTypeMapper">
    
    <resultMap type="AssetType" id="AssetTypeResult">
        <result property="assetId"    column="asset_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="assetName"    column="asset_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAssetTypeVo">
        select asset_id, parent_id, ancestors, asset_name, order_num, status, del_flag, create_by, create_time, update_by, update_time from asset_type
    </sql>

    <select id="selectAssetTypeList" parameterType="AssetType" resultMap="AssetTypeResult">
        <include refid="selectAssetTypeVo"/>
        <where>
            and del_flag = 'N'
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="assetName != null  and assetName != ''"> and asset_name like concat('%', #{assetName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectAssetTypeByAssetId" parameterType="Long" resultMap="AssetTypeResult">
        <include refid="selectAssetTypeVo"/>
        where asset_id = #{assetId}
    </select>
    <select id="selectAssetTypeListTree" parameterType="AssetType" resultMap="AssetTypeResult">
        <include refid="selectAssetTypeVo"/>
        <where>
            del_flag = 'N'
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="assetName != null  and assetName != ''"> and asset_name like concat('%', #{assetName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by parent_id, order_num
    </select>

    <insert id="insertAssetType" parameterType="AssetType" useGeneratedKeys="true" keyProperty="assetId">
        insert into asset_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="assetName != null">asset_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="assetName != null">#{assetName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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>
         </trim>
    </insert>

    <update id="updateAssetType" parameterType="AssetType">
        update asset_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="assetName != null">asset_name = #{assetName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where asset_id = #{assetId}
    </update>
    <update id="updateAssetTypeChildren" parameterType="java.util.List">
        update asset_type set ancestors =
        <foreach collection="assetTypes" item="item" index="index"
                 separator=" " open="case asset_id" close="end">
            when #{item.assetId} then #{item.ancestors}
        </foreach>
        where asset_id in
        <foreach collection="assetTypes" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.item.assetId}
        </foreach>
    </update>
    <select id="checkAssetNameUnique" resultMap="AssetTypeResult">
        <include refid="selectAssetTypeVo"/>
        where asset_name=#{assetName} and parent_id = #{parentId} and del_flag = '0' limit 1
    </select>
    <select id="selectNormalChildrenAssetById" parameterType="Long" resultType="int">
        select count(*) from asset_type where status = 0 and del_flag = '0' and find_in_set(#{assetId}, ancestors)
    </select>
    <select id="hasChildByAssetId" parameterType="Long" resultType="int">
        select count(1) from asset_type
        where del_flag = '0' and parent_id = #{assetId} limit 1
    </select>
    <select id="selectChildrenAssetTypeById" parameterType="Long" resultMap="AssetTypeResult">
        select * from asset_type where find_in_set(#{assetId}, ancestors)
    </select>

    <delete id="deleteAssetTypeByAssetId" parameterType="Long">
        delete from asset_type where asset_id = #{assetId}
    </delete>

    <delete id="deleteAssetTypeByAssetIds" parameterType="String">
        delete from asset_type where asset_id in 
        <foreach item="assetId" collection="array" open="(" separator="," close=")">
            #{assetId}
        </foreach>
    </delete>
</mapper>