<?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.BookTaskBindingMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.BookTaskBinding" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="task_name" property="taskName" jdbcType="VARCHAR" />
    <result column="book_id" property="bookId" jdbcType="INTEGER" />
    <result column="book_name" property="bookName" jdbcType="VARCHAR" />
    <result column="isbn" property="isbn" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="BIT" />
    <result column="delete" property="delete" jdbcType="BIT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, task_id, task_name, book_id, book_name, isbn, state, delete, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from book_task_binding
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from book_task_binding
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByTask">
    delete from book_task_binding where task_id = #{taskId}
  </delete>

  <insert id="insert" parameterType="com.pcloud.book.timecontrol.entity.BookTaskBinding" >
    insert into book_task_binding (id, task_id, task_name, 
      book_id, book_name, isbn, 
      state, delete, create_time, 
      update_time)
    values (#{id,jdbcType=INTEGER}, #{taskId,jdbcType=INTEGER}, #{taskName,jdbcType=VARCHAR}, 
      #{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{isbn,jdbcType=VARCHAR}, 
      #{state,jdbcType=BIT}, #{delete,jdbcType=BIT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.timecontrol.entity.BookTaskBinding" >
    insert into book_task_binding
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="taskId != null" >
        task_id,
      </if>
      <if test="taskName != null" >
        task_name,
      </if>
      <if test="bookId != null" >
        book_id,
      </if>
      <if test="bookName != null" >
        book_name,
      </if>
      <if test="isbn != null" >
        isbn,
      </if>
      <if test="state != null" >
        state,
      </if>
      <if test="delete != null" >
        delete,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="taskId != null" >
        #{taskId,jdbcType=INTEGER},
      </if>
      <if test="taskName != null" >
        #{taskName,jdbcType=VARCHAR},
      </if>
      <if test="bookId != null" >
        #{bookId,jdbcType=INTEGER},
      </if>
      <if test="bookName != null" >
        #{bookName,jdbcType=VARCHAR},
      </if>
      <if test="isbn != null" >
        #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        #{state,jdbcType=BIT},
      </if>
      <if test="delete != null" >
        #{delete,jdbcType=BIT},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.timecontrol.entity.BookTaskBinding" >
    update book_task_binding
    <set >
      <if test="taskId != null" >
        task_id = #{taskId,jdbcType=INTEGER},
      </if>
      <if test="taskName != null" >
        task_name = #{taskName,jdbcType=VARCHAR},
      </if>
      <if test="bookId != null" >
        book_id = #{bookId,jdbcType=INTEGER},
      </if>
      <if test="bookName != null" >
        book_name = #{bookName,jdbcType=VARCHAR},
      </if>
      <if test="isbn != null" >
        isbn = #{isbn,jdbcType=VARCHAR},
      </if>
      <if test="state != null" >
        state = #{state,jdbcType=BIT},
      </if>
      <if test="delete != null" >
        delete = #{delete,jdbcType=BIT},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.timecontrol.entity.BookTaskBinding" >
    update book_task_binding
    set task_id = #{taskId,jdbcType=INTEGER},
      task_name = #{taskName,jdbcType=VARCHAR},
      book_id = #{bookId,jdbcType=INTEGER},
      book_name = #{bookName,jdbcType=VARCHAR},
      isbn = #{isbn,jdbcType=VARCHAR},
      state = #{state,jdbcType=BIT},
      delete = #{delete,jdbcType=BIT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--批量新增任务配置发布-->
  <insert id="batchInsert">
    INSERT INTO book_task_binding (
        task_id, task_name, book_id, book_name, isbn, state, `delete`, create_time
    ) VALUES
    <foreach collection="bookDtos" item="book" separator=",">
      ( #{timeControlTask.taskId}, #{timeControlTask.taskName}, #{book.bookId}, #{book.bookName}, #{book.isbn}, 0, 0, NOW())
    </foreach>
  </insert>

  <!--获取任务配置发布数量-->
  <select id="getBookTaskBindingCount" resultType="java.lang.Integer">
    SELECT count(1) FROM book_task_binding a LEFT JOIN time_control_task b ON a.task_id = b.task_id
    WHERE a.`delete` = 0
    <if test="proLabelId != null">
      AND b.pro_label_id = #{proLabelId}
    </if>
    <if test="depLabelId != null">
      AND b.dep_label_id = #{depLabelId}
    </if>
    <if test="purLabelId != null">
      AND b.pur_label_id = #{purLabelId}
    </if>
    <if test="bookIds!=null">
      AND book_id IN
      <foreach collection="bookIds" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
  </select>

  <!--分页查询分页配置发布-->
  <select id="selectBookTaskBinding4Pcloud"
          resultType="com.pcloud.book.timecontrol.vo.BookTaskBindingVO">
    SELECT
    a.id,
    a.task_id taskId,
    a.task_name taskName,
    a.book_id bookId,
    a.book_name bookName,
    a.isbn,
    CONCAT( 'BK', a.book_id ) serialNumber,
    a.state,
    b.pro_label_id proLabelId,
    b.dep_label_id depLabelId,
    b.pur_label_id purLabelId
    FROM book_task_binding a LEFT JOIN time_control_task b ON a.task_id = b.task_id
    WHERE a.`delete` = 0
    <if test="proLabelId != null">
      AND b.pro_label_id = #{proLabelId}
    </if>
    <if test="depLabelId != null">
      AND b.dep_label_id = #{depLabelId}
    </if>
    <if test="purLabelId != null">
      AND b.pur_label_id = #{purLabelId}
    </if>
    <if test="bookIds!=null">
      AND a.book_id IN
      <foreach collection="bookIds" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
    LIMIT #{pageNum}, #{numPerPage}
  </select>

  <select id="checkHasBinding" resultType="java.lang.Integer">
    SELECT count(1) FROM book_task_binding
    WHERE
    <foreach collection="bookIds" item="bookId" open="(" close=") OR ">
      book_id = #{bookId} AND task_id = #{taskId}
    </foreach>
    0
  </select>

  <!--批量更新任务配置发布状态-->
  <update id="batchUpdateBookTaskState">
    UPDATE book_task_binding SET state = #{state} WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </update>

  <!--批量删除任务配置发布-->
  <update id="batchDeleteBookTask">
    UPDATE  book_task_binding SET `delete` = 1 WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </update>

  <update id="updateTaskName">
    UPDATE book_task_binding set task_name = #{taskName}
    where task_id = #{taskId}
  </update>

</mapper>