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

    <resultMap type="ZoneMemberType" id="ZoneMemberTypeResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="introduction" column="introduction"/>
        <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="selectZoneMemberTypeVo">
        select id,
               name,
               sort,
               `introduction`,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               zone_id
        from zone_member_type
    </sql>

    <sql id="selectZoneMemberTypeAliasVo">
        select zmt.id,
               zmt.name,
               zmt.sort,
               zmt.`introduction`,
               zmt.dept_id,
               zmt.del_flag,
               zmt.create_by,
               zmt.create_time,
               zmt.update_by,
               zmt.update_time,
               zmt.zone_id
        from zone_member_type zmt
    </sql>

    <select id="selectZoneMemberTypeList" parameterType="ZoneMemberType" resultMap="ZoneMemberTypeResult">
        <include refid="selectZoneMemberTypeAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and zmt.name like concat('%', #{name}, '%')</if>
            <if test="introduction != null  and introduction != ''">and zmt.`introduction` = #{introduction}</if>
            <if test="deptId != null ">and zmt.dept_id = #{deptId}</if>
            <if test="zoneId != null ">and zmt.zone_id = #{zoneId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            order by sort
        </where>
    </select>

    <select id="selectZoneMemberTypeById" parameterType="Long" resultMap="ZoneMemberTypeResult">
        <include refid="selectZoneMemberTypeVo"/>
        where id = #{id}
    </select>
    <select id="selectZoneMemberTypeByIdAndZoneId" resultMap="ZoneMemberTypeResult">
        <include refid="selectZoneMemberTypeVo"/>
        where id = #{memberTypeId}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneMemberTypeListByNameAndZoneId" resultMap="ZoneMemberTypeResult">
        <include refid="selectZoneMemberTypeVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneMemberTypeByNameAndZoneId" resultMap="ZoneMemberTypeResult">
        <include refid="selectZoneMemberTypeVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectMaxSortByZoneId" resultType="java.lang.Integer">
        select MAX(sort)
        from zone_member_type
        where zone_id = #{zoneId};
    </select>
    <select id="selectCountByZoneId" resultType="java.lang.Integer">
        select count(0)
        from zone_member_type
        where zone_id = #{zoneId}
    </select>
    <select id="selectDataByOrderAndZoneId" resultType="com.microservices.zone.member.domain.ZoneMemberType">
        <include refid="selectZoneMemberTypeVo"/>
        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_type
        where zone_id = #{zoneId};
    </select>
    <update id="batchUpdateSortIncreased">
        update zone_member_type
        set sort=sort + 1
        where sort &gt;= #{setSort}
          and zone_id = #{zoneId};
    </update>

    <insert id="insertZoneMemberType" parameterType="ZoneMemberType" useGeneratedKeys="true" keyProperty="id">
        insert into zone_member_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="introduction != null and introduction != ''">`introduction`,</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="sort != null">sort,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="introduction != null and introduction != ''">#{introduction},</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="sort != null">#{sort},</if>
        </trim>
    </insert>

    <update id="updateZoneMemberType" parameterType="ZoneMemberType">
        update zone_member_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="introduction != null">`introduction` = #{introduction},</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="sort != null">sort = #{sort},</if>
        </trim>
        where id = #{id}
    </update>


    <delete id="deleteZoneMemberTypeById" parameterType="Long">
        delete
        from zone_member_type
        where id = #{id}
    </delete>

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