<?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.TimeControlTaskItemWeekMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.TimeControlWeekDay" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_item_id" property="taskItemId" jdbcType="INTEGER" />
    <result column="week_day" property="weekDay" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>


  <sql id="Base_Column_List" >
    id, task_item_id, week_day, create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskItem" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_task_item_week(task_item_id, week_day, create_time)
    values (#{taskItemId}, #{weekDay},  now())
  </insert>

  <insert id="batchInsert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskItem" useGeneratedKeys="true" keyProperty="id">
    insert into time_control_task_item_week(task_item_id, week_day, create_time)
    values
    <foreach collection="list" item="item" index="index" separator="," >
      (#{item.taskItemId}, #{item.weekDay},  now())
    </foreach>
  </insert>

  <delete id="batchDelete" parameterType="list">
    delete
    from
    time_control_task_item_week
    where
    task_item_id in
    <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </delete>


  <select id="selectByItemId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from time_control_task_item_week
    where task_item_id = #{taskItemId}
  </select>

  <select id="getByTaskItemIds" resultMap="BaseResultMap" parameterType="list" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_item_week
    where task_item_id IN
    <foreach collection="taskItemIds" item="taskItemId" open="(" close=")" separator=",">
      #{taskItemId}
    </foreach>
  </select>

  <select id="getMaxWeekDay" parameterType="integer" resultType="integer">
    select
    max(week_day)
    from
    time_control_task_item_week
    where
    task_item_id = #{taskItemId}
  </select>

</mapper>
