<?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.TabHaobanStaffClerkRelationMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanStaffClerkRelation">
        <id column="staff_clerk_relation_id" property="staffClerkRelationId" jdbcType="VARCHAR"/>
        <result column="staff_id" property="staffId" 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="clerk_id" property="clerkId" jdbcType="VARCHAR"/>
        <result column="clerk_code" property="clerkCode" 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="open_concat_flag" property="openConcatFlag" jdbcType="INTEGER"/>
        <result column="manage_flag" property="manageFlag" jdbcType="INTEGER"/>
        <result column="clerk_type" property="clerkType" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
        staff_clerk_relation_id,
        staff_id,
        wx_enterprise_id,
        enterprise_id,
        store_id,
        clerk_id,
        clerk_code,
        status_flag,
        create_time,
        update_time,
        open_concat_flag,
        manage_flag,
        clerk_type
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_clerk_relation
        where staff_clerk_relation_id = #{staffClerkRelationId,jdbcType=VARCHAR}
    </select>

    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaffClerkRelation">
        insert into tab_haoban_staff_clerk_relation (staff_clerk_relation_id, staff_id, wx_enterprise_id,
                                                     enterprise_id, store_id,
                                                     clerk_id, clerk_code, status_flag,
                                                     create_time, update_time, open_concat_flag, manage_flag,
                                                     clerk_type)
        values (#{staffClerkRelationId,jdbcType=VARCHAR}, #{staffId,jdbcType=VARCHAR},
                #{wxEnterpriseId,jdbcType=VARCHAR},
                #{enterpriseId,jdbcType=VARCHAR}, #{storeId,jdbcType=VARCHAR},
                #{clerkId,jdbcType=VARCHAR}, #{clerkCode,jdbcType=VARCHAR}, #{statusFlag,jdbcType=INTEGER},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{openConcatFlag}, #{manageFlag},
                #{clerkType})
    </insert>


    <select id="listBindCode" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO" parameterType="java.lang.String">
        <include refid="leftJoinStaffSQL"/>
        where a.enterprise_id = #{enterpriseId}
        and a.wx_enterprise_id = #{wxEnterpriseId} and a.status_flag = 1 and b.status_flag = 1
        and a.clerk_id  in
        <foreach collection="clerkCodeList" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="listBindCodeByStaffId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL"/>
        where a.staff_id = #{staffId,jdbcType=VARCHAR}
        and a.status_flag = 1 and b.status_flag = 1
        <if test="enterpriseIdList!=null and enterpriseIdList.size &gt; 0">
            and a.enterprise_id in
            <foreach collection="enterpriseIdList" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="getOneByClerkId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
       <include refid="leftJoinStaffSQL"/>
        where a.status_flag = 1 and a.clerk_id = #{clerkId} and b.status_flag = 1
        <if test="null != wxEnterpriseId">
        	and a.wx_enterprise_id = #{wxEnterpriseId}
        </if>
        limit 1
    </select>
    
     <select id="getByCodeAndEnterpriseId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
      <include refid="leftJoinStaffSQL"/>
         where a.status_flag = 1 and b.status_flag = 1
        and a.enterprise_id = #{enterpriseId}
        and a.clerk_id = #{clerkCode} order by a.create_time desc limit 1
    </select>

    <select id="getStaffClerkRelationDTO" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL"/>
        where a.status_flag = 1 and b.status_flag = 1
        and a.manage_flag = 0
        and a.enterprise_id = #{enterpriseId}
        and a.clerk_id = #{clerkCode} order by a.create_time desc limit 1
    </select>
    
    
    <update id="changeStatusByClerkId">
        update tab_haoban_staff_clerk_relation
        set status_flag = #{status},
            update_time = now()
        where clerk_id = #{clerkId,jdbcType=VARCHAR}
          and status_flag != #{status}
    </update>


    <select id="listByClerkIds" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
	    <include refid="leftJoinStaffSQL"/>
        where a.status_flag=1 and b.status_flag = 1 
        <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
            and a.wx_enterprise_id = #{wxEnterpriseId}
        </if>
        and a.clerk_id in
        <foreach collection="clerkIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>
    
    <select id="getOneByStoreIdAndStaffId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL" />
        where a.status_flag=1 and b.status_flag=1
        and a.staff_id = #{staffId,jdbcType=VARCHAR}
        <if test="null != storeId and storeId != ''">
            and a.store_id = #{storeId}
        </if>
        order by a.update_time desc limit 1
    </select>


    <update id="cleanStaffDepart">
        update tab_haoban_staff_clerk_relation
        set
        status_flag = 0,
        update_time = now()
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1 and
        staff_id in
        <foreach collection="staffIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <update id="delByPrimaryKey">
        update tab_haoban_staff_clerk_relation
        set status_flag = 0,
            update_time = now()
        where staff_clerk_relation_id = #{staffClerkRelationId,jdbcType=VARCHAR}
          and status_flag = 1
    </update>

    <update id="delByClerkId">
        update tab_haoban_staff_clerk_relation
        set status_flag = 0,
            update_time = now()
        where clerk_id = #{clerkId,jdbcType=VARCHAR}
          and status_flag = 1
    </update>

    <select id="listByEnterpriseIdAndStaffid" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL" />
        where a.enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        and a.staff_id = #{staffId} and a.status_flag=1 and b.status_flag=1
    </select>


    <select id="listByWxEnterpriseId" resultType="java.lang.String">
        select a.staff_id
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on a.staff_id = b.staff_id 
        where a.wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
          and a.status_flag = 1 and b.status_flag = 1
    </select>

    <select id="lisByStaffId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL" />
        where a.wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and a.staff_id = #{staffId}
        and a.status_flag = 1 and b.status_flag = 1 
        <if test="null != storeIdList">
            and a.store_id in
            <foreach collection="storeIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        order by a.create_time desc
    </select>

    <select id="pageByWxEnterpriseId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL"/>
        where a.wx_enterprise_id = #{wxEnterpriseId}
        and a.status_flag =1
    </select>

    <select id="listBindByStoreId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select a.staff_clerk_relation_id staffClerkRelationId,
               a.staff_id                staffId,
               a.wx_enterprise_id        wxEnterpriseId,
               a.enterprise_id           enterpriseId,
               a.store_id                storeId,
               a.clerk_id                clerkId,
               a.clerk_code              clerkCode,
               a.status_flag             statusFlag,
               a.open_concat_flag        openConcatFlag ,
               a.clerk_type              clerkType , 
               a.manage_flag             manageFlag ,
               b.wx_user_id              qwUserId,
               b.wx_open_user_id         openUserId
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
          and a.store_id = #{storeId}
          and a.status_flag = 1
    </select>


    <update id="delByStoreIds">
        UPDATE
        tab_haoban_staff_clerk_relation
        SET
        status_flag=0,
        update_time=now()
        where store_id in
        <foreach collection="storeIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and status_flag =1
    </update>

    <select id="listBindStoreIdByEnterpriseId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL" />
        where a.enterprise_id = #{wxEnterpriseId}
          and a.status_flag = 1 and b.status_flag = 1 and a.store_id <![CDATA[<>]]> ''
        group by a.store_id
    </select>

    <select id="listRelationsStaffId" resultType="java.lang.String">
        select a.staff_id
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on a.staff_id = b.staff_id 
        where a.staff_id in
        <foreach collection="staffIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and a.status_flag = 1 and b.status_flag = 1 group by a.staff_id
    </select>

    <select id="listMemberStaffRelNew" resultType="com.gic.haoban.manage.service.entity.ext.MemberStaffRelExtDO">
        select * from (
        select ecr.member_id as memberId,
        ecr.external_user_id as memberExternalUserId,
        ecr.create_time as addFriendTime ,
        scr.staff_id as staffId,
        scr.clerk_id as staffClerkId
        from tab_haoban_external_clerk_related as ecr
        left join
        (select b.staff_id,
        a.clerk_id
        from tab_haoban_staff_clerk_relation a
        left join tab_haoban_staff b on a.staff_id = b.staff_id and b.status_flag = 1
        where a.clerk_id in
        <foreach collection="clerkIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and a.status_flag = 1
        group by a.staff_id, a.clerk_id) as scr on scr.staff_id = ecr.staff_id
        where ecr.status_flag = 1 AND ecr.`external_status` in (1)
        and ecr.enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        and ecr.wx_enterprise_id=#{wxEnterpriseId}
        and ecr.member_id in
        <foreach collection="memberIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        ) t where t.staffId is not null ORDER BY t.addFriendTime DESC 
    </select>


    <select id="listWxUserIdByClerkIds" resultType="string">
        select
        <if test="type==1">
	        DISTINCT b.wx_user_id
        </if>
        <if test="type==2">
        	DISTINCT b.wx_open_user_id
        </if>
        from tab_haoban_staff_clerk_relation a
        left join tab_haoban_staff b on a.staff_id = b.staff_id
        where a.status_flag=1 and b.status_flag=1 and a.wx_enterprise_id = #{wxEnterpriseId}
        and a.clerk_id in
        <foreach collection="clerkIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="listIdsByWxUserIds" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select
        a.staff_clerk_relation_id staffClerkRelationId,
        a.store_id storeId,
        a.wx_enterprise_id wxEnterpriseId,
        a.enterprise_id enterpriseId,
        b.wx_user_id wxUserId,
        a.staff_id staffId,
        a.clerk_id clerkId,
        b.staff_name staffName,
        b.wx_open_user_id wxOpenUserId
        from tab_haoban_staff_clerk_relation a
        left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.status_flag=1 and b.status_flag = 1
        and a.wx_enterprise_id = #{wxEnterpriseId}
        and a.enterprise_id = #{enterpriseId}
        <if test="type==1">
        	and b.wx_user_id in
	        <foreach collection="wxUserIds" item="id" index="index" open="(" close=")" separator=",">
	            #{id,jdbcType=VARCHAR}
	        </foreach>
        </if>
        <if test="type==2">
        	 and b.wx_open_user_id in
	        <foreach collection="wxUserIds" item="id" index="index" open="(" close=")" separator=",">
	            #{id,jdbcType=VARCHAR}
	        </foreach>
        </if>
        group by a.staff_clerk_relation_id
    </select>

    <update id="updateOpenConcatFlagById">
        UPDATE tab_haoban_staff_clerk_relation
        SET open_concat_flag = #{openConcatFlag},
            update_time      = now()
        WHERE staff_clerk_relation_id = #{staffClerkRelationId}
    </update>


    <select id="listIdsByNotInWxOpenUserIds" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select
        a.staff_clerk_relation_id staffClerkRelationId,
        a.store_id storeId,
        a.wx_enterprise_id wxEnterpriseId,
        a.enterprise_id enterpriseId,
        b.wx_user_id wxUserId,
        a.staff_id staffId,
        a.clerk_id clerkId,
        b.staff_name staffName,
        b.wx_open_user_id wxOpenUserId
        from tab_haoban_staff_clerk_relation a
        left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.status_flag=1 and b.status_flag = 1
        and a.wx_enterprise_id = #{wxEnterpriseId}
        and a.enterprise_id = #{enterpriseId}
        and b.wx_open_user_id not in
        <foreach collection="wxOpenUserIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        group by a.staff_clerk_relation_id
    </select>


    <select id="listIdsByNotInWxUserIds" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select
        a.staff_clerk_relation_id staffClerkRelationId,
        a.store_id storeId,
        a.wx_enterprise_id wxEnterpriseId,
        a.enterprise_id enterpriseId,
        b.wx_user_id wxUserId,
        a.staff_id staffId,
        a.clerk_id clerkId,
        b.staff_name staffName,
        b.wx_open_user_id wxOpenUserId
        from tab_haoban_staff_clerk_relation a
        left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.status_flag=1 and b.status_flag = 1
        and a.wx_enterprise_id = #{wxEnterpriseId}
        and a.enterprise_id = #{enterpriseId}
        and b.wx_user_id not in
        <foreach collection="wxUserIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        group by a.staff_clerk_relation_id
    </select>

    <update id="deleteByEnterpriseId">
        update tab_haoban_staff_clerk_relation
        set status_flag = 0,
            update_time=now()
        where enterprise_id = #{enterpriseId}
    </update>


    <select id="getBindByStaffIdAndEnterpriseId" resultType="java.lang.Integer">
        select count(*)
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on a.staff_id = b.staff_id 
       where a.status_flag = 1 and b.status_flag = 1
          and a.enterprise_id = #{enterpriseId}
          and a.staff_id = #{staffId}
    </select>


    <select id="listManageBindByStaffId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
    	<include refid="leftJoinStaffSQL"/>
        	where a.status_flag=1 and a.manage_flag = 1 and a.staff_id = #{staffId}
        	and b.status_flag = 1 
    </select>

    <sql id="leftJoinStaffSQL">
    	 select a.staff_clerk_relation_id staffClerkRelationId,
               a.staff_id                staffId,
               a.wx_enterprise_id        wxEnterpriseId,
               a.enterprise_id           enterpriseId,
               a.store_id                storeId,
               a.clerk_id                clerkId,
               a.clerk_code              clerkCode,
               a.status_flag             statusFlag,
               a.open_concat_flag        openConcatFlag ,
               a.clerk_type              clerkType ,
               a.create_time			 createTime ,
               a.update_time 			 updateTime , 
               a.manage_flag             manageFlag ,
               b.wx_user_id              qwUserId,
               b.wx_open_user_id         openUserId ,
               b.head_img  staffHeadImg
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on b.staff_id = a.staff_id
    </sql>
    
     <select id="listByStoreIds" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
     	<include refid="leftJoinStaffSQL"/>
        where a.status_flag=1 and b.status_flag=1 
        <if test="null != storeIds">
        	and a.store_id in
	        <foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
	              #{item}
	        </foreach>
        </if>
        <if test="null != wxEnterpriseId">
        	and a.wx_enterprise_id = #{wxEnterpriseId}
        </if>
    </select>

    <select id="listAllClerkIdsByEnterpriseId" resultType="java.lang.String">
        select a.clerk_id clerkId
        from tab_haoban_staff_clerk_relation a inner join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.enterprise_id = #{enterpriseId} and a.status_flag=1 and b.status_flag = 1
    </select>

    <select id="listAlStoreIdsByEnterpriseId" resultType="java.lang.String">
        select distinct(a.store_id) storeId
        from tab_haoban_staff_clerk_relation a inner join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.enterprise_id = #{enterpriseId} and a.status_flag=1 and b.status_flag = 1
    </select>

    <select id="listByWxEnterpriseIdAndStaffId" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select
        staff_id  staffId,
        enterprise_id  enterpriseId ,
        clerk_id clerkId , store_id storeId
        from tab_haoban_staff_clerk_relation
        WHERE
            wx_enterprise_id = #{wxEnterpriseId}
          AND status_flag = 1
          AND staff_id in
        <foreach collection="staffIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>


    <select id="listByStaffIdsForClerk" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        select
        staff_id  staffId
        from tab_haoban_staff_clerk_relation
        WHERE
        wx_enterprise_id = #{wxEnterpriseId}
        AND status_flag = 1 and manage_flag <![CDATA[<>]]> 1
        AND staff_id in
        <foreach collection="staffIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="listAllStaffIdByEnterpriseId" resultType="java.lang.String">
        select a.staff_id
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.enterprise_id = #{enterpriseId} and a.status_flag=1 and b.status_flag = 1 and a.wx_enterprise_id = #{wxEnterpriseId}
        GROUP BY
            a.staff_id
    </select>

    <select id="getByStaffAndClerk" resultType="com.gic.haoban.manage.api.dto.StaffClerkRelationDTO">
        <include refid="leftJoinStaffSQL" />
        where a.status_flag=1 and b.status_flag=1
        and a.staff_id = #{staffId} and a.clerk_id = #{clerkId}
        order by a.update_time desc limit 1
    </select>

    <select id="taskoverClerkIdList" resultType="java.lang.String">
        select clerk_id  from tab_haoban_staff_clerk_relation  where staff_id =#{staffId} and status_flag = 0 and update_time >= #{delTime}
    </select>

    <select id="taskoverClerkIdListAll" resultMap="BaseResultMap" resultType="java.lang.String">
        select  <include refid="Base_Column_List"/>  from tab_haoban_staff_clerk_relation  where staff_id =#{staffId} order by update_time desc
    </select>

    <select id="listAllStaffIdForOpenStaff" resultType="java.lang.String">
        select a.staff_id
        from tab_haoban_staff_clerk_relation a left join tab_haoban_staff b on b.staff_id = a.staff_id
        where a.enterprise_id = #{enterpriseId} and a.status_flag=1 and b.status_flag = 1 and a.wx_enterprise_id = #{wxEnterpriseId}
        <if test="null != clerkId and clerkId != '' ">
            and a.clerk_id = #{clerkId}
        </if>
        <if test="null != storeIdList">
            AND a.store_id in
            <foreach collection="storeIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY  a.staff_id
    </select>

</mapper>