<?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.clock.dao.impl.BookClockKeywordDaoImpl">

    <!-- 微信群打卡实体 -->
    <resultMap id="BaseResultMap" type="com.pcloud.book.clock.entity.BookClockKeyword">
        <id column="book_clock_keyword_id" property="bookClockKeywordId" jdbcType="BIGINT"/>
        <result column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="operation" property="operation" jdbcType="VARCHAR"/>
        <result column="keyword" property="keyword" jdbcType="VARCHAR"/>
        <result column="guide" property="guide" jdbcType="VARCHAR"/>
        <result column="sequence_num" property="sequenceNum" 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"/>
    </resultMap>

    <!-- 微信群打卡实体DTO -->
    <resultMap id="BaseResultMapDTO" type="com.pcloud.book.clock.dto.BookClockKeywordDTO">
        <id column="book_clock_keyword_id" property="bookClockKeywordId" jdbcType="BIGINT"/>
        <result column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="operation" property="operation" jdbcType="VARCHAR"/>
        <result column="keyword" property="keyword" jdbcType="VARCHAR"/>
        <result column="guide" property="guide" jdbcType="VARCHAR"/>
        <result column="sequence_num" property="sequenceNum" jdbcType="INTEGER"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <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"/>
    </resultMap>

    <sql id="Base_Column_List">
        book_clock_info_id, operation, keyword, guide, sequence_num, create_user, create_time, update_user, update_time
    </sql>

    <!-- 新增关键词信息-->
    <insert id="insert" parameterType="bookClockKeyword" useGeneratedKeys="true" keyProperty="bookClockKeywordId" flushCache="true">
        INSERT INTO book_clock_keyword (
        book_clock_info_id, operation, keyword, guide, sequence_num, create_user, create_time, update_user, update_time
        )
        VALUES (
        #{bookClockInfoId, jdbcType=BIGINT}, #{operation, jdbcType=VARCHAR}, #{keyword, jdbcType=VARCHAR},
        #{guide, jdbcType=VARCHAR}, #{sequenceNum, jdbcType=BIGINT}, #{createUser, jdbcType=BIGINT}, NOW(), #{updateUser, jdbcType=BIGINT}, NOW()
        )
    </insert>

    <!--校验关键词有无重复-->
    <select id="checkKeywordHasExist" parameterType="map" resultType="long">
        SELECT DISTINCT(book_clock_info_id)
        FROM book_clock_keyword
        WHERE create_user = #{adviserId}
        AND keyword IN
        <foreach collection="keyWords" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>

    <!--获取该作品/应用操作最大排序值-->
    <select id="getMaxSequence4Keyword" parameterType="Long" resultType="long">
        SELECT sequence_num
        FROM book_clock_keyword
        WHERE book_clock_info_id = #{bookClockInfoId}
        ORDER BY sequence_num DESC
        LIMIT 1
    </select>

    <!--批量插入-->
    <insert id="batchInsert" parameterType="bookClockKeyword" keyProperty="bookClockKeywordId" useGeneratedKeys="true" flushCache="true">
        INSERT INTO book_clock_keyword (
        book_clock_info_id, operation, keyword, guide, sequence_num, create_user, create_time, update_user, update_time
        )
        VALUES
        <foreach collection="list" separator="," item="item" >
            (#{item.bookClockInfoId,jdbcType=BIGINT},
            #{item.operation,jdbcType=VARCHAR},
            #{item.keyword,jdbcType=VARCHAR},
            #{item.guide,jdbcType=VARCHAR},
            #{item.sequenceNum,jdbcType=BIGINT},
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW())
        </foreach>
    </insert>

    <!--删除旧的关键词-->
    <delete id="deleteBookClockKeyword" parameterType="map">
        DELETE FROM book_clock_keyword
        WHERE book_clock_info_id = #{bookClockInfoId}
    </delete>

    <!--获取微信打卡作品/应用关键词-->
    <select id="getKeywordsByInfoId" parameterType="map" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM book_clock_keyword
        WHERE book_clock_info_id = #{bookClockInfoId}
    </select>

    <!--获取该关键词已关联的书及分类-->
    <select id="getInfoIdByKeyword" parameterType="map" resultType="long">
        SELECT book_clock_info_id
        FROM book_clock_keyword
        WHERE create_user = #{adviserId}
        AND keyword = #{keyword}
    </select>

    <!--根据接收到的关键词获取对应的作品/应用ID-->
    <select id="getInfoIdByKeywordAndweixinGroupId" parameterType="map" resultMap="BaseResultMapDTO">
        SELECT
        bck.book_clock_info_id,
        bck.operation,
        bck.keyword,
        bck.guide,
        bgc.channel_id
        FROM
        book_group_clock bgc
        LEFT JOIN book_clock_keyword bck ON bgc.book_clock_info_id = bck.book_clock_info_id
        WHERE
        bck.keyword = #{keyword}
        AND bck.create_user = #{adviserId}
        AND (
        bgc.book_group_id = #{bookGroupId} OR bgc.classify_id = #{classifyId})
        LIMIT 1
    </select>

    <!--获取微信群打卡动作关键词-->
    <select id="getBookClockKeyword4Clock" parameterType="map" resultType="com.pcloud.book.clock.dto.BookGroupClockKeywordDTO">
        SELECT book_clock_info_id bookClockInfoId,
        operation,
        keyword,
        guide,
        sequence_num sequence,
        create_user createUser
        FROM book_clock_keyword
        WHERE book_clock_info_id = #{bookClockInfoId}
        AND operation = #{operation}
        LIMIT 1
    </select>

</mapper>