<?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.pcloud.book.timecontrol.mapper.TimeControlTaskSendMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.TimeControlTaskSend" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="book_id" property="bookId" jdbcType="INTEGER" />
    <result column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="task_item_id" property="taskItemId" jdbcType="INTEGER" />
    <result column="subscribe_id" property="subscribeId" jdbcType="INTEGER" />
    <result column="send_state" property="sendState" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="send_day" property="sendDay" jdbcType="TIMESTAMP" />
    <result column="working" property="working" jdbcType="INTEGER" />
    <result column="complete_state" property="completeState" jdbcType="INTEGER" />
  </resultMap>

  <sql id="Base_Column_List">
    id, wx_user_id, book_id, task_id, task_item_id, subscribe_id, send_day, send_state, create_time, complete_state
  </sql>

  <insert id="batchInsert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskSend" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_task_send
      (wx_user_id, book_id, task_id, task_item_id, subscribe_id, send_day, send_state, create_time, send_hour)
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.wxUserId,jdbcType=VARCHAR}, #{item.bookId,jdbcType=INTEGER}, #{item.taskId,jdbcType=INTEGER},
      #{item.taskItemId,jdbcType=INTEGER}, #{item.subscribeId,jdbcType=INTEGER}, #{item.sendDay,jdbcType=VARCHAR},
      #{item.sendState,jdbcType=INTEGER}, NOW(), #{item.sendHour}
      )
    </foreach>
  </insert>

  <select id="listTodayTask" resultType="TaskSendDto">
    select
    a.id id,
    b.robot_wx_id robotWxId,
    a.wx_user_id wxUserId,
    a.task_id taskId,
    a.task_item_id taskItemId
    from time_control_task_send a
    left join time_control_task_subscribe b on a.subscribe_id = b.id
    where a.send_state = 0
    and CONCAT_WS(" ",a.send_day,SUBSTRING_INDEX(a.send_hour,":",1))  = DATE_FORMAT(NOW(), '%Y-%m-%d %H');
  </select>


  <select id="countMsgSend" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM time_control_task_send WHERE send_state = 1
    AND task_id = #{taskId} AND book_id = #{bookId}
  </select>

  <select id="countTodayNotSend" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM time_control_task_send WHERE send_state = 0
    AND send_day = #{sendDay}
    AND task_id = #{taskId} AND book_id = #{bookId}
  </select>

  <delete id="cancelSend">
    update time_control_task_send set send_state = #{sendState}, update_time = now(), working = 0
    where subscribe_id = #{subscribeId} and send_state = 0
  </delete>

  <delete id="cancelSendByTaskId">
    update time_control_task_send set send_state = 2, update_time = now(), working = 0
    where task_id = #{taskId} and send_state = 0
  </delete>

  <delete id="cancelSendByItemId">
    update time_control_task_send set send_state = 2, update_time = now(), working = 0
    where task_item_id = #{taskItemId} and send_state = 0
  </delete>

  <update id="updateSendState">
    update time_control_task_send set send_state = #{sendState}, update_time = now()
    <if test="completeState !=null">
      ,complete_state = #{completeState}
    </if>
    where id = #{sendId}
  </update>

  <select id="getById" resultMap="BaseResultMap">
    select id, wx_user_id, book_id, task_id, task_item_id, subscribe_id, send_day, send_state, working, complete_state
    from time_control_task_send
    where id = #{taskSendId}
  </select>

  <select id="getNoCompletedItem" resultType="java.lang.Integer">
    select ifnull(count(1),0)
    from time_control_task_send
    WHERE task_id =#{taskId}
    and complete_state !=1 AND send_state !=2
  </select>

  <select id="todayTaskSendCount" resultType="java.lang.Integer">
    SELECT ifnull(COUNT(1),0) FROM time_control_task_send WHERE send_state = 1
    AND send_day =DATE_FORMAT(NOW(), '%Y-%m-%d')
  </select>

  <select id="todayTaskNotSendCount" resultType="java.lang.Integer">
    SELECT ifnull(COUNT(1),0) FROM time_control_task_send WHERE send_state = 0
    AND send_day =DATE_FORMAT(NOW(), '%Y-%m-%d')
  </select>

  <select id="getTaskSendDateList" resultType="java.lang.String">
    SELECT DISTINCT send_day from time_control_task_send where SUBSTRING_INDEX(send_day,"-",2)=#{date}
    ORDER BY send_day
  </select>

  <select id="getTaskItemSendCount" resultType="java.lang.Integer">
    select count(1) from (
    select c.robot_wx_id
    from
    time_control_task_send a LEFT JOIN
    time_control_task b on a.task_id=b.task_id
    LEFT JOIN time_control_task_subscribe c on a.subscribe_id=c.id
    LEFT JOIN time_control_reply_message d on a.task_item_id=d.task_item_id
    where  1=1
    <if test="startTime !=null">
      and a.send_day <![CDATA[ >= ]]> STR_TO_DATE(#{startTime},'%Y-%m-%d')
    </if>
    <if test="endTime !=null">
      and a.send_day <![CDATA[ <= ]]> STR_TO_DATE(#{endTime},'%Y-%m-%d')
    </if>
    <if test="robotWxId !=null">
      and c.robot_wx_id=#{robotWxId}
    </if>
    <if test="taskName !=null">
      and b.task_name like concat('%', #{taskName}, '%')
    </if>
    <if test="sendContent !=null">
      and d.content like concat('%', #{sendContent}, '%')
    </if>
    group by
    a.task_item_id,c.robot_wx_id
    )s
  </select>

  <select id="listTaskItemSendRecord4Pcloud" resultType="com.pcloud.book.timecontrol.dto.TaskItemSendDto">
    select
    a.task_id taskId,
    b.task_name taskName,
    a.task_item_id taskItemId,
    c.robot_wx_id robotWxId,
    ifnull(count(DISTINCT a.wx_user_id),0) sendPersonCount,
    e.create_time createTime
    from
    time_control_task_send a LEFT JOIN
    time_control_task b on a.task_id=b.task_id
    LEFT JOIN time_control_task_subscribe c on a.subscribe_id=c.id
    LEFT JOIN time_control_reply_message d on a.task_item_id=d.task_item_id
    LEFT JOIN time_control_task_item e on a.task_item_id = e.id
    where  1=1
    <if test="startTime !=null">
      and a.send_day <![CDATA[ >= ]]> STR_TO_DATE(#{startTime},'%Y-%m-%d')
    </if>
    <if test="endTime !=null">
      and a.send_day <![CDATA[ <= ]]> STR_TO_DATE(#{endTime},'%Y-%m-%d')
    </if>
    <if test="robotWxId !=null">
      and c.robot_wx_id=#{robotWxId}
    </if>
    <if test="taskName !=null">
      and b.task_name like concat('%', #{taskName}, '%')
    </if>
    <if test="sendContent !=null">
      and d.content like concat('%', #{sendContent}, '%')
    </if>
    GROUP BY
    a.task_item_id,c.robot_wx_id
    <if test="pageNum != null">
      limit #{pageNum}, #{numPerPage}
    </if>
  </select>

  <select id="getTaskItemSendUserCount" resultType="java.lang.Integer">
    SELECT ifnull(COUNT(DISTINCT a.wx_user_id),0) FROM time_control_task_send a
    LEFT JOIN time_control_task_subscribe b ON a.subscribe_id = b.id
    where 1=1
      <if test="wxIds != null and wxIds.size()>0">
          and a.wx_user_id IN
          <foreach collection="wxIds" open="(" close=")" separator="," item="item">
              #{item}
          </foreach>
      </if>
    and a.task_item_id=#{taskItemId} AND b.robot_Wx_id = #{robotWxId}
    <if test="startTime !=null">
      and a.send_day <![CDATA[ >= ]]> STR_TO_DATE(#{startTime},'%Y-%m-%d')
    </if>
    <if test="endTime !=null">
      and a.send_day <![CDATA[ <= ]]> STR_TO_DATE(#{endTime},'%Y-%m-%d')
    </if>
  </select>

  <select id="listTaskItemSendUser4Pcloud" resultType="com.pcloud.book.timecontrol.dto.TaskSendUserDto">
    SELECT
    a.task_id taskId,
    a.task_item_id taskItemId,
    a.wx_user_id wxUserId,
    a.send_state sendState, a.send_day sendDay, a.send_hour sendHour
    FROM time_control_task_send a
    LEFT JOIN time_control_task_subscribe b on a.subscribe_id = b.id
    where 1=1
      <if test="wxIds != null and wxIds.size()>0">
          and a.wx_user_id IN
          <foreach collection="wxIds" open="(" close=")" separator="," item="item">
              #{item}
          </foreach>
      </if>
    and a.task_item_id=#{taskItemId} AND b.robot_wx_id = #{robotWxId}
    <if test="startTime !=null">
      and a.send_day <![CDATA[ >= ]]> STR_TO_DATE(#{startTime},'%Y-%m-%d')
    </if>
    <if test="endTime !=null">
      and a.send_day <![CDATA[ <= ]]> STR_TO_DATE(#{endTime},'%Y-%m-%d')
    </if>
    <if test="pageNum != null">
      limit #{pageNum}, #{numPerPage}
    </if>
  </select>

  <select id="getTaskNoCompletedCount" resultType="com.pcloud.book.timecontrol.dto.SubscribeTaskUserDTO">
    select
    wx_user_id wxUserId,
    ifnull(count(1),0) noCompletedCount
    from time_control_task_send
    where task_id=#{taskId}
    and complete_state=0
    and send_state !=2
    <if test="wxIds !=null and wxIds.size()>0">
      and wx_user_id IN
      <foreach collection="wxIds" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
    group by wx_user_id
  </select>

  <select id="getNoCompletedCount" resultType="java.lang.Integer">
    select ifnull(count(1),0)
    from time_control_task_send
    WHERE subscribe_id =#{subscribeId}
    and complete_state !=1 AND send_state !=2
  </select>

  <select id="getCompletedCount" resultType="java.lang.Integer">
    select ifnull(count(1),0)
    from time_control_task_send
    WHERE subscribe_id =#{subscribeId}
    and working =1 and complete_state = 1
  </select>

  <select id="getUserTaskItemProgress" resultType="com.pcloud.book.timecontrol.dto.TaskProgressItemDTO">
    SELECT
        s.id taskSendId,
        s.task_id taskId,
        s.task_item_id taskItemId,
        CONCAT(s.send_day, ' ', s.send_hour) taskItemName,
        s.complete_state completeState,
        IF (i.id IS NULL, 0, 1) hasPrize
    FROM
        time_control_task_send s
    LEFT JOIN time_control_task_prize_item i ON s.task_id = i.task_id
    AND s.task_item_id = i.task_item_id
    WHERE
        s.task_id = #{taskId}
    AND s.wx_user_id = #{wxUserId}
    AND working = 1
    GROUP BY
        s.id
    ORDER BY
        s.send_day,
        s.send_hour
  </select>
</mapper>