<?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.KeywordWarehouseDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.KeywordWarehouse" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="keyword" property="keyword" jdbcType="VARCHAR" />
    <result column="guide" property="guide" jdbcType="VARCHAR" />
    <result column="scope" property="scope" jdbcType="INTEGER" />
    <result column="matching_rule" property="matchingRule" jdbcType="INTEGER" />
    <result column="audit_status" property="auditStatus" jdbcType="INTEGER" />
    <result column="editor_id" property="editorId" 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, guide, scope, matching_rule, audit_status, editor_id, create_user, create_time, update_user,
    update_time, is_delete
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from book_keyword_warehouse
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="listByIds" parameterType="list" resultMap="BaseResultMap">
    select id, keyword, guide, scope, matching_rule, audit_status, editor_id from book_keyword_warehouse
    where id in
    <foreach collection="list" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
    order by id desc
  </select>

  <select id="listBookKeywordWarehouse" resultType="com.pcloud.book.book.vo.response.BookKeywordResponseVO" parameterType="map">
    select
    a.id, a.keyword, a.guide, a.matching_rule as matchingRule, editor_id as editorId, a.scope
    from book_keyword_warehouse a left join book_keyword_warehouse_detail b on a.id = b.keyword_id
    where a.is_delete = 0 and b.is_delete = 0
    <if test="keyword != null and keyword != '' ">
      and a.keyword LIKE CONCAT('%', #{keyword},'%')
    </if>
    <if test="professionalLabelId != null and professionalLabelId != '' ">
      and b.professional_label_id = #{professionalLabelId}
    </if>
    <if test="depthLabelId != null and depthLabelId != '' ">
      and b.depth_label_id = #{depthLabelId}
    </if>
    <if test="purposeLabelId != null and purposeLabelId != '' ">
      and b.purpose_label_id = #{purposeLabelId}
    </if>
    <if test="id != null and id != '' ">
      and a.id = #{id}
    </if>
    group by a.id
    order by a.create_time desc
  </select>

  <select id="checkKeyword" resultType="com.pcloud.book.book.vo.response.CheckKeywordResponseVO">
     SELECT
        a.keyword_id as keywordId,
        a.depth_label_id as depthLabelId,
        count(*) AS num
    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
    AND b.keyword = #{keyword}
    AND a.depth_label_id IN
    <foreach collection="list" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </select>

  <insert id="insert" parameterType="com.pcloud.book.book.entity.KeywordWarehouse" useGeneratedKeys="true" keyProperty="id" >
    insert into book_keyword_warehouse (id, keyword, guide, scope,
      matching_rule, audit_status, editor_id,
      create_user, create_time, update_user, 
      update_time, is_delete)
    values (#{id,jdbcType=BIGINT}, #{keyword,jdbcType=VARCHAR}, #{guide,jdbcType=VARCHAR}, #{scope,jdbcType=INTEGER},
      #{matchingRule,jdbcType=INTEGER}, #{auditStatus,jdbcType=INTEGER}, #{editorId,jdbcType=INTEGER},
      #{createUser,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=INTEGER}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{isDelete,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.book.entity.KeywordWarehouse" >
    insert into book_keyword_warehouse
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="guide != null" >
        guide,
      </if>
      <if test="keyword != null" >
        keyword,
      </if>
      <if test="scope != null" >
        scope,
      </if>
      <if test="matchingRule != null" >
        matching_rule,
      </if>
      <if test="auditStatus != null" >
        audit_status,
      </if>
      <if test="editorId != null" >
        editor_id,
      </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="guide != null" >
        #{guide,jdbcType=VARCHAR},
      </if>
      <if test="keyword != null" >
        #{keyword,jdbcType=VARCHAR},
      </if>
      <if test="scope != null" >
        #{scope,jdbcType=INTEGER},
      </if>
      <if test="matchingRule != null" >
        #{matchingRule,jdbcType=INTEGER},
      </if>
      <if test="auditStatus != null" >
        #{auditStatus,jdbcType=INTEGER},
      </if>
      <if test="editorId != null" >
        #{editorId,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.KeywordWarehouse" >
    update book_keyword_warehouse
    <set >
      <if test="keyword != null" >
        keyword = #{keyword,jdbcType=VARCHAR},
      </if>
      <if test="scope != null" >
        scope = #{scope,jdbcType=INTEGER},
      </if>
      <if test="guide != null" >
        guide = #{guide,jdbcType=VARCHAR},
      </if>
      <if test="matchingRule != null" >
        matching_rule = #{matchingRule,jdbcType=INTEGER},
      </if>
      <if test="auditStatus != null" >
        audit_status = #{auditStatus,jdbcType=INTEGER},
      </if>
      <if test="editorId != null" >
        editor_id = #{editorId,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.KeywordWarehouse" >
    update book_keyword_warehouse
    set keyword = #{keyword,jdbcType=VARCHAR},
      scope = #{scope,jdbcType=INTEGER},
      guide = #{guide,jdbcType=VARCHAR},
      matching_rule = #{matchingRule,jdbcType=INTEGER},
      audit_status = #{auditStatus,jdbcType=INTEGER},
      editor_id = #{editorId,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>