<?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.hm.HmLinkChatMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.hm.TabHmLinkChat"
		id="result-map-tabHaobanHmLinkChat">
		<result column="link_chat_id" property="linkChatId" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="filter_json" property="filterJson" />
		<result column="chat_hm_id" property="chatHmId" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="member_type" property="memberType" />
		<result column="sort_num" property="sortNum" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="delete_flag" property="deleteFlag" />
		<result column="link_id" property="linkId" />
		<result column="other_flag" property="otherFlag"/>
	</resultMap>
	<sql id="Base_Column_List">
		link_chat_id,
		wx_enterprise_id,
		filter_json,
		chat_hm_id,
		enterprise_id,
		member_type,
		sort_num,
		create_time,
		update_time,
		delete_flag , link_id , other_flag
	</sql>
	<!-- ===================== 新增 ======================== -->
	<insert id="insert"
		parameterType="com.gic.haoban.manage.service.entity.hm.TabHmLinkChat">
		  <![CDATA[
		  INSERT INTO tab_haoban_hm_link_chat(
			  link_chat_id,
			  wx_enterprise_id,
			  filter_json,
			  chat_hm_id,
			  enterprise_id,
			  member_type,
			  sort_num,
			  create_time,
			  update_time,
			  delete_flag , link_id , other_flag 
		  )VALUES(
			  #{linkChatId},
			  #{wxEnterpriseId},
			  #{filterJson},
			  #{chatHmId},
			  #{enterpriseId},
			  #{memberType},
			  #{sortNum},
			  now(),
			  now(),
			  0 , 
			  #{linkId} , #{otherFlag}
		  )
		  ]]>
	</insert>

	<!-- =====================删除==================== -->
	<update id="delete">
		UPDATE tab_haoban_hm_link_chat SET delete_flag = 1 , update_time =now()  WHERE link_id = #{linkId}
		<if test="null != notDelIdList and notDelIdList.size > 0">
			and link_chat_id not in
			<foreach collection="notDelIdList" item="item" separator="," index="index"
				close=")" open="(">
				#{item}
			</foreach>
		</if>
		<if test="null !=otherFlag">
			and other_flag = #{otherFlag}
		</if>
	</update>

	<!-- ==================更新 ========== -->
	<update id="update"
		parameterType="com.gic.haoban.manage.service.entity.hm.TabHmLinkChat">
	  <![CDATA[
	  UPDATE tab_haoban_hm_link_chat SET
		  filter_json=#{filterJson},
		  chat_hm_id=#{chatHmId},
		  member_type=#{memberType},
		  sort_num=#{sortNum},
		  update_time=now()
	  where link_chat_id = #{linkChatId}
	  ]]>
	</update>

	<select id="listByLinkId" resultType="com.gic.haoban.manage.api.dto.hm.HmLinkChatDTO">
		select
		link_chat_id linkChatId ,
		filter_json filterJson ,
		chat_hm_id chatHmId,
		member_type memberType 
		from  tab_haoban_hm_link_chat where link_id = #{linkId} and wx_enterprise_id = #{wxEnterpriseId}  and enterprise_id = #{enterpriseId} and other_flag = 0
		and delete_flag = 0
	</select>
	
	
	<select id="getLinkCountForHm" resultType="com.gic.haoban.manage.api.dto.hm.HmLinkChatDTO">
		select chat_hm_id chatHmId, COUNT(DISTINCT(link_id)) linkCount  from tab_haoban_hm_link_chat where wx_enterprise_id = #{wxEnterpriseId} 
			and chat_hm_id in
			<foreach collection="chatHmIdList" item="item" separator="," index="index"	close=")" open="(">
				#{item}
			</foreach>
			and delete_flag = 0 group by chat_hm_id 
	</select>
	
	
	<select id="listPage" parameterType="com.gic.haoban.manage.api.dto.qdto.hm.HmLinkChatQDTO" resultType="com.gic.haoban.manage.api.dto.hm.HmLinkChatDTO">
		SELECT  a.link_id linkId , b.`name` name , b.`link_code` linkCode , b.`short_code` shortCode  FROM tab_haoban_hm_link_chat a LEFT JOIN tab_haoban_hm_link b ON a.link_id = b.link_id 
		WHERE  a.wx_enterprise_id=#{wxEnterpriseId} and a.enterprise_id=#{enterpriseId} and a.chat_hm_id = #{chatHmId} and a.delete_flag = 0 
		GROUP BY a.`link_id`
	</select>
	
</mapper> 