<?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.CustomPlanMapper" >
  <resultMap id="BaseResultMap" type="CustomPlan" >
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="plan_number" jdbcType="VARCHAR" property="planNumber" />
    <result column="plan_name" jdbcType="VARCHAR" property="planName" />
    <result column="create_user_name" jdbcType="VARCHAR" property="createUserName" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="open_feedback" jdbcType="INTEGER" property="openFeedback" />
    <result column="paper_id" jdbcType="INTEGER" property="paperId" />
    <result column="paper_title" jdbcType="VARCHAR" property="paperTitle" />
    <result column="paper_desc" jdbcType="VARCHAR" property="paperDesc" />
    <result column="use_state" jdbcType="INTEGER" property="useState" />
    <result column="pdf_url" jdbcType="VARCHAR" property="pdfUrl" />
    <result column="h5_url" jdbcType="VARCHAR" property="h5Url" />
    <result column="preview_qrcode_url" jdbcType="VARCHAR" property="previewQrcodeUrl" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>

  <sql id="Base_Column_List">
    id, plan_number, plan_name, create_user_name, description, open_feedback, paper_id,
    paper_title, paper_desc, use_state, pdf_url, h5_url, preview_qrcode_url, create_time,
    update_time
  </sql>

  <insert id="insert" parameterType="CustomPlan" useGeneratedKeys="true" keyProperty="id">
    insert into custom_plan (plan_number, plan_name, create_user_name,
      description, open_feedback, paper_id,
      paper_title, paper_desc, use_state,
      pdf_url, h5_url, preview_qrcode_url,
      create_time, update_time)
    values (#{planNumber,jdbcType=VARCHAR}, #{planName,jdbcType=VARCHAR}, #{createUserName,jdbcType=VARCHAR},
      #{description,jdbcType=VARCHAR}, #{openFeedback,jdbcType=INTEGER}, #{paperId,jdbcType=INTEGER},
      #{paperTitle,jdbcType=VARCHAR}, #{paperDesc,jdbcType=VARCHAR}, #{useState,jdbcType=INTEGER},
      #{pdfUrl,jdbcType=VARCHAR}, #{h5Url,jdbcType=VARCHAR}, #{previewQrcodeUrl,jdbcType=VARCHAR},
      NOW(), NOW())
  </insert>

  <update id="update" parameterType="CustomPlan">
    update custom_plan
    <set>
      <if test="planNumber != null">
        plan_number = #{planNumber,jdbcType=VARCHAR},
      </if>
      <if test="planName != null">
        plan_name = #{planName,jdbcType=VARCHAR},
      </if>
      <if test="createUserName != null">
        create_user_name = #{createUserName,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        description = #{description,jdbcType=VARCHAR},
      </if>
      <if test="openFeedback != null">
        open_feedback = #{openFeedback,jdbcType=INTEGER},
      </if>
      <if test="paperId != null">
        paper_id = #{paperId,jdbcType=INTEGER},
      </if>
      <if test="paperTitle != null">
        paper_title = #{paperTitle,jdbcType=VARCHAR},
      </if>
      <if test="paperDesc != null">
        paper_desc = #{paperDesc,jdbcType=VARCHAR},
      </if>
      <if test="useState != null">
        use_state = #{useState,jdbcType=INTEGER},
      </if>
      <if test="pdfUrl != null">
        pdf_url = #{pdfUrl,jdbcType=VARCHAR},
      </if>
      <if test="h5Url != null">
        h5_url = #{h5Url,jdbcType=VARCHAR},
      </if>
      <if test="previewQrcodeUrl != null">
        preview_qrcode_url = #{previewQrcodeUrl,jdbcType=VARCHAR},
      </if>
        update_time = NOW(),
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

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

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

  <select id="getCustomPlanCount" resultType="int">
    select
      count(1)
    from custom_plan
    where 1= 1
    <if test="content != null">
      and (plan_number like  concat('%', #{content}, '%')
      or  plan_name like  concat('%', #{content}, '%')
      or  create_user_name like  concat('%', #{content}, '%'))
    </if>
    <if test="useState != null">
      and use_state = #{useState}
    </if>
  </select>

  <select id="listCustomPlanByPage" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_plan
    where 1= 1
    <if test="content != null">
     and (plan_number like  concat('%', #{content}, '%')
      or  plan_name like  concat('%', #{content}, '%')
      or  create_user_name like  concat('%', #{content}, '%'))
    </if>
    <if test="useState != null">
      and use_state = #{useState}
    </if>
    ORDER BY update_time DESC
    LIMIT #{pageNum}, #{numPerPage}
  </select>

  <select id="getPlanPaperCount" parameterType="map"  resultType="int">
    select
    count(1)
    from custom_plan
    where open_feedback = 1
    and paper_id is not null
    <if test="content != null">
      and (plan_name like  concat('%', #{content}, '%')
      or  paper_title like  concat('%', #{content}, '%'))
    </if>
    <if test="paperIds != null and paperIds.size() > 0">
      and  paper_id in
      <foreach collection="paperIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
  </select>

  <select id="listPlanPaperByPage" parameterType="map" resultType="CustomPlanPaperVO">
    select
    id id,
    plan_number planNumber,
    plan_name planName,
    paper_id paperId,
    paper_title paperTitle,
    create_time createTime
    from custom_plan
    where open_feedback = 1
    and paper_id is not null
    <if test="content != null">
      and (plan_name like  concat('%', #{content}, '%')
      or  paper_title like  concat('%', #{content}, '%'))
    </if>
    <if test="paperIds != null and paperIds.size() > 0">
      and  paper_id in
      <foreach collection="paperIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
    ORDER BY update_time DESC
    LIMIT #{pageNum}, #{numPerPage}
  </select>
</mapper>