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

    <resultMap type="com.djhk.uav.label.domain.Plotting" id="PlottingResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="plottingType"    column="plotting_type"    />
        <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="dataType"    column="data_type"    />
        <result property="type"    column="type"    />
        <result property="folderId"    column="folder_id"    />
        <result property="delFlag"    column="del_flag"    />
        <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="data"    column="data"    />
        <result property="source"    column="source"    />
        <result property="uuid"    column="uuid"    />
        <result property="itemCode"    column="item_code"    />
        <result property="order"    column="order"    />
    </resultMap>

    <sql id="selectPlottingVo">
        select id,name, plotting_type, app_id, org_id, show_status_web,
            data_type, type, del_flag, create_time, create_user_id,
            update_time, update_user_id, delete_time, delete_user_id, data, uuid,item_code,`order`
        from plotting
    </sql>

    <select id="selectPlottingList" parameterType="com.djhk.uav.label.domain.Plotting" resultMap="PlottingResult">
        <include refid="selectPlottingVo"/>
        <where>
            and del_flag = '0'
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="plottingType != null  and plottingType != ''"> and plotting_type = #{plottingType}</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="dataType != null  and dataType != ''"> and data_type = #{dataType}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="folderId != null "> and folder_id = #{folderId}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</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="data != null  and data != ''"> and data = #{data}</if>
            <if test="source"> and source = #{source}</if>
            <if test="uuid"> and uuid = #{uuid}</if>
        </where>
        order by `order` asc,id desc
    </select>

    <select id="selectPlottingById" parameterType="Long" resultMap="PlottingResult">
        <include refid="selectPlottingVo"/>
        where id = #{id}
    </select>

    <select id="selectPlottingByUUId" parameterType="String" resultMap="PlottingResult">
        <include refid="selectPlottingVo"/>
        where uuid = #{uuid}
        ORDER BY create_time DESC
        LIMIT 1
    </select>

    <select id="selectPlottingUUId" parameterType="String" resultMap="PlottingResult">
        select id,name, plotting_type, app_id, org_id, show_status_web,
        data_type, type, del_flag, create_time, create_user_id,
        update_time, update_user_id, delete_time, delete_user_id, data, uuid,item_code,`order`
        from plotting ORDER BY create_time DESC
        LIMIT 1
    </select>


    <select id="selectMaxLockStatusByFolderIdIds" resultType="java.lang.Integer">
        select ifnull(o.m,0) from (
            select max(lock_status) m
            from plotting
            where del_flag = '0'
            and folder_id in
            <foreach collection="folderIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        )o
    </select>
    <select id="selectLockedNum" resultType="java.lang.Integer">
        select count(distinct id)
        from plotting
        where lock_status = 1
        and id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectLockedNumByUUId" resultType="java.lang.Integer">
        select count(distinct id)
        from plotting
        where lock_status = 1
        and uuid in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectSeqByPlottingName" resultType="java.lang.Integer">
        SELECT max(seq) FROM `plotting` where name regexp  #{name} and del_flag = "0"
    </select>

    <insert id="insertPlotting" parameterType="com.djhk.uav.label.domain.Plotting" useGeneratedKeys="true" keyProperty="id">
        insert into plotting
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="plottingType != null">plotting_type,</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="dataType != null and dataType != ''">data_type,</if>
            <if test="type != null">type,</if>
            <if test="folderId != null">folder_id,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</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="data != null">data,</if>
            <if test="source != null">source,</if>
            <if test="uuid != null and uuid != ''" >uuid,</if>
            <if test="itemCode != null and itemCode != ''" >item_code,</if>
            <if test="seq != null" >seq,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="plottingType != null">#{plottingType},</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="dataType != null and dataType != ''">#{dataType},</if>
            <if test="type != null">#{type},</if>
            <if test="folderId != null">#{folderId},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</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="data != null">#{data},</if>
            <if test="source != null">#{source},</if>
            <if test="uuid != null and uuid != ''">#{uuid},</if>
            <if test="itemCode != null and itemCode != '' ">#{itemCode},</if>
            <if test="seq != null">#{seq},</if>
         </trim>
    </insert>

    <update id="updatePlotting" parameterType="com.djhk.uav.label.domain.Plotting">
        update plotting
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="plottingType != null">plotting_type = #{plottingType},</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="dataType != null and dataType != ''">data_type = #{dataType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="folderId != null">folder_id = #{folderId},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</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="data != null">data = #{data},</if>
            <if test="source != null">source = #{source},</if>
            <if test="uuid != null and uuid != ''">uuid = #{uuid},</if>
            <if test="itemCode != null and itemCode != '' ">item_code = #{itemCode}</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updatePlottingByUUId" parameterType="com.djhk.uav.label.domain.Plotting">
        update plotting
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="plottingType != null">plotting_type = #{plottingType},</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="dataType != null and dataType != ''">data_type = #{dataType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="folderId != null">folder_id = #{folderId},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</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="data != null">data = #{data},</if>
            <if test="source != null">source = #{source},</if>
            <if test="uuid != null and uuid != ''">uuid = #{uuid},</if>
            <if test="itemCode != null and itemCode != '' ">item_code = #{itemCode}</if>
        </trim>
        where uuid = #{uuid}
    </update>

    <update id="updateLockStatusByFolderId">
        update plotting
        set lock_status = #{lockStatus}
        where folder_id = #{folderId}
    </update>

    <update id="batchUpdateShowStatusAppByFolderId">
        update plotting
        set show_status_app = #{showStatusApp}
        where folder_id in
        <foreach collection="folderIds" item="folderId" open="(" separator="," close=")">
            #{folderId}
        </foreach>
        and del_flag = '0'
        and lock_status = 0
    </update>

    <update id="batchUpdateShowStatusWebByFolderId">
        update plotting
        set show_status_web = #{showStatusWeb}
        where folder_id in
        <foreach collection="folderIds" item="folderId" open="(" separator="," close=")">
            #{folderId}
        </foreach>
        and del_flag = '0'
        and lock_status = 0
    </update>
    <update id="batchUpdateShareStatusByFolderId">
        update plotting
        set share_status = #{shareStatus}
        where folder_id in
        <foreach collection="folderIds" item="folderId" open="(" separator="," close=")">
            #{folderId}
        </foreach>
        and del_flag = '0'
        and lock_status = 0
    </update>

    <delete id="deletePlottingById" parameterType="Long">
        delete from plotting where id = #{id}
    </delete>

    <update id="deletePlottingByIds" parameterType="String">
        update plotting
        set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        and lock_status = 0
    </update>

    <update id="deletePlottingByUUId" parameterType="String">
        update plotting
        set del_flag = '2'
        where uuid = #{uuid}
        and lock_status = 0
    </update>

    <insert id="batchInsertPlotting" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into plotting
        (name,plotting_type,app_id,org_id,show_status_web,data_type,type,data,uuid,create_by)
        values
        <foreach collection="plottings" item="i" open="(" separator="," close=")">
            #{i.name},#{i.plottingType},#{i.appId},#{i.orgId},#{i.showStatusWeb},
            #{i.dataType},#{i.type},#{i.data},#{i.uuid},#{i.createBy}
        </foreach>
    </insert>

    <update id="changeLockStatus">
        update plotting
        set lock_status = #{lockStatus}
        where id = #{id}
        and del_flag = '0'
    </update>
    <update id="changeShareStatus">
        update plotting
        set share_status = #{shareStatus}
        where id = #{id}
        and del_flag = '0'
        and lock_status = 0
    </update>
    <update id="changeShowStatusWeb">
        update plotting
        set show_status_web = #{showStatus}
        where id = #{id}
        and del_flag = '0'
        and lock_status = 0
    </update>

    <update id="changeShowStatusApp">
        update plotting
        set show_status_app = #{showStatus}
        where id = #{id}
          and del_flag = '0'
          and lock_status = 0
    </update>
    <select id="selectPlottingListById" resultType="com.djhk.uav.label.domain.Plotting">
        select id,name,plotting_type plottingType,data_type dataType,data,uuid,show_status_web
        from plotting
        <where>
            and del_flag = '0'
            <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="type != null">
                and type = #{type}
            </if>
            <if test="shareStatus != null">
                and share_status = #{shareStatus}
            </if>
            <if test="plottingType != null and plottingType != '' ">
                and plotting_type = #{plottingType}
            </if>
            <if test="showStatusApp != null">
                and show_status_app = #{showStatusApp}
            </if>
            <if test="showStatusWeb != null">
                and show_status_web = #{showStatusWeb}
            </if>
            <if test="lastId != null">
                and id &gt; #{lastId}
            </if>
        </where>
        order by id asc
    </select>

</mapper>
