<?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.pcloudkeyword.dao.impl.PcloudRobotClassifyDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.pcloudkeyword.entity.PcloudRobotClassify" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="classify_name" property="classifyName" jdbcType="VARCHAR" />
    <result column="is_delete" property="isDelete" jdbcType="INTEGER" />
    <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
    <result column="qrcode_url" property="qrcodeUrl" jdbcType="VARCHAR" />
    <result column="keyword_classify_id" property="keywordClassifyId" jdbcType="VARCHAR" />
      <result column="welcome_duration" property="welcomeDuration" jdbcType="INTEGER" />
      <result column="is_open_push_h5url" property="isOpenPushH5url" jdbcType="INTEGER" />
      <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
      <result column="mimi_app_qr" property="miniAppQr" jdbcType="VARCHAR"/>
      <result column="open_status" property="openStatus" jdbcType="INTEGER"/>
  </resultMap>

    <resultMap id="pageResultMap" type="com.pcloud.book.pcloudkeyword.facade.response.PcloudRobotClassifyResponseVO" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="classify_name" property="classifyName" jdbcType="VARCHAR" />
        <result column="qrcode_url" property="qrcodeUrl" jdbcType="VARCHAR" />
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="mini_app_content" property="miniAppContent" jdbcType="VARCHAR"/>
        <result column="open_status" property="openStatus" jdbcType="INTEGER"/>
       <!-- <association property="robotCount" column="id" fetchType="eager"
                     select="com.pcloud.book.pcloudkeyword.dao.impl.PcloudRobotDaoImpl.getRobotCountByClassifyId" />-->
    </resultMap>
  <sql id="Base_Column_List" >
    id, classify_name, link_url, qrcode_url,keyword_classify_id, is_delete, welcome_duration,is_open_push_h5url, seq_num, mimi_app_qr,open_status
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudRobotClassify" useGeneratedKeys="true" keyProperty="id">
    insert into pcloud_robot_classify (
        classify_name,
        is_delete,
        open_status
      )
    values (
      #{classifyName,jdbcType=VARCHAR},
      #{isDelete,jdbcType=INTEGER},
      #{openStatus}
      )
  </insert>

    <update id="update" parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudRobotClassify">
        update pcloud_robot_classify
        <set>
            <if test="classifyName != null">
                classify_name = #{classifyName},
            </if>
            <if test="linkUrl != null">
                link_url = #{linkUrl},
            </if>
            <if test="qrcodeUrl != null">
                qrcode_url = #{qrcodeUrl},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
            <if test="welcomeDuration != null">
                welcome_duration = #{welcomeDuration},
            </if>
            <if test="seqNum != null">
                seq_num = #{seqNum},
            </if>
            <if test="openStatus != null">
                open_status = #{openStatus},
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="listAllRobotClassify" resultType="com.pcloud.book.pcloudkeyword.facade.response.PcloudRobotClassifyResponseVO">
        select
        id, classify_name as classifyName,
        plan_top_pic planTopPic,
        plan_bottom_pic planBottomPic,
        open_status openStatus
        from pcloud_robot_classify where is_delete = 0
        order by seq_num
    </select>

    <select id="getById" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from pcloud_robot_classify where id=#{id}
    </select>

    <select id="listClassifyByPage" resultMap="pageResultMap" parameterType="map">
        select
        id, classify_name, qrcode_url, seq_num, mimi_app_qr miniAppQr,mini_app_content,open_status
        from pcloud_robot_classify
        where is_delete = 0
        <if test="testShow!=null and testShow==false">
            and open_status=1
        </if>
        order by seq_num
    </select>

    <update id="updateRobotClassifyKeywordClassify" parameterType="map">
        update pcloud_robot_classify
        set keyword_classify_id=#{keywordClassifyId}
        where id=#{id}
    </update>

    <update id="updateWelcomeDuration" parameterType="map">
        update pcloud_robot_classify
        set welcome_duration = #{welcomeDuration},
        is_open_push_h5url=#{isOpenPushH5url}
        where id=#{id}
    </update>

    <update id="updateSeqNum4RobotClassify" parameterType="com.pcloud.book.pcloudkeyword.vo.ClassifySeqNumUpdateVO">
        update pcloud_robot_classify
        set seq_num = #{seqNum} where id = #{id}
    </update>

    <update id="updateOpenStatus4RobotClassify" parameterType="com.pcloud.book.pcloudkeyword.vo.ClassifyOpenStatusUpdateVO">
        update pcloud_robot_classify
        set open_status = #{openStatus} where id = #{id}
    </update>

    <update id="updateMiniAppQr" parameterType="map">
        update pcloud_robot_classify set mimi_app_qr = #{url} where id = id
    </update>


    <select id="getClassifyInfo"  parameterType="long" resultType="com.pcloud.book.pcloudKeyword.dto.RobotClassifyDTO">
        select
        id classifyId,
        classify_name classifyName,
        qrcode_url qrcodeUrl,
        open_status openStatus
        from
        pcloud_robot_classify
        where
        id = #{robotType}
    </select>

    <select id="getOpenTempletIds" resultType="Long">
        select DISTINCT a.book_template_id
        FROM book_rays_classify a LEFT JOIN pcloud_robot_classify b
        on a.rays_classify_id=b.id
        where b.open_status=1
   </select>

    <select id="getClassifyInfoBatch" parameterType="map" resultType="com.pcloud.book.pcloudKeyword.dto.RobotClassifyDTO">
        select
        id classifyId,
        classify_name classifyName
        from
        pcloud_robot_classify
        where
        id in
        <foreach collection="classifyIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>