<?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.TimeControlTaskItemMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.TimeControlTaskItem" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="start_day" property="startDay" jdbcType="INTEGER" />
    <result column="finish_confirm" property="finishConfirm" jdbcType="INTEGER" />
    <result column="finish_key_word" property="finishKeyWord" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="td_confirm" property="tdConfirm" jdbcType="INTEGER" />
    <result column="finish_link_introduce" property="finishLinkIntroduce" jdbcType="VARCHAR" />
    <result column="finish_link_url" property="finishLinkUrl" jdbcType="VARCHAR" />
    <result column="add_send_time" property="addSendTime" jdbcType="INTEGER" />
    <result column="send_hour" property="sendHour" jdbcType="VARCHAR" />
    <result column="progress_guidance" property="progressGuidance" jdbcType="INTEGER" />
    <result column="item_introduce" property="itemIntroduce" jdbcType="VARCHAR" />
    <result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
    <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
    <result column="push_type" property="pushType" jdbcType="INTEGER" />
  </resultMap>

  <resultMap id="ReplyMessageBaseDtoMap" type="com.pcloud.book.timecontrol.dto.ReplyMessageBaseDto">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="start_day" property="startDay" jdbcType="INTEGER" />
    <result column="send_hour" property="sendHour" jdbcType="VARCHAR" />
    <result column="configState" property="configState" jdbcType="INTEGER" />
    <result column="start_time" property="startTime" jdbcType="TIMESTAMP" />
    <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />
    <result column="push_type" property="pushType" jdbcType="INTEGER" />
    <collection property="weekdays"  ofType="integer">
      <result column="week_day" jdbcType="INTEGER"/>
    </collection>
  </resultMap>
  <sql id="Base_Column_List" >
    id, task_id, start_day, finish_confirm, finish_key_word, create_time, update_time,td_confirm,finish_link_introduce,finish_link_url
    ,add_send_time,send_hour,progress_guidance, item_introduce,start_time,end_time,push_type
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskItem" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_task_item(task_id, start_day, finish_confirm, finish_key_word, create_time, update_time
    ,td_confirm,finish_link_introduce,finish_link_url,add_send_time,send_hour,progress_guidance, item_introduce,start_time,end_time,push_type)
    values (#{taskId}, #{startDay}, #{finishConfirm}, #{finishKeyWord}, now(), now(),#{tdConfirm},#{finishLinkIntroduce},#{finishLinkUrl}
    ,#{addSendTime},#{sendHour},#{progressGuidance}, #{itemIntroduce},#{startTime},#{endTime},#{pushType})
  </insert>

  <update id="updateTaskItem" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskItem">
    update time_control_task_item set start_day = #{startDay}, finish_confirm = #{finishConfirm}, finish_key_word = #{finishKeyWord},
    td_confirm=#{tdConfirm},
    <if test="finishLinkIntroduce !=null">
      finish_link_introduce=#{finishLinkIntroduce},
    </if>
   <if test="finishLinkUrl">
     finish_link_url=#{finishLinkUrl},
   </if>
    add_send_time=#{addSendTime},
    send_hour=#{sendHour},
    progress_guidance=#{progressGuidance},
    update_time = now(), item_introduce = #{itemIntroduce},
    start_time = #{startTime},
    end_time= #{endTime},
    push_type = #{pushType}
    where id = #{id}
  </update>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from time_control_task_item
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="getByTaskIdAndDay" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from time_control_task_item
    where task_id = #{taskId} and start_day = #{startDay}
  </select>

  <select id="listTaskItemDay" resultMap="ReplyMessageBaseDtoMap">
    select
    item.id,
    item.task_id ,
    item.start_day ,
    item.send_hour ,
    if(item.finish_confirm=0,0,2) configState,
    week.task_item_id,
    week.week_day,
    item.start_time,
    item.end_time,
    item.push_type
    from time_control_task_item item
    left join time_control_task_item_week  week on item.id = week.task_item_id
    where task_id = #{taskId}
    order by start_time asc,end_time asc,start_day asc, send_hour asc
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from time_control_task_item
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByTaskId" parameterType="integer">
    delete from time_control_task_item
    where task_id = #{taskId}
  </delete>

  <select id="getByItemIdList" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from time_control_task_item
    where id IN
    <foreach collection="taskItemIds" open="(" close=")" separator="," item="item">
      #{item}
    </foreach>
  </select>

  <select id="getTaskItemCount" parameterType="Integer" resultType="Integer">
    SELECT
        count(1)
    FROM
        time_control_task_item
    WHERE
        task_id = #{taskId}
  </select>

  <select id="listByTaskId" parameterType="Integer" resultMap="BaseResultMap">
    SELECT id, task_id, start_day, finish_confirm, finish_key_word, td_confirm,finish_link_introduce,finish_link_url
    ,add_send_time,send_hour,progress_guidance, item_introduce
    FROM
        time_control_task_item
    WHERE
        task_id = #{taskId}
  </select>

  <select id="getByTaskId" parameterType="Integer" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List" />
    FROM
        time_control_task_item
    WHERE
        task_id = #{taskId}
  </select>

  <select id="getMaxEndTime" parameterType="Integer" resultType="date">
    select
    max(end_time)
    from
    time_control_task_item
    where
    task_id = #{taskId}
  </select>

  <select id="getMonthDay" parameterType="String" resultType="com.pcloud.book.timecontrol.vo.request.TimeControlTaskItemVO">
    select a.id,if(min(a.start_time)<![CDATA[ < ]]>DATE_ADD(#{date},interval -day(#{date})+1 day),DATE_ADD(#{date},interval -day(#{date})+1 day),min(a.start_time)) startDay,
    if(max(a.end_time)<![CDATA[ > ]]>last_day(#{date}),last_day(#{date}),max(a.end_time)) endDay
    ,a.push_type pushType,b.send_type sendType from time_control_task_item a
    LEFT JOIN time_control_task b on b.task_id=a.task_id
    where b.send_type=2
    and end_time<![CDATA[ >= ]]>DATE_ADD(#{date},interval -day(#{date})+1 day)
    AND start_time<![CDATA[ <= ]]>last_day(#{date})
    and push_type=1
    GROUP BY push_type;
  </select>

  <select id="getMonthSecondDay" parameterType="String" resultType="com.pcloud.book.timecontrol.vo.request.TimeControlTaskItemVO">
    select a.id,if(a.start_time<![CDATA[ < ]]>DATE_ADD(#{date},interval -day(#{date})+1 day) ,DATE_ADD(#{date},interval -day(#{date})+1 day),a.start_time)startDay,
    if(a.end_time<![CDATA[ > ]]>last_day(#{date}),last_day(#{date}),a.end_time) endDay
    ,a.push_type pushType,b.send_type sendType ,GROUP_CONCAT(c.week_day) weekDays from time_control_task_item a
    LEFT JOIN time_control_task b on b.task_id=a.task_id
    LEFT JOIN time_control_task_item_week c on a.id=c.task_item_id
    where b.send_type=2
    and end_time<![CDATA[ >= ]]>DATE_ADD(#{date},interval -day(#{date})+1 day)
    AND start_time<![CDATA[ <= ]]>last_day(#{date})
    and push_type=2
    GROUP BY a.id;
  </select>
</mapper>
