<?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" />
  </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
  </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)
    values (#{taskId}, #{startDay}, #{finishConfirm}, #{finishKeyWord}, now(), now(),#{tdConfirm},#{finishLinkIntroduce},#{finishLinkUrl}
    ,#{addSendTime},#{sendHour},#{progressGuidance}, #{itemIntroduce})
  </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}
    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" resultType="com.pcloud.book.timecontrol.dto.ReplyMessageBaseDto">
    select id, task_id taskId, start_day startDay, send_hour sendHour,
    if(finish_confirm=0,0,2) configState
    from time_control_task_item where task_id = #{taskId}
    order by startDay asc, send_hour asc
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from time_control_task_item
    where id = #{id,jdbcType=INTEGER}
  </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>
</mapper>