<?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.chat.GroupChatUserMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatUser"
		id="result-map-tabHaobanGroupChatUser">
		<result column="chat_user_id" property="chatUserId" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="user_type" property="userType" />
		<result column="join_time" property="joinTime" />
		<result column="user_name" property="userName" />
		<result column="nick_name" property="nickName" />
		<result column="user_id" property="userId" />
		<result column="unionid" property="unionid" />
		<result column="member_id" property="memberId" />
		<result column="invitor_user_id" property="invitorUserId" />
		<result column="invitor_user_name" property="invitorUserName"/>
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="delete_flag" property="deleteFlag" />
		<result column="group_chat_id" property="groupChatId" />
		<result column="status_flag" property="statusFlag" />
		<result column="user_quit_time" property="userQuitTime" />
		<result column="quit_scene" property="quitScene" />
		<result column="join_scene" property="joinScene" />
		<result column="join_remark" property="joinRemark" />
		<result column="join_state" property="joinState" />
		<result column="admin_flag" property="adminFlag"/>
		<result column="quit_staff_id" property="quitStaffId"/>
		<result column="quit_staff_store" property="quitStaffStore" />
	</resultMap>
	<sql id="Base_Column_List">
		chat_user_id,
		wx_enterprise_id,
		enterprise_id,
		user_type,
		join_time,
		user_name,
		nick_name,
		user_id,
		unionid,
		member_id,
		invitor_user_id,
		create_time,
		update_time,
		delete_flag,
		group_chat_id,
		status_flag,
		user_quit_time,
		quit_scene,
		join_scene,
		join_remark,
		join_state , invitor_user_name , admin_flag , quit_staff_id , quit_staff_store
	</sql>
	<!-- ===================== 新增 ======================== -->
	<insert id="insert"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatUser">
  <![CDATA[
  INSERT INTO tab_haoban_group_chat_user(
	  chat_user_id,
	  wx_enterprise_id,
	  enterprise_id,
	  user_type,
	  join_time,
	  user_name,
	  nick_name,
	  user_id,
	  unionid,
	  member_id,
	  invitor_user_id,
	  create_time,
	  update_time,
	  delete_flag,
	  group_chat_id,
	  status_flag,
	  user_quit_time,
	  quit_scene,
	  join_scene,
	  join_remark,
	  join_state , invitor_user_name , admin_flag
  )VALUES(
	  #{chatUserId},
	  #{wxEnterpriseId},
	  #{enterpriseId},
	  #{userType},
	  #{joinTime},
	  #{userName},
	  #{nickName},
	  #{userId},
	  #{unionid},
	  #{memberId},
	  #{invitorUserId},
	  #{createTime},
	  #{updateTime},
	  #{deleteFlag},
	  #{groupChatId},
	  #{statusFlag},
	  #{userQuitTime},
	  #{quitScene},
	  #{joinScene},
	  #{joinRemark},
	  #{joinState} , #{invitorUserName} , #{adminFlag}
  )
  ]]>
	</insert>
	
	<insert id="batchInsert">
		 INSERT INTO tab_haoban_group_chat_user(
			  chat_user_id,
			  wx_enterprise_id,
			  enterprise_id,
			  user_type,
			  join_time,
			  user_name,
			  nick_name,
			  user_id,
			  unionid,
			  member_id,
			  invitor_user_id,
			  create_time,
			  update_time,
			  delete_flag,
			  group_chat_id,
			  status_flag,
			  user_quit_time,
			  quit_scene,
			  join_scene,
			  join_remark,
			  join_state , invitor_user_name , admin_flag
		)VALUES
		<foreach collection="list" item="item" separator=","  >
			(  #{item.chatUserId},
				  #{item.wxEnterpriseId},
				  #{item.enterpriseId},
				  #{item.userType},
				  #{item.joinTime},
				  #{item.userName},
				  #{item.nickName},
				  #{item.userId},
				  #{item.unionid},
				  #{item.memberId},
				  #{item.invitorUserId},
				  #{item.createTime},
				  #{item.updateTime},
				  #{item.deleteFlag},
				  #{item.groupChatId},
				  #{item.statusFlag},
				  #{item.userQuitTime},
				  #{item.quitScene},
				  #{item.joinScene},
				  #{item.joinRemark},
				  #{item.joinState} , #{item.invitorUserName} , #{item.adminFlag}
			)
		</foreach>
	</insert>
	


	<!-- =====================废弃群==================== -->
	<update id="dismiss">
		UPDATE tab_haoban_group_chat_user SET status_flag = 2 , update_time=now() , user_quit_time = now() , quit_scene = 0 , quit_staff_id = #{ownerId} ,
			quit_staff_store = #{quitStaffStore}
		                  WHERE chat_user_id in
		<foreach collection="idList" close=")" open="(" index="index" item="id" separator=",">
			#{id}
		</foreach>
	</update>


	<!-- ==================更新 ========== -->
	<update id="update"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatUser">
	  <![CDATA[
	  UPDATE tab_haoban_group_chat_user SET
		  user_type=#{userType},
		  join_time=#{joinTime},
		  user_name=#{userName},
		  nick_name=#{nickName},
		  user_id=#{userId},
		  unionid=#{unionid},
		  member_id=#{memberId},
		  invitor_user_id=#{invitorUserId},
		  update_time=now(),
		  group_chat_id=#{groupChatId},
		  join_scene=#{joinScene},
		  join_remark=#{joinRemark},
		  join_state=#{joinState} , 
		  admin_flag = #{adminFlag}
	  where chat_user_id = #{chatUserId}
	  ]]>
	</update>

	<!-- ============ 查询============= -->
	<select id="selectById" parameterType="long" resultMap="result-map-tabHaobanGroupChatUser">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat_user WHERE chat_user_id = #{chatUserId} and delete_flag = 0
	</select>

	<select id="selectByUserIdAndChatId" resultMap="result-map-tabHaobanGroupChatUser">
		select
		<include refid="Base_Column_List" />
		from tab_haoban_group_chat_user where group_chat_id = #{groupChatId}
		and user_id =#{userId} and delete_flag = 0
	</select>

	<select id="listAllChatUser" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List" /> from tab_haoban_group_chat_user where group_chat_id = #{groupChatId} and wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1  and delete_flag = 0
	</select>

	<select id="listByUserId" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List" /> from tab_haoban_group_chat_user
		where enterprise_id = #{enterpriseId} and user_id = #{userId}
		<if test="statusFlag != null ">
			and status_flag = #{statusFlag}
		</if>
		and delete_flag = 0
		order by join_time asc,chat_user_id asc
	</select>

	<select id="listByExternalUseridForMatch" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List" /> from tab_haoban_group_chat_user where user_id = #{externalUserid}
		and wx_enterprise_id = #{wxEnterpriseId}  and enterprise_id = #{enterpriseId}
		and user_type != 3
		and status_flag = 1  and delete_flag = 0
	</select>
	
	
	<update id="updateToGicMember">
		update tab_haoban_group_chat_user set user_type = 3 , member_id = #{memberId} , update_time =now()  where user_id = #{externalUserid}
		and wx_enterprise_id = #{wxEnterpriseId}  and enterprise_id = #{enterpriseId} 
		and user_type != 3
		and status_flag = 1  and delete_flag = 0 
	</update>
	
	
	
	<select id="listPage" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatUserSearchQDTO" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatUserDTO">
		select 
			b.name chatName ,
			b.wx_chat_id wxChatId ,
			c.staff_id staffId ,
			c.staff_name staffName ,
			a.chat_user_id chatUserId ,
			a.user_type userType ,
			a.join_time joinTime ,
			a.user_name userName ,
			a.nick_name nickName ,
			a.member_id memberId ,
			a.invitor_user_id invitorUserId,
			a.invitor_user_name invitorUserName ,
			a.user_quit_time userQuitTime,
			a.quit_staff_store quitStaffStore ,
			a.join_scene joinScene,
			a.join_state joinState 
			from tab_haoban_group_chat_user a
			left join tab_haoban_group_chat b on a.group_chat_id = b.group_chat_id 
			<choose>
				<when test="status==2">
					left join tab_haoban_staff c on a.quit_staff_id = c.staff_id
				</when>
				<otherwise>
					left join tab_haoban_staff c on b.staff_id = c.staff_id 
				</otherwise>
			</choose>
			where a.wx_enterprise_id = #{wxEnterpriseId}  and a.enterprise_id = #{enterpriseId}	 and a.delete_flag = 0
			<if test="null != groupChatId">
				and a.group_chat_id = #{groupChatId} 
			</if>
	    <!-- 正常 -->
		<if test="status==1">
			 and a.status_flag = 1  
			 <if test="null != startDate">
				and a.join_time <![CDATA[ >=]]>   #{startDate} and a.join_time  <![CDATA[ <= ]]>  #{endDate}
			</if>
		</if>
		<!-- 退群记录 -->
		<if test="status == 2">
			 and  a.status_flag = 2  
			<if test="null != startDate">
				and a.user_quit_time <![CDATA[>=]]> #{startDate} and a.user_quit_time <![CDATA[<=]]> #{endDate}
			</if>
			<if test="null != searchParams and '' != searchParams">
				and (b.name like #{searchParams} or c.staff_name like #{searchParams} or b.wx_chat_id like #{wxChatId} )
			</if>
			<if test="staffIdList != null and staffIdList.size() != 0">
				and c.staff_id  in
				<foreach collection="staffIdList" item="item" separator="," open="(" close=")" >
					#{item}
				</foreach>
			</if>
		</if>
		<if test="null != userType">
			and a.user_type = #{userType}
		</if>
		<choose>
			<when test="status==2">
				order by a.user_quit_time desc , a.chat_user_id desc
			</when>
			<otherwise>
				order by a.join_time desc , a.chat_user_id  desc
			</otherwise>
		</choose>
	</select>

	<select id="queryGroupChatUserList" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List"/>
		    from tab_haoban_group_chat_user
		<where>
			status_flag = 1  and delete_flag = 0
		    <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
				and wx_enterprise_id = #{wxEnterpriseId}
			</if>
			<if test="enterpriseId != null and enterpriseId != ''">
				and enterprise_id = #{enterpriseId}
			</if>
			<if test="chatId != null and chatId != ''">
				and group_chat_id = #{chatId}
			</if>
		</where>
	</select>

	<select id="listQuitNum" resultType="com.gic.haoban.manage.service.pojo.bo.chat.UserQuitBO">
		select quit_staff_id staffId , count(*) num from tab_haoban_group_chat_user where wx_enterprise_id=#{wxEnterpriseId} and user_quit_time >= #{start}
		and user_quit_time <![CDATA[<=]]> #{end} and status_flag = 2 group by quit_staff_id
	</select>

    <select id="getGroupUserInfo" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List"/> from tab_haoban_group_chat_user where group_chat_id = #{groupChatId} and user_id = #{userId}
			and delete_flag = 0 and status_flag = 1 limit 1
	</select>


	<select id="getByMember" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List"/> from tab_haoban_group_chat_user where enterprise_id = #{enterpriseId}
		and member_id = #{memberId} and status_flag = 1  and delete_flag = 0 order by create_time desc
	</select>

	<select id="isAddGroupChat" resultType="int">
		select count(*) from tab_haoban_group_chat_user where enterprise_id = #{enterpriseId}
		and member_id = #{memberId} and status_flag = 1  and delete_flag = 0
	</select>

    <select id="listMemberChatHmIdByChatHmIdsAndMemberId"
            resultType="com.gic.haoban.manage.service.pojo.bo.chat.GroupChatHmMemberRelationBO">
        select chat_hm_id chatHmId, member_id memberId
        from tab_haoban_group_chat_hm_relation t1
        inner join tab_haoban_group_chat_user t2 on t1.group_chat_id = t2.group_chat_id
        where chat_hm_id in
        <foreach collection="chatHmIds" open="(" close=")" index="index" item="chatHmId" separator=",">
            #{chatHmId}
        </foreach>
        and t2.enterprise_id = #{enterpriseId}
        and member_id = #{memberId}
        and t2.delete_flag = 0
        and t1.delete_flag = 0
        and t1.status_flag = 1
        and t2.status_flag = 1
    </select>

	<select id="getByUnionId" resultMap="result-map-tabHaobanGroupChatUser">
		select <include refid="Base_Column_List"/> from tab_haoban_group_chat_user where enterprise_id = #{enterpriseId}
		and unionid = #{unionid} and status_flag = 1  and delete_flag = 0 order by create_time desc
	</select>

	<select id="listByUnionIdAndChatHmId" resultType="java.lang.Long">
		select distinct chr.chat_hm_id
		from tab_haoban_group_chat_hm_relation chr left join
		tab_haoban_group_chat_user cu on chr.group_chat_id = cu.group_chat_id
		where chr.enterprise_id = #{enterpriseId} and chr.delete_flag = 0 and chr.status_flag = 1
		and chr.chat_hm_id in
		<foreach collection="chatHmIdList" close=")" open="(" index="index" item="item" separator=",">
			#{item}
		</foreach>
		and cu.unionid = #{unionid} and cu.status_flag = 1  and cu.delete_flag = 0
	</select>

	<select id="getMemberJoinChatGroupCountByHmName" resultType="java.lang.Integer">
		select count(t2.group_chat_id)
		from tab_haoban_group_chat_hm t1
				 inner join tab_haoban_group_chat_hm_relation t2 on t1.chat_hm_id = t2.chat_hm_id
				 inner join tab_haoban_group_chat_user t3 on t2.group_chat_id = t3.group_chat_id
		where t2.status_flag = 1
		  and t2.delete_flag = 0
		  and t3.delete_flag = 0
		  and t3.status_flag = 1
		  and t1.status_flag = 1
		  and t1.delete_flag = 0
		  and t3.member_id is not null
		  and t3.enterprise_id = #{enterpriseId}
		  and t3.member_id  = #{memberId}
		  and t1.name like CONCAT('%',#{hmName},'%')
	</select>


</mapper> 