<?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.backendsys.mapper.Ai.Aiivh.AiivhTimbreMapper">

    <sql id="includeAiivhTimbrePublic">
        id,
        timbre_key,
        timbre_name,
        timbre_sample,
        COALESCE(timbre_desc, '') timbre_desc,
        COALESCE(timbre_gender, '') timbre_gender,
        emotional_categories,
        expire_date,
        create_time,
        update_time
    </sql>
    <sql id="includeAiivhTimbre">
        id,
        user_id,
        stage_status,
        timbre_key,
        timbre_name,
        timbre_sample,
        COALESCE(timbre_desc, '') timbre_desc,
        COALESCE(timbre_gender, '') timbre_gender,
        emotional_categories,
        expire_date,
        create_time,
        update_time
    </sql>

    <resultMap id="resultMapAiivhTimbrePublic" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="timbre_key" column="timbre_key" />
        <result property="timbre_name" column="timbre_name" />
        <result property="timbre_sample" column="timbre_sample" />
        <result property="timbre_desc" column="timbre_desc" />
        <result property="timbre_gender" column="timbre_gender" javaType="java.lang.Long"/>
        <result property="emotional_categories" column="emotional_categories" />
        <result property="expire_date" column="expire_date" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>
    <resultMap id="resultMapAiivhTimbre" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" javaType="java.lang.Long"/>
        <result property="stage_status" column="stage_status" />

        <result property="timbre_key" column="timbre_key" />
        <result property="timbre_name" column="timbre_name" />
        <result property="timbre_sample" column="timbre_sample" />
        <result property="timbre_desc" column="timbre_desc" />
        <result property="timbre_gender" column="timbre_gender" javaType="java.lang.Long"/>
        <result property="emotional_categories" column="emotional_categories" />
        <result property="expire_date" column="expire_date" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryAiivhPublicTimbreList" resultMap="resultMapAiivhTimbrePublic">
        SELECT <include refid="includeAiivhTimbrePublic" />
        FROM ai_ivh_timbre_public
        <where>
            <if test="emotional_categorie != null and emotional_categorie != ''">
                AND emotional_categories REGEXP CONCAT(#{emotional_categorie})
            </if>
            <if test="timbre_key != null and timbre_key != ''">
                AND timbre_key = #{timbre_key}
            </if>
            <if test="timbre_name != null and timbre_name != ''">
                AND timbre_name LIKE CONCAT('%', #{timbre_name}, '%')
            </if>
            <if test="timbre_gender != null and timbre_gender != ''">
                AND timbre_gender = #{timbre_gender}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="queryAiivhTimbreList" resultMap="resultMapAiivhTimbre">
        SELECT <include refid="includeAiivhTimbre" />
        FROM ai_ivh_timbre
        <where>
            user_id = #{user_id}
            <if test="timbre_key != null and timbre_key != ''">
                AND timbre_key = #{timbre_key}
            </if>
            <if test="timbre_name != null and timbre_name != ''">
                AND timbre_name LIKE CONCAT('%', #{timbre_name}, '%')
            </if>
            <if test="timbre_gender != null and timbre_gender != ''">
                AND timbre_gender = #{timbre_gender}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 批量插入 (腾讯-数智人-个人资产管理-分页查询音色列表) -->
    <insert id="insertAiivhPublicTimbreBatch" parameterType="java.util.List">
        INSERT INTO ai_ivh_timbre_public (
            timbre_key, timbre_name, timbre_sample,
            timbre_desc, timbre_gender, emotional_categories, expire_date
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.timbre_key}, #{item.timbre_name}, #{item.timbre_sample},
            #{item.timbre_desc}, #{item.timbre_gender}, #{item.emotional_categories}, #{item.expire_date}
            )
        </foreach>
    </insert>

    <delete id="cleanAiivhPublicTimbre">
        DELETE FROM ai_ivh_timbre_public
    </delete>

</mapper>