<?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.member.mapper.ZoneMemberLevelMapper">

    <resultMap type="ZoneMemberLevel" id="ZoneMemberLevelResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sort" column="sort"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectZoneMemberLevelVo">
        select id,
               name,
               sort,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               zone_id,
               remark
        from zone_member_level
    </sql>

    <sql id="selectZoneMemberLevelAliasVo">
        select zml.id,
               zml.name,
               zml.sort,
               zml.dept_id,
               zml.del_flag,
               zml.create_by,
               zml.create_time,
               zml.update_by,
               zml.update_time,
               zml.zone_id,
               zml.remark
        from zone_member_level zml
    </sql>

    <select id="selectZoneMemberLevelList" parameterType="ZoneMemberLevel" resultMap="ZoneMemberLevelResult">
        <include refid="selectZoneMemberLevelAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and zml.name like concat('%', #{name}, '%')</if>
            <if test="sort != null ">and zml.sort = #{sort}</if>
            <if test="deptId != null ">and zml.dept_id = #{deptId}</if>
            <if test="zoneId != null ">and zml.zone_id = #{zoneId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by sort
    </select>

    <select id="selectZoneMemberLevelById" parameterType="Long" resultMap="ZoneMemberLevelResult">
        <include refid="selectZoneMemberLevelVo"/>
        where id = #{id}
    </select>
    <select id="selectZoneMemberLevelByIdAndZoneId" resultMap="ZoneMemberLevelResult">
        <include refid="selectZoneMemberLevelVo"/>
        where id = #{memberLevelId}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneMemberLevelListByNameAndZoneId" resultMap="ZoneMemberLevelResult">
        <include refid="selectZoneMemberLevelVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneMemberLevelByNameAndZoneId" resultMap="ZoneMemberLevelResult">
        <include refid="selectZoneMemberLevelVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectMaxSortByZoneId" resultType="java.lang.Integer">
        select MAX(sort)
        from zone_member_level
        where zone_id = #{zoneId};
    </select>
    <select id="selectCountByZoneId" resultType="java.lang.Integer">
        select count(0)
        from zone_member_level
        where zone_id = #{zoneId}
    </select>
    <select id="selectZoneMemberLevelByOrderAndZoneId"
            resultType="com.microservices.zone.member.domain.ZoneMemberLevel">
        <include refid="selectZoneMemberLevelVo"/>
        where zone_id=#{data.zoneId}
        <if test="!isContainsSelf">
            and id &lt;&gt; #{data.id}
        </if>
        ORDER BY sort ASC
        LIMIT ${order-1},1
    </select>
    <select id="selectMinSortByZoneId" resultType="java.lang.Integer">
        select MIN(sort)
        from zone_member_level
        where zone_id = #{zoneId};
    </select>

    <insert id="insertZoneMemberLevel" parameterType="ZoneMemberLevel" useGeneratedKeys="true" keyProperty="id">
        insert into zone_member_level
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="sort != null">sort,</if>
            <if test="deptId != null">dept_id,</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>
            <if test="zoneId != null">zone_id,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="sort != null">#{sort},</if>
            <if test="deptId != null">#{deptId},</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>
            <if test="zoneId != null">#{zoneId},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateZoneMemberLevel" parameterType="ZoneMemberLevel">
        update zone_member_level
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</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>
            <if test="zoneId != null">zone_id = #{zoneId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="batchUpdateSortIncreased">
        update zone_member_level
        set sort=sort + 1
        where sort &gt;= #{setSort}
          and zone_id = #{zoneId};
    </update>

    <delete id="deleteZoneMemberLevelById" parameterType="Long">
        delete
        from zone_member_level
        where id = #{id}
    </delete>

    <delete id="deleteZoneMemberLevelByIds" parameterType="String">
        delete from zone_member_level where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteZoneMemberLevelByZoneId">
        delete
        from zone_member_level
        where zone_id = #{zoneId}
    </delete>
</mapper>