<?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.djhk.uav.label.mapper.FolderInfoMapper">

    <resultMap type="com.djhk.uav.label.domain.FolderInfo" id="FolderInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="parentId"    column="parent_id"    />
        <result property="appId"    column="app_id"    />
        <result property="orgId"    column="org_id"    />
        <result property="showStatusApp"    column="show_status_app"    />
        <result property="showStatusWeb"    column="show_status_web"    />
        <result property="lockStatus"    column="lock_status"    />
        <result property="shareStatus"    column="share_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserId"    column="update_user_id"    />
        <result property="deleteTime"    column="delete_time"    />
        <result property="deleteUserId"    column="delete_user_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="source"    column="source"    />
        <result property="init"    column="init"    />
        <result property="itemCode"    column="item_code"    />
        <result property="order"    column="order"    />
    </resultMap>

    <sql id="selectFolderInfoVo">
        select id, name, type, parent_id, app_id, org_id, show_status_app,show_status_web, lock_status,
               share_status, create_time, create_user_id, update_time, update_user_id,
               delete_time, delete_user_id, del_flag, source, init,item_code,`order`
        from folder_info
    </sql>

    <select id="selectFolderInfoList" parameterType="com.djhk.uav.label.domain.FolderInfo" resultMap="FolderInfoResult">
        <include refid="selectFolderInfoVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="appId != null  and appId != ''"> and app_id = #{appId}</if>
            <if test="orgId != null "> and org_id = #{orgId}</if>
            <if test="itemCode != null and itemCode != '' ">and item_code = #{itemCode}</if>
            <if test="showStatusApp != null "> and show_status_app = #{showStatusApp}</if>
            <if test="showStatusWeb != null "> and show_status_web = #{showStatusWeb}</if>
            <if test="lockStatus != null "> and lock_status = #{lockStatus}</if>
            <if test="shareStatus != null "> and share_status = #{shareStatus}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="updateUserId != null "> and update_user_id = #{updateUserId}</if>
            <if test="deleteTime != null "> and delete_time = #{deleteTime}</if>
            <if test="deleteUserId != null "> and delete_user_id = #{deleteUserId}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
            <if test="source != null "> and source = #{source}</if>
            <if test="init != null "> and init = #{init}</if>
        </where>
    </select>

    <select id="selectFolderInfoById" parameterType="Long" resultMap="FolderInfoResult">
        <include refid="selectFolderInfoVo"/>
        where id = #{id}
    </select>
    <select id="selectLockFolderNames" resultType="java.lang.String">
        select name
        from folder_info
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 1
    </select>
    <select id="queryFolderVos"
            parameterType="com.djhk.uav.label.query.FolderTreeQuery"
            resultType="com.djhk.uav.label.domain.vo.FolderVo">

        select f.id, name, type, f.parent_id, f.app_id, f.org_id, show_status_app,show_status_web, lock_status,
        share_status, f.create_time,  source, init,f.item_code ,
        a.application_name as appName,o.org_name , t.item_name
        from folder_info f
            left join app_application_management a on f.app_id = a.app_id
            left join org_info o on o.org_id = f.org_id
            left join t_item t on t.item_code = f.item_code

        where f.del_flag = '0'
        <if test="appId != null and appId != ''">
            and f.app_id = #{appId}
        </if>
        <if test="orgId != null">
            and f.org_id = #{orgId}
        </if>
        <if test="itemCode != null and itemCode != '' ">
            and f.item_code = #{itemCode}
        </if>
        <if test="type != null">
            and f.type = #{type}
        </if>
        <if test="name != null and name != ''">
            and name like concat('%',#{name},'%')
        </if>
        and f.app_id != '-1'
        group by f.id
        order by f.`order` asc,f.id desc
    </select>
    <select id="findFirstParentFolders"
            resultType="com.djhk.uav.label.domain.vo.FolderVo">
        <include refid="selectFolderInfoVo"/>
        where del_flag = '0'
        and id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="selectIdsByParentIds" resultType="java.lang.Long">
        select distinct id
        from folder_info
        where parent_id in
        <foreach collection="parentIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectHaveLockPlottingFolders" resultType="java.lang.String">
        select f.name
        from folder_info f,plotting p
        where f.id = p.folder_id
        and f.del_flag = '0'
        and p.del_flag = '0'
        and p.lock_status = '1'
        and f.id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="selectSelfAndChildren" resultType="com.djhk.uav.label.domain.FolderInfo">
        SELECT f.*
        FROM(
            SELECT
                @ids AS _ids,
                ( SELECT @ids := GROUP_CONCAT( id ) FROM folder_info WHERE FIND_IN_SET( parent_id, @ids ) ) AS cids,
                @l := @l + 1 AS LEVEL
            FROM folder_info,
            ( SELECT @ids := #{id}, @l := 0 ) b
            WHERE @ids IS NOT NULL
            ) ID,
            folder_info f
        WHERE
            f.del_flag = '0'
            AND FIND_IN_SET( f.id, ID._ids )
    </select>

    <insert id="insertFolderInfo" parameterType="com.djhk.uav.label.domain.FolderInfo" useGeneratedKeys="true" keyProperty="id">
        insert into folder_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="type != null">type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="appId != null and appId != ''">app_id,</if>
            <if test="orgId != null">org_id,</if>
            <if test="showStatusApp != null ">show_status_app,</if>
            <if test="showStatusWeb != null ">show_status_web,</if>
            <if test="lockStatus != null">lock_status,</if>
            <if test="shareStatus != null">share_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserId != null">update_user_id,</if>
            <if test="deleteTime != null">delete_time,</if>
            <if test="deleteUserId != null">delete_user_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="source != null">source,</if>
            <if test="init != null">init,</if>
            <if test="itemCode != null and itemCode != '' ">item_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="appId != null and appId != ''">#{appId},</if>
            <if test="orgId != null">#{orgId},</if>
            <if test="showStatusApp != null ">#{showStatusApp},</if>
            <if test="showStatusWeb != null ">#{showStatusWeb},</if>
            <if test="lockStatus != null">#{lockStatus},</if>
            <if test="shareStatus != null">#{shareStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserId != null">#{updateUserId},</if>
            <if test="deleteTime != null">#{deleteTime},</if>
            <if test="deleteUserId != null">#{deleteUserId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="source != null">#{source},</if>
            <if test="init != null">#{init},</if>
            <if test="itemCode != null and itemCode != '' ">#{itemCode},</if>
         </trim>
    </insert>

    <update id="updateFolderInfo" parameterType="com.djhk.uav.label.domain.FolderInfo">
        update folder_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="appId != null and appId != ''">app_id = #{appId},</if>
            <if test="orgId != null">org_id = #{orgId},</if>
            <if test="showStatusApp != null ">show_status_app = #{showStatusApp},</if>
            <if test="showStatusWeb != null ">show_status_web = #{showStatusWeb},</if>
            <if test="lockStatus != null">lock_status = #{lockStatus},</if>
            <if test="shareStatus != null">share_status = #{shareStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserId != null">update_user_id = #{updateUserId},</if>
            <if test="deleteTime != null">delete_time = #{deleteTime},</if>
            <if test="deleteUserId != null">delete_user_id = #{deleteUserId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="itemCode != null and itemCode != '' ">item_code = #{itemCode},</if>
        </trim>
        where id = #{id}
        and lock_status = 0
    </update>

    <update id="deleteFolderInfoById" parameterType="Long">
        update folder_info
        set del_flag = '2'
        where id = #{id}
        and lock_status = 0
    </update>

    <update id="deleteFolderInfoByIds" parameterType="String">
        update folder_info
        set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 0
        and init = 0
    </update>
    <update id="batchUpdateShowStatusApp">
        update folder_info
        set show_status_app = #{showStatusApp}
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 0
    </update>
    <update id="batchUpdateShowStatusWeb">
        update folder_info
        set show_status_web = #{showStatusWeb}
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 0
    </update>
    <update id="batchUpdateShareStatus">
        update folder_info
        set share_status = #{shareStatus}
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 0
    </update>
    <update id="changeLockStatus">
        update folder_info
        set lock_status = #{lockStatus}
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
