<?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.GroupChatHmMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatHm"
		id="result-map-tabHaobanGroupChatHm">
		<result column="chat_hm_id" property="chatHmId" />
		<result column="chat_hm_code" property="chatHmCode" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="name" property="name" />
		<result column="remark" property="remark" />
		<result column="auto_create_room" property="autoCreateRoom" />
		<result column="room_base_id" property="roomBaseId" />
		<result column="room_base_name" property="roomBaseName" />
		<result column="wx_config_id" property="wxConfigId" />
		<result column="wx_qr_code" property="wxQrCode" />
		<result column="creator_id" property="creatorId" />
		<result column="creator_name" property="creatorName" />
		<result column="modifier_id" property="modifierId" />
		<result column="modifier_name" property="modifierName" />
		<result column="delete_flag" property="deleteFlag" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="chat_count" property="chatCount" />
		<result column="link_count" property="linkCount"/>
		<result column="status_flag" property="statusFlag"/>
		<result column="store_id" property="storeId" />
	</resultMap>
	<sql id="Base_Column_List">
		chat_hm_id,
		chat_hm_code,
		wx_enterprise_id,
		enterprise_id,
		name,
		remark,
		auto_create_room,
		room_base_id,
		room_base_name,
		wx_config_id,
		wx_qr_code,
		creator_id,
		creator_name,
		modifier_id,
		modifier_name,
		delete_flag,
		create_time,
		update_time,
		chat_count, link_count ,
		status_flag , store_id
	</sql>
	<!-- ===================== 新增 ======================== -->
	<insert id="insert"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatHm">
  <![CDATA[
  INSERT INTO tab_haoban_group_chat_hm(
	  chat_hm_id,
	  chat_hm_code,
	  wx_enterprise_id,
	  enterprise_id,
	  name,
	  remark,
	  auto_create_room,
	  room_base_id,
	  room_base_name,
	  wx_config_id,
	  wx_qr_code,
	  creator_id,
	  creator_name,
	  modifier_id,
	  modifier_name,
	  delete_flag,
	  create_time,
	  update_time,
	  chat_count,
	  status_flag , store_id
  )VALUES(
	  #{chatHmId},
	  #{chatHmCode},
	  #{wxEnterpriseId},
	  #{enterpriseId},
	  #{name},
	  #{remark},
	  #{autoCreateRoom},
	  #{roomBaseId},
	  #{roomBaseName},
	  #{wxConfigId},
	  #{wxQrCode},
	  #{creatorId},
	  #{creatorName},
	  #{modifierId},
	  #{modifierName},
	  0,
	  now(),
	  now(),
	  #{chatCount},
	   1 , #{storeId}
  )
  ]]>
	</insert>

	<!-- =====================删除==================== -->
	<update id="deleteById" parameterType="long">
		UPDATE
		tab_haoban_group_chat_hm SET delete_flag = 1 WHERE chat_hm_id = #{chatHmId}
	</update>


	<!-- ==================更新 ========== -->
	<update id="updateById"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatHm">
  <![CDATA[
  UPDATE tab_haoban_group_chat_hm SET
	  name=#{name},
	  remark=#{remark},
	  auto_create_room=#{autoCreateRoom},
	  room_base_id=#{roomBaseId},
	  room_base_name=#{roomBaseName},
	  modifier_id=#{modifierId},
	  modifier_name=#{modifierName},
	  update_time=now(),
	  chat_count=#{chatCount} , store_id = #{storeId}
  where chat_hm_id = #{chatHmId}
  ]]>
	</update>
	
	
	<update id="discard">
		update tab_haoban_group_chat_hm set status_flag = 2 , update_time = now() , modifier_id=#{clerkId} , modifier_name=#{clerkName} where chat_hm_id = #{chatHmId} and wx_enterprise_id = #{wxEnterpriseId}
	</update>

	<!-- ============ 查询============= -->
	<select id="selectById" parameterType="long"
		resultMap="result-map-tabHaobanGroupChatHm">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat_hm WHERE chat_hm_id = #{chatHmId} and  delete_flag = 0
	</select>
	
	<update id="updateLinkCount">
		update tab_haoban_group_chat_hm set link_count = #{linkCount} where chat_hm_id = #{chatHmId} 
	</update>
	
	<select id="list"  resultMap="result-map-tabHaobanGroupChatHm" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.ChatHmSearchQDTO">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat_hm WHERE	wx_enterprise_id =#{wxEnterpriseId} and enterprise_id=#{enterpriseId}
		and delete_flag = 0
		<if test="null != startDate">
			and create_time >= #{startDate} and create_time   <![CDATA[<=]]> #{endDate}
		</if>
		<if test="null != searchParams and '' !=searchParams ">
			and ( name like #{searchParams} or chat_hm_code like #{searchParamsCode} or creator_name like  #{searchParams} )
		</if>
		<if test="null != status">
			and status_flag = #{status}
		</if>
		<if test="null !=chatHmIdList and chatHmIdList.size>0">
			and chat_hm_id in 
			<foreach collection="chatHmIdList" close=")" index="index" item="id" open="(" separator=",">
				#{id}
			</foreach>
		</if>
		<if test="null != linkCount and linkCount == 0">
			and link_count = 0 
		</if>
		<if test="null != creatorIdList and creatorIdList.size > 0">
			and creator_id in 
			<foreach collection="creatorIdList" close=")" index="index" item="id" open="(" separator=",">
				#{id}
			</foreach>
		</if>
		<if test="null != storeId and ''!=storeId">
			and store_id = #{storeId}
		</if>
		order by create_time desc
	</select>
	<select id="listAvailableByEnterpriseId"
			resultMap="result-map-tabHaobanGroupChatHm">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat_hm
		WHERE enterprise_id=#{enterpriseId}
		and status_flag = 1
		and delete_flag = 0
		and name like '%骑行%'
		order by create_time desc
	</select>

</mapper> 