修复: 代码审查4个必改项+4个建议改进
- 工单编号改为业务格式 TK-yyMMdd-NNN
- 类型断言加 comma-ok 防 panic
- priority 用指针区分未传/P0
- json.Marshal 错误处理
- 提取 ParseID 公共函数消除重复
- HTTP client 包级别复用
- LIKE 查询特殊字符转义
- interface{} → any
- auth 中间件用 dto.Fail 统一响应
This commit is contained in:
@@ -13,6 +13,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var httpClient = &http.Client{Timeout: 30 * time.Second}
|
||||
|
||||
type GLMRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
@@ -32,10 +34,10 @@ type Choice struct {
|
||||
}
|
||||
|
||||
type AnalysisResult struct {
|
||||
Category string `json:"category"`
|
||||
Category string `json:"category"`
|
||||
Priority json.Number `json:"priority"`
|
||||
Summary string `json:"summary"`
|
||||
SuggestRole string `json:"suggest_role"`
|
||||
Summary string `json:"summary"`
|
||||
SuggestRole string `json:"suggest_role"`
|
||||
}
|
||||
|
||||
func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title, content, contactname, contactphone string) (*model.TicketAiAnalysis, error) {
|
||||
@@ -62,7 +64,10 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, _ := json.Marshal(reqBody)
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("构建请求失败: %w", err)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequest("POST", baseURL+"/chat/completions", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
@@ -72,8 +77,7 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+apikey)
|
||||
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Do(httpReq)
|
||||
resp, err := httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -90,7 +94,6 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
|
||||
return nil, fmt.Errorf("AI返回为空")
|
||||
}
|
||||
|
||||
// Clean markdown code block wrapping from AI response
|
||||
aiContent := strings.TrimSpace(glmResp.Choices[0].Message.Content)
|
||||
aiContent = strings.TrimPrefix(aiContent, "```json")
|
||||
aiContent = strings.TrimPrefix(aiContent, "```")
|
||||
@@ -102,7 +105,7 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
|
||||
return nil, fmt.Errorf("解析AI响应失败: %v, 原始内容: %s", err, aiContent)
|
||||
}
|
||||
|
||||
priority := int16(2) // default P2
|
||||
priority := int16(2)
|
||||
if n, err := result.Priority.Int64(); err == nil {
|
||||
priority = int16(n)
|
||||
}
|
||||
@@ -142,9 +145,6 @@ func ConfirmAnalysis(db *gorm.DB, analysisid int, category string, priority int1
|
||||
if category != "" {
|
||||
updates["category"] = category
|
||||
}
|
||||
if priority >= 0 {
|
||||
updates["priority"] = priority
|
||||
}
|
||||
if summary != "" {
|
||||
updates["summary"] = summary
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/casehub/ticket-workbench/internal/model"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -30,7 +30,9 @@ func ListTickets(db *gorm.DB, status *int16, category string, priority *int16, k
|
||||
query = query.Where("priority = ?", *priority)
|
||||
}
|
||||
if keyword != "" {
|
||||
query = query.Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
||||
escaped := strings.ReplaceAll(keyword, "%", "\\%")
|
||||
escaped = strings.ReplaceAll(escaped, "_", "\\_")
|
||||
query = query.Where("title LIKE ? OR content LIKE ?", "%"+escaped+"%", "%"+escaped+"%")
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -55,8 +57,11 @@ func GetTicketByID(db *gorm.DB, ticketid int) (*model.TicketInfo, error) {
|
||||
}
|
||||
|
||||
func CreateTicket(db *gorm.DB, title, content, contactname, contactphone, source, category string, priority int16, submitterid int) (*model.TicketInfo, error) {
|
||||
ticketno := fmt.Sprintf("TKT%s", uuid.New().String()[:8])
|
||||
now := time.Now()
|
||||
dateStr := now.Format("060102")
|
||||
var count int64
|
||||
db.Model(&model.TicketInfo{}).Where("DATE(createtime) = CURDATE()").Count(&count)
|
||||
ticketno := fmt.Sprintf("TK-%s-%03d", dateStr, count+1)
|
||||
|
||||
ticket := &model.TicketInfo{
|
||||
Ticketno: ticketno,
|
||||
@@ -82,7 +87,7 @@ func CreateTicket(db *gorm.DB, title, content, contactname, contactphone, source
|
||||
return ticket, nil
|
||||
}
|
||||
|
||||
func UpdateTicket(db *gorm.DB, ticketid int, title, content, contactname, contactphone, category string, priority int16, handlerid *int, operatorid int) error {
|
||||
func UpdateTicket(db *gorm.DB, ticketid int, title, content, contactname, contactphone, category string, priority *int16, handlerid *int, operatorid int) error {
|
||||
updates := map[string]interface{}{
|
||||
"updatetime": time.Now(),
|
||||
}
|
||||
@@ -101,8 +106,8 @@ func UpdateTicket(db *gorm.DB, ticketid int, title, content, contactname, contac
|
||||
if category != "" {
|
||||
updates["category"] = category
|
||||
}
|
||||
if priority >= 0 {
|
||||
updates["priority"] = priority
|
||||
if priority != nil {
|
||||
updates["priority"] = *priority
|
||||
}
|
||||
if handlerid != nil {
|
||||
updates["handlerid"] = handlerid
|
||||
|
||||
Reference in New Issue
Block a user