<?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.gic.haoban.manage.service.dao.mapper.HandoverExternalMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHandoverExternal">
        <id column="handover_external_id" jdbcType="VARCHAR" property="handoverExternalId"/>
        <result column="wx_enterprise_id" jdbcType="VARCHAR" property="wxEnterpriseId"/>
        <result column="staff_id" jdbcType="VARCHAR" property="staffId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="external_user_id" jdbcType="VARCHAR" property="externalUserId"/>
        <result column="external_user_name" jdbcType="VARCHAR" property="externalUserName"/>
        <result column="relation_id" jdbcType="VARCHAR" property="relationId"/>
        <result column="haoban_friend_flag" jdbcType="INTEGER" property="haobanFriendFlag"/>
        <result column="external_head_url" jdbcType="VARCHAR" property="externalHeadUrl"/>
        <result column="status_flag" jdbcType="INTEGER" property="statusFlag"/>
        <result column="handover_transfer_id" jdbcType="VARCHAR" property="handoverTransferId"/>
        <result column="takeover_time" jdbcType="TIMESTAMP" property="takeoverTime"/>
        <result column="transfer_status" jdbcType="INTEGER" property="transferStatus"/>
        <result column="transfer_time" jdbcType="TIMESTAMP" property="transferTime"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <resultMap id="FiendCountResultMap" type="com.gic.haoban.manage.api.dto.HandoverStaffFriendCountDTO">
        <result column="staff_id" jdbcType="VARCHAR" property="staffId"/>
        <result column="friendCount" jdbcType="INTEGER" property="friendCount"/>
        <result column="haobanFriendCount" jdbcType="INTEGER" property="haobanFriendCount"/>
    </resultMap>
    <sql id="Base_Column_List">
    handover_external_id, wx_enterprise_id, staff_id, user_id, external_user_id, external_user_name, relation_id,
    haoban_friend_flag, external_head_url, status_flag, handover_transfer_id, takeover_time, 
    transfer_status, transfer_time, create_time, update_time
  </sql>
    
    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHandoverExternal">
        update tab_haoban_handover_external
        <set>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="staffId != null">
                staff_id = #{staffId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                user_id = #{userId,jdbcType=VARCHAR},
            </if>
            <if test="externalUserId != null">
                external_user_id = #{externalUserId,jdbcType=VARCHAR},
            </if>
            <if test="externalUserName != null">
                external_user_name = #{externalUserName,jdbcType=VARCHAR},
            </if>
            <if test="relationId != null">
                relation_id = #{relationId,jdbcType=VARCHAR},
            </if>
            <if test="haobanFriendFlag != null">
                haoban_friend_flag = #{haobanFriendFlag,jdbcType=INTEGER},
            </if>
            <if test="externalHeadUrl != null">
                external_head_url = #{externalHeadUrl,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="handoverTransferId != null">
                handover_transfer_id = #{handoverTransferId,jdbcType=VARCHAR},
            </if>
            <if test="takeoverTime != null">
                takeover_time = #{takeoverTime,jdbcType=TIMESTAMP},
            </if>
            <if test="transferStatus != null">
                transfer_status = #{transferStatus},
            </if>
            <if test="transferTime != null">
                transfer_time = #{transferTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where handover_external_id = #{handoverExternalId,jdbcType=VARCHAR}
    </update>

    <insert id="insertBatch">
        insert into tab_haoban_handover_external (handover_external_id, wx_enterprise_id,
        staff_id, user_id, external_user_id,
        external_user_name,relation_id, haoban_friend_flag, external_head_url,
        status_flag, create_time,
        update_time)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.handoverExternalId,jdbcType=VARCHAR}, #{item.wxEnterpriseId,jdbcType=VARCHAR},
            #{item.staffId,jdbcType=VARCHAR}, #{item.userId,jdbcType=VARCHAR}, #{item.externalUserId,jdbcType=VARCHAR},
            #{item.externalUserName,jdbcType=VARCHAR},#{item.relationId}, #{item.haobanFriendFlag,jdbcType=INTEGER},
            #{item.externalHeadUrl,jdbcType=VARCHAR},
            #{item.statusFlag,jdbcType=INTEGER}, now(),
            now())
        </foreach>

    </insert>

    <select id="listByStaffIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        <if test="staffIds!=null and staffIds.size>0">
            and staff_id in
            <foreach collection="staffIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        and status_flag = #{status}
        <if test="search!=null and search!=''">
            and external_user_name like CONCAT('%',#{search},'%')
        </if>
        <if test="transferIds!=null and transferIds.size>0">
            and handover_transfer_id in
            <foreach collection="transferIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        order by create_time desc
    </select>

    <select id="listIdByStaffIds" resultMap="BaseResultMap">
        select
        handover_external_id
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        <if test="staffIds!=null and staffIds.size>0">
            and staff_id in
            <foreach collection="staffIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        and status_flag=1
    </select>

    <select id="listByIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and handover_external_id in
        <foreach collection="ids" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and status_flag=1
    </select>

    <update id="updateAfterTransfer">
        update tab_haoban_handover_external
        set
        status_flag = 2,
        handover_transfer_id = #{transferId,jdbcType=VARCHAR},
        transfer_status = 0,
        transfer_time = now(),
        update_time = now()
        where handover_external_id in
        <foreach collection="ids" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <select id="getByTransferId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and handover_transfer_id = #{transferId,jdbcType=VARCHAR}
        and external_user_id=#{externalUserId}
        and status_flag=2
    </select>

    <select id="getByExternalUserIdAndStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and staff_id = #{staffId,jdbcType=VARCHAR}
        and external_user_id=#{externalUserId}
        and status_flag!=0
    </select>

    <update id="delNoTransferExternal">
        update tab_haoban_handover_external
        set
        status_flag = 0,
        update_time = now()
        where
        wx_enterprise_id=#{wxEnterpriseId}
        <if test="userIds!=null and userIds.size>0">
            and user_id in
            <foreach collection="userIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        and status_flag=1
    </update>

    <select id="listPreCountByStaffIds" resultMap="FiendCountResultMap">
        select
        staff_id ,
        COUNT(external_user_id) friendCount,
        sum(haoban_friend_flag) haobanFriendCount
        from tab_haoban_handover_external
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and staff_id in
        <foreach collection="staffIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and status_flag=1
        group by staff_id
    </select>
</mapper>