<?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.CustomReadPlanPeriodDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.custom.entity.CustomReadPlanPeriod" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="custom_read_plan_id" property="customReadPlanId" jdbcType="BIGINT" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="addition_explain" property="additionExplain" jdbcType="VARCHAR" />
    <result column="seq_num" property="seqNum" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List">
    id, custom_read_plan_id,name,addition_explain,seq_num,create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.custom.entity.CustomReadPlanPeriod" useGeneratedKeys="true" keyProperty="id">
    insert into custom_read_plan_period
    (custom_read_plan_id,name,addition_explain,seq_num,create_time)
    values
    (#{customReadPlanId},#{name},#{additionExplain},#{seqNum},now())
  </insert>

  <update id="update" parameterType="com.pcloud.book.custom.entity.CustomReadPlanPeriod">
    update custom_read_plan_period
    <set>
      <if test="customReadPlanId != null">
        custom_read_plan_id = #{customReadPlanId},
      </if>
      <if test="name != null">
        name = #{name},
      </if>
      <if test="additionExplain != null">
        addition_explain = #{additionExplain},
      </if>
      <if test="seqNum != null">
        seq_num = #{seqNum},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <delete id="deleteById">
    delete from custom_read_plan_period
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <select id="getById" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_read_plan_period
    where id = #{id,jdbcType=INTEGER}
  </select>

  <insert id="batchInsert"  parameterType="com.pcloud.book.custom.entity.CustomReadPlanPeriod" useGeneratedKeys="true" keyProperty="id">
    insert into custom_read_plan_period (
      custom_read_plan_id,
      name,
      addition_explain,
      seq_num,
      create_time
    ) values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.customReadPlanId,jdbcType=BIGINT},
      #{item.name,jdbcType=VARCHAR},
      #{item.additionExplain,jdbcType=VARCHAR},
      #{item.seqNum,jdbcType=INTEGER},
      NOW()
      )
    </foreach>
  </insert>

  <delete id="deleteByCustomReadPlanId" parameterType="long">
    delete from custom_read_plan_period
    where  custom_read_plan_id = #{customReadPlanId}
  </delete>

  <select id="getListByCustomReadPlanId" parameterType="long" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from custom_read_plan_period
    where  custom_read_plan_id = #{customReadPlanId}
  </select>

  <select id="getPeriodCountGroupByReadPlanId" parameterType="list" resultType="com.pcloud.book.custom.dto.PeriodCountAndReadPlanDTO">
    select
    custom_read_plan_id customReadPlanId,
    ifnull(count(1),0) periodCount
    from custom_read_plan_period
    where custom_read_plan_id in
    <foreach collection="list" item="item" open="(" separator="," close=")">
      ${item}
    </foreach>
    group by custom_read_plan_id
  </select>

</mapper>