<?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.GroupChatMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChat"
		id="result-map-tabHaobanGroupChat">
		<result column="group_chat_id" property="groupChatId" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="name" property="name" />
		<result column="staff_id" property="staffId" />
		<result column="wx_chat_id" property="wxChatId" />
		<result column="chat_add_time" property="chatAddTime" />
		<result column="chat_dissolve_time" property="chatDissolveTime" />
		<result column="chat_notice" property="chatNotice" />
		<result column="chat_status" property="chatStatus" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="delete_flag" property="deleteFlag" />
		<result column="total_count" property="totalCount" />
		<result column="total_member_count" property="totalMemberCount" />
		<result column="add_member_count" property="addMemberCount" />
		<result column="add_count" property="addCount" />
		<result column="quit_count" property="quitCount" />
		<result column="quit_member_count" property="quitMemberCount" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="gic_flag" property="gicFlag" />
		<result column="init_flag" property="initFlag" />
		<result column="wx_chat_id_dk" property="wxChatIdDk" />
		<result column="off_time" property="offTime"/>
		<result column="original_staff_id" property="originalStaffId"/>
		<result column="chat_remark" property="chatRemark"/>
	</resultMap>
	<sql id="Base_Column_List">
		group_chat_id,
		wx_enterprise_id,
		name,
		staff_id,
		wx_chat_id,
		chat_add_time,
		chat_dissolve_time,
		chat_notice,
		chat_status,
		create_time,
		update_time,
		delete_flag,
		total_count,
		total_member_count,
		add_member_count,
		add_count,
		quit_count,
		quit_member_count,
		enterprise_id,
		gic_flag , init_flag , wx_chat_id_dk , off_time, original_staff_id , chat_remark
	</sql>
	<!-- ===================== 新增 ======================== -->
	<insert id="insert"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChat">
  <![CDATA[
  INSERT INTO tab_haoban_group_chat(
	  group_chat_id,
	  wx_enterprise_id,
	  name,
	  staff_id,
	  wx_chat_id,
	  wx_chat_id_dk,
	  chat_add_time,
	  chat_dissolve_time,
	  chat_notice,
	  chat_status,
	  create_time,
	  update_time,
	  delete_flag,
	  total_count,
	  total_member_count,
	  add_member_count,
	  add_count,
	  quit_count,
	  quit_member_count,
	  enterprise_id,
	  gic_flag , init_flag , original_staff_id 
  )VALUES(
	  #{groupChatId},
	  #{wxEnterpriseId},
	  #{name},
	  #{staffId},
	  #{wxChatId},
	  #{wxChatIdDk},
	  #{chatAddTime},
	  #{chatDissolveTime},
	  #{chatNotice},
	  #{chatStatus},
	  #{createTime},
	  #{updateTime},
	  #{deleteFlag},
	  #{totalCount},
	  #{totalMemberCount},
	  #{addMemberCount},
	  #{addCount},
	  #{quitCount},
	  #{quitMemberCount},
	  #{enterpriseId},
	  #{gicFlag} , #{initFlag} , #{originalStaffId} 
  )
  ]]>
	</insert>

	<!-- ==================更新 ========== -->
	<update id="update" parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChat">
	  <![CDATA[
	  UPDATE tab_haoban_group_chat SET
	  	  enterprise_id = #{enterpriseId},
		  group_chat_id=#{groupChatId},
		  name=#{name},
		  staff_id=#{staffId},
		  chat_add_time=#{chatAddTime},
		  chat_dissolve_time=#{chatDissolveTime},
		  chat_notice=#{chatNotice},
		  chat_status=#{chatStatus},
		  update_time=now(),
		  gic_flag=#{gicFlag},
		  original_staff_id = #{originalStaffId} ,
		  init_flag = 1 ,
		  chat_remark = null 
	  where group_chat_id = #{groupChatId}
  ]]>
	</update>
	
	<update id="updateChatErrorRemark">
	  update tab_haoban_group_chat set 
		  chat_remark = #{chatRemark} , init_flag = 2 , update_time = now() , staff_id = null
	    where group_chat_id = #{groupChatId} 
	</update>
	
	<update id="updateData">
		update tab_haoban_group_chat set 
		  total_count = #{totalCount} ,
		  total_member_count  = #{totalMemberCount},
		  add_member_count  = #{addMemberCount} + add_member_count ,
		  add_count  = #{addCount} + add_count,
		  quit_count  = #{quitCount} + quit_count,
		  quit_member_count  = #{quitMemberCount} + quit_member_count
	    where group_chat_id = #{groupChatId} 
	</update>
	
	<update id="addMemberCount">
		update tab_haoban_group_chat set 
		  total_member_count  = 1 + total_member_count,
		  add_member_count  = 1 + add_member_count
	    where group_chat_id = #{groupChatId} 
	</update>
	
	<update id="updateWxChatIdDk">
		update tab_haoban_group_chat set wx_chat_id_dk=#{wxChatIdDk} where staff_id = #{staffId} and chat_add_time=#{chatAddTime} and  name=#{name} and wx_chat_id_dk is null 
	</update>
	
	<update id="updateChatStatus">
		update tab_haoban_group_chat set chat_status = #{chatStatus} 
		<if test="chatStatus==4">
			 , chat_dissolve_time = now() 
		</if>
		  , update_time =now()  where group_chat_id = #{groupChatId}
	</update>
	
	<update id="chatOff">
		update tab_haoban_group_chat set  off_time = now() , update_time =now()  where group_chat_id = #{groupChatId}
	</update>

	<!-- ============ 查询============= -->
	<select id="selectById" parameterType="long"
		resultMap="result-map-tabHaobanGroupChat">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE group_chat_id = #{groupChatId} and
		delete_flag = 0
	</select>

	<select id="listByIdSet" resultMap="result-map-tabHaobanGroupChat">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE group_chat_id in
		<foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
		and delete_flag = 0
	</select>

	<select id="selectByWxChatId" resultMap="result-map-tabHaobanGroupChat">
		select
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE wx_chat_id = #{wxChatId} 
		and wx_enterprise_id = #{wxEnterpriseId}
		and delete_flag = 0
	</select>
	
	<select id="selectByWxChatIdDk" resultMap="result-map-tabHaobanGroupChat">
		select
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE wx_chat_id_dk = #{wxChatIdDk} 
		and wx_enterprise_id = #{wxEnterpriseId}
		and delete_flag = 0
	</select>
	
	<select id="listByWxWxChatIdList" resultMap="result-map-tabHaobanGroupChat">
		select
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE wx_chat_id in 
		<foreach collection="wxChatIdList" close=")" index="index" item="wxChatId" open="(" separator=",">
			#{wxChatId} 
		</foreach>
		and wx_enterprise_id = #{wxEnterpriseId}
		and delete_flag = 0
	</select>
	
	<select id="listByWxWxChatIdListDk" resultMap="result-map-tabHaobanGroupChat">
		select
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat WHERE wx_chat_id_dk in 
		<foreach collection="wxChatIdDkList" close=")" index="index" item="wxChatId" open="(" separator=",">
			#{wxChatId} 
		</foreach>
		and wx_enterprise_id = #{wxEnterpriseId}
		and delete_flag = 0
	</select>
	
	
	<select id="getCountByStaffId" resultType="int">
		select count(*) from tab_haoban_group_chat where staff_id = #{staffId} and delete_flag = 0
	</select>

	<select id="listAllNeedInit" resultMap="result-map-tabHaobanGroupChat">
		select <include refid="Base_Column_List" /> from tab_haoban_group_chat where update_time >  DATE_ADD(NOW(),INTERVAL -1 day) and init_flag = 0
	</select>
	
	<select id="listAllNeedInitWxChatIdDk" resultMap="result-map-tabHaobanGroupChat">
		SELECT a.wx_enterprise_id wxEnterpriseId , a.staff_id staffId FROM tab_haoban_group_chat a LEFT JOIN tab_haoban_wx_enterprise b ON a.wx_enterprise_id=b.wx_enterprise_id 
 			WHERE a.update_time >  DATE_ADD(NOW(),INTERVAL -7 DAY) AND a.wx_chat_id_dk IS NULL AND a.init_flag = 1 AND a.chat_status != 4  AND a.name <![CDATA[ <> ]]>'' AND b.wx_security_type > 0		
		GROUP BY a.wx_enterprise_id , a.staff_id
	</select>
	
	<select id="list" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatDTO" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatSearchQDTO">
		select 
		a.chat_remark chatRemark ,
		a.init_flag initFlag , 
		b.staff_name staffName ,
		b.head_img   staffImage ,
		b.open_concat_flag openConcatFlag , 
		a.group_chat_id groupChatId ,
		a.name name ,
		a.staff_id staffId ,
		a.wx_chat_id wxChatId ,
		a.chat_add_time chatAddTime ,
		a.chat_dissolve_time chatDissolveTime ,
		a.chat_notice chatNotice ,
		a.chat_status chatStatus ,
		a.create_time createTime ,
		a.total_count totalCount ,
		a.total_member_count totalMemberCount ,
		a.add_member_count addMemberCount ,
		a.add_count addCount ,
		a.quit_count quitCount ,
		a.quit_member_count quitMemberCount ,
		a.enterprise_id enterpriseId ,
		a.gic_flag gicFlag , a.init_flag initFlag , a.wx_chat_id_dk wxChatIdDk , a.off_time offTime , 
		a.original_staff_id  originalStaffId
		 from tab_haoban_group_chat a left join tab_haoban_staff b on a.staff_id = b.staff_id 
		  <if test="departmentIdList != null and departmentIdList.size() > 0">
            left join tab_haoban_staff_department_related depart  on a.staff_id = depart.staff_id and depart.status_flag = 1
     	</if>
		 where a.wx_enterprise_id=#{wxEnterpriseId}
		<if test="null != gicFlag and gicFlag ==0 ">
			and a.enterprise_id is null
		</if>
		<if test="null == gicFlag or gicFlag ==1 ">
			and a.enterprise_id = #{enterpriseId}
		</if>
		<if test="null != staffId">
			and a.staff_id = #{staffId}
		</if>
		<if test="null == status or  0==status">
			<if test="null != chatHmId">
				and a.chat_status in (0,1,2,3,4)
			</if>
			<if test="null == chatHmId">
				and a.chat_status in (0,3) 
			</if>
		</if>
		<if test="null != searchPage and '' != searchPage ">
			<if test="searchPage == 'chatWidget' ">
				and a.init_flag = 1 and a.wx_chat_id_dk is not null
			</if>
		</if>
		<if test="null != status and 1==status">
			and a.chat_status = 1
		</if>
		<if test="null != status and 2==status">
			and a.chat_status = 2
		</if>
		<if test="null != status and 4==status">
			and a.chat_status = 4
		</if>
		<if test="null != hmAddStatus and 1==hmAddStatus">
			and a.total_count <![CDATA[ < ]]> 200
		</if>
		<if test="null != hmAddStatus and 2==hmAddStatus">
			and a.total_count >= 200
		</if>
		
		<if test="null != searchParams and ''!=searchParams ">
			and (a.name like #{searchParams} or a.wx_chat_id like #{wxChatId} )
		</if>
		<if test="null != staffIdList and staffIdList.size>0">
			and a.staff_id in 
			<foreach collection="staffIdList" close=")" index="index" item="id" open="(" separator=",">
				#{id}
			</foreach>
		</if>
		<if test="null != startDate">
			and a.chat_add_time <![CDATA[ >=]]>   #{startDate} and a.chat_add_time  <![CDATA[ <= ]]>  #{endDate}
		</if>
		<if test="null != dismissStartDate">
			and a.chat_dissolve_time <![CDATA[ >=]]>   #{startDate} and a.chat_dissolve_time  <![CDATA[ <= ]]>  #{endDate}	
		</if>
		<if test="null != groupChatIdList and groupChatIdList.size>0">
			and a.group_chat_id in 
			<foreach collection="groupChatIdList" close=")" open="(" index="index" item="groupChatId" separator=",">
				#{groupChatId}
			</foreach>
		</if>
		<if test="null !=canUseFlag and 1==canUseFlag">
			and b.open_concat_flag = 1 and b.active_flag = 1   and b.status_flag = 1
		</if>
		<if test="departmentIdList != null and departmentIdList.size() > 0">
            and depart.department_id IN
            <foreach collection="departmentIdList" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
     </if>
		 and a.delete_flag = 0 
		 
		<if test="departmentIdList != null and departmentIdList.size() > 0">
  	 		group by a.staff_id 
     	</if>
		 <if test="null == sortColumn">
		 	 order by a.chat_add_time  desc 
		</if>
		<if test="null != sortColumn">
			 order by a.${sortColumn}  ${sortType} 	
		</if>
	</select>
	
	<update id="updateChatEnterpriseId">
		update tab_haoban_group_chat set enterprise_id = #{enterpriseId} , update_time=now() , gic_flag = 1 where wx_enterprise_id=#{wxEnterpriseId} 
		<if test="null != groupChatIdList and groupChatIdList.size > 0">
			and group_chat_id in
			<foreach collection="groupChatIdList" item="id" index="index" separator=","  open="(" close=")">
				#{id}
			</foreach>
		</if>
		<if test="force==0">
			and enterprise_id is null 
		</if>
		and delete_flag = 0
	</update>
	
	<select id="listChangeOwner" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatDTO">
		select wx_enterprise_id  wxEnterpriseId , staff_id staffId FROM tab_haoban_group_chat WHERE update_time > DATE_ADD(NOW(),INTERVAL -7  DAY) AND chat_status = 1 	and staff_id is not null
	</select>
	
</mapper> 