<?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"/>
	  <result column="exec_type" property="execType" />
	  <result column="data_get_time" property="dataGetTime" />
  </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 , exec_type , data_get_time
  </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  , exec_type
		)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 , #{item.execType}
			)
		</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} , data_get_time = now() ,
  		update_time = now() where owner_log_id = #{id}
  	]]>
  </update>
	<update id="updateExecType">
		update tab_haoban_group_chat_plan_owner_log set exec_type =#{execType} where plan_id=#{planId} and delete_flag = 0
	</update>

	<update id="updateMsgid">
		update tab_haoban_group_chat_plan_owner_log set msgid=#{msgid} where owner_log_id = #{id}
	</update>

	<update id="giveUp" parameterType="java.lang.Long">
		update tab_haoban_group_chat_plan_owner_log set send_status = 3 where owner_log_id = #{id}
	</update>
	<update id="staffSendResult">
		update tab_haoban_group_chat_plan_owner_log set send_status = #{statusFlag} , send_time = #{sendTime} , remark = #{failRemark} where plan_id = #{planId} and staff_id = #{staffId}
	</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 , a.exec_type execType
  	  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="getOwnerInfo"  resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
		select
		a.owner_log_id ownerLogId ,
		b.plan_id   planId ,
		b.name      planName ,
		b.exec_type execType ,
		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}
		<if test="null != clerkId">
			and a.clerk_id = #{clerkId}
		</if>
	</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 ,
  	 	b.exec_type execType ,
  	 	<!-- 已发送 -->
  	 	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>
	  <if test="1==clerkTaskStatus">
		  and b.end_time > now() and a.send_status =1
	  </if>
	  <if test="2==clerkTaskStatus">
		  and ( b.end_time <![CDATA[<=]]> now() or a.send_status !=1 )
	  </if>
	  group by b.plan_id
	  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="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>

	<select id="listForMsgid" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
		SELECT
			a.owner_log_id ownerLogId ,
			b.plan_id   planId ,
			a.staff_id  staffId ,
			a.send_time  sendTime
		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.exec_type =  1 AND a.send_status = 2 AND a.msgid IS NULL
	</select>


	<select id="listForCancelSend" resultType="com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO">
		SELECT
		    a.wx_enterprise_id wxEnterpriseId ,
			a.owner_log_id ownerLogId ,
			b.plan_id   planId ,
			a.staff_id  staffId ,
			a.send_time  sendTime,
			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 b.exec_type = 0 AND a.send_status = 1 AND a.msgid IS not NULL and b.end_time <![CDATA[<=]]> now()   AND end_time > DATE_ADD(CURDATE(), INTERVAL -2 DAY)
	</select>


	<!-- 查询所有任务数-->
	<select id="getTotalTask" resultType="int">
		select count(1) 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}  and b.end_time <![CDATA[ > ]]> now()
							AND a.clerk_id = #{clerkId}  AND a.send_status = 1 and a.delete_flag = 0

	</select>

	<!-- 查询逾期任务数-->
	<select id="getInvalidTask" resultType="int">
		select count(1) 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}  and b.end_time = #{time}
		  AND a.clerk_id = #{clerkId}  AND a.send_status = 1 and a.delete_flag = 0
	</select>

	<!-- 查询失效任务数-->
	<select id="getOverdueTask" resultType="int">
		select count(1) 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}  and b.end_time = now()
		  AND a.clerk_id = #{clerkId}  AND a.send_status = 1 and a.delete_flag = 0
	</select>
    <select id="getTaskNum" resultType="java.lang.Integer">
		select count(1) from (
		select a.plan_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  a.enterprise_id =  #{enterpriseId}
		  and a.store_id = #{storeId}
		  <if test="null != clerkId">
			  AND a.clerk_id = #{clerkId}
		  </if>
		  and b.end_time <![CDATA[ > ]]> now()
		   AND a.send_status = 1 and a.delete_flag = 0 group by a.plan_id  ) t
	</select>

</mapper>