<?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.microservices.zone.detail.mapper.ZoneSortMapper">

    <resultMap type="com.microservices.zone.detail.domain.ZoneSort" id="ZoneSortResult">
        <result property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <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"/>
        <result property="zoneId" column="zone_id"/>
        <result property="sort" column="sort"/>
    </resultMap>

    <sql id="selectZoneSortVo">
        select id,
               sort,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               zone_id
        from ${table}
    </sql>

    <select id="selectMaxSortByZoneId" resultType="java.lang.Integer">
        select MAX(sort)
        from ${table}
        where zone_id = #{zoneId};
    </select>
    <select id="selectCountByZoneId" resultType="java.lang.Integer">
        select count(0)
        from ${table}
        where zone_id = #{zoneId}
    </select>
    <select id="selectDataByOrderAndZoneId" resultMap="ZoneSortResult">
        <include refid="selectZoneSortVo"/>
        where zone_id=#{data.zoneId}
        <if test="!isContainsSelf">
            and id &lt;&gt; #{data.id}
        </if>
        <choose>
            <when test='isAsc'>
                ORDER BY sort ASC
            </when>
            <otherwise>
                ORDER BY sort DESC
            </otherwise>
        </choose>
        LIMIT ${order-1},1
    </select>
    <select id="selectMinSortByZoneId" resultType="java.lang.Integer">
        select MIN(sort)
        from ${table}
        where zone_id = #{zoneId};
    </select>
    <update id="batchUpdateSortIncreased">
        update ${table}
        set sort=sort + 1
        where sort &gt;= #{setSort}
          and zone_id = #{zoneId};
    </update>
    <update id="batchUpdateSortDecrease">
        update ${table}
        set sort=sort - 1
        where sort &lt;= #{setSort}
          and zone_id = #{zoneId};
    </update>

</mapper>