<?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="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" />
  </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
  </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)
    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())
  </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>
        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>