<?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.GroupChatPlanOwnerLogMapper">
  <resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlanOwnerLog" id="result-map-tabHaobanGroupChatPlanOwnerLog">
      <result column="owner_log_id"  property="ownerLogId"/>
      <result column="plan_id"  property="planId"/>
      <result column="wx_enterprise_id"  property="wxEnterpriseId"/>
      <result column="enterprise_id"  property="enterpriseId"/>
      <result column="staff_id"  property="staffId"/>
      <result column="store_id"  property="storeId"/>
      <result column="clerk_id"  property="clerkId"/>
      <result column="send_status"  property="sendStatus"/>
      <result column="send_count"  property="sendCount"/>
      <result column="create_time"  property="createTime"/>
      <result column="update_time"  property="updateTime"/>
      <result column="remark" property="remark"/>
      <result column="msgid" property="msgid"/>
      <result column="delete_flag" property="deleteFlag"/>
      <result column="done_flag" property="doneFlag"/>
      <result column="send_time" property="sendTime"/>
      <result column="fail_count" property="failCount"/>
  </resultMap>
  <sql id="Base_Column_List">
	  owner_log_id,
	  plan_id,
	  wx_enterprise_id,
	  enterprise_id,
	  staff_id,
	  store_id,
	  clerk_id,
	  send_status,
	  send_count,
	  create_time,
	  update_time , remark , msgid , delete_flag , done_flag , send_time ,  fail_count
  </sql>
  <!-- ===================== 新增 ======================== -->
  
  <insert id="batchInsert">
		 INSERT INTO tab_haoban_group_chat_plan_owner_log(
		  owner_log_id,
		  plan_id,
		  wx_enterprise_id,
		  enterprise_id,
		  staff_id,
		  store_id,
		  clerk_id,
		  send_status,
		  send_count,
		  fail_count,
		  create_time,
		  update_time , remark , msgid , delete_flag , done_flag 
		)VALUES
		<foreach collection="list" item="item" separator=","  >
			(     #{item.ownerLogId,jdbcType=BIGINT},
				  #{item.planId,jdbcType=BIGINT},
				  #{item.wxEnterpriseId},
				  #{item.enterpriseId},
				  #{item.staffId},
				  #{item.storeId},
				  #{item.clerkId},
				  #{item.sendStatus},
				  0,
				  0,
				  #{item.createTime},
				  #{item.updateTime} , #{item.remark} , #{item.msgid} , 0 , 0
			)
		</foreach>
	</insert>
	
	<update id="update" parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlanOwnerLog">
		 <![CDATA[
			  UPDATE tab_haoban_group_chat_plan_owner_log SET
				  store_id=#{storeId},
				  clerk_id=#{clerkId},
				  send_status=#{sendStatus},
				  update_time=now(),
				  remark=#{remark},
				  msgid=#{msgid}
			  where owner_log_id = #{ownerLogId}
			  ]]>
	</update>
	
	<update id="deleteByStaffIds">
		update tab_haoban_group_chat_plan_owner_log set delete_flag = 1 , update_time =now() where plan_id=#{planId} 
		and staff_id in 
		<foreach collection="staffIdList" close=")" open="(" separator="," item="id">
			#{id}
		</foreach>
	</update>
	
   <!-- ==================更新 ========== -->
  <update id="updateSendCount">
  	<![CDATA[
  		UPDATE tab_haoban_group_chat_plan_owner_log SET send_count = #{sendCount} ,  fail_count =#{failCount} , done_flag = 1 , 
  		send_status = 2 , send_time = #{sendTime} , 
  		update_time = now() where owner_log_id = #{id}
  	]]>
  </update>
  
  <!-- ============ 查询=============-->
  <select id="selectById" parameterType="long" resultMap="result-map-tabHaobanGroupChatPlanOwnerLog">
  SELECT <include refid="Base_Column_List"/> FROM tab_haoban_group_chat_plan_owner_log WHERE owner_log_id = #{ownerLogId} and delete_flag = 0
  </select>
  
  <select id="listForTimer" resultType="long">
  	  SELECT a.owner_log_id  FROM tab_haoban_group_chat_plan_owner_log a left join tab_haoban_group_chat_plan b on a.plan_id = b.plan_id 
  	  WHERE b.end_time >= DATE_ADD(NOW(),INTERVAL -6 hour) and a.done_flag = 0  and a.delete_flag = 0  and b.delete_flag = 0 and a.msgid is not null  
  	  <if test="null != planId and -1 != planId">
	  	  and a.plan_id = #{planId}
  	  </if>
  </select>
  
  <select id="listForDoPlan" resultMap="result-map-tabHaobanGroupChatPlanOwnerLog">
  	select <include refid="Base_Column_List"/> FROM tab_haoban_group_chat_plan_owner_log WHERE plan_id = #{planId} and delete_flag = 0 limit ${startItem},${pageSize}
  </select>
  
  <select id="listPage"  parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
  	 select 
  	    a.owner_log_id ownerLogId , 
  	    a.wx_enterprise_id wxEnterpriseId ,
  	    a.plan_id planId ,
  	 	b.staff_name staffName ,
  	 	a.staff_id  staffId , 
  	 	a.clerk_id  clerkId ,
  	 	a.store_id  storeId ,
  	 	a.send_status sendStatus ,
  	 	a.send_count  sendCount , 
  	 	a.send_time   sendTime ,
  	 	a.create_time createTime ,
  	 	a.done_flag   doneFlag ,
  	 	a.msgid       msgid 
  	  from tab_haoban_group_chat_plan_owner_log a left join tab_haoban_staff b on a.staff_id = b.staff_id 
  	  where 
  	  	a.wx_enterprise_id = #{wxEnterpriseId} and a.plan_id = #{planId}  and a.delete_flag = 0
  	  	<if test="null != sendStatus">
  	  		and send_status = #{sendStatus}
  	  	</if>
  	  	<if test="null != searchParams and ''!=searchParams">
  	  		and b.staff_name like #{searchParams}
  	  	</if>
  	  	<if test="null != startDate">
			and a.create_time <![CDATA[>=]]> #{startDate} and a.create_time <![CDATA[<=]]> #{endDate}
	    </if>
  	  	order by a.create_time , a.owner_log_id
  </select>
  
  <select id="listOwnerLogPageForWxaTotal"  parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO" resultType="com.gic.haoban.manage.api.dto.chat.ChatOwnerTotalDTO">
  		select 
  		b.plan_id   planId   ,
  	 	b.name      planName ,
  	 	b.start_time  startTime , 
  	 	b.end_time    endTime , 
  	 	<!-- 已发送 -->
  	 	SUM(CASE a.send_status WHEN 2 THEN 1 ELSE 0 END ) sendCount,
  	 	<!-- 待发送 -->
  	 	SUM(CASE a.send_status WHEN 1 THEN 1 ELSE 0 END ) notSendCount 
  	 	
  	  from tab_haoban_group_chat_plan_owner_log a left join tab_haoban_group_chat_plan b on a.plan_id = b.plan_id 
  	  where 
  	  	a.wx_enterprise_id = #{wxEnterpriseId} and b.delete_flag = 0
  	  	<if test="null != storeId">
  	  		 and a.store_id = #{storeId}
  	  	</if>
  	  	<if test="null != clerkId">
  	  		and a.clerk_id = #{clerkId}
  	  	</if>
  	  	<if test="null != sendStatus">
  	  		and a.send_status = #{sendStatus}
  	  	</if>
  	  	<if test="null != searchParams">
  	  		and b.name like #{searchParams}
  	  	</if>
  	  	and b.end_time > now() 
  	  	group by b.plan_id
  	  	having notSendCount 
  	  	order by b.end_time desc ,  a.create_time desc
  </select>
  
  <select id="listOwnerLogPageForWxa"  parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
  		select 
  		a.owner_log_id ownerLogId ,
  		b.plan_id   planId , 
  	 	b.name      planName ,
  	 	a.staff_id  staffId , 
  	 	a.clerk_id  clerkId ,
  	 	a.store_id  storeId ,
  	 	a.send_status sendStatus ,
  	 	a.send_count  sendCount , 
  	 	a.fail_count  failCount ,
  	 	a.send_time   sendTime ,
  	 	a.create_time createTime ,
  	 	b.end_time  expireDate ,
  	 	b.end_time  endTime ,
  	 	b.start_time startTime ,
  	 	a.done_flag   doneFlag ,
  	 	a.msgid       msgid 
  	  from tab_haoban_group_chat_plan_owner_log a left join tab_haoban_group_chat_plan b on a.plan_id = b.plan_id 
  	  where a.wx_enterprise_id = #{wxEnterpriseId} and a.plan_id = #{planId}  and b.delete_flag = 0
  	  	<if test="null != storeId">
  	  		 and a.store_id = #{storeId}
  	  	</if>
  	  	<if test="null != clerkId">
  	  		and a.clerk_id = #{clerkId}
  	  	</if>
  	  	<if test="null != sendStatus">
  	  		and a.send_status = #{sendStatus}
  	  	</if>
  	  	 order by a.create_time desc , a.owner_log_id
  </select>
  
  <select id="getOwnerInfo"  resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
  		select 
  		a.owner_log_id ownerLogId ,
  		b.plan_id   planId , 
  	 	b.name      planName ,
  	 	a.staff_id  staffId , 
  	 	a.clerk_id  clerkId ,
  	 	a.store_id  storeId ,
  	 	a.send_status sendStatus ,
  	 	a.send_count  sendCount , 
  	 	a.fail_count  failCount ,
  	 	a.send_time   sendTime ,
  	 	a.create_time createTime ,
  	 	b.end_time  expireDate ,
  	 	b.end_time  endTime ,
  	 	b.start_time startTime ,
  	 	a.done_flag   doneFlag ,
  	 	a.msgid       msgid 
  	  from tab_haoban_group_chat_plan_owner_log a left join tab_haoban_group_chat_plan b on a.plan_id = b.plan_id 
  	  where a.wx_enterprise_id = #{wxEnterpriseId} and a.plan_id = #{planId}  and b.delete_flag = 0 and a.staff_id = #{staffId}
  	  and a.clerk_id = #{clerkId}
  </select>


	<select id="getTotalCount" resultType="java.lang.Integer">
		SELECT
			count(*)
		FROM
			tab_haoban_group_chat_plan_owner_log a
		left join tab_haoban_group_chat_plan b on  a.plan_id = b.plan_id
		WHERE
			a.enterprise_id = #{enterpriseId}
		<if test="null != storeId">
			AND a.store_id = #{storeId}
		</if>
		<if test="null != clerkId">
			AND a.clerk_id = #{clerkId}
		</if>
		  AND a.send_status = 1
		  AND a.delete_flag = 0
		  AND b.delete_flag = 0
		  AND b.status_flag = 1
		  AND b.end_time >= now()
	</select>
  
  
  
</mapper> 