<?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.partners.mapper.ZonePartnersMapper">
    
    <resultMap type="ZonePartners" id="ZonePartnersResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="link"    column="link"    />
        <result property="logo"    column="logo"    />
        <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="typeId" column="type_id"/>
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectZonePartnersVo">
        select id,
               name,
               link,
               logo,
               sort,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               zone_id,
               type_id,
               remark
        from zone_partners
    </sql>

    <sql id="selectZonePartnersAliasVo">
        select zp.id,
               zp.name,
               zp.link,
               zp.logo,
               zp.sort,
               zp.dept_id,
               zp.del_flag,
               zp.create_by,
               zp.create_time,
               zp.update_by,
               zp.update_time,
               zp.zone_id,
               zp.type_id,
               zp.remark
        from zone_partners as zp
                 left join zone_partners_type zpt on zp.type_id = zpt.id
    </sql>

    <select id="selectZonePartnersList" parameterType="ZonePartners" resultMap="ZonePartnersResult">
        <include refid="selectZonePartnersAliasVo"/>
        <include refid="selectWhere"/>
    </select>

    <sql id="selectWhere">
        <where>
            <if test="name != null  and name != ''">and zp.name like concat('%', #{name}, '%')</if>
            <if test="link != null  and link != ''">and zp.link = #{link}</if>
            <if test="logo != null  and logo != ''">and zp.logo = #{logo}</if>
            <if test="deptId != null ">and zp.dept_id = #{deptId}</if>
            <if test="zoneId != null ">and zp.zone_id = #{zoneId}</if>
            <if test="typeId != null ">and zp.type_id = #{typeId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            order by
            zpt.sort ASC,
            zp.sort ASC
        </where>
    </sql>

    <select id="selectZonePartnersById" parameterType="Long" resultMap="ZonePartnersResult">
        <include refid="selectZonePartnersVo"/>
        where id = #{id}
    </select>
    <select id="selectZonePartnersCount" resultType="java.lang.Integer">
        select count(0) from zone_partners zp
        left join zone_partners_type zpt on zp.type_id = zpt.id
        <include refid="selectWhere"/>
    </select>
    <select id="selectMaxSortByZoneId" resultType="java.lang.Integer">
        select MAX(sort)
        from zone_partners
        where zone_id = #{zoneId}
          and type_id = #{typeId};
    </select>
    <select id="selectCountByZoneId" resultType="java.lang.Integer">
        select count(0)
        from zone_partners
        where zone_id = #{zoneId}
          and type_id = #{typeId};
    </select>
    <select id="selectZonePartnersByOrderAndZoneId" resultMap="ZonePartnersResult">
        <include refid="selectZonePartnersVo"/>
        where zone_id=#{zonePartners.zoneId}
        and type_id = #{zonePartners.typeId}
        <if test="!isContainsSelf">
            and id &lt;&gt; #{zonePartners.id}
        </if>
        ORDER BY sort ASC
        LIMIT ${order-1},1
    </select>
    <select id="selectMinSortByZoneId" resultType="java.lang.Integer">
        select MIN(sort)
        from zone_partners
        where zone_id = #{zoneId}
          and type_id = #{typeId};
    </select>

    <insert id="insertZonePartners" parameterType="ZonePartners" useGeneratedKeys="true" keyProperty="id">
        insert into zone_partners
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="link != null and link != ''">link,</if>
            <if test="logo != null and logo != ''">logo,</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="typeId != null">type_id,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="link != null and link != ''">#{link},</if>
            <if test="logo != null and logo != ''">#{logo},</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="typeId != null">#{typeId},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateZonePartners" parameterType="ZonePartners">
        update zone_partners
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="link != null and link != ''">link = #{link},</if>
            <if test="logo != null and logo != ''">logo = #{logo},</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="typeId != null">type_id = #{typeId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="batchUpdateSortIncreased">
        update zone_partners
        set sort=sort + 1
        where sort &gt;= #{setSort}
          and zone_id = #{zonePartners.zoneId}
          and type_id = #{zonePartners.typeId};
    </update>

    <delete id="deleteZonePartnersById" parameterType="Long">
        delete
        from zone_partners
        where id = #{id}
    </delete>

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