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

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbMeetingTranscriptionDTO">
        <id column="transcription_id" property="transcriptionId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="meeting_id" property="meetingId" jdbcType="VARCHAR"/>
        <result column="speaker_id" property="speakerId" jdbcType="VARCHAR"/>
        <result column="speaker_name" property="speakerName" jdbcType="VARCHAR"/>
        <result column="speaker_type" property="speakerType" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="content_raw" property="contentRaw" jdbcType="VARCHAR"/>
        <result column="language" property="language" jdbcType="VARCHAR"/>
        <result column="confidence" property="confidence" jdbcType="NUMERIC"/>
        <result column="speech_start_time" property="speechStartTime" jdbcType="TIMESTAMP"/>
        <result column="speech_end_time" property="speechEndTime" jdbcType="TIMESTAMP"/>
        <result column="duration_ms" property="durationMs" jdbcType="INTEGER"/>
        <result column="audio_url" property="audioUrl" jdbcType="VARCHAR"/>
        <result column="segment_index" property="segmentIndex" jdbcType="INTEGER"/>
        <result column="is_final" property="isFinal" jdbcType="BOOLEAN"/>
        <result column="service_provider" property="serviceProvider" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.MeetingTranscriptionVO">
        <id column="transcription_id" property="transcriptionId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="meeting_id" property="meetingId" jdbcType="VARCHAR"/>
        <result column="speaker_id" property="speakerId" jdbcType="VARCHAR"/>
        <result column="speaker_name" property="speakerName" jdbcType="VARCHAR"/>
        <result column="speaker_type" property="speakerType" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="content_raw" property="contentRaw" jdbcType="VARCHAR"/>
        <result column="language" property="language" jdbcType="VARCHAR"/>
        <result column="confidence" property="confidence" jdbcType="NUMERIC"/>
        <result column="speech_start_time" property="speechStartTime" jdbcType="TIMESTAMP"/>
        <result column="speech_end_time" property="speechEndTime" jdbcType="TIMESTAMP"/>
        <result column="duration_ms" property="durationMs" jdbcType="INTEGER"/>
        <result column="audio_url" property="audioUrl" jdbcType="VARCHAR"/>
        <result column="segment_index" property="segmentIndex" jdbcType="INTEGER"/>
        <result column="is_final" property="isFinal" jdbcType="BOOLEAN"/>
        <result column="service_provider" property="serviceProvider" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        transcription_id, optsn, meeting_id, speaker_id, speaker_name, speaker_type,
        content, content_raw, language, confidence, speech_start_time, speech_end_time, duration_ms,
        audio_url, segment_index, is_final, service_provider, create_time
    </sql>

    <insert id="insertMeetingTranscription" parameterType="org.xyzh.api.workcase.dto.TbMeetingTranscriptionDTO">
        INSERT INTO workcase.tb_meeting_transcription (
            optsn, transcription_id, meeting_id, speaker_id, speaker_name, speaker_type,
            content, speech_start_time, speech_end_time, duration_ms
            <if test="contentRaw != null">, content_raw</if>
            <if test="language != null">, language</if>
            <if test="confidence != null">, confidence</if>
            <if test="audioUrl != null">, audio_url</if>
            <if test="segmentIndex != null">, segment_index</if>
            <if test="isFinal != null">, is_final</if>
            <if test="serviceProvider != null">, service_provider</if>
        ) VALUES (
            #{optsn}, #{transcriptionId}, #{meetingId}, #{speakerId}, #{speakerName}, #{speakerType},
            #{content}, #{speechStartTime}, #{speechEndTime}, #{durationMs}
            <if test="contentRaw != null">, #{contentRaw}</if>
            <if test="language != null">, #{language}</if>
            <if test="confidence != null">, #{confidence}</if>
            <if test="audioUrl != null">, #{audioUrl}</if>
            <if test="segmentIndex != null">, #{segmentIndex}</if>
            <if test="isFinal != null">, #{isFinal}</if>
            <if test="serviceProvider != null">, #{serviceProvider}</if>
        )
    </insert>

    <update id="updateMeetingTranscription" parameterType="org.xyzh.api.workcase.dto.TbMeetingTranscriptionDTO">
        UPDATE workcase.tb_meeting_transcription
        <set>
            <if test="content != null">content = #{content},</if>
            <if test="contentRaw != null">content_raw = #{contentRaw},</if>
            <if test="confidence != null">confidence = #{confidence},</if>
            <if test="isFinal != null">is_final = #{isFinal},</if>
        </set>
        WHERE transcription_id = #{transcriptionId}
    </update>

    <delete id="deleteMeetingTranscription" parameterType="org.xyzh.api.workcase.dto.TbMeetingTranscriptionDTO">
        DELETE FROM workcase.tb_meeting_transcription
        WHERE transcription_id = #{transcriptionId}
    </delete>

    <select id="selectMeetingTranscriptionById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_transcription
        WHERE transcription_id = #{transcriptionId}
    </select>

    <select id="selectMeetingTranscriptionList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_transcription
        <where>
            <if test="filter.transcriptionId != null and filter.transcriptionId != ''">AND transcription_id = #{filter.transcriptionId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.speakerId != null and filter.speakerId != ''">AND speaker_id = #{filter.speakerId}</if>
            <if test="filter.speakerType != null and filter.speakerType != ''">AND speaker_type = #{filter.speakerType}</if>
            <if test="filter.language != null and filter.language != ''">AND language = #{filter.language}</if>
            <if test="filter.isFinal != null">AND is_final = #{filter.isFinal}</if>
            <if test="filter.serviceProvider != null and filter.serviceProvider != ''">AND service_provider = #{filter.serviceProvider}</if>
        </where>
        ORDER BY segment_index ASC
    </select>

    <select id="selectMeetingTranscriptionPage" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_meeting_transcription
        <where>
            <if test="filter.transcriptionId != null and filter.transcriptionId != ''">AND transcription_id = #{filter.transcriptionId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.speakerId != null and filter.speakerId != ''">AND speaker_id = #{filter.speakerId}</if>
            <if test="filter.speakerType != null and filter.speakerType != ''">AND speaker_type = #{filter.speakerType}</if>
            <if test="filter.language != null and filter.language != ''">AND language = #{filter.language}</if>
            <if test="filter.isFinal != null">AND is_final = #{filter.isFinal}</if>
            <if test="filter.serviceProvider != null and filter.serviceProvider != ''">AND service_provider = #{filter.serviceProvider}</if>
        </where>
        ORDER BY segment_index ASC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countMeetingTranscriptions" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_meeting_transcription
        <where>
            <if test="filter.transcriptionId != null and filter.transcriptionId != ''">AND transcription_id = #{filter.transcriptionId}</if>
            <if test="filter.meetingId != null and filter.meetingId != ''">AND meeting_id = #{filter.meetingId}</if>
            <if test="filter.speakerId != null and filter.speakerId != ''">AND speaker_id = #{filter.speakerId}</if>
            <if test="filter.speakerType != null and filter.speakerType != ''">AND speaker_type = #{filter.speakerType}</if>
            <if test="filter.language != null and filter.language != ''">AND language = #{filter.language}</if>
            <if test="filter.isFinal != null">AND is_final = #{filter.isFinal}</if>
            <if test="filter.serviceProvider != null and filter.serviceProvider != ''">AND service_provider = #{filter.serviceProvider}</if>
        </where>
    </select>

</mapper>
