<?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.book.dao.impl.KeywordWarehouseDetailDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.KeywordWarehouseDetail">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="keyword_id" property="keywordId" jdbcType="INTEGER"/>
        <result column="professional_label_id" property="professionalLabelId" jdbcType="INTEGER"/>
        <result column="depth_label_id" property="depthLabelId" jdbcType="INTEGER"/>
        <result column="purpose_label_id" property="purposeLabelId" jdbcType="INTEGER"/>
        <result column="group_id" property="groupId" jdbcType="CHAR"/>
        <result column="product_id" property="productId" jdbcType="INTEGER"/>
        <result column="product_type" property="productType" jdbcType="VARCHAR"/>
        <result column="channel_id" property="channelId" jdbcType="INTEGER"/>
        <result column="type_code" property="typeCode" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="pic_url" property="picUrl" jdbcType="VARCHAR"/>
        <result column="use_num" property="useNum" jdbcType="INTEGER"/>
        <result column="create_user" property="createUser" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="INTEGER"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="is_delete" property="isDelete" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
    id, keyword_id, professional_label_id, depth_label_id, purpose_label_id, group_id, product_id,
    product_type, channel_id, type_code, description, content, link_url, pic_url, use_num, create_user, create_time, update_user,
    update_time, is_delete
  </sql>

    <update id="addOne">
    update book_keyword_warehouse_detail set use_num = use_num + 1, update_user = #{adviserId}, update_time = NOW() where id = #{id,jdbcType=BIGINT}
  </update>

    <select id="hotKeyword" parameterType="map" resultType="com.pcloud.book.book.vo.BookKeywordProductVO" >
        SELECT
            a.keyword_id as keywordId,
            a.id as keywordDetailId,
            b.guide,
            a.description,
            a.content,
            a.link_url as linkUrl,
            a.pic_url as picUrl,
            a.product_id as productId,
            a.product_type as productType,
            a.channel_id as channelId,
            a.type_code as typeCode,
            b.keyword,
            b.matching_rule as matchingRule,
            a.use_num as useNum,
            b.guide
        FROM
            book_keyword_warehouse_detail a left join book_keyword_warehouse b
            on a.keyword_id = b.id
        WHERE
        a.is_delete = 0 and b.is_delete = 0
        <if test="isForAdviser!=null">
            <if test="isForAdviser">
                and b.editor_id = #{adviserId}
            </if>
            <if test="!isForAdviser">
                and b.scope = 0
            </if>
        </if>
        <if test="isForAdviser == null">
            and (b.scope = 0 or b.editor_id = #{adviserId})
        </if>
        <if test="depthLabelId != null and depthLabelId != '' ">
            and a.depth_label_id = #{depthLabelId}
        </if>
        <if test="keyword != null and keyword != '' ">
            and b.keyword like CONCAT('%', #{keyword},'%')
        </if>
        ORDER BY
          a.use_num DESC, a.create_time DESC
    </select>

    <update id="deleteDetails" parameterType="long">
    update book_keyword_warehouse_detail set is_delete = 1 where keyword_id = #{keywordId} and is_delete = 0
  </update>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from book_keyword_warehouse_detail
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="listByKeywordIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from book_keyword_warehouse_detail
        where is_delete = 0
        and keyword_id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ORDER BY
        id ASC
    </select>

    <insert id="insert" parameterType="com.pcloud.book.book.entity.KeywordWarehouseDetail" useGeneratedKeys="true" keyProperty="id">
    insert into book_keyword_warehouse_detail (id, keyword_id, professional_label_id,
      depth_label_id, purpose_label_id, group_id, product_id, product_type, channel_id, type_code, description,content, link_url, pic_url, use_num,
      create_user, create_time, update_user, update_time, is_delete)
    values (#{id,jdbcType=BIGINT}, #{keywordId,jdbcType=INTEGER}, #{professionalLabelId,jdbcType=INTEGER},
      #{depthLabelId,jdbcType=INTEGER}, #{purposeLabelId,jdbcType=INTEGER}, #{groupId,jdbcType=CHAR},
      #{productId,jdbcType=INTEGER},#{productType,jdbcType=VARCHAR}, #{channelId,jdbcType=INTEGER},#{typeCode,jdbcType=VARCHAR},
      #{description,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},
      #{linkUrl,jdbcType=VARCHAR}, #{picUrl,jdbcType=VARCHAR}, #{useNum,jdbcType=INTEGER}, #{createUser,jdbcType=INTEGER},
      #{createTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=INTEGER}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{isDelete,jdbcType=INTEGER})
  </insert>

    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into book_keyword_warehouse_detail (keyword_id, professional_label_id,
        depth_label_id, purpose_label_id, group_id, product_id, product_type, channel_id, type_code, description,content, link_url,
        pic_url, use_num,
        create_user, create_time, update_user, update_time, is_delete)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.keywordId,jdbcType=INTEGER}, #{item.professionalLabelId,jdbcType=INTEGER},
            #{item.depthLabelId,jdbcType=INTEGER}, #{item.purposeLabelId,jdbcType=INTEGER},
            #{item.groupId,jdbcType=CHAR},
            #{item.productId,jdbcType=INTEGER},#{item.productType,jdbcType=VARCHAR}, #{item.channelId,jdbcType=INTEGER},
            #{item.typeCode,jdbcType=VARCHAR},#{item.description,jdbcType=VARCHAR},#{item.content,jdbcType=VARCHAR},
            #{item.linkUrl,jdbcType=VARCHAR}, #{item.picUrl,jdbcType=VARCHAR},
            #{item.useNum,jdbcType=INTEGER}, #{item.createUser,jdbcType=INTEGER},
            #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateUser,jdbcType=INTEGER},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.isDelete,jdbcType=INTEGER})
        </foreach>
    </insert>

    <insert id="insertSelective" parameterType="com.pcloud.book.book.entity.KeywordWarehouseDetail">
        insert into book_keyword_warehouse_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="keywordId != null">
                keyword_id,
            </if>
            <if test="professionalLabelId != null">
                professional_label_id,
            </if>
            <if test="depthLabelId != null">
                depth_label_id,
            </if>
            <if test="purposeLabelId != null">
                purpose_label_id,
            </if>
            <if test="groupId != null">
                group_id,
            </if>
            <if test="productId != null">
                product_id,
            </if>
            <if test="productType != null">
                product_type,
            </if>
            <if test="channelId != null">
                channel_id,
            </if>
            <if test="typeCode != null">
                type_code,
            </if>
            <if test="content != null">
                content,
            </if>
            <if test="useNum != null">
                use_num,
            </if>
            <if test="createUser != null">
                create_user,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateUser != null">
                update_user,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="isDelete != null">
                is_delete,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="keywordId != null">
                #{keywordId,jdbcType=INTEGER},
            </if>
            <if test="professionalLabelId != null">
                #{professionalLabelId,jdbcType=INTEGER},
            </if>
            <if test="depthLabelId != null">
                #{depthLabelId,jdbcType=INTEGER},
            </if>
            <if test="purposeLabelId != null">
                #{purposeLabelId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                #{groupId,jdbcType=CHAR},
            </if>
            <if test="productId != null">
                #{productId,jdbcType=INTEGER},
            </if>
            <if test="productType != null">
                #{productType,jdbcType=VARCHAR},
            </if>
            <if test="channelId != null">
                #{channelId,jdbcType=INTEGER},
            </if>
            <if test="typeCode != null">
                #{typeCode,jdbcType=VARCHAR},
            </if>
            <if test="content != null">
                #{content,jdbcType=VARCHAR},
            </if>
            <if test="useNum != null">
                #{useNum,jdbcType=INTEGER},
            </if>
            <if test="createUser != null">
                #{createUser,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                #{updateUser,jdbcType=INTEGER},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="isDelete != null">
                #{isDelete,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.pcloud.book.book.entity.KeywordWarehouseDetail">
        update book_keyword_warehouse_detail
        <set>
            <if test="keywordId != null">
                keyword_id = #{keywordId,jdbcType=INTEGER},
            </if>
            <if test="professionalLabelId != null">
                professional_label_id = #{professionalLabelId,jdbcType=INTEGER},
            </if>
            <if test="depthLabelId != null">
                depth_label_id = #{depthLabelId,jdbcType=INTEGER},
            </if>
            <if test="purposeLabelId != null">
                purpose_label_id = #{purposeLabelId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=CHAR},
            </if>
            <if test="productId != null">
                product_id = #{productId,jdbcType=INTEGER},
            </if>
            <if test="productType != null">
                product_type = #{productType,jdbcType=VARCHAR},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=INTEGER},
            </if>
            <if test="typeCode != null">
                type_code = #{typeCode,jdbcType=VARCHAR},
            </if>
            <if test="content != null">
                content = #{content,jdbcType=VARCHAR},
            </if>
            <if test="useNum != null">
                use_num = #{useNum,jdbcType=INTEGER},
            </if>
            <if test="createUser != null">
                create_user = #{createUser,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=INTEGER},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey"
            parameterType="com.pcloud.book.book.entity.KeywordWarehouseDetail">
    update book_keyword_warehouse_detail
    set keyword_id = #{keywordId,jdbcType=INTEGER},
      professional_label_id = #{professionalLabelId,jdbcType=INTEGER},
      depth_label_id = #{depthLabelId,jdbcType=INTEGER},
      purpose_label_id = #{purposeLabelId,jdbcType=INTEGER},
      group_id = #{groupId,jdbcType=CHAR},
      product_id = #{productId,jdbcType=INTEGER},
      product_type = #{productType,jdbcType=VARCHAR},
      channel_id = #{channelId,jdbcType=INTEGER},
      content = #{content,jdbcType=VARCHAR},
      type_code = #{typeCode,jdbcType=VARCHAR},
      use_num = #{useNum,jdbcType=INTEGER},
      create_user = #{createUser,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      is_delete = #{isDelete,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>