<?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.TimeControlTaskSendDetailMapper" >
  <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" />
    <result column="send_week" property="sendWeek" jdbcType="INTEGER" />
    <result column="send_count" property="sendCount" 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,send_week,send_count
  </sql>

  <insert id="batchInsert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskSend" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_task_send_detail
      (wx_user_id, book_id, task_id, task_item_id, subscribe_id, send_day, send_state, create_time, send_hour,send_week)
    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},  DATE_FORMAT(NOW(),'%Y-%m-%d'),
      1, NOW(), #{item.sendHour},#{item.sendWeek}
      )
    </foreach>
  </insert>




  <select id="getTaskItemSendCount" resultType="java.lang.Integer">
    select count(1) from (
    select c.robot_wx_id
    from
    time_control_task_send_detail 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_detail 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_detail 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_detail 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_detail 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>

  <update id="updateSendState4Week">
    update
    time_control_task_send
    set
    send_state = 0
    where
    working = 1
    and
    complete_state = 0
    and
    send_state = 1
    and
    send_week is not null
  </update>

  <select id="listTodayTask4Week"  parameterType="map" resultType="com.pcloud.book.timecontrol.dto.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
    left join time_control_task_item t on a.task_item_id = t.id
    where
    a.send_state = 0
    and
    a.send_week is not null
    and
    DATE_FORMAT(NOW(), '%Y-%m-%d') &gt;= t.start_time
    and
    DATE_FORMAT(NOW(), '%Y-%m-%d') &lt;= t.end_time
    and
    a.send_week = #{weekDay}
    and
    a.task_item_id = #{taskItemId}
  </select>

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

  <update id="addSendCount" parameterType="integer">
    update
    time_control_task_send
    set
    send_count = send_count + 1
    where
    id = #{id}
  </update>

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