<?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.ruoyi.ai.mapper.AiTagMapper">
    
    <resultMap type="AiTag" id="AiTagResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <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="parentId"    column="parent_id"    />
        <result property="aiId"    column="ai_id"    />
        <result property="title"    column="title"    />
        <result property="status"    column="status"    />
        <result property="prompt"    column="prompt"    />
        <result property="titleHK"    column="title_HK"    />
        <result property="titleUS"    column="title_US"    />
    </resultMap>

    <sql id="selectAiTagVo">
        select id, del_flag, create_by, create_time, update_by, update_time, remark, parent_id, ai_id, title, status, prompt, title_HK, title_US from ai_tag
    </sql>

    <select id="selectAiTagList" parameterType="AiTag" resultMap="AiTagResult">
        <include refid="selectAiTagVo"/>
        <where>  
            <if test="aiId != null "> and ai_id = #{aiId}</if>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="prompt != null  and prompt != ''"> and prompt = #{prompt}</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
        </where>
    </select>

    <select id="selectUserAiTagList" parameterType="AiTag" resultMap="AiTagResult">
        SELECT
        id,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        remark,
        parent_id,
        ai_id,
        CASE
        WHEN #{language} = 'zh_HK' THEN COALESCE(NULLIF(title_HK, ''), title_US)
        WHEN #{language} = 'en_US' THEN COALESCE(title_US, title_US)
        ELSE title
        END AS title,
        status
        FROM ai_tag
        <where>
            <if test="aiId != null "> and ai_id = #{aiId}</if>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="prompt != null  and prompt != ''"> and prompt = #{prompt}</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
            and del_flag = 0
        </where>
    </select>

    <select id="selectAiTagById" parameterType="Long" resultMap="AiTagResult">
        <include refid="selectAiTagVo"/>
        where id = #{id}
    </select>

    <select id="selectAiTagListByIds" resultMap="AiTagResult">
        <include refid="selectAiTagVo"/>
        <where>
            id IN
            <foreach collection="tags" item="tagId" open="(" separator="," close=")">
                #{tagId}
            </foreach>
        </where>
        ORDER BY
        <if test="parentTags != null and parentTags.length > 0">
            -- 按 parentIds 顺序排序，null 的 parent_id 排最后
            FIELD(IFNULL(parent_id, -1),
            <foreach collection="parentTags" item="parentId" separator=",">
                #{parentId}
            </foreach>
            , -1
            ),
        </if>
        -- 兜底排序：parentIds 为空时，按子标签 ID 排序（可根据业务调整）
        id ASC;
    </select>

    <insert id="insertAiTag" parameterType="AiTag">
        insert into ai_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</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="aiId != null">ai_id,</if>
            <if test="title != null">title,</if>
            <if test="status != null">status,</if>
            <if test="prompt != null">prompt,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="titleHK != null">title_HK,</if>
            <if test="titleUS != null">title_US,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</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="aiId != null">#{aiId},</if>
            <if test="title != null">#{title},</if>
            <if test="status != null">#{status},</if>
            <if test="prompt != null">#{prompt},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="titleHK != null">#{titleHK},</if>
            <if test="titleUS != null">#{titleUS},</if>
         </trim>
    </insert>

    <update id="updateAiTag" parameterType="AiTag">
        update ai_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <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="aiId != null">ai_id = #{aiId},</if>
            <if test="title != null">title = #{title},</if>
            <if test="status != null">status = #{status},</if>
            <if test="prompt != null">prompt = #{prompt},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="titleHK != null">title_HK = #{titleHK},</if>
            <if test="titleUS != null">title_US = #{titleUS},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiTagById" parameterType="Long">
        delete from ai_tag where id = #{id}
    </delete>

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