<?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.GroupChatOwnerMapper">
  <resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatOwner" id="result-map-tabHaobanGroupChatOwner">
      <result column="staff_id"  property="staffId"/>
      <result column="wx_enterprise_id"  property="wxEnterpriseId"/>
      <result column="chat_count"  property="chatCount"/>
      <result column="create_time"  property="createTime"/>
      <result column="update_time"  property="updateTime"/>
  </resultMap>
  <sql id="Base_Column_List">
	  staff_id,
	  wx_enterprise_id,
	  chat_count,
	  create_time,
	  update_time
  </sql>
  <!-- ===================== 新增 ======================== -->
  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatOwner">
  <![CDATA[
  INSERT INTO tab_haoban_group_chat_owner(
	  staff_id,
	  wx_enterprise_id,
	  chat_count,
	  create_time,
	  update_time
  )VALUES(
	  #{staffId},
	  #{wxEnterpriseId},
	  #{chatCount},
	  now(),
	  now()
  )
  ]]>
  </insert>
  
  
  <select id="selectByStaffId" resultMap="result-map-tabHaobanGroupChatOwner">
  	select <include refid="Base_Column_List"/> from tab_haoban_group_chat_owner where staff_id =#{staffId}
  </select>
  
   <!-- ==================更新 ========== -->
  <update id="updateChatCount">
  <![CDATA[
  UPDATE tab_haoban_group_chat_owner SET
	  chat_count=#{chatCount},
	  update_time=now()
   where staff_id = #{staffId}
  ]]>
  </update>
  
  
  <select id="listOwner"  parameterType="com.gic.haoban.manage.api.dto.qdto.chat.OwnerSearchQDTO" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatOwnerDTO">
  	 SELECT a.staff_id staffId  , b.`staff_name` staffName , a.wx_enterprise_id wxEnterpriseId ,
  	 b.wx_user_id wxUserId , b.wx_open_user_id wxOpenUserId 
  	 FROM tab_haoban_group_chat_owner 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 c on a.staff_id = c.staff_id and c.status_flag = 1
     </if>
  	 where a.wx_enterprise_id=#{wxEnterpriseId} 
     <if test="departmentIdList != null and departmentIdList.size() > 0">
            and c.department_id IN
            <foreach collection="departmentIdList" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
     </if>
  	 <if test="null != searchParams and ''!=searchParams ">
  	 	and b.staff_name like #{searchParams}
  	 </if>
  	 <if test="status==1">
  	 	 and b.status_flag = 1 
  	 </if>
  	 <if test="status==2">
  	 	 and b.status_flag = 0
  	 </if>
  	 <if test="departmentIdList != null and departmentIdList.size() > 0">
  	 	group by a.staff_id 
     </if>
  </select>
  
  <select id="listOwnerForStatistic" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatOwnerDTO">
  	SELECT a.staff_id staffId  , a.wx_enterprise_id wxEnterpriseId ,
  	 b.wx_user_id wxUserId , b.wx_open_user_id wxOpenUserId 
  	 FROM tab_haoban_group_chat_owner a LEFT JOIN tab_haoban_staff b ON a.`staff_id` = b.`staff_id` 
  	 where a.wx_enterprise_id=#{wxEnterpriseId} and b.status_flag = 1 limit ${startItem},${pageSize}
  </select>
  
  
   <select id="dimissionOwnerList"  resultType="com.gic.haoban.manage.api.dto.chat.GroupChatOwnerDTO">
  	 SELECT a.staff_id staffId  , b.`staff_name` staffName , a.wx_enterprise_id wxEnterpriseId ,
  	 b.wx_user_id wxUserId , b.wx_open_user_id wxOpenUserId 
  	 FROM tab_haoban_group_chat_owner a LEFT JOIN tab_haoban_staff b ON a.`staff_id` = b.`staff_id` 
  	 where  a.update_time >  DATE_ADD(CURDATE(), INTERVAL -1 DAY) and b.status_flag = 0  
  </select>
  
</mapper> 