<?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="org.xyzh.workcase.mapper.TbWordCloudMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbWordCloudDTO">
        <id column="word_id" property="wordId" jdbcType="VARCHAR"/>
        <result column="word" property="word" jdbcType="VARCHAR"/>
        <result column="frequency" property="frequency" jdbcType="VARCHAR"/>
        <result column="category" property="category" jdbcType="VARCHAR"/>
        <result column="stat_date" property="statDate" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        word_id, word, frequency, category, stat_date, create_time, update_time
    </sql>

    <insert id="insertWordCloud" parameterType="org.xyzh.api.workcase.dto.TbWordCloudDTO">
        INSERT INTO workcase.tb_word_cloud (
            word_id, word, stat_date
            <if test="frequency != null">, frequency</if>
            <if test="category != null">, category</if>
        ) VALUES (
            #{wordId}, #{word}, #{statDate}::date
            <if test="frequency != null">, #{frequency}</if>
            <if test="category != null">, #{category}</if>
        )
    </insert>

    <update id="updateWordCloud" parameterType="org.xyzh.api.workcase.dto.TbWordCloudDTO">
        UPDATE workcase.tb_word_cloud
        <set>
            <if test="word != null and word != ''">word = #{word},</if>
            <if test="frequency != null">frequency = #{frequency},</if>
            <if test="category != null">category = #{category},</if>
            update_time = now()
        </set>
        WHERE word_id = #{wordId}
    </update>

    <select id="selectWordCloudById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_word_cloud
        WHERE word_id = #{wordId}
    </select>

    <select id="selectWordCloudOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_word_cloud
        <where>
            <if test="filter.wordId != null and filter.wordId != ''">
                AND word_id = #{filter.wordId}
            </if>
            <if test="filter.word != null and filter.word != ''">
                AND word = #{filter.word}
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.statDate != null and filter.statDate != ''">
                AND stat_date = #{filter.statDate}::date
            </if>
        </where>
        LIMIT 1
    </select>

    <select id="selectWordCloudList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_word_cloud
        <where>
            <if test="filter.wordId != null and filter.wordId != ''">
                AND word_id = #{filter.wordId}
            </if>
            <if test="filter.word != null and filter.word != ''">
                AND word LIKE CONCAT('%', #{filter.word}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.statDate != null and filter.statDate != ''">
                AND stat_date = #{filter.statDate}::date
            </if>
        </where>
        ORDER BY frequency DESC, create_time DESC
        <if test="filter.limit != null and filter.limit > 0">
            LIMIT #{filter.limit}
        </if>
    </select>

    <select id="selectWordCloudPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_word_cloud
        <where>
            <if test="filter.wordId != null and filter.wordId != ''">
                AND word_id = #{filter.wordId}
            </if>
            <if test="filter.word != null and filter.word != ''">
                AND word LIKE CONCAT('%', #{filter.word}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.statDate != null and filter.statDate != ''">
                AND stat_date = #{filter.statDate}::date
            </if>
        </where>
        ORDER BY frequency DESC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countWordClouds" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_word_cloud
        <where>
            <if test="filter.wordId != null and filter.wordId != ''">
                AND word_id = #{filter.wordId}
            </if>
            <if test="filter.word != null and filter.word != ''">
                AND word LIKE CONCAT('%', #{filter.word}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.statDate != null and filter.statDate != ''">
                AND stat_date = #{filter.statDate}::date
            </if>
        </where>
    </select>

    <update id="incrementFrequency">
        UPDATE workcase.tb_word_cloud
        SET frequency = frequency + #{count}, update_time = now()
        WHERE word_id = #{wordId}
    </update>

</mapper>
