<?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.GroupChatHmRelationMapper">
	<resultMap
		type="com.gic.haoban.manage.service.entity.chat.TabGroupChatHmRelation"
		id="result-map-tabHaobanGroupChatHmRelation">
		<result column="relation_id" property="relationId" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="chat_hm_id" property="chatHmId" />
		<result column="group_chat_id" property="groupChatId" />
		<result column="delete_flag" property="deleteFlag" />
		<result column="status_flag" property="statusFlag" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
	</resultMap>
	<sql id="Base_Column_List">
		relation_id,
		wx_enterprise_id,
		enterprise_id,
		chat_hm_id,
		group_chat_id,
		delete_flag,
		status_flag,
		create_time,
		update_time
	</sql>
	<insert id="insertBatch">
		 INSERT INTO tab_haoban_group_chat_hm_relation(
		  relation_id,
		  wx_enterprise_id,
		  enterprise_id,
		  chat_hm_id,
		  group_chat_id,
		  delete_flag,
		  status_flag,
		  create_time,
		  update_time
		)VALUES
		<foreach collection="list" item="item" separator=","  >
			(  #{item.relationId},
				  #{item.wxEnterpriseId},
				  #{item.enterpriseId},
				  #{item.chatHmId},
				  #{item.groupChatId},
				  0,
				  1,
				  now(),
				  now()
			)
		</foreach>
	</insert>
	

	<!-- =====================删除==================== -->
	<delete id="delete">
		UPDATE tab_haoban_group_chat_hm_relation SET delete_flag = 1 ,  status_flag = #{statusFlag} , update_time=now() WHERE
		 chat_hm_id = #{chatHmId} and  group_chat_id in 
		 <foreach collection="groupChatIdList" item="id" close=")" index="index" open="(" separator=",">
		 	#{id}
		 </foreach>
	</delete>


	<!-- ==================更新 ========== -->
	<update id="updateStatus">
	  <![CDATA[
	  UPDATE tab_haoban_group_chat_hm_relation SET
		  status_flag=#{statusFlag},
		  update_time=now()
	  where chat_hm_id = #{chatHmId} and  group_chat_id= #{groupChatId}
	  ]]>
	</update>

	<select id="listByChatHmId" resultMap="result-map-tabHaobanGroupChatHmRelation">
		select
		<include refid="Base_Column_List" />
		from tab_haoban_group_chat_hm_relation  where chat_hm_id = #{chatHmId} and delete_flag = 0 and status_flag = 1
	</select>
	
	<select id="listByChatHmIdIncludeOff" resultMap="result-map-tabHaobanGroupChatHmRelation">
		select
		<include refid="Base_Column_List" />
		from tab_haoban_group_chat_hm_relation  where chat_hm_id = #{chatHmId} and ((delete_flag = 0 and status_flag = 1) or (status_flag = 2 and delete_flag = 1)) 
	</select>
	
	<select id="listChatHmId" resultType="long">
			select
				distinct(chat_hm_id)
		from tab_haoban_group_chat_hm_relation  where group_chat_id = #{groupChatId} and delete_flag = 0
	</select>

	<select id="listChatHmByGroupChatIds" resultMap="result-map-tabHaobanGroupChatHmRelation">
		select
		<include refid="Base_Column_List" />
		from tab_haoban_group_chat_hm_relation  where group_chat_id in
		<foreach collection="groupChatIds" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
		 and delete_flag = 0 and status_flag = 1
	</select>
	
	
	<select id="getHmValidChatCount" resultType="com.gic.haoban.manage.service.pojo.bo.chat.GroupChatHmRelationBO">
		SELECT chat_hm_id chatHmId, COUNT(1) usableChatCount FROM `tab_haoban_group_chat_hm_relation` relation
		 LEFT JOIN tab_haoban_group_chat chat ON relation.`group_chat_id` = chat.`group_chat_id` 
		 WHERE relation.chat_hm_id in
		 <foreach collection="hmList" close=")" index="idex" item="id" open="(" separator=",">
		 	#{id}
		 </foreach>
		 and relation.delete_flag = 0 and relation.status_flag = 1 and chat.delete_flag = 0 and chat.chat_status in (0,1,2,3)
		 and chat.total_count <![CDATA[<]]> 200 GROUP BY relation.`chat_hm_id` 
	</select>
</mapper> 