<?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.custom.dao.impl.CustomTimeControlDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.custom.entity.CustomTimeControl" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="time_control_task_id" property="timeControlTaskId" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <resultMap id="PageResultMap" type="com.pcloud.book.custom.dto.CustomTimeControlPageDTO">
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="time_control_task_id"  property="timeControlTaskId" jdbcType="INTEGER" />
    <result column="task_name" property="taskName" jdbcType="VARCHAR"/>
    <result column="task_introduce" property="taskIntroduce" jdbcType="VARCHAR"/>
  </resultMap>

  <resultMap id="DTOResultMap" type="com.pcloud.book.custom.dto.CustomTimeControlDTO">
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="time_control_task_id"  property="timeControlTaskId" jdbcType="INTEGER" />
    <collection property="relatedBooks" column="id"
                select="com.pcloud.book.custom.mapper.CustomTimeControlBookMapper.getBookListByCustomTimeControlId"/>
    <collection property="replyList4Subscribe" column="id"
                select="com.pcloud.book.custom.mapper.CustomTimeControlReplyMapper.getSubscribeReplyList"/>
    <collection property="replyList4Unsubscribe" column="id"
                select="com.pcloud.book.custom.mapper.CustomTimeControlReplyMapper.getUnSubscribeReplyList"/>
  </resultMap>
  <sql id="Base_Column_List" >
    id, time_control_task_id, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from custom_time_control
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from custom_time_control
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.custom.entity.CustomTimeControl" useGeneratedKeys="true" keyProperty="id" flushCache="true" >
    insert into custom_time_control (id, time_control_task_id, create_time,
    update_time)
    values (#{id,jdbcType=INTEGER}, #{timeControlTaskId,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
    #{updateTime,jdbcType=TIMESTAMP})
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.custom.entity.CustomTimeControl" >
    update custom_time_control
    <set >
      <if test="timeControlTaskId != null" >
        time_control_task_id = #{timeControlTaskId,jdbcType=INTEGER},
      </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.custom.entity.CustomTimeControl" >
    update custom_time_control
    set time_control_task_id = #{timeControlTaskId,jdbcType=INTEGER},
    create_time = #{createTime,jdbcType=TIMESTAMP},
    update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getCustomTimeControlPage" parameterType="map" resultMap="PageResultMap">
    select
    c.id,
    c.time_control_task_id,
    t.task_name,
    t.task_introduce
    from
    custom_time_control c
    left join time_control_task t on c.time_control_task_id = t.task_id
    <where>
      <if test="taskName != null">
        t.task_name like concat("%",#{taskName},"%")
      </if>
    </where>
    order by c.create_time desc
  </select>

  <select id="getCustomTimeControlById" parameterType="integer"  resultMap="DTOResultMap">
    select
    c.id,
    c.time_control_task_id
    from
    custom_time_control c
    where id = #{id}
  </select>
</mapper>