<?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="cn.qihangerp.mapper.AiUserRoleMapper">
    <resultMap type="cn.qihangerp.model.entity.AiUserRole" id="AiUserRoleResult">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="roleName" column="role_name"/>
        <result property="systemPrompt" column="system_prompt"/>
        <result property="isDefault" column="is_default"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectAiUserRoleVo">
        select id, user_id, role_name, system_prompt, is_default, create_time, update_time from ai_user_role
    </sql>

    <select id="selectAiUserRoleById" parameterType="Long" resultMap="AiUserRoleResult">
        <include refid="selectAiUserRoleVo"/>
        where id = #{id}
    </select>

    <select id="selectAiUserRoleList" parameterType="cn.qihangerp.model.entity.AiUserRole" resultMap="AiUserRoleResult">
        <include refid="selectAiUserRoleVo"/>
        <where>
            <if test="roleName != null and roleName != ''">
                AND role_name LIKE CONCAT('%', #{roleName}, '%')
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="isDefault != null">
                AND is_default = #{isDefault}
            </if>
        </where>
    </select>

    <select id="selectAiUserRolesByUserId" parameterType="Long" resultMap="AiUserRoleResult">
        <include refid="selectAiUserRoleVo"/>
        where user_id = #{userId}
        order by is_default desc, create_time desc
    </select>

    <select id="selectDefaultAiUserRoleByUserId" parameterType="Long" resultMap="AiUserRoleResult">
        <include refid="selectAiUserRoleVo"/>
        where user_id = #{userId} and is_default = 1
    </select>

    <insert id="insertAiUserRole" parameterType="cn.qihangerp.model.entity.AiUserRole" useGeneratedKeys="true" keyProperty="id">
        insert into ai_user_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="roleName != null">role_name,</if>
            <if test="systemPrompt != null">system_prompt,</if>
            <if test="isDefault != null">is_default,</if>
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="roleName != null">#{roleName},</if>
            <if test="systemPrompt != null">#{systemPrompt},</if>
            <if test="isDefault != null">#{isDefault},</if>
            now(),
            now()
        </trim>
    </insert>

    <update id="updateAiUserRole" parameterType="cn.qihangerp.model.entity.AiUserRole">
        update ai_user_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleName != null">role_name = #{roleName},</if>
            <if test="systemPrompt != null">system_prompt = #{systemPrompt},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            update_time = now()
        </trim>
        where id = #{id}
    </update>

    <update id="updateNonDefaultRolesByUserId" parameterType="Long">
        update ai_user_role
        set is_default = 0, update_time = now()
        where user_id = #{userId}
    </update>

    <delete id="deleteAiUserRoleById" parameterType="Long">
        delete from ai_user_role where id = #{id}
    </delete>

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