<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.keywords.dao.impl.BookKeywordDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.keywords.entity.BookKeyword">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="keyword_id" property="keywordId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="rank" property="rank" jdbcType="TINYINT"/>
        <result column="set_type" property="setType" jdbcType="TINYINT"/>
        <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_warehouse" property="isWarehouse" jdbcType="TINYINT"/>
        <result column="warehouse_id" property="warehouseId" jdbcType="BIGINT"/>
        <result column="is_edit" property="isEdit" jdbcType="TINYINT"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,keyword_id,book_group_id, book_id, channel_id, classify_id, rank, set_type, is_delete,create_user, create_time, update_user,
        update_time, is_warehouse, warehouse_id, is_edit
    </sql>
    <insert id="insert" parameterType="bookKeyword" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_id,
            channel_id,
            book_group_id,
            classify_id,
            set_type,
            rank,
            keyword_id,
            is_warehouse,
            warehouse_id,
            create_user,
            create_time,
            is_edit,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{setType,jdbcType=TINYINT},
            #{rank,jdbcType=TINYINT},
            #{keywordId,jdbcType=BIGINT},
            #{isWarehouse,jdbcType=TINYINT},
            #{warehouseId,jdbcType=BIGINT},
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{isEdit,jdbcType=TINYINT},
        </trim>
    </insert>
    
    <insert id="batchInsert" parameterType="bookKeyword" useGeneratedKeys="true" keyProperty="id">
        insert into book_keyword(book_id,
        channel_id,
        book_group_id,
        classify_id,
        set_type,
        rank,
        keyword_id,
        is_warehouse,
        warehouse_id,
        create_user,
        create_time,
        is_edit) values
        <foreach collection="list" item="item" separator=",">
            (#{item.bookId,jdbcType=BIGINT},
            #{item.channelId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.setType,jdbcType=TINYINT},
            #{item.rank,jdbcType=TINYINT},
            #{item.keywordId,jdbcType=BIGINT},
            #{item.isWarehouse,jdbcType=TINYINT},
            #{item.warehouseId,jdbcType=BIGINT},
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.isEdit,jdbcType=TINYINT})
        </foreach>
    </insert>

    <update id="deleteById" parameterType="map">
        update book_keyword
        <set>
            is_delete = 1,
            update_user = #{updateUser},
            update_time = now()
        </set>
        where keyword_id = #{keywordId,jdbcType=BIGINT}
    </update>

    <update id="setRank" parameterType="bookKeyword">
        update book_keyword
        <set>
            rank = #{rank},
            update_user = #{updateUser},
            update_time = now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getMaxRank" resultType="Integer" parameterType="map">
        select
            IFNULL(MAX(rank), 0)
        from
            book_keyword
        where
            classify_id = #{classifyId}
        <if test="bookGroupId!=null">
            AND book_group_id = #{bookGroupId}
        </if>
          AND is_delete = 0
    </select>

    <select id="listKeywordsByClassify" resultType="ListKeywordVO" parameterType="map">
        SELECT
            bk.id bookKeywordId,
            k.id keywordId,
            k.keywords,
            k.matching_rule matchingRule,
            k.guide,
            bk.rank,
            k.content,
            k.description,
            k.link_url linkUrl,
            k.pic_url picUrl,
            k.reply_type replyType,
            k.serve_id serveId,
            k.serve_type serveType,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            bk.is_edit as isEdit
        FROM
            book_keyword bk
        JOIN
            keyword k ON bk.keyword_id = k.id
        WHERE
            bk.is_delete = 0
        AND
            k.is_delete = 0
        AND classify_id = #{classifyId}
        <if test="bookGroupId!=null">
            AND book_group_id = #{bookGroupId}
        </if>
        <if test="keywords!=null">
            AND keywords like concat('%', #{keywords}, '%')
        </if>
        order by bk.rank ASC, bk.update_time desc, bk.id DESC
    </select>

    <select id="listFiveKeyword" resultType="com.pcloud.book.keywords.dto.KeywordDTO" parameterType="map">
        SELECT
            k.keywords,
            k.id keywordId,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            k.guide,
            k.content,
            k.description,
            k.link_url as linkUrl,
            k.serve_id AS serveId,
            k.serve_type AS serveType,
            k.reply_type AS replyType
        FROM
            book_keyword bk
        JOIN
            keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND
        k.is_delete = 0
        AND classify_id = #{classifyId}
        AND book_group_id = #{bookGroupId} order by bk.rank, bk.id desc
    </select>

    <select id="getKeywordId" resultType="ReplyKeywordDTO" parameterType="map">
        SELECT
            k.id keywordId,
            k.keywords,
            k.reply_type replyType,
            k.content,
            k.description,
            k.link_url linkUrl,
            k.pic_url picUrl,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            k.reply_type replyType,
            k.serve_id as serveId,
            k.serve_type as serveType
        FROM
            book_keyword bk
        JOIN
            keyword k ON bk.keyword_id = k.id
        WHERE
            bk.is_delete = 0 AND k.is_delete = 0
            AND classify_id in (0, ${classifyId})
            AND book_group_id = #{bookGroupId}
            AND ((k.keywords = #{content} and matching_rule = 1 ) or (k.keywords like
            concat('%',#{content},'%') and matching_rule = 0))
            order by set_type desc, matching_rule desc, bk.id desc limit 1
    </select>

    <select id="listAllKeywordByClassifyIdAndBookGroupId" resultType="ReplyKeywordDTO" parameterType="map">
        SELECT
            k.id keywordId,
            k.keywords,
            k.reply_type replyType,
            k.content,
            k.description,
            k.link_url linkUrl,
            k.pic_url picUrl,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            k.reply_type replyType,
            k.serve_id as serveId,
            k.serve_type as serveType
        FROM
            book_keyword bk
        JOIN
            keyword k ON bk.keyword_id = k.id
        WHERE
            bk.is_delete = 0 AND k.is_delete = 0
            AND classify_id in (0, ${classifyId})
            AND book_group_id = #{bookGroupId}
        GROUP BY k.id
        order by set_type desc, matching_rule desc, bk.id desc
    </select>

    <select id="getKeywordIds" resultType="ReplyKeywordDTO" parameterType="map">
        SELECT
            k.id keywordId,
            k.keywords,
            k.reply_type replyType,
            k.content,
            k.description,
            k.link_url linkUrl,
            k.pic_url picUrl,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            bk.classify_id as classifyId,
            bk.book_group_id as bookGroupId,
            k.reply_type replyType
        FROM
            book_keyword bk
        JOIN
            keyword k ON bk.keyword_id = k.id
        WHERE
            bk.is_delete = 0 AND k.is_delete = 0
            AND classify_id in (0, ${classifyId})
            AND book_group_id in (${bookGroupId})
            AND ((k.keywords = #{content} and matching_rule = 1 ) or (k.keywords like
            concat('%',#{content},'%') and matching_rule = 0))
            order by set_type desc, matching_rule desc, bk.id desc
    </select>

    <select id="checkKeyword" resultType="Boolean" parameterType="map">
        SELECT
            count(1)
        FROM
            book_keyword bk
        JOIN keyword k ON bk.keyword_id = k.id
        WHERE
            classify_id = #{classifyId}
        AND book_group_id = #{bookGroupId}
        <if test="keywordId != null">
            AND keyword_id != #{keywordId}
        </if>
        AND keywords = #{keywords}
        AND bk.is_delete = 0
        AND k.is_delete = 0
    </select>

    <update id="updateIsWarehouse" parameterType="long" >
        update book_keyword set is_warehouse = 1, warehouse_id = #{warehouseId} where id = #{bookKeywordId}
    </update>

    <select id="getByBookKeyword" resultType="KeywordVO" parameterType="Long">
        SELECT
            k.id keywordId,
            bk.id bookKeywordId,
            bk.classify_id classifyId,
            bk.book_group_id bookGroupId,
            keywords,
            matching_rule matchingRule,
            guide,
            content,
            description,
            link_url linkUrl,
            pic_url picUrl,
            reply_type replyType,
            serve_id serveId,
            bk.is_warehouse as isWarehouse,
            bk.warehouse_id as warehouseId,
            serve_type serveType
        FROM
            book_keyword bk
        JOIN
            keyword k
        ON
            bk.keyword_id = k.id
        WHERE
          bk.id = #{_parameter} AND bk.is_delete = 0 AND k.is_delete = 0 limit 1
    </select>


    <select id="getKeywordCount" resultType="Integer" parameterType="map">
        SELECT
          count(1)
        FROM
        book_keyword bk
        JOIN
        keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND
        k.is_delete = 0
        <if test="classifyId != null">
            AND classify_id = #{classifyId}
        </if>
        AND book_group_id = #{bookGroupId}
    </select>

    <!--根据社群书id获取关键词统计-->
    <select id="getKeywordInfoByBookGroupId" parameterType="map" resultType="com.pcloud.book.keywords.dto.KeywordStatisticsDTO">
        SELECT
        t.keyword_id as keywordId,
        t.create_time as keywordCreateDate,
        t.is_warehouse as isWarehouse,
        t.warehouse_id as warehouseId,
        t2.keywords as keyword
        FROM
        book_keyword t
        INNER JOIN keyword t2 on t.keyword_id=t2.id
        WHERE
        t.is_delete = 0
        AND t.book_group_id = #{bookGroupId}
        <if test="classifyId!=null">
            AND t.classify_id=#{classifyId}
        </if>
        GROUP BY t.keyword_id
        order by t.create_time desc
    </select>

    <!--根据分类id获取-->
    <select id="getKeywordsByClassifyId" parameterType="map" resultType="com.pcloud.book.keywords.vo.ListKeywordVO">
        SELECT
        bk.id bookKeywordId,
        k.id keywordId,
        k.keywords,
        k.matching_rule matchingRule,
        k.guide,
        bk.rank,
        k.content,
        k.description,
        k.link_url linkUrl,
        k.pic_url picUrl,
        k.reply_type replyType,
        k.serve_id serveId,
        k.serve_type serveType,
        bk.classify_id classifyId
        FROM
        book_keyword bk
        JOIN keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND k.is_delete = 0
        AND (
        bk.classify_id = #{classifyId}  OR bk.classify_id = 0
        )
        AND bk.book_id = #{bookId}
        GROUP BY
        bk.keyword_id
        ORDER BY
        bk.rank ASC,bk.id DESC
    </select>

    <select id="getKeywordsIdsByBookGroupId" parameterType="map" resultType="com.pcloud.book.group.vo.SyncKeyworsVO">
        select k.id keywordsId, bk.is_warehouse isWarehouse, bk.warehouse_id warehouseId from book_keyword bk
        JOIN keyword k ON bk.keyword_id = k.id where bk.book_group_id = #{bookGroupId} and bk.classify_id = #{classifyId}
        and k.is_delete = 0 and bk.is_delete = 0
        group by k.id
    </select>

    <update id="deleteKeywords" parameterType="map">
        update book_keyword
        <set>
            is_delete = 1,
            update_user = #{partyId},
            update_time = now()
        </set>
        where keyword_id = #{keywordId,jdbcType=BIGINT}
        and book_group_id = #{bookGroupId,jdbcType=BIGINT}
        and classify_id = #{classifyId,jdbcType=BIGINT}
    </update>

    <update id="updateRank" parameterType="map">
        update book_keyword
        <set>
            rank = #{rank},
            update_user = #{partyId},
            update_time = now()
        </set>
        where keyword_id = #{keywordId,jdbcType=BIGINT}
        and book_group_id = #{bookGroupId,jdbcType=BIGINT}
        and is_delete = 0
    </update>
    
    <select id="getServiceByWeixinGroup" parameterType="map" resultType="com.pcloud.book.keywords.dto.ServiceResourceDTO">
        SELECT
        t.channel_id as channelId,
        t2.pic_url as coverImg,
        t2.serve_id as fromId,
        t2.serve_type as typeCode,
        t2.link_url as url,
        t2.keywords as keyword
        FROM
        book_keyword t
        INNER JOIN keyword t2 on t.keyword_id=t2.id
        WHERE
        t.is_delete = 0
        AND t2.is_delete = 0
        AND t.book_group_id = #{bookGroupId}
        AND t2.reply_type = 4
        order by t.create_time desc
    </select>

    <!--根据社群码id获取列表-->
    <select id="getListByBookGroupId" resultType="com.pcloud.book.keywords.dto.KeywordDTO" parameterType="Long">
        SELECT
        k.keywords,
        k.id keywordId,
        bk.is_warehouse as isWarehouse,
        bk.warehouse_id as warehouseId,
        k.guide
        FROM
        book_keyword bk
        JOIN
        keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND
        k.is_delete = 0
        AND book_group_id = #{bookGroupId} order by bk.rank, bk.id desc
    </select>

    <!--根据社群码id获取列表-->
    <select id="getListByBookGroupIdsAndType" resultType="com.pcloud.book.keywords.dto.KeywordDTO" parameterType="map">
        SELECT
        k.keywords,
        k.id keywordId,
        bk.is_warehouse as isWarehouse,
        bk.warehouse_id as warehouseId,
        bk.book_group_id bookGroupId,
        k.guide,
        k.reply_type replyType,
        k.content,
        k.description,
        k.link_url linkUrl,
        k.serve_id serveId,
        k.serve_type serveType,
        bk.create_user createUser
        FROM
        book_keyword bk
        JOIN
        keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND
        k.is_delete = 0
        AND bk.book_group_id in
        <foreach collection="bookGroupIds" separator="," index="index" item="item" open="(" close=")">
            #{item}
        </foreach>
        and k.reply_type=#{replyType}
        group by k.id,bk.book_group_id
    </select>

    <!--根据社群码id逻辑删除-->
    <update id="deleteByBookGroupIds" parameterType="list">
        update book_keyword set is_delete=1,update_time=now()
        where book_group_id in
        <foreach collection="list" separator="," index="index" item="item" open="(" close=")">
            #{item}
        </foreach>
    </update>

    <!--社群码下资源数量（关键词及1v1配置资源）-->
    <select id="getBookGroupResourceCount" parameterType="map" resultType="com.pcloud.book.keywords.dto.KeywordResourceDTO">
        SELECT
        COUNT(1) count,
        a.book_group_id bookGroupId
        FROM
        (
        SELECT
            serve_id,
            serve_type,
            book_group_id
        FROM
            book_keyword b
        JOIN keyword k ON b.keyword_id = k.id
        WHERE
            b.is_delete = 0
        AND k.is_delete = 0
        AND b.book_group_id IN
        <foreach collection="bookGroupIds" index="index" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
        AND k.serve_type IN ('APP', 'PRODUCT')
        UNION
            SELECT
                serve_id,
                serve_type,
                book_group_id
            FROM
                book_group_serve
            WHERE 1 = 1
            AND book_group_id IN
            <foreach collection="bookGroupIds" index="index" item="item" open="(" separator="," close=")">
                ${item}
            </foreach>
        ) AS a
        GROUP BY
        a.book_group_id
    </select>

    <!--社群码下资源列表（关键词及1v1配置资源）-->
    <select id="getBookGroupResourceList" parameterType="map" resultType="com.pcloud.book.group.dto.BookGroupKeywordResourceDTO">
        SELECT
            serve_id serveId,
            serve_type serveType, link_url serveUrl
        FROM
            book_keyword b
        JOIN keyword k ON b.keyword_id = k.id
        WHERE
            b.is_delete = 0
        AND k.is_delete = 0
        AND b.book_group_id = #{bookGroupId}
        AND k.serve_type IN ('APP', 'PRODUCT')
        UNION
            SELECT
                serve_id serveId,
                serve_type serveType, serve_url serveUrl
            FROM
                book_group_serve
            WHERE
                1 = 1
            AND book_group_id = #{bookGroupId}
    </select>

    <!--查询所有的作品应用类型的关键词-->
    <select id="getAllAppKeyword" resultType="com.pcloud.book.group.entity.BookGroupApp">
        SELECT
        t.book_group_id bookGroupId,
        IFNULL(t.classify_id, 0) classifyId,
        t1.serve_id serveId,
        t1.serve_type serveType,
        t.create_user createUser,
        2 AS originType
        FROM
        book_keyword t
        INNER JOIN keyword t1 ON t.keyword_id = t1.id
        WHERE
        t.is_delete = 0
        AND t1.is_delete = 0
        AND t1.reply_type = 4
        GROUP BY
        t.book_group_id,
        t.classify_id,
        t1.serve_id,
        t1.serve_type
    </select>

    <select id="getAppKeywordsByClassifyId" parameterType="map" resultType="com.pcloud.book.keywords.vo.ListKeywordVO">
        SELECT
        bk.id bookKeywordId,
        k.id keywordId,
        k.keywords,
        k.matching_rule matchingRule,
        k.guide,
        bk.rank,
        k.content,
        k.description,
        k.link_url linkUrl,
        k.pic_url picUrl,
        k.reply_type replyType,
        k.serve_id serveId,
        k.serve_type serveType,
        bk.classify_id classifyId
        FROM
        book_keyword bk
        JOIN keyword k ON bk.keyword_id = k.id
        WHERE
        bk.is_delete = 0
        AND k.is_delete = 0
        AND bk.classify_id = #{classifyId}
        AND bk.book_group_id = #{bookGroupId}
        AND k.reply_type = 4
        <if test="keywordIdList != null and keywordIdList.size>0">
            AND k.id NOT IN
            <foreach collection = "keywordIdList" index="index" item = "item" open = "("  separator= "," close = ")">
            ${item}
        </foreach>
        </if>
        GROUP BY
        bk.keyword_id
        ORDER BY
        bk.rank ASC,
        bk.update_time DESC,
        bk.id DESC
    </select>

</mapper>