<?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.KeywordUseRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.KeywordUseRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="keyword_id" property="keywordId" jdbcType="BIGINT"/>
        <result column="keyword_detail_id" property="keywordDetailId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="use_num" property="useNum" jdbcType="INTEGER"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <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, keyword_detail_id, adviser_id, use_num, create_user, create_time, update_user, update_time,
    is_delete
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from book_keyword_use_record
        where id = #{id,jdbcType=BIGINT}
    </select>
    <select id="getByAdviserIdAndKeywordId" resultMap="BaseResultMap">
      select
      id, keyword_id, keyword_detail_id, adviser_id, use_num
      from book_keyword_use_record
      where adviser_id = #{adviserId,jdbcType=BIGINT} and keyword_detail_id = #{keywordDetailId,jdbcType=BIGINT}
    </select>
    <insert id="insert" parameterType="com.pcloud.book.book.entity.KeywordUseRecord">
    insert into book_keyword_use_record (id, keyword_id, keyword_detail_id, adviser_id,
      use_num, create_user, create_time, 
      update_user, update_time, is_delete
      )
    values (#{id,jdbcType=BIGINT}, #{keywordId,jdbcType=BIGINT}, #{keywordDetailId,jdbcType=BIGINT}, #{adviserId,jdbcType=BIGINT},
      #{useNum,jdbcType=INTEGER}, #{createUser,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateUser,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP}, #{isDelete,jdbcType=INTEGER}
      )
  </insert>
    <insert id="insertSelective" parameterType="com.pcloud.book.book.entity.KeywordUseRecord">
        insert into book_keyword_use_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="keywordId != null">
                keyword_id,
            </if>
            <if test="keywordDetailId != null">
                keyword_detail_id,
            </if>
            <if test="adviserId != null">
                adviser_id,
            </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=BIGINT},
            </if>
            <if test="keywordDetailId != null">
                #{keywordDetailId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="useNum != null">
                #{useNum,jdbcType=INTEGER},
            </if>
            <if test="createUser != null">
                #{createUser,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                #{updateUser,jdbcType=BIGINT},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="isDelete != null">
                #{isDelete,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>

    <select id="oftenKeyword" resultType="com.pcloud.book.book.vo.BookKeywordProductVO" parameterType="map">
        SELECT
            a.keyword_id as keywordId,
            a.keyword_detail_id as keywordDetailId,
            b.description,
            b.link_url as linkUrl,
            b.pic_url as picUrl,
            b.product_id as productId,
            b.product_type as productType,
            b.type_code as typeCode,
            b.content,
            c.matching_rule as matchingRule,
            a.use_num as useNum,
            c.guide
        FROM
            book_keyword_use_record a
        LEFT JOIN book_keyword_warehouse_detail b ON a.keyword_detail_id = b.id
        LEFT JOIN book_keyword_warehouse c ON a.keyword_id = c.id
        WHERE
        b.is_delete = 0
        AND a.adviser_id = #{adviserId}
        <if test="depthLabelId != null and depthLabelId != '' ">
            and b.depth_label_id = #{depthLabelId}
        </if>
        order by a.use_num DESC, a.create_time DESC
    </select>

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

    <update id="updateByPrimaryKeySelective"
            parameterType="com.pcloud.book.book.entity.KeywordUseRecord">
        update book_keyword_use_record
        <set>
            <if test="keywordId != null">
                keyword_id = #{keywordId,jdbcType=BIGINT},
            </if>
            <if test="keywordDetailId != null">
                keyword_detail_id = #{keywordDetailId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="useNum != null">
                use_num = #{useNum,jdbcType=INTEGER},
            </if>
            <if test="createUser != null">
                create_user = #{createUser,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </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.KeywordUseRecord">
    update book_keyword_use_record
    set keyword_id = #{keywordId,jdbcType=BIGINT},
      keyword_detail_id = #{keywordDetailId,jdbcType=BIGINT},
      adviser_id = #{adviserId,jdbcType=BIGINT},
      use_num = #{useNum,jdbcType=INTEGER},
      create_user = #{createUser,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      is_delete = #{isDelete,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>