<?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.GroupChatDataMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatData"
		id="result-map-tabHaobanGroupChatData">
		<result column="data_id" property="dataId" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="staff_id" property="staffId" />
		<result column="date" property="date" />
		<result column="new_chat_cnt" property="newChatCnt" />
		<result column="chat_total" property="chatTotal" />
		<result column="chat_has_msg" property="chatHasMsg" />
		<result column="new_member_cnt" property="newMemberCnt" />
		<result column="member_total" property="memberTotal" />
		<result column="member_has_msg" property="memberHasMsg" />
		<result column="msg_total" property="msgTotal" />
		<result column="migrate_trainee_chat_cnt" property="migrateTraineeChatCnt" />
		<result column="create_time" property="createTime" />
		<result column="quit_user_count" property="quitUserCount" />
	</resultMap>
	<sql id="Base_Column_List">
		data_id,
		wx_enterprise_id,
		staff_id,
		date,
		new_chat_cnt,
		chat_total,
		chat_has_msg,
		new_member_cnt,
		member_total,
		member_has_msg,
		msg_total,
		migrate_trainee_chat_cnt,
		create_time , quit_user_count
	</sql>
	
	<insert id="batchInsert">
		 INSERT INTO tab_haoban_group_chat_data(
			  data_id,
			  wx_enterprise_id,
			  staff_id,
			  date,
			  new_chat_cnt,
			  chat_total,
			  chat_has_msg,
			  new_member_cnt,
			  member_total,
			  member_has_msg,
			  msg_total,
			  migrate_trainee_chat_cnt,
			  create_time , quit_user_count
		)VALUES
		<foreach collection="list" item="item" separator=","  >
			(     #{item.dataId},
				  #{item.wxEnterpriseId},
				  #{item.staffId},
				  #{item.date},
				  #{item.newChatCnt},
				  #{item.chatTotal},
				  #{item.chatHasMsg},
				  #{item.newMemberCnt},
				  #{item.memberTotal},
				  #{item.memberHasMsg},
				  #{item.msgTotal},
				  #{item.migrateTraineeChatCnt},
				  now() , 0
			)
		</foreach>
	</insert>
	<update id="updateQuitCount">
		update tab_haoban_group_chat_data set quit_user_count = #{quitCount} where wx_enterprise_id=#{wxEnterpriseId}
			and date = #{date} and staff_id = #{staffId}
	</update>

	<select id="listTotalData" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.ChatDataSearchQDTO"
		resultType="com.gic.haoban.manage.api.dto.chat.GroupChatDataDTO">
		select  sum(chatTotal) chatTotal ,  sum(memberTotal) memberTotal,
		  SUM(newChatCnt) newChatCnt,  SUM(chatHasMsg) chatHasMsg,  SUM(newMemberCnt) newMemberCnt,
		  SUM(memberHasMsg) memberHasMsg,  SUM(msgTotal)  msgTotal , sum(quitUserCount) quitUserCount
		  from (
		SELECT 
		  staffId,  `date`,  chatTotal,  memberTotal,
		  SUM(newChatCnt) newChatCnt,  SUM(chatHasMsg) chatHasMsg,  SUM(newMemberCnt) newMemberCnt,
		  SUM(memberHasMsg) memberHasMsg,  SUM(msgTotal)  msgTotal , sum(quitUserCount) quitUserCount
		FROM
		  (SELECT 
		      a.staff_id staffId,   a.date `date`,
		    a.chat_total chatTotal,    a.member_total memberTotal,
		    a.new_chat_cnt newChatCnt,    a.chat_has_msg chatHasMsg,   a.new_member_cnt newMemberCnt,
		    a.member_has_msg memberHasMsg,    a.msg_total msgTotal , a.quit_user_count quitUserCount
		  FROM    tab_haoban_group_chat_data a  
		where a.wx_enterprise_id = #{wxEnterpriseId}
		and a.date >= #{startDate} and a.date <![CDATA[ <=  ]]> #{endDate}
		<if test="null != staffIdList and staffIdList.size > 0">
			and a.staff_id in 
			<foreach collection="staffIdList" close=")" open="(" index="index" item="staffId" separator=",">
				#{staffId}
			</foreach>
		</if>
		 ORDER BY a.date DESC 
		)  t1 GROUP BY staffId ) t2
	</select>
	
	
	<select id="listForChat" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.ChatDataSearchQDTO"
		resultType="com.gic.haoban.manage.api.dto.chat.GroupChatDataDTO">
		select 
			a.date date ,
			sum(a.new_chat_cnt) newChatCnt ,
			sum(a.chat_has_msg)  chatHasMsg ,
			sum(a.new_member_cnt)  newMemberCnt ,
			sum(a.member_has_msg) memberHasMsg ,
			sum(a.msg_total) msgTotal  ,
			sum(a.quit_user_count) quitUserCount
		from tab_haoban_group_chat_data a 
		where a.wx_enterprise_id = #{wxEnterpriseId}
		and a.date >= #{startDate} and a.date <![CDATA[ <=  ]]> #{endDate}
		<if test="null != staffIdList and staffIdList.size > 0">
			and a.staff_id in 
			<foreach collection="staffIdList" close=")" open="(" index="index" item="staffId" separator=",">
				#{staffId}
			</foreach>
		</if>
		group by a.date 
	</select>

	<select id="listStaffData" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.ChatDataSearchQDTO"
		resultType="com.gic.haoban.manage.api.dto.chat.GroupChatDataDTO">
		SELECT 
		  staffId,  staffName,  `date`,  chatTotal,  memberTotal,
		  SUM(newChatCnt) newChatCnt,  SUM(chatHasMsg) chatHasMsg,  SUM(newMemberCnt) newMemberCnt,
		  SUM(memberHasMsg) memberHasMsg,  SUM(msgTotal)  msgTotal , sum(quitUserCount) quitUserCount
		FROM
		  (SELECT 
		    b.staff_name staffName,    a.staff_id staffId,   a.date `date`,
		    a.chat_total chatTotal,    a.member_total memberTotal,
		    a.new_chat_cnt newChatCnt,    a.chat_has_msg chatHasMsg,   a.new_member_cnt newMemberCnt,
		    a.member_has_msg memberHasMsg,    a.msg_total msgTotal  , a.quit_user_count quitUserCount
		  FROM    tab_haoban_group_chat_data a  LEFT JOIN tab_haoban_staff b     ON a.staff_id = b.staff_id 
		where a.wx_enterprise_id = #{wxEnterpriseId}
		and a.date >= #{startDate} and a.date <![CDATA[ <=  ]]> #{endDate}
		<if test="null != staffIdList and staffIdList.size > 0">
			and a.staff_id in 
			<foreach collection="staffIdList" close=")" open="(" index="index" item="staffId" separator=",">
				#{staffId}
			</foreach>
		</if>
		 ORDER BY a.date DESC 
		)  tb GROUP BY staffId ORDER BY ${sortColumn}  ${sortType}
	</select>


</mapper> 