<?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.mapper.CustomPlanModuleMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.custom.vo.CustomPlanModuleVO" >
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="plan_id" jdbcType="INTEGER" property="planId" />
    <result column="parent_id" jdbcType="INTEGER" property="parentId" />
    <result column="module_type" jdbcType="INTEGER" property="moduleType" />
    <result column="pic_url" jdbcType="VARCHAR" property="picUrl" />
    <result column="type_name" jdbcType="VARCHAR" property="typeName" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="skill_id" jdbcType="INTEGER" property="skillId" />
    <result column="skill_cover" jdbcType="VARCHAR" property="skillCover" />
    <result column="app_id" jdbcType="BIGINT" property="appId" />
    <result column="agent_name" jdbcType="VARCHAR" property="agentName" />
    <result column="product_id" jdbcType="BIGINT" property="productId" />
    <result column="merchant_name" jdbcType="VARCHAR" property="merchantName" />
    <result column="product_unique_number" jdbcType="VARCHAR" property="productUniqueNumber" />
    <result column="link_url" jdbcType="VARCHAR" property="linkUrl" />
    <result column="open_feedback" jdbcType="INTEGER" property="openFeedback" />
    <result column="book_name" jdbcType="VARCHAR" property="bookName" />
    <result column="read_type" jdbcType="INTEGER" property="readType" />
    <result column="parent_attend" jdbcType="INTEGER" property="parentAttend" />
  </resultMap>

  <sql id="Base_Column_List">
    id, plan_id, parent_id, module_type, pic_url, type_name, title, content, skill_id, skill_cover,
    app_id, agent_name, product_id, merchant_name, product_unique_number, link_url, create_time, update_time, open_feedback, book_name,
    read_type,parent_attend
  </sql>

  <insert id="insert" parameterType="CustomPlanModule" useGeneratedKeys="true" keyProperty="id">
    insert into custom_plan_module (plan_id,parent_id, module_type, pic_url,
      type_name, title, content,
      skill_id, skill_cover, app_id, agent_name,
      product_id, merchant_name, product_unique_number, link_url, create_time,
      update_time, open_feedback, book_name, read_type,parent_attend)
    values (#{planId,jdbcType=INTEGER}, #{parentId,jdbcType=INTEGER}, #{moduleType,jdbcType=INTEGER}, #{picUrl,jdbcType=VARCHAR},
      #{typeName,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR},
      #{skillId,jdbcType=INTEGER}, #{skillCover,jdbcType=VARCHAR}, #{appId,jdbcType=BIGINT}, #{agentName,jdbcType=VARCHAR},
      #{productId,jdbcType=BIGINT}, #{merchantName,jdbcType=VARCHAR}, #{productUniqueNumber,jdbcType=VARCHAR}, #{linkUrl,jdbcType=VARCHAR},
      NOW(), NOW(), #{openFeedback}, #{bookName}, #{readType},#{parentAttend})
  </insert>

  <insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="id">
    insert into custom_plan_module (plan_id,parent_id, module_type, pic_url,
    type_name, title, content,
    skill_id, skill_cover, app_id, agent_name,
    product_id, merchant_name, product_unique_number, link_url, create_time,
    update_time, open_feedback, book_name, read_type,parent_attend)
    values
    <foreach collection="list" separator="," item="item">
     (#{item.planId,jdbcType=INTEGER}, #{item.parentId,jdbcType=INTEGER}, #{item.moduleType,jdbcType=INTEGER}, #{item.picUrl,jdbcType=VARCHAR},
    #{item.typeName,jdbcType=VARCHAR}, #{item.title,jdbcType=VARCHAR}, #{item.content,jdbcType=VARCHAR},
    #{item.skillId,jdbcType=INTEGER}, #{item.skillCover,jdbcType=VARCHAR}, #{item.appId,jdbcType=BIGINT}, #{item.agentName,jdbcType=VARCHAR},
    #{item.productId,jdbcType=BIGINT}, #{item.merchantName,jdbcType=VARCHAR}, #{item.productUniqueNumber,jdbcType=VARCHAR}, #{item.linkUrl,jdbcType=VARCHAR},
    NOW(), NOW(), #{item.openFeedback}, #{item.bookName}, #{item.readType},#{item.parentAttend})
    </foreach>
  </insert>

  <update id="update" parameterType="CustomPlanModule">
    update custom_plan_module
    <set>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=INTEGER},
      </if>
      <if test="moduleType != null">
        module_type = #{moduleType,jdbcType=INTEGER},
      </if>
      <if test="picUrl != null">
        pic_url = #{picUrl,jdbcType=VARCHAR},
      </if>
      <if test="typeName != null">
        type_name = #{typeName,jdbcType=VARCHAR},
      </if>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
      <if test="skillId != null">
        skill_id = #{skillId,jdbcType=INTEGER},
      </if>
      <if test="skillCover != null">
        skill_cover = #{skillCover,jdbcType=VARCHAR},
      </if>
      <if test="appId != null">
        app_id = #{appId,jdbcType=BIGINT},
      </if>
      <if test="productId != null">
        product_id = #{productId,jdbcType=BIGINT},
      </if>
      <if test="linkUrl != null">
        link_url = #{linkUrl,jdbcType=VARCHAR},
      </if>
      <if test="bookName != null">
        book_name = #{bookName,jdbcType=VARCHAR},
      </if>
      <if test="readType != null">
        read_type = #{readType,jdbcType=INTEGER},
      </if>
      <if test="parentAttend != null">
        parent_attend = #{parentAttend,jdbcType=INTEGER},
      </if>
        update_time = NOW(),
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <delete id="deleteByPlanId" >
    delete from custom_plan_module
    where plan_id = #{planId,jdbcType=INTEGER}
  </delete>

  <select id="getCustomPlanModuleByPlanId" resultMap="BaseResultMap">
    select
        <include refid="Base_Column_List" />
    from custom_plan_module
    where plan_id = #{planId,jdbcType=INTEGER}
    and parent_id = #{parentId,jdbcType=INTEGER}
  </select>

</mapper>