PromptTemplate.java
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// ai/entity/PromptTemplate.java
package com.aigeo.ai.entity;
import org.hibernate.annotations.JdbcTypeCode;
import java.sql.Types;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;
/**
* 提示模板实体类
* 对应数据库表:ai_prompt_templates
*
* 存储AI生成内容时使用的提示词模板,包括:
* - 模板内容和变量定义
* - 语言和适用场景配置
* - 使用统计和版本管理
*
* @author AIGEO Team
* @since 1.0.0
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "ai_prompt_templates", indexes = {
@Index(name = "idx_prompt_templates_company", columnList = "company_id"),
@Index(name = "idx_prompt_templates_active", columnList = "is_active"),
@Index(name = "idx_prompt_templates_language", columnList = "language")
})
public class PromptTemplate {
/**
* 主键ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Integer id;
/**
* 公司ID(多租户字段)
*/
@NotNull(message = "公司ID不能为空")
@Column(name = "company_id", nullable = false)
private Integer companyId;
/**
* 模板名称
*/
@NotBlank(message = "模板名称不能为空")
@Column(name = "name", nullable = false, length = 200)
private String name;
/**
* 模板描述
*/
@Column(name = "description", length = 500)
private String description;
/**
* 模板语言
*/
@Column(name = "language", length = 10)
@Builder.Default
private String language = "zh-CN";
/**
* 模板内容
*/
@NotBlank(message = "模板内容不能为空")
@Column(name = "content", nullable = false, columnDefinition = "TEXT")
private String content;
/**
* 模板变量(JSON格式存储变量定义)
*/
@Column(name = "variables", columnDefinition = "JSON")
private String variables;
/**
* 模板类型(如:article, product, seo等)
*/
@Column(name = "template_type", length = 50)
@Builder.Default
private String templateType = "article";
/**
* 使用次数统计
*/
@Column(name = "usage_count")
@Builder.Default
private Integer usageCount = 0;
/**
* 是否激活
*/
@Column(name = "is_active")
@Builder.Default
private Boolean isActive = true;
/**
* 创建时间
*/
@CreationTimestamp
@Column(name = "created_at", updatable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@UpdateTimestamp
@Column(name = "updated_at")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
/**
* 实体创建前的处理
*/
@PrePersist
protected void onCreate() {
if (isActive == null) isActive = true;
if (language == null) language = "zh-CN";
if (templateType == null) templateType = "article";
if (usageCount == null) usageCount = 0;
}
/**
* 检查是否为激活状态
*/
public boolean isActive() {
return Boolean.TRUE.equals(isActive);
}
/**
* 增加使用次数
*/
public void incrementUsageCount() {
this.usageCount = (usageCount == null ? 0 : usageCount) + 1;
}
/**
* 检查是否为热门模板(使用次数超过阈值)
*/
public boolean isPopularTemplate() {
return usageCount != null && usageCount >= 50;
}
/**
* 获取模板的复杂度评级(基于内容长度和变量数量)
*/
public String getComplexityLevel() {
if (content == null) return "SIMPLE";
int contentLength = content.length();
int variableCount = (variables != null && !variables.trim().isEmpty()) ?
variables.split(",").length : 0;
if (contentLength > 2000 || variableCount > 10) {
return "COMPLEX";
} else if (contentLength > 500 || variableCount > 3) {
return "MEDIUM";
} else {
return "SIMPLE";
}
}
/**
* 检查是否包含指定变量
*/
public boolean hasVariable(String variableName) {
if (variables == null || variables.trim().isEmpty()) {
return false;
}
return variables.contains("\"" + variableName + "\"") ||
variables.contains(variableName);
}
}