<?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.SelfRobotKeywordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.keywords.entity.SelfRobotKeyword">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="keyword" property="keyword" jdbcType="VARCHAR"/>
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="delay_time" property="delayTime" jdbcType="INTEGER"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, book_group_id, keyword, seq_num, delay_time, create_user, create_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.keywords.entity.SelfRobotKeyword" useGeneratedKeys="true"
            keyProperty="id">
        insert into self_robot_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_group_id,
            keyword,
            seq_num,
            delay_time,
            create_user,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookGroupId,jdbcType=BIGINT},
            #{keyword,jdbcType=VARCHAR},
            #{seqNum,jdbcType=INTEGER},
            #{delayTime,jdbcType=INTEGER},
            #{createUser,jdbcType=BIGINT},
            NOW()
        </trim>
    </insert>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.keywords.entity.SelfRobotKeyword" useGeneratedKeys="true" keyProperty="id">
        insert into self_robot_keyword (
        book_group_id,
        keyword,
        seq_num,
        delay_time,
        create_user,
        create_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.keyword,jdbcType=VARCHAR},
            #{item.seqNum,jdbcType=INTEGER},
            #{item.delayTime,jdbcType=INTEGER},
            #{item.createUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <update id="updateSelfRobotKeyword" parameterType="com.pcloud.book.keywords.entity.SelfRobotKeyword">
        update self_robot_keyword
        <set>
            book_group_id = #{bookGroupId,jdbcType=BIGINT},
            keyword = #{keyword,jdbcType=VARCHAR},
            seq_num = #{seqNum,jdbcType=INTEGER},
            delay_time = #{delayTime,jdbcType=INTEGER}
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--删除-->
    <delete id="deleteById" parameterType="long">
        delete from self_robot_keyword
        where id=#{id}
    </delete>

    <!--根据社群码id集合删除-->
    <delete id="deleteByBookGroupIds" parameterType="map">
        delete from self_robot_keyword
        where book_group_id in
        <foreach collection="bookGroupIds" open="(" close=")" item="item" separator=",">
            ${item}
        </foreach>
    </delete>

    <!--根据社群书id获取-->
    <select id="getListByBookGroupId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from self_robot_keyword
        where book_group_id=#{bookGroupId}
        order by seq_num
    </select>

    <select id="getNextKeyWord" parameterType="map" resultMap="BaseResultMap">
        SELECT
        k.id, k.book_group_id, k.keyword, k.seq_num, k.delay_time, k.create_user, k.create_time
        FROM
        self_robot_keyword k
        LEFT JOIN self_robot_guide g ON k.id = g.keyword_id
        WHERE
        k.book_group_id = #{bookGroupId}
        AND k.seq_num > (SELECT seq_num FROM self_robot_keyword WHERE keyword = #{keyword} AND book_group_id=#{bookGroupId} LIMIT 1)
        AND k.delay_time IS NOT NULL
        AND g.id > 0
        ORDER BY
        k.seq_num
        LIMIT 1
    </select>

    <!--根据社群码和关键词获取-->
    <select id="getByBookGroupIdAndKeyword" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from self_robot_keyword
        where book_group_id=#{bookGroupId}
        and keyword=#{keyword}
        limit 1
    </select>

    <!--根据社群码id获取数量-->
    <select id="getCountByBookGroupId" parameterType="map" resultType="integer">
        select count(1)
        from self_robot_keyword
        where book_group_id=#{bookGroupId}
    </select>

    <!--批量获取最大排序值-->
    <select id="getMaxSeqNumByBookGroupIds" parameterType="map" resultType="com.pcloud.book.keywords.dto.MaxSeqNAndBGDTO">
        select
            book_group_id bookGroupId,
            ifnull(max(seq_num), 0) maxSeqNum
        from self_robot_keyword
        where book_group_id in
        <foreach collection="bookGroupIds" open="(" close=")" item="item" separator=",">
            ${item}
        </foreach>
        group by book_group_id
    </select>

    <!--获取列表-->
    <select id="getSelfRobotKeywordList" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from self_robot_keyword
        where book_group_id=#{bookGroupId}
        <if test="keyword!=null">
            and keyword like concat('%', #{keyword},'%')
        </if>
        order by seq_num
    </select>

    <!--更新最大排序值-->
    <update id="updateSeqNum" parameterType="map">
        update self_robot_keyword
        set seq_num=#{seqNum}
        where id=#{id}
    </update>

    <!--获取关键词列表-->
    <select id="getKeywords" parameterType="map" resultType="string">
        select keyword from self_robot_keyword
        where book_group_id=#{bookGroupId}
    </select>

    <select id="getListByBookGroupIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from self_robot_keyword
        where book_group_id in
        <foreach collection="bookGroupIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <select id="getById" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from self_robot_keyword
        where id=#{id}
    </select>
</mapper>