<?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.cmvr.tts.mapper.TtsSynthesizeTaskMapper">

    <resultMap type="com.cmvr.tts.domain.TtsSynthesizeTask" id="TtsTaskResult">
        <result property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="taskCode" column="task_code"/>
        <result property="text" column="text"/>
        <result property="language" column="language"/>
        <result property="dialect" column="dialect"/>
        <result property="voice" column="voice"/>
        <result property="emotion" column="emotion"/>
        <result property="speed" column="speed"/>
        <result property="volume" column="volume"/>
        <result property="pitch" column="pitch"/>
        <result property="sampleRate" column="sample_rate"/>
        <result property="outputFormat" column="output_format"/>
        <result property="styleTemplate" column="style_template"/>
        <result property="status" column="status"/>
        <result property="audioFilePath" column="audio_file_path"/>
        <result property="audioDuration" column="audio_duration"/>
        <result property="fileSize" column="file_size"/>
        <result property="errorMsg" column="error_msg"/>
    </resultMap>

    <resultMap type="com.cmvr.tts.domain.vo.TtsSynthesizeTaskVo" id="TtsTaskVoResult">
        <result property="id" column="id"/>
        <result property="taskCode" column="task_code"/>
        <result property="text" column="text"/>
        <result property="language" column="language"/>
        <result property="languageName" column="language_name"/>
        <result property="dialect" column="dialect"/>
        <result property="voice" column="voice"/>
        <result property="voiceName" column="voice_name"/>
        <result property="emotion" column="emotion"/>
        <result property="emotionName" column="emotion_name"/>
        <result property="speed" column="speed"/>
        <result property="volume" column="volume"/>
        <result property="pitch" column="pitch"/>
        <result property="sampleRate" column="sample_rate"/>
        <result property="outputFormat" column="output_format"/>
        <result property="styleTemplate" column="style_template"/>
        <result property="status" column="status"/>
        <result property="statusName" column="status_name"/>
        <result property="audioFilePath" column="audio_file_path"/>
        <result property="audioDuration" column="audio_duration"/>
        <result property="fileSize" column="file_size"/>
        <result property="errorMsg" column="error_msg"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectTtsTaskVo">
        select t.id, t.task_code, t.text, t.language, t.dialect, t.voice, t.emotion,
               t.speed, t.volume, t.pitch, t.sample_rate, t.output_format, t.style_template,
               t.status, t.audio_file_path, t.audio_duration, t.file_size, t.error_msg,
               t.create_by, t.create_time, t.update_by, t.update_time, t.remark,
               dict_lang.dict_label as language_name,
               dict_voice.dict_label as voice_name,
               dict_emotion.dict_label as emotion_name,
               case t.status
                   when '0' then '待处理'
                   when '1' then '合成中'
                   when '2' then '成功已完成'
                   when '3' then '失败'
                   else '未知'
               end as status_name
        from tts_synthesize_task t
        left join sys_dict_data dict_lang on t.language = dict_lang.dict_value and dict_lang.dict_type = 'tts_language'
        left join sys_dict_data dict_voice on t.voice = dict_voice.dict_value and dict_voice.dict_type = 'tts_voice'
        left join sys_dict_data dict_emotion on t.emotion = dict_emotion.dict_value and dict_emotion.dict_type = 'tts_emotion'
    </sql>

    <select id="selectTtsTaskList" parameterType="com.cmvr.tts.domain.TtsSynthesizeTask" resultMap="TtsTaskResult">
        select id, task_code, text, language, dialect, voice, emotion, speed, volume, pitch,
               sample_rate, output_format, style_template, status, audio_file_path, audio_duration,
               file_size, error_msg, create_by, create_time, update_by, update_time, remark
        from tts_synthesize_task
        <where>
            <if test="taskCode != null and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="text != null and text != ''"> and text like concat('%', #{text}, '%')</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="language != null and language != ''"> and language = #{language}</if>
            <if test="voice != null and voice != ''"> and voice = #{voice}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTtsTaskById" parameterType="String" resultMap="TtsTaskResult">
        select id, task_code, text, language, dialect, voice, emotion, speed, volume, pitch,
               sample_rate, output_format, style_template, status, audio_file_path, audio_duration,
               file_size, error_msg, create_by, create_time, update_by, update_time, remark
        from tts_synthesize_task
        where id = #{id}
    </select>

    <select id="selectTtsTaskVoList" parameterType="com.cmvr.tts.domain.TtsSynthesizeTask" resultMap="TtsTaskVoResult">
        <include refid="selectTtsTaskVo"/>
        <where>
            <if test="taskCode != null and taskCode != ''"> and t.task_code = #{taskCode}</if>
            <if test="text != null and text != ''"> and t.text like concat('%', #{text}, '%')</if>
            <if test="status != null and status != ''"> and t.status = #{status}</if>
            <if test="params.beginTime != null and params.beginTime != ''">
                and date_format(t.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and date_format(t.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by t.create_time desc
    </select>

    <insert id="insertTtsTask" parameterType="com.cmvr.tts.domain.TtsSynthesizeTask">
        insert into tts_synthesize_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="text != null">text,</if>
            <if test="language != null">language,</if>
            <if test="dialect != null">dialect,</if>
            <if test="voice != null">voice,</if>
            <if test="emotion != null">emotion,</if>
            <if test="speed != null">speed,</if>
            <if test="volume != null">volume,</if>
            <if test="pitch != null">pitch,</if>
            <if test="sampleRate != null">sample_rate,</if>
            <if test="outputFormat != null">output_format,</if>
            <if test="styleTemplate != null">style_template,</if>
            <if test="status != null">status,</if>
            <if test="audioFilePath != null">audio_file_path,</if>
            <if test="audioDuration != null">audio_duration,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="errorMsg != null">error_msg,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="text != null">#{text},</if>
            <if test="language != null">#{language},</if>
            <if test="dialect != null">#{dialect},</if>
            <if test="voice != null">#{voice},</if>
            <if test="emotion != null">#{emotion},</if>
            <if test="speed != null">#{speed},</if>
            <if test="volume != null">#{volume},</if>
            <if test="pitch != null">#{pitch},</if>
            <if test="sampleRate != null">#{sampleRate},</if>
            <if test="outputFormat != null">#{outputFormat},</if>
            <if test="styleTemplate != null">#{styleTemplate},</if>
            <if test="status != null">#{status},</if>
            <if test="audioFilePath != null">#{audioFilePath},</if>
            <if test="audioDuration != null">#{audioDuration},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="errorMsg != null">#{errorMsg},</if>
        </trim>
    </insert>

    <update id="updateTtsTask" parameterType="com.cmvr.tts.domain.TtsSynthesizeTask">
        update tts_synthesize_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="text != null">text = #{text},</if>
            <if test="language != null">language = #{language},</if>
            <if test="dialect != null">dialect = #{dialect},</if>
            <if test="voice != null">voice = #{voice},</if>
            <if test="emotion != null">emotion = #{emotion},</if>
            <if test="speed != null">speed = #{speed},</if>
            <if test="volume != null">volume = #{volume},</if>
            <if test="pitch != null">pitch = #{pitch},</if>
            <if test="sampleRate != null">sample_rate = #{sampleRate},</if>
            <if test="outputFormat != null">output_format = #{outputFormat},</if>
            <if test="styleTemplate != null">style_template = #{styleTemplate},</if>
            <if test="status != null">status = #{status},</if>
            <if test="audioFilePath != null">audio_file_path = #{audioFilePath},</if>
            <if test="audioDuration != null">audio_duration = #{audioDuration},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="errorMsg != null">error_msg = #{errorMsg},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTtsTaskById" parameterType="String">
        delete from tts_synthesize_task where id = #{id}
    </delete>

    <select id="selectDashboardStats" resultType="java.util.Map">
        select 
            count(*) as totalTasks,
            sum(case when date_format(create_time,'%Y-%m-%d') = curdate() then 1 else 0 end) as todayTasks,
            count(distinct language) as totalLanguages,
            count(distinct voice) as totalVoices,
            avg(audio_duration) as avgDuration
        from tts_synthesize_task
    </select>

    <select id="selectYesterdayStats" resultType="java.util.Map">
        select 
            count(*) as totalTasks,
            sum(case when date_format(create_time,'%Y-%m-%d') = date_sub(curdate(), interval 1 day) then 1 else 0 end) as todayTasks,
            count(distinct language) as totalLanguages,
            count(distinct voice) as totalVoices,
            avg(audio_duration) as avgDuration
        from tts_synthesize_task
        where date_format(create_time,'%Y-%m-%d') &lt;= date_sub(curdate(), interval 1 day)
    </select>

    <select id="selectStatsByLanguage" resultType="java.util.Map">
        select language as name, count(*) as value
        from tts_synthesize_task
        group by language
        order by value desc
    </select>

    <select id="selectStatsByVoice" resultType="java.util.Map">
        select voice as name, count(*) as value
        from tts_synthesize_task
        group by voice
        order by value desc
    </select>

    <select id="selectStatsByEmotion" resultType="java.util.Map">
        select emotion as name, count(*) as value
        from tts_synthesize_task
        group by emotion
        order by value desc
    </select>

    <select id="selectLast7DaysTrend" resultType="java.util.Map">
        select date_format(create_time,'%Y-%m-%d') as date, count(*) as value
        from tts_synthesize_task
        where create_time &gt;= date_sub(curdate(), interval 6 day)
        group by date_format(create_time,'%Y-%m-%d')
        order by date
    </select>

</mapper>
