/**
 * @ProjectName: 智慧评标-主观分助手
 * @Package: com.zhpb.gdyd_zhpb_zgf.entity.mysql
 * @ClassName: ModelRequestLog
 * @Description: 模型请求日志实体，记录所有模型/向量化调用信息
 * @Author: 张志锋
 * @Date: 2025-11-25
 * @Version: 1.0
 * @Copyright: © 2025
 */
package com.zhpb.gdyd_zhpb_zgf.entity.mysql;

import jakarta.persistence.*;
import lombok.Data;

import java.time.LocalDateTime;

@Entity
@Table(name = "t_model_request_log")
@Data
public class ModelRequestLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 请求发起时间
     */
    @Column(name = "request_time", nullable = false)
    private LocalDateTime requestTime;

    /**
     * 请求来源（QA、SUMMARY、VECTORIZE等）
     */
    @Column(name = "request_source", length = 64)
    private String requestSource;

    /**
     * 请求地址
     */
    @Column(name = "request_url", length = 512)
    private String requestUrl;

    /**
     * HTTP方法
     */
    @Column(name = "request_method", length = 16)
    private String requestMethod;

    /**
     * 模型名称
     */
    @Column(name = "model_name", length = 128)
    private String modelName;

    /**
     * 请求体
     */
    @Column(name = "request_payload", columnDefinition = "TEXT")
    private String requestPayload;

    /**
     * 响应体
     */
    @Column(name = "response_payload", columnDefinition = "TEXT")
    private String responsePayload;

    /**
     * 请求状态（SUCCESS/FAILED）
     */
    @Column(name = "status", length = 32)
    private String status;

    /**
     * 耗时（毫秒）
     */
    @Column(name = "duration_ms")
    private Long durationMs;

    /**
     * API密钥
     */
    @Column(name = "api_key", length = 256)
    private String apiKey;

    /**
     * AppKey（私有/办公模型用）
     */
    @Column(name = "app_key", length = 256)
    private String appKey;

    /**
     * AppSecret/AppSecret
     */
    @Column(name = "app_secret", length = 256)
    private String appSecret;

    /**
     * Prompt Token数量
     */
    @Column(name = "prompt_tokens")
    private Integer promptTokens;

    /**
     * Completion Token数量
     */
    @Column(name = "completion_tokens")
    private Integer completionTokens;

    /**
     * 总Token数量
     */
    @Column(name = "total_tokens")
    private Integer totalTokens;

    /**
     * TraceId
     */
    @Column(name = "trace_id", length = 128)
    private String traceId;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 额外信息（用于存放header、扩展字段等）
     */
    @Column(name = "extra_info", columnDefinition = "TEXT")
    private String extraInfo;

    @PrePersist
    public void prePersist() {
        if (this.requestTime == null) {
            this.requestTime = LocalDateTime.now();
        }
    }
}


