refactor: remove Subject field from Conversation model and related logic, replace with CustomerName

This commit is contained in:
mlogclub
2026-04-27 19:22:40 +08:00
parent 2d2d2d2051
commit 3a9bc4914a
24 changed files with 49 additions and 64 deletions
+6 -24
View File
@@ -2,10 +2,7 @@ package services
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"log/slog"
"cs-agent/internal/events"
@@ -61,7 +58,8 @@ func (s *conversationService) ListConversations(userID int64, filter request.Age
if strs.IsNotBlank(keyword) {
keyword = strings.TrimSpace(keyword)
cnd.Where("subject LIKE ? OR last_message_summary LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
keywordLike := "%" + keyword + "%"
cnd.Where("last_message_summary LIKE ? OR customer_id IN (SELECT id FROM t_customer WHERE name LIKE ?)", keywordLike, keywordLike)
}
switch filter {
@@ -103,8 +101,6 @@ func (s *conversationService) getLatestNotFinishedByCustomerID(db *gorm.DB, cust
}
func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, channelID, aiAgentID int64) (*models.Conversation, error) {
subject := s.buildDefaultSubject(externalInfo)
aiAgent := AIAgentService.Get(aiAgentID)
if aiAgent == nil || aiAgent.Status != enums.StatusOk {
return nil, errorsx.InvalidParam("AI Agent not found")
@@ -127,7 +123,6 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha
AIAgentID: aiAgentID,
ChannelID: channelID,
CustomerID: customerID,
Subject: subject,
Status: s.resolveInitialStatus(aiAgent.ServiceMode),
ServiceMode: aiAgent.ServiceMode,
Priority: 0,
@@ -667,7 +662,10 @@ func (s *conversationService) BuildConversationSummary(conversation *models.Conv
if strings.TrimSpace(conversation.LastMessageSummary) != "" {
return conversation.LastMessageSummary
}
return strings.TrimSpace(conversation.Subject)
if customer := CustomerService.Get(conversation.CustomerID); customer != nil {
return strings.TrimSpace(customer.Name)
}
return ""
}
func (s *conversationService) canCloseConversation(conversation *models.Conversation, operator *dto.AuthPrincipal) bool {
@@ -806,22 +804,6 @@ func (s *conversationService) canLinkConversationCustomer(conv *models.Conversat
}
}
func (s *conversationService) buildDefaultSubject(externalInfo openidentity.ExternalInfo) string {
if strs.IsNotBlank(externalInfo.ExternalName) {
return externalInfo.ExternalName
}
return fmt.Sprintf("访客%s", hashUUID(externalInfo.ExternalID))
}
func hashUUID(uuid string) string {
if uuid == "" {
return "unknown"
}
h := md5.Sum([]byte(uuid))
return hex.EncodeToString(h[:])[:8]
}
func (s *conversationService) resolveInitialStatus(serviceMode enums.IMConversationServiceMode) enums.IMConversationStatus {
switch serviceMode {
case enums.IMConversationServiceModeHumanOnly:
+11
View File
@@ -1,6 +1,8 @@
package services
import (
"crypto/md5"
"encoding/hex"
"log/slog"
"cs-agent/internal/models"
@@ -154,6 +156,15 @@ func buildExternalCustomerName(externalInfo openidentity.ExternalInfo) string {
return "访客" + hashUUID(externalInfo.ExternalID)
}
func hashUUID(uuid string) string {
if uuid == "" {
return "unknown"
}
h := md5.Sum([]byte(uuid))
return hex.EncodeToString(h[:])[:8]
}
func (s *customerService) CreateCustomer(req request.CreateCustomerRequest, operator *dto.AuthPrincipal) (*models.Customer, error) {
if operator == nil {
return nil, errorsx.Unauthorized("未登录或登录已过期")
@@ -55,7 +55,7 @@ func buildConversationAssignedNotifyBody(conversation *models.Conversation, assi
}
lines := []string{
fmt.Sprintf("会话ID: #%d", conversation.ID),
fmt.Sprintf("会话主题: %s", strs.DefaultIfBlank(conversation.Subject, "-")),
fmt.Sprintf("会话摘要: %s", strs.DefaultIfBlank(services.ConversationService.BuildConversationSummary(conversation), "-")),
fmt.Sprintf("接入渠道: %s", resolveConversationChannelLabel(conversation)),
fmt.Sprintf("当前状态: %s", enums.GetIMConversationStatusLabel(conversation.Status)),
fmt.Sprintf("处理人: %s", resolveNotifyUserLabel(assigneeID)),
+1 -1
View File
@@ -852,7 +852,7 @@ func (s *ticketService) CreateFromConversation(req request.CreateTicketFromConve
}
title := strings.TrimSpace(req.Title)
if title == "" {
title = strings.TrimSpace(conversation.Subject)
title = ConversationService.BuildConversationSummary(conversation)
}
description := strings.TrimSpace(req.Description)
if description == "" {