<?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.dora.mapper.WechatKeywordReplyMapper">

    <resultMap id="BaseResultMap" type="com.dora.entity.WechatKeywordReply">
        <id column="id" property="id" />
        <result column="keyword" property="keyword" />
        <result column="match_type" property="matchType" />
        <result column="template_id" property="templateId" />
        <result column="is_enabled" property="isEnabled" />
        <result column="priority" property="priority" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        id, keyword, match_type, template_id, is_enabled, priority, create_time, update_time
    </sql>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_keyword_reply
        WHERE id = #{id}
    </select>

    <select id="selectByKeyword" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_keyword_reply
        WHERE keyword = #{keyword}
        AND is_enabled = 1
        ORDER BY priority DESC
    </select>

    <select id="selectAllEnabled" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_keyword_reply
        WHERE is_enabled = 1
        ORDER BY priority DESC
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO wechat_keyword_reply (
            keyword, match_type, template_id, is_enabled, priority
        ) VALUES (
            #{keyword}, #{matchType}, #{templateId}, #{isEnabled}, #{priority}
        )
    </insert>

    <update id="updateById">
        UPDATE wechat_keyword_reply
        SET keyword = #{keyword},
            match_type = #{matchType},
            template_id = #{templateId},
            is_enabled = #{isEnabled},
            priority = #{priority},
            update_time = CURRENT_TIMESTAMP
        WHERE id = #{id}
    </update>

    <delete id="deleteById">
        DELETE FROM wechat_keyword_reply WHERE id = #{id}
    </delete>

    <delete id="deleteByTemplateId">
        DELETE FROM wechat_keyword_reply WHERE template_id = #{templateId}
    </delete>

    <update id="updateStatusBatch">
        UPDATE wechat_keyword_reply
        SET is_enabled = #{isEnabled}, update_time = CURRENT_TIMESTAMP
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectKeywordReplyList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_keyword_reply
        <where>
            <if test="keyword != null and keyword != ''">
                AND keyword LIKE CONCAT('%', #{keyword}, '%')
            </if>
            <if test="matchType != null and matchType != ''">
                AND match_type = #{matchType}
            </if>
            <if test="isEnabled != null">
                AND is_enabled = #{isEnabled}
            </if>
        </where>
        ORDER BY priority DESC, create_time DESC
        LIMIT #{offset}, #{size}
    </select>

    <select id="countKeywordReplyList" resultType="int">
        SELECT COUNT(*) FROM wechat_keyword_reply
        <where>
            <if test="keyword != null and keyword != ''">
                AND keyword LIKE CONCAT('%', #{keyword}, '%')
            </if>
            <if test="matchType != null and matchType != ''">
                AND match_type = #{matchType}
            </if>
            <if test="isEnabled != null">
                AND is_enabled = #{isEnabled}
            </if>
        </where>
    </select>

</mapper>
