<?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.generator.mapper.front.FrontProjectGenerationMapper">

    <resultMap type="FrontProjectGeneration" id="FrontProjectGenerationResult">
        <id property="generationId" column="generation_id" />
        <result property="projectId" column="project_id" />
        <result property="userId" column="user_id" />
        <result property="generateType" column="generate_type" />
        <result property="provider" column="provider" />
        <result property="promptSummary" column="prompt_summary" />
        <result property="requestPayload" column="request_payload" />
        <result property="responsePayload" column="response_payload" />
        <result property="success" column="success" />
        <result property="errorMessage" column="error_message" />
        <result property="tokenUsage" column="token_usage" />
        <result property="elapsedMs" column="elapsed_ms" />
        <result property="createTime" column="create_time" />
    </resultMap>

    <sql id="selectFrontProjectGenerationVo">
        select generation_id, project_id, user_id, generate_type, provider, prompt_summary, request_payload, response_payload, success, error_message, token_usage, elapsed_ms, create_time from front_project_generation
    </sql>

    <select id="selectFrontProjectGenerationById" parameterType="Long" resultMap="FrontProjectGenerationResult">
        <include refid="selectFrontProjectGenerationVo"/>
        where generation_id = #{generationId}
    </select>

    <select id="selectGenerationsByProjectId" parameterType="Long" resultMap="FrontProjectGenerationResult">
        <include refid="selectFrontProjectGenerationVo"/>
        where project_id = #{projectId}
        order by create_time desc
    </select>

    <select id="selectFrontProjectGenerationList" parameterType="FrontProjectGeneration" resultMap="FrontProjectGenerationResult">
        <include refid="selectFrontProjectGenerationVo"/>
        <where>
            <if test="projectId != null"> and project_id = #{projectId}</if>
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="generateType != null and generateType != ''"> and generate_type = #{generateType}</if>
            <if test="success != null and success != ''"> and success = #{success}</if>
        </where>
    </select>

    <insert id="insertFrontProjectGeneration" parameterType="FrontProjectGeneration" useGeneratedKeys="true" keyProperty="generationId">
        insert into front_project_generation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="generateType != null">generate_type,</if>
            <if test="provider != null">provider,</if>
            <if test="promptSummary != null">prompt_summary,</if>
            <if test="requestPayload != null">request_payload,</if>
            <if test="responsePayload != null">response_payload,</if>
            <if test="success != null">success,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="tokenUsage != null">token_usage,</if>
            <if test="elapsedMs != null">elapsed_ms,</if>
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="generateType != null">#{generateType},</if>
            <if test="provider != null">#{provider},</if>
            <if test="promptSummary != null">#{promptSummary},</if>
            <if test="requestPayload != null">#{requestPayload},</if>
            <if test="responsePayload != null">#{responsePayload},</if>
            <if test="success != null">#{success},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="tokenUsage != null">#{tokenUsage},</if>
            <if test="elapsedMs != null">#{elapsedMs},</if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateFrontProjectGeneration" parameterType="FrontProjectGeneration">
        update front_project_generation
        <trim prefix="SET" suffixOverrides=",">
            <if test="responsePayload != null">response_payload = #{responsePayload},</if>
            <if test="success != null">success = #{success},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="tokenUsage != null">token_usage = #{tokenUsage},</if>
            <if test="elapsedMs != null">elapsed_ms = #{elapsedMs},</if>
        </trim>
        where generation_id = #{generationId}
    </update>

    <delete id="deleteFrontProjectGenerationById" parameterType="Long">
        delete from front_project_generation where generation_id = #{generationId}
    </delete>
</mapper>
