<?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.InvestigateDispositionMapper">
    
    <resultMap type="InvestigateDisposition" id="InvestigateDispositionResult">
        <result property="investigateDispositionId"    column="investigate_disposition_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="orderNum"    column="order_num"    />
        <result property="title"    column="title"    />
        <result property="url"    column="url"    />
        <result property="status"    column="status"    />
        <result property="parentName" column="parent_name" />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectInvestigateDispositionVo">
        select investigate_disposition_id, parent_id, ancestors, order_num, title, url, status, create_by, create_time, update_by, update_time, remark from investigate_disposition
    </sql>

    <select id="selectInvestigateDispositionList" parameterType="InvestigateDisposition" resultMap="InvestigateDispositionResult">
        <include refid="selectInvestigateDispositionVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="url != null  and url != ''"> and url = #{url}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectInvestigateDispositionByInvestigateDispositionId" parameterType="Long" resultMap="InvestigateDispositionResult">
        <include refid="selectInvestigateDispositionVo"/>
        where investigate_disposition_id = #{investigateDispositionId}
    </select>
        
    <insert id="insertInvestigateDisposition" parameterType="InvestigateDisposition" useGeneratedKeys="true" keyProperty="investigateDispositionId">
        insert into investigate_disposition
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="title != null">title,</if>
            <if test="url != null">url,</if>
            <if test="status != null">status,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="title != null">#{title},</if>
            <if test="url != null">#{url},</if>
            <if test="status != null">#{status},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateInvestigateDisposition" parameterType="InvestigateDisposition">
        update investigate_disposition
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="title != null">title = #{title},</if>
            <if test="url != null">url = #{url},</if>
            <if test="status != null">status = #{status},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where investigate_disposition_id = #{investigateDispositionId}
    </update>

    <delete id="deleteInvestigateDispositionByInvestigateDispositionId" parameterType="Long">
        delete from investigate_disposition where investigate_disposition_id = #{investigateDispositionId}
    </delete>

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

    <select id="selectInvestigateDispositionById" parameterType="Long" resultMap="InvestigateDispositionResult">
        select d.investigate_disposition_id, d.parent_id, d.ancestors, d.order_num, d.title, d.url, d.status,
            (select title from investigate_disposition where investigate_disposition_id = d.investigate_disposition_id) parent_name
        from investigate_disposition d
        where d.investigate_disposition_id = #{investigateDispositionId}
    </select>
    <select id="selectChildrenInvestigateDispositionById" parameterType="Long" resultMap="InvestigateDispositionResult">
        select * from investigate_disposition where find_in_set(#{investigateDispositionId}, ancestors)
    </select>

    <update id="updateInvestigateDispositionChildren" parameterType="java.util.List">
        update investigate_disposition set ancestors =
        <foreach collection="investigateDispositions" item="item" index="index"
                 separator=" " open="case investigate_disposition_id" close="end">
            when #{investigateDispositionId} then #{item.ancestors}
        </foreach>
        where investigate_disposition_id in
        <foreach collection="investigateDispositions" item="item" index="index"
                 separator="," open="(" close=")">
            #{investigateDispositionId}
        </foreach>
    </update>
</mapper>