<?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.TbChatRoomSummaryMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbChatRoomSummaryDTO">
        <id column="summary_id" property="summaryId" jdbcType="VARCHAR"/>
        <result column="room_id" property="roomId" jdbcType="VARCHAR"/>
        <result column="question" property="question" jdbcType="VARCHAR"/>
        <result column="needs" property="needs" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="answer" property="answer" jdbcType="VARCHAR"/>
        <result column="workcloud" property="workcloud" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="message_count" property="messageCount" jdbcType="INTEGER"/>
        <result column="summary_time" property="summaryTime" jdbcType="TIMESTAMP"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        summary_id, room_id, question, needs, answer, workcloud, message_count, summary_time,
        optsn, creator, create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertChatRoomSummary" parameterType="org.xyzh.api.workcase.dto.TbChatRoomSummaryDTO">
        INSERT INTO workcase.tb_chat_room_summary (
            optsn, summary_id, room_id, creator
            <if test="question != null">, question</if>
            <if test="needs != null">, needs</if>
            <if test="answer != null">, answer</if>
            <if test="workcloud != null">, workcloud</if>
            <if test="messageCount != null">, message_count</if>
            <if test="summaryTime != null">, summary_time</if>
        ) VALUES (
            #{optsn}, #{summaryId}, #{roomId}, #{creator}
            <if test="question != null">, #{question}</if>
            <if test="needs != null">, #{needs, jdbcType=ARRAY, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="answer != null">, #{answer}</if>
            <if test="workcloud != null">, #{workcloud, jdbcType=ARRAY, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="messageCount != null">, #{messageCount}</if>
            <if test="summaryTime != null">, #{summaryTime}::timestamptz</if>
        )
    </insert>

    <update id="updateChatRoomSummary" parameterType="org.xyzh.api.workcase.dto.TbChatRoomSummaryDTO">
        UPDATE workcase.tb_chat_room_summary
        <set>
            <if test="question != null">question = #{question},</if>
            <if test="needs != null">needs = #{needs, jdbcType=ARRAY, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler},</if>
            <if test="answer != null">answer = #{answer},</if>
            <if test="workcloud != null">workcloud = #{workcloud, jdbcType=ARRAY, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler},</if>
            <if test="messageCount != null">message_count = #{messageCount},</if>
            <if test="summaryTime != null">summary_time = #{summaryTime}::timestamptz,</if>
            update_time = now()
        </set>
        WHERE summary_id = #{summaryId}
    </update>

    <select id="selectChatRoomSummaryById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_summary
        WHERE summary_id = #{summaryId} AND deleted = false
    </select>

    <select id="selectLatestSummaryByRoomId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_summary
        WHERE room_id = #{roomId} AND deleted = false
        ORDER BY summary_time DESC
        LIMIT 1
    </select>

    <select id="selectChatRoomSummaryList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_summary
        <where>
            deleted = false
            <if test="filter.summaryId != null and filter.summaryId != ''">
                AND summary_id = #{filter.summaryId}
            </if>
            <if test="filter.roomId != null and filter.roomId != ''">
                AND room_id = #{filter.roomId}
            </if>
            <if test="filter.question != null and filter.question != ''">
                AND question LIKE CONCAT('%', #{filter.question}, '%')
            </if>
        </where>
        ORDER BY summary_time DESC
    </select>

    <select id="selectChatRoomSummaryPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_summary
        <where>
            deleted = false
            <if test="filter.summaryId != null and filter.summaryId != ''">
                AND summary_id = #{filter.summaryId}
            </if>
            <if test="filter.roomId != null and filter.roomId != ''">
                AND room_id = #{filter.roomId}
            </if>
            <if test="filter.question != null and filter.question != ''">
                AND question LIKE CONCAT('%', #{filter.question}, '%')
            </if>
        </where>
        ORDER BY summary_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countChatRoomSummaries" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_chat_room_summary
        <where>
            deleted = false
            <if test="filter.summaryId != null and filter.summaryId != ''">
                AND summary_id = #{filter.summaryId}
            </if>
            <if test="filter.roomId != null and filter.roomId != ''">
                AND room_id = #{filter.roomId}
            </if>
            <if test="filter.question != null and filter.question != ''">
                AND question LIKE CONCAT('%', #{filter.question}, '%')
            </if>
        </where>
    </select>

    <update id="deleteChatRoomSummary">
        UPDATE workcase.tb_chat_room_summary
        SET deleted = true, delete_time = now()
        WHERE summary_id = #{summaryId}
    </update>

</mapper>
