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

    <resultMap type="com.cmvr.tts.domain.CorpusInfo" id="CorpusResult">
        <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="text" column="text"/>
        <result property="pinyin" column="pinyin"/>
        <result property="categoryId" column="category_id"/>
        <result property="language" column="language"/>
        <result property="dialect" column="dialect"/>
        <result property="emotion" column="emotion"/>
        <result property="voice" column="voice"/>
        <result property="speaker" column="speaker"/>
        <result property="scene" column="scene"/>
        <result property="audioFilePath" column="audio_file_path"/>
        <result property="audioDuration" column="audio_duration"/>
        <result property="fileSize" column="file_size"/>
    </resultMap>

    <resultMap type="com.cmvr.tts.domain.vo.CorpusInfoVo" id="CorpusVoResult">
        <result property="id" column="id"/>
        <result property="text" column="text"/>
        <result property="pinyin" column="pinyin"/>
        <result property="categoryId" column="category_id"/>
        <result property="categoryName" column="category_name"/>
        <result property="language" column="language"/>
        <result property="languageName" column="language_name"/>
        <result property="dialect" column="dialect"/>
        <result property="emotion" column="emotion"/>
        <result property="emotionName" column="emotion_name"/>
        <result property="voice" column="voice"/>
        <result property="voiceName" column="voice_name"/>
        <result property="speaker" column="speaker"/>
        <result property="scene" column="scene"/>
        <result property="audioFilePath" column="audio_file_path"/>
        <result property="audioDuration" column="audio_duration"/>
        <result property="fileSize" column="file_size"/>
        <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="selectCorpusVo">
        select c.id, c.text, c.pinyin, c.category_id, c.language, c.dialect, c.emotion,
               c.voice, c.speaker, c.scene, c.audio_file_path, c.audio_duration, c.file_size,
               c.create_by, c.create_time, c.update_by, c.update_time, c.remark,
               cc.category_name as category_name,
               dict_lang.dict_label as language_name,
               dict_emotion.dict_label as emotion_name,
               dict_voice.dict_label as voice_name
        from tts_corpus_info c
        left join tts_corpus_category cc on c.category_id = cc.id
        left join sys_dict_data dict_lang on c.language = dict_lang.dict_value and dict_lang.dict_type = 'tts_language'
        left join sys_dict_data dict_emotion on c.emotion = dict_emotion.dict_value and dict_emotion.dict_type = 'tts_emotion'
        left join sys_dict_data dict_voice on c.voice = dict_voice.dict_value and dict_voice.dict_type = 'tts_voice'
    </sql>

    <select id="selectCorpusList" parameterType="com.cmvr.tts.domain.CorpusInfo" resultMap="CorpusResult">
        select id, text, pinyin, category_id, language, dialect, emotion, voice, speaker, scene,
               audio_file_path, audio_duration, file_size,
               create_by, create_time, update_by, update_time, remark
        from tts_corpus_info
        <where>
            <if test="ids != null and ids.length > 0">
                and id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="text != null and text != ''"> and text like concat('%', #{text}, '%')</if>
            <if test="categoryId != null and categoryId != ''"> and category_id = #{categoryId}</if>
            <if test="language != null and language != ''"> and language = #{language}</if>
            <if test="dialect != null and dialect != ''"> and dialect = #{dialect}</if>
            <if test="emotion != null and emotion != ''"> and emotion = #{emotion}</if>
            <if test="voice != null and voice != ''"> and voice = #{voice}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCorpusById" parameterType="String" resultMap="CorpusResult">
        select id, text, pinyin, category_id, language, dialect, emotion, voice, speaker, scene,
               audio_file_path, audio_duration, file_size,
               create_by, create_time, update_by, update_time, remark
        from tts_corpus_info
        where id = #{id}
    </select>

    <select id="selectCorpusVoList" parameterType="com.cmvr.tts.domain.CorpusInfo" resultMap="CorpusVoResult">
        <include refid="selectCorpusVo"/>
        <where>
            <if test="ids != null and ids.length > 0">
                and c.id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="text != null and text != ''"> and c.text like concat('%', #{text}, '%')</if>
            <if test="categoryId != null and categoryId != ''"> and c.category_id = #{categoryId}</if>
            <if test="language != null and language != ''"> and c.language = #{language}</if>
            <if test="dialect != null and dialect != ''"> and c.dialect = #{dialect}</if>
            <if test="emotion != null and emotion != ''"> and c.emotion = #{emotion}</if>
            <if test="voice != null and voice != ''"> and c.voice = #{voice}</if>
        </where>
        order by c.create_time desc
    </select>

    <insert id="insertCorpus" parameterType="com.cmvr.tts.domain.CorpusInfo">
        insert into tts_corpus_info
        <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="text != null">text,</if>
            <if test="pinyin != null">pinyin,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="language != null">language,</if>
            <if test="dialect != null">dialect,</if>
            <if test="emotion != null">emotion,</if>
            <if test="voice != null">voice,</if>
            <if test="speaker != null">speaker,</if>
            <if test="scene != null">scene,</if>
            <if test="audioFilePath != null">audio_file_path,</if>
            <if test="audioDuration != null">audio_duration,</if>
            <if test="fileSize != null">file_size,</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="text != null">#{text},</if>
            <if test="pinyin != null">#{pinyin},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="language != null">#{language},</if>
            <if test="dialect != null">#{dialect},</if>
            <if test="emotion != null">#{emotion},</if>
            <if test="voice != null">#{voice},</if>
            <if test="speaker != null">#{speaker},</if>
            <if test="scene != null">#{scene},</if>
            <if test="audioFilePath != null">#{audioFilePath},</if>
            <if test="audioDuration != null">#{audioDuration},</if>
            <if test="fileSize != null">#{fileSize},</if>
        </trim>
    </insert>

    <update id="updateCorpus" parameterType="com.cmvr.tts.domain.CorpusInfo">
        update tts_corpus_info
        <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="text != null">text = #{text},</if>
            <if test="pinyin != null">pinyin = #{pinyin},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="language != null">language = #{language},</if>
            <if test="dialect != null">dialect = #{dialect},</if>
            <if test="emotion != null">emotion = #{emotion},</if>
            <if test="voice != null">voice = #{voice},</if>
            <if test="speaker != null">speaker = #{speaker},</if>
            <if test="scene != null">scene = #{scene},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCorpusById" parameterType="String">
        delete from tts_corpus_info where id = #{id}
    </delete>

    <delete id="deleteCorpusByIds" parameterType="String">
        delete from tts_corpus_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

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

    <select id="selectYesterdayStats" resultType="java.util.Map">
        select 
            count(*) as totalCorpus,
            sum(case when date_format(create_time,'%Y-%m-%d') = date_sub(curdate(), interval 1 day) then 1 else 0 end) as todayCorpus,
            count(distinct language) as totalLanguages,
            count(distinct voice) as totalVoices,
            avg(audio_duration) as avgDuration
        from tts_corpus_info
        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_corpus_info
        group by language
        order by value desc
    </select>

    <select id="selectStatsByDialect" resultType="java.util.Map">
        select dialect as name, count(*) as value
        from tts_corpus_info
        where dialect is not null
        group by dialect
        order by value desc
    </select>

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

    <select id="selectStatsByCategory" resultType="java.util.Map">
        select cc.category_name as name, count(c.id) as value
        from tts_corpus_category cc
        left join tts_corpus_info c on cc.id = c.category_id
        group by cc.id, cc.category_name
        order by value desc
    </select>

</mapper>
