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

    <resultMap type="ZonePointsApply" id="ZonePointsApplyResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="zoneId" column="zone_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="applyType" column="apply_type"/>
        <result property="eventId" column="event_id"/>
        <result property="reply" column="reply"/>
        <result property="status" column="status"/>
        <result property="points" column="points"/>
        <result property="replyId" column="reply_id"/>
        <result property="phone" column="phone"/>
        <result property="realName" column="real_name"/>
        <result property="description" column="description"/>
        <result property="organization" column="organization"/>
        <result property="wechat" column="wechat"/>
        <result property="email" column="email"/>
        <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="memberName" column="member_name"/>
        <association property="sysUser" column="user_id" javaType="com.microservices.system.api.domain.SysUser"
                     resultMap="userResult"/>
    </resultMap>

    <resultMap id="userResult" type="com.microservices.system.api.domain.SysUser">
        <id property="userId" column="userId"/>
        <result property="gitlinkUserId" column="gitlink_user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="delFlag" column="userDelFlag"/>
        <result property="status" column="status"/>
    </resultMap>

    <sql id="selectZonePointsApplyVo">
        select id, user_id, zone_id, dept_id, apply_type, event_id, reply, status, points, reply_id,
               phone, real_name, description, organization, wechat, email,
               create_by, create_time, update_by, update_time
        from zone_points_apply
    </sql>

    <sql id="selectZonePointsApplyAliasVo">
        select zpa.id, zpa.user_id, zpa.zone_id, zpa.dept_id, zpa.apply_type, zpa.event_id,
               zpa.reply, zpa.status, zpa.points, zpa.reply_id,
               zpa.phone, zpa.real_name, zpa.description, zpa.organization, zpa.wechat, zpa.email,
               zpa.create_by, zpa.create_time, zpa.update_by, zpa.update_time,
               zm.name as member_name,
               u.user_id  as userId,
               u.gitlink_user_id,
               u.user_name,
               u.nick_name,
               u.email,
               u.avatar,
               u.phonenumber,
               u.sex,
               u.status,
               u.del_flag as userDelFlag
        from zone_points_apply zpa
                 left join sys_user u on u.user_id = zpa.user_id
                 left join zone_member zm on zm.user_id = zpa.user_id and zm.zone_id = zpa.zone_id
    </sql>

    <select id="selectZonePointsApplyList" parameterType="com.microservices.zone.member.domain.vo.ZonePointsApplySearchVo" resultMap="ZonePointsApplyResult">
        <include refid="selectZonePointsApplyAliasVo"/>
        <where>
            <if test="userId != null">and zpa.user_id = #{userId}</if>
            <if test="zoneId != null">and zpa.zone_id = #{zoneId}</if>
            <if test="applyType != null and applyType != ''">and zpa.apply_type = #{applyType}</if>
            <if test="eventId != null and eventId != ''">and zpa.event_id = #{eventId}</if>
            <if test="status != null and status != ''">and zpa.status = #{status}</if>
            <if test="phone != null and phone != ''">and zpa.phone = #{phone}</if>
            <if test="realName != null and realName != ''">and zpa.real_name like concat('%', #{realName}, '%')</if>
            <if test="email != null and email != ''">and zpa.email = #{email}</if>
            <if test="memberName != null and memberName != ''">and zm.name like concat('%', #{memberName}, '%')</if>
            <if test="userName != null and userName != ''">and u.user_name like concat('%', #{userName}, '%')</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectZonePointsApplyById" parameterType="Long" resultMap="ZonePointsApplyResult">
        <include refid="selectZonePointsApplyVo"/>
        where id = #{id}
    </select>

    <select id="selectZonePointsApplyByUserIdAndZoneId" resultMap="ZonePointsApplyResult">
        <include refid="selectZonePointsApplyVo"/>
        where user_id = #{userId} and zone_id = #{zoneId}
    </select>

    <select id="countPendingByUserZoneAndType" resultType="int">
        select count(1) from zone_points_apply
        where user_id = #{userId} and zone_id = #{zoneId} and apply_type = #{applyType} and status = 'pending'
    </select>

    <insert id="insertZonePointsApply" parameterType="ZonePointsApply" useGeneratedKeys="true" keyProperty="id">
        insert into zone_points_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="zoneId != null">zone_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="applyType != null">apply_type,</if>
            <if test="eventId != null">event_id,</if>
            <if test="reply != null">reply,</if>
            <if test="status != null">status,</if>
            <if test="points != null">points,</if>
            <if test="replyId != null">reply_id,</if>
            <if test="phone != null">phone,</if>
            <if test="realName != null">real_name,</if>
            <if test="description != null">description,</if>
            <if test="organization != null">organization,</if>
            <if test="wechat != null">wechat,</if>
            <if test="email != null">email,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="zoneId != null">#{zoneId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="applyType != null">#{applyType},</if>
            <if test="eventId != null">#{eventId},</if>
            <if test="reply != null">#{reply},</if>
            <if test="status != null">#{status},</if>
            <if test="points != null">#{points},</if>
            <if test="replyId != null">#{replyId},</if>
            <if test="phone != null">#{phone},</if>
            <if test="realName != null">#{realName},</if>
            <if test="description != null">#{description},</if>
            <if test="organization != null">#{organization},</if>
            <if test="wechat != null">#{wechat},</if>
            <if test="email != null">#{email},</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>
        </trim>
    </insert>

    <update id="updateZonePointsApply" parameterType="ZonePointsApply">
        update zone_points_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="zoneId != null">zone_id = #{zoneId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="applyType != null">apply_type = #{applyType},</if>
            <if test="eventId != null">event_id = #{eventId},</if>
            <if test="reply != null">reply = #{reply},</if>
            <if test="status != null">status = #{status},</if>
            <if test="points != null">points = #{points},</if>
            <if test="replyId != null">reply_id = #{replyId},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="realName != null">real_name = #{realName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="organization != null">organization = #{organization},</if>
            <if test="wechat != null">wechat = #{wechat},</if>
            <if test="email != null">email = #{email},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteZonePointsApplyById" parameterType="Long">
        delete from zone_points_apply where id = #{id}
    </delete>

    <delete id="deleteZonePointsApplyByIds" parameterType="String">
        delete from zone_points_apply where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>