<?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="com.pcloud.book.custom.entity.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" />
    <result column="suggestion_count" jdbcType="INTEGER" property="suggestionCount" />
    <result column="confirm_feedback_reply" jdbcType="VARCHAR" property="confirmFeedbackReply" />
    <result column="robot_type" jdbcType="TINYINT" property="robotType" />
    <result column="book_id" jdbcType="BIGINT" property="bookId" />
    <result column="read_type" jdbcType="INTEGER" property="readType" />
    <result column="rights_pic" jdbcType="VARCHAR" property="rightsPic" />
    <result column="button_name" jdbcType="VARCHAR" property="buttonName" />
    <result column="custom_time_control_id" jdbcType="INTEGER" property="customTimeControlId" />
    <result column="pcloud_skill_id" property="pcloudSkillId" jdbcType="BIGINT"/>
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
  </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, confirm_feedback_reply,robot_type,book_id,read_type,rights_pic,button_name,
    custom_time_control_id,pcloud_skill_id,agent_id
  </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, confirm_feedback_reply,robot_type,
      book_id,read_type,rights_pic,button_name,custom_time_control_id,pcloud_skill_id,
      agent_id)
    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(), #{confirmFeedbackReply},#{robotType},#{bookId},#{readType},#{rightsPic},#{buttonName},#{customTimeControlId},#{pcloudSkillId},
      #{agentId})
  </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>
      <if test="confirmFeedbackReply != null">
        confirm_feedback_reply = #{confirmFeedbackReply},
      </if>
      <if test="robotType != null">
       robot_type = #{robotType},
      </if>
      <if test="bookId != null">
        book_id = #{bookId},
      </if>
      <if test="readType != null">
        read_type = #{readType},
      </if>
      <if test="rightsPic != null">
        rights_pic = #{rightsPic},
      </if>
      <if test="buttonName != null">
        button_name = #{buttonName},
      </if>
      <if test="customTimeControlId != null">
        custom_time_control_id = #{customTimeControlId},
      </if>
      <if test="pcloudSkillId != null">
        pcloud_skill_id = #{pcloudSkillId},
      </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 a
    LEFT JOIN custom_plan_module m ON a.id=m.plan_id AND m.module_type=6
    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>
    <if test="hasPdf == 1">
      and pdf_url is not null
    </if>
    <if test="robotType != null">
      AND a.robot_type = #{robotType}
    </if>
    <if test="readType != null">
      AND a.read_type = #{readType}
    </if>
    <if test="projectIds != null and projectIds.size() > 0">
      AND  a.book_id IN
      <foreach collection="projectIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
    <if test="agentId != null">
      AND a.agent_id = #{agentId}
    </if>
  </select>

  <select id="listCustomPlanByPage" resultMap="BaseResultMap">
    select
    a.id, plan_number, plan_name, create_user_name, description, a.open_feedback,
    use_state, pdf_url, h5_url, preview_qrcode_url, a.create_time, COUNT(DISTINCT b.batch_id) suggestion_count,
    a.robot_type,t.classify_name robotTypeName, a.read_type,a.book_id ,a.rights_pic ,a.button_name,
    a.agent_id
    from custom_plan a left join custom_plan_module_suggestion b
    on a.id = b.plan_id
    left join pcloud_robot_classify t on a.robot_type = t.id
    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>
    <if test="hasPdf == 1">
      and pdf_url is not null
    </if>
    <if test="robotType != null">
      AND a.robot_type = #{robotType}
    </if>
    <if test="readType != null">
      AND a.read_type = #{readType}
    </if>
    <if test="projectIds != null and projectIds.size() > 0">
      AND  a.book_id IN
      <foreach collection="projectIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
    <if test="agentId != null">
      AND a.agent_id = #{agentId}
    </if>
    GROUP BY a.id
    ORDER BY a.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>
    <if test="agentId != null">
      AND agent_id = #{agentId}
    </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,
    agent_id agentId
    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>
    <if test="agentId != null">
      AND agent_id = #{agentId}
    </if>
    ORDER BY update_time DESC
    LIMIT #{pageNum}, #{numPerPage}
  </select>

  <update id="updatePdfUrl" parameterType="CustomPlan">
    update custom_plan set pdf_url =#{pdfUrl}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getPlanReadTypesByBookId" parameterType="long" resultType="com.pcloud.book.custom.dto.PlanReadTypeDto">
    select
    id customPlanId,
    read_type readType
    from custom_plan
    where book_id = #{bookId}
  </select>

  <select id="getLastCustomPlan" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_plan
    where read_type = #{readType}
    AND book_id IN
    <foreach collection="bookIds" item="item" separator="," open="(" close=")">
        ${item}
    </foreach>
    ORDER BY id DESC
    LIMIT 1
  </select>

  <select id="getLastCustomPlanByClassifyId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_plan
    where read_type = #{readType}
    AND robot_type = #{classifyId}
    ORDER BY id DESC
    LIMIT 1
  </select>

  <select id="getLastCustomPlanByBookIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_plan
    where book_id IN
    <foreach collection="bookIds" item="item" separator="," open="(" close=")">
      ${item}
    </foreach>
    ORDER BY id DESC
    LIMIT 1
  </select>
  <select id="getCustomPlanByIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from custom_plan
    where id IN
    <foreach collection="customPlanIds" item="item" separator="," open="(" close=")">
      ${item}
    </foreach>
    ORDER BY id DESC
  </select>
</mapper>
