<?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.TabHaobanExternalClerkRelatedMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        <id column="external_clerk_related_id" property="externalClerkRelatedId" jdbcType="VARCHAR"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="enterprise_id" property="enterpriseId" jdbcType="VARCHAR"/>
        <result column="store_id" property="storeId" jdbcType="VARCHAR"/>
        <result column="member_id" property="memberId" jdbcType="VARCHAR"/>
        <result column="unionid" property="unionid" jdbcType="VARCHAR"/>
        <result column="external_name" property="externalName" jdbcType="VARCHAR"/>
        <result column="head_url" property="headUrl" jdbcType="VARCHAR"/>
        <result column="external_user_id" property="externalUserId" jdbcType="VARCHAR"/>
        <result column="add_create_time" property="addCreateTime" jdbcType="VARCHAR"/>
        <result column="member_unionid_related_id" property="memberUnionidRelatedId" jdbcType="VARCHAR"/>
        <result column="clerk_id" property="clerkId" jdbcType="VARCHAR"/>
        <result column="staff_id" property="staffId" jdbcType="VARCHAR"/>
        <result column="relation_key" property="relationKey" jdbcType="VARCHAR"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="self_external_userid" property="selfExternalUserid"/>
        <result column="add_time" property="addTime" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        external_clerk_related_id, wx_enterprise_id, enterprise_id, store_id, member_id,
      unionid, head_url,external_name, add_create_time, member_unionid_related_id, clerk_id, staff_id,
      relation_key, status_flag, create_time, update_time,external_user_id ,self_external_userid,add_time
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where external_clerk_related_id = #{externalClerkRelatedId,jdbcType=VARCHAR}
    </select>

    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        insert into tab_haoban_external_clerk_related
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="externalClerkRelatedId != null">
                external_clerk_related_id,
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="enterpriseId != null">
                enterprise_id,
            </if>
            <if test="storeId != null">
                store_id,
            </if>
            <if test="memberId != null">
                member_id,
            </if>
            <if test="unionid != null">
                unionid,
            </if>
            <if test="headUrl != null">
                head_url,
            </if>
            <if test="externalName != null">
                external_name,
            </if>
            <if test="externalUserId != null">
                external_user_id,
            </if>
            <if test="addCreateTime != null">
                add_create_time,
            </if>
            <if test="memberUnionidRelatedId != null">
                member_unionid_related_id,
            </if>
            <if test="clerkId != null">
                clerk_id,
            </if>
            <if test="staffId != null">
                staff_id,
            </if>
            <if test="pushTimes != null">
                push_times,
            </if>
            <if test="relationKey != null">
                relation_key,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="selfExternalUserid != null">
            	self_external_userid,
            </if>
            <if test="addTime != null">
                add_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="externalClerkRelatedId != null">
                #{externalClerkRelatedId,jdbcType=VARCHAR},
            </if>
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="enterpriseId != null">
                #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="storeId != null">
                #{storeId,jdbcType=VARCHAR},
            </if>
            <if test="memberId != null">
                #{memberId,jdbcType=VARCHAR},
            </if>
            <if test="unionid != null">
                #{unionid,jdbcType=VARCHAR},
            </if>
            <if test="headUrl != null">
                #{headUrl,jdbcType=VARCHAR},
            </if>
            <if test="externalName != null">
                #{externalName,jdbcType=VARCHAR},
            </if>
            <if test="externalUserId != null">
                #{externalUserId,jdbcType=VARCHAR},
            </if>
            <if test="addCreateTime != null">
                #{addCreateTime,jdbcType=VARCHAR},
            </if>
            <if test="memberUnionidRelatedId != null">
                #{memberUnionidRelatedId,jdbcType=VARCHAR},
            </if>
            <if test="clerkId != null">
                #{clerkId,jdbcType=VARCHAR},
            </if>
            <if test="staffId != null">
                #{staffId,jdbcType=VARCHAR},
            </if>
            <if test="pushTimes != null">
                #{pushTimes},
            </if>
            <if test="relationKey != null">
                #{relationKey,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="selfExternalUserid != null">
            	#{selfExternalUserid},
            </if>
            <if test="addTime != null">
                #{addTime},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        <set>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="enterpriseId != null">
                enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="storeId != null">
                store_id = #{storeId,jdbcType=VARCHAR},
            </if>
            <if test="memberId != null">
                member_id = #{memberId,jdbcType=VARCHAR},
            </if>
            <if test="unionid != null">
                unionid = #{unionid,jdbcType=VARCHAR},
            </if>
            <if test="headUrl != null">
                head_url = #{headUrl,jdbcType=VARCHAR},
            </if>
            <if test="externalName != null">
                external_name = #{externalName,jdbcType=VARCHAR},
            </if>
            <if test="externalUserId != null">
                external_user_id = #{externalUserId,jdbcType=VARCHAR},
            </if>
            <if test="selfExternalUserid != null">
                self_external_userid = #{selfExternalUserid} ,
            </if>
            <if test="addCreateTime != null">
                add_create_time = #{addCreateTime,jdbcType=VARCHAR},
            </if>
            <if test="memberUnionidRelatedId != null">
                member_unionid_related_id = #{memberUnionidRelatedId,jdbcType=VARCHAR},
            </if>
            <if test="clerkId != null">
                clerk_id = #{clerkId,jdbcType=VARCHAR},
            </if>
            <if test="staffId != null">
                staff_id = #{staffId,jdbcType=VARCHAR},
            </if>
            <if test="relationKey != null">
                relation_key = #{relationKey,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="addTime != null">
                add_time = #{addTime,jdbcType=INTEGER},
            </if>
        </set>
        where external_clerk_related_id = #{externalClerkRelatedId,jdbcType=VARCHAR}
    </update>

    <select id="selectByClerkIdAndExternalUserId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where clerk_id = #{clerkId,jdbcType=VARCHAR}
        and external_user_id = #{externalUserId}
        and status_flag = 1
    </select>

    <update id="delByUserIdAndExternalUserId">
        update tab_haoban_external_clerk_related
        set status_flag = 2,
            update_time = now()
        where external_user_id = #{externalUserId}
          and staff_id = #{staffId}
          and status_flag in (1, 3, 4)
    </update>

    <update id="delByUserIdAndExternalUserIdBatch"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        set
        status_flag = 2,
        update_time = now()
        where
        wx_enterprise_id=#{wxEnterpriseId}
        and staff_id = #{staffId}
        and status_flag in(1,3,4)
        and external_user_id in
        <foreach collection="externalUserIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <update id="delByClerkIdAndStaffId"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        set status_flag = 2,
            update_time = now()
        where clerk_id = #{clerkId,jdbcType=VARCHAR}
          and staff_id = #{staffId}
          and status_flag in (1, 3, 4)
    </update>

    <update id="delByStoreIdAndStaffId"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        set status_flag = 2,
            update_time = now()
        where store_id = #{storeId,jdbcType=VARCHAR}
          and staff_id = #{staffId}
          and status_flag in (1, 3, 4)
    </update>

    <update id="delByStoreIds"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        set
        status_flag = 2,
        update_time = now()
        where store_id IN
        <foreach collection="storeIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and status_flag in(1,3,4)
    </update>

    <update id="cleanStaffRelated" parameterType="com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated">
        update tab_haoban_external_clerk_related
        set
        status_flag = 2,
        update_time = now()
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and staff_id IN
        <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        and status_flag = 1
    </update>


    <select id="listByExTernalUseridAndWxUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and external_user_id = #{externalUserid}
        and status_flag = #{statusFlag}
    </select>


    <select id="listByExternalUseridAndWxEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where external_user_id = #{externalUserid} and wx_enterprise_id = #{wxEnterpriseId}
        <if test="statusFlags != null">
            and status_flag IN
            <foreach collection="statusFlags" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="listByExTernalUseridAndWxUserIdAllStatus" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and external_user_id = #{externalUserid}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag in(1,3,4)
    </select>
    <select id="listByEnterpriseIdAndDate" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where status_flag = 1
        and enterprise_id = #{enterpriseId}
        <if test="date != null">
            and TO_DAYS(create_time) <![CDATA[ >= ]]> TO_DAYS(#{date,jdbcType=TIMESTAMP})
        </if>
    </select>

    <select id="pageMemberUnionByParams" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where enterprise_id = #{enterpriseId}
        and status_flag = 1
        and member_id in
        <foreach item="memberId" index="index" collection="sendMemberIds" open="(" separator="," close=")">
            #{memberId, jdbcType=CHAR}
        </foreach>
        and staff_id in
        <foreach item="staffId" index="index" collection="staffIdList" open="(" separator="," close=")">
            #{staffId, jdbcType=CHAR}
        </foreach>
        and member_id is not null
        group by external_user_id
    </select>

    <select id="listByWxEnterpriseIdAndWxUserId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
    </select>

    <select id="listByWxEnterpriseIdAndWxUserIds" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id in
        <foreach item="staffId" index="index" collection="staffIdList" open="(" separator="," close=")">
            #{staffId, jdbcType=CHAR}
        </foreach>
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
    </select>

    <select id="getByMemberId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId}
        and member_id = #{memberId}
        and status_flag = 1
        and external_user_id is not null
        order by update_time desc
        limit 1
    </select>

    <select id="listExternalByUnionId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where enterprise_id = #{enterpriseId}
        and unionid = #{unionId}
        and status_flag = 1
    </select>


    <select id="getByRelationKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where
        relation_key = #{relationKey} 
        <if test="filterMember==1">
	        and member_id is null 
        </if>
        and status_flag != 2 
         order by create_time desc limit 1
    </select>

    <select id="listByWxEnterpriseIdAndWxUserIdAndStatus" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag in
        <foreach collection="statusFlags" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <update id="pushExternalByRelationKey">
        update tab_haoban_external_clerk_related
        set status_flag = 4,
            push_times  =push_times + 1,
            update_time = now()
        where relation_key = #{relationKey,jdbcType=VARCHAR}
          and status_flag in (3, 4)
    </update>

    <select id="getExternalClerkByWxUserIdAncExternalUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and enterprise_id = #{enterpriseId} 
        and external_user_id = #{externalUserId}
        and status_flag in(1,3,4)
        order by update_time desc limit 1
    </select>
    
    <select id="getExternalClerkByWxUserIdAncExternalUserIdSelf" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and enterprise_id = #{enterpriseId} 
        and self_external_userid = #{externalUserId}
        and status_flag in(1,3,4)
        order by update_time desc limit 1
    </select>

    <select id="listByMemberIdAndWxEnterpriseId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and enterprise_id = #{enterpriseId}
        and member_id = #{memberId}
        and status_flag = 1
    </select>
    
    
     <select id="listByMemberIdList" resultType="com.gic.haoban.manage.api.dto.ExternalClerkRelatedDTO">
        select
           member_id memberId , external_user_id  externalUserId 
        from tab_haoban_external_clerk_related
        where  member_id in 
         <foreach collection="memberIdList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and wx_enterprise_id = #{wxEnterpriseId}
        and enterprise_id = #{enterpriseId}
        and status_flag = 1 and external_status = 1
		and external_user_id is not null
        group by member_id
    </select>
    
    <select id="getLastByExTernalUseridAndWxUserIdNoStatus" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and staff_id = #{staffId}
        and external_user_id = #{externalUserid}
        order by create_time desc limit 1
    </select>

    <select id="getLastByExTernalUseridAndWxUserIdNoStatusSelf" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and staff_id = #{staffId}
        and self_external_userid = #{externalUserid}
        order by create_time desc limit 1
    </select>


    <select id="listByIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and external_clerk_related_id in
        <foreach collection="ids" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>
    <select id="listMemberIdByEnterpriseId" resultType="String">
        select member_id
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
          and enterprise_id = #{enterpriseId}
          and status_flag = 1
    </select>

    <select id="listMemberIdsByMemberIds" resultType="String">
        select
        member_id
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and member_id in
        <foreach collection="memberIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        and status_flag = 1
    </select>

    <select id="listByMemberId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where member_id = #{memberId}
        and status_flag = 1
        <if test="enterpriseId != null and enterpriseId != ''">
            and enterprise_id = #{enterpriseId}
        </if>
    </select>

    <select id="listStoreIdByMemberId" resultType="string">
        select
        store_id
        from tab_haoban_external_clerk_related
        where member_id = #{memberId}
        <if test="null != addCreateTime">
            and add_create_time <![CDATA[ <=  ]]> #{addCreateTime}
        </if>
        and status_flag = 1
    </select>

    <select id="listByExternalUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
        and external_user_id = #{externalUserId}
    </select>

    <update id="updateMemberIdByMemberId">
        UPDATE
            tab_haoban_external_clerk_related
        SET member_id=#{memberId},
            update_time = now()
        where member_id = #{oldMemberId}
          and enterprise_id = #{enterpriseId} and status_flag = 1
    </update>

    <update id="updateStatusByMemberId">
        UPDATE
            tab_haoban_external_clerk_related
        SET status_flag = 2,
            update_time = now()
        where member_id = #{memberId}
          and enterprise_id = #{enterpriseId}
    </update>


    <select id="getByMemberIdAndStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where
        member_id = #{memberId}
        and enterprise_id = #{enterpriseId}
        AND staff_id = #{staffId}
        AND status_flag = 1 limit 1
    </select>

    <select id="getByUnionIdAndStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where
        member_id = #{memberId}
        and enterprise_id = #{enterpriseId}
        AND unionid = #{unionid}
        AND status_flag = 1 limit 1
    </select>


    <select id="pageExternalByEnterpriseId" resultMap="BaseResultMap">
        select wx_enterprise_id,
               enterprise_id,
               external_user_id
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
          AND status_flag = 1
          and external_user_id is not null
    </select>


    <update id="updateExternalUserIdByOldExternalUserId">
        UPDATE
            tab_haoban_external_clerk_related
        SET external_user_id = #{newExternalUserId},
            update_time      = now()
        where external_user_id = #{oldExternalUserId}
          and wx_enterprise_id = #{wxEnterpriseId}
    </update>

    <select id="countByClerkId" resultType="Integer">
        select count(*)
        from tab_haoban_external_clerk_related
        where wx_enterprise_id = #{wxEnterpriseId}
          and enterprise_id = #{enterpriseId}
          and staff_id = #{staffId}
          and clerk_id = #{clerkId}
          AND status_flag = 1
    </select>


    <select id="getByEnterpriseIdAndExternalUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and enterprise_id = #{enterpriseId}
        and external_user_id = #{externalUserId}
        and status_flag in (3, 4) 
        order by update_time desc limit 1
    </select>


    <select id="getByWxUserIdAndWxEnterpriseId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId} and status_flag in(1,3,4)
    </select>

    <select id="getByWxEnterpriseIdAndUnionId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where staff_id = #{staffId}
        and wx_enterprise_id = #{wxEnterpriseId}
        and unionid = #{unionid}
        and status_flag = 1
        order by update_time desc limit 1
    </select>


    <select id="getByEnterpriseIdAndMemberId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_external_clerk_related
        where enterprise_id = #{enterpriseId}
        and member_id = #{memberId}
        <if test="statusFlag != null and statusFlag == 1">
            and status_flag in (1,3,4)
        </if>

        <if test="statusFlag != null and statusFlag == 2">
            and status_flag in (0,2)
        </if>
        order by create_time desc limit 1
    </select>


    <select id="countByMemberId" resultType="int">
        select count(*)
        from tab_haoban_external_clerk_related
        where enterprise_id = #{enterpriseId}
          and member_id = #{memberId}
          and status_flag = 1
    </select>
    
     <select id="getIdList" resultType="string">
        select external_clerk_related_id from tab_haoban_external_clerk_related
        where enterprise_id = #{enterpriseId} and member_id = #{memberId}  and status_flag = 1
    </select>
    
    <update id="updateMemberByIdList">
    	UPDATE
            tab_haoban_external_clerk_related
        SET member_id=#{memberId},
            update_time = now()
        where external_clerk_related_id in 
        	<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            	#{id}
        	</foreach>
          and status_flag = 1
    </update>
    
    <select id="listPageByWxenterpriseId" resultMap="BaseResultMap">
        select DISTINCT(external_user_id) external_user_id , create_time from tab_haoban_external_clerk_related where wx_enterprise_id = #{wxEnterpriseId}  and 
        <if test="type==0">
        	status_flag in (1)
        </if>
        <if test="type==1">
        	status_flag in (3,4)
        </if>
        limit ${startItem},${pageSize} 
    </select>


    <select id="getByStoreIdAndStaffId" resultType="java.lang.String">
        select member_id
        from tab_haoban_external_clerk_related
        where store_id = #{storeId,jdbcType=VARCHAR}
          and staff_id = #{staffId}
          and status_flag in (1, 3, 4)
    </select>
    
    <select id="listMemberAndClerk"  resultType="java.lang.String">
    	 select member_id
        from tab_haoban_external_clerk_related
        where member_id in
        <foreach collection="memberIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and clerk_id in
        <foreach collection="clerkIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and enterprise_id = #{enterpriseId}
        AND status_flag = 1 
    </select>

    <select id="getCountExternalClerk" resultType="java.lang.Integer">
        select count(*)
        from tab_haoban_external_clerk_related
        where
            wx_enterprise_id = #{wxEnterpriseId}
          and staff_id = #{staffId}
          and status_flag in
            <foreach collection="statusFlags" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
    </select>

    <select id="getCountByMemberId" resultType="java.lang.Integer" parameterType="java.lang.String">
        select
        count(*)
        from tab_haoban_external_clerk_related
        where
        member_id = #{memberId} and status_flag in (1,3,4)
    </select>
    
    <update id="updateClerkInfo">
		 UPDATE
            tab_haoban_external_clerk_related
        SET clerk_id = #{newClerkId},
            store_id = #{storeId},
            update_time = now()
        where staff_id = #{staffId}
          and clerk_id = #{oldClerkId}
          and status_flag in (1,3,4) 
	</update>
	
	<select id="listByClerkId" resultType="String">
		select member_id from tab_haoban_external_clerk_related 
		 where staff_id = #{staffId}
          and clerk_id = #{clerkId}
          and status_flag = 1
	</select>
	
	<update id="updateExternalStatus">
		update tab_haoban_external_clerk_related set external_status = #{status} where 
		external_user_id = #{externalUserid} and wx_enterprise_id = #{wxEnterpriseId}
	</update>
	
	<select id="getMemberForExternalId"  resultMap="BaseResultMap">
           select <include refid="Base_Column_List"/>
       	from tab_haoban_external_clerk_related  where wx_enterprise_id = #{wxEnterpriseId}  
           and external_user_id = #{externalUserId} and status_flag in (1,3,4) and member_id <![CDATA[ <> '' ]]>
           limit 1
	</select>

    <select id="queryExternalUserIdWithStaffId" resultType="java.lang.String">
        SELECT external_user_id from tab_haoban_external_clerk_related
        WHERE staff_id = #{staffId}
          and status_flag in (1, 3, 4);
    </select>

    <select id="queryClerkMemberRelations" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
            from tab_haoban_external_clerk_related
        where staff_id = #{staffId}
        and status_flag in (1, 3, 4)
        and member_id in
            <foreach collection="memberIds" item="memberId" open="(" close=")" separator=",">
                #{memberId}
            </foreach>
    </select>

	<select id="listMemberAndStaff"  resultType="java.lang.String">
    	 select member_id
        from tab_haoban_external_clerk_related
        where member_id in
        <foreach collection="memberIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and staff_id = #{staffId}
        and enterprise_id = #{enterpriseId}
        AND status_flag = 1 
    </select>

    <select id="listByWxEnterpriseId"  resultType="java.lang.String">
        select staff_id
        from tab_haoban_external_clerk_related
        WHERE
	    wx_enterprise_id = #{wxEnterpriseId}
	    AND enterprise_id IN
	    <foreach collection="enterpriseIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
	    AND status_flag = 1
	    AND external_user_id IS NULL
    </select>

    <update id="deleteByWxEnterpriseId" >
        update  tab_haoban_external_clerk_related
        set status_flag = 2,update_time = now()
        WHERE
        wx_enterprise_id = #{wxEnterpriseId}
        AND enterprise_id IN
        <foreach collection="enterpriseIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND status_flag = 1
        AND external_user_id IS NULL
    </update>
    <select id="getAddTimeByMemberId"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
            tab_haoban_external_clerk_related
        WHERE
            member_id = #{memberId}
          AND enterprise_id = #{enterpriseId}
        <if test="null != flag">
            and status_flag in (1)
        </if>
    </select>

    <select id="getMemberIdByExternalId"  resultType="java.lang.String">
        select member_id
        from tab_haoban_external_clerk_related  where wx_enterprise_id = #{wxEnterpriseId}
        and external_user_id = #{externalUserId} and status_flag =1
        UNION all
        select member_id
        from tab_haoban_external_clerk_related  where wx_enterprise_id = #{wxEnterpriseId}
        and self_external_userid = #{externalUserId} and status_flag =1
        limit 1
    </select>

</mapper>