From 2d2d2d205178809c15584fb373d06ecd90f27bde Mon Sep 17 00:00:00 2001 From: mlogclub Date: Mon, 27 Apr 2026 19:09:29 +0800 Subject: [PATCH] feat: implement EnsureExternalCustomer method in CustomerService and refactor customer creation logic --- internal/services/conversation_service.go | 39 +------------------- internal/services/customer_service.go | 44 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/internal/services/conversation_service.go b/internal/services/conversation_service.go index fd5f03e..69250d2 100644 --- a/internal/services/conversation_service.go +++ b/internal/services/conversation_service.go @@ -113,7 +113,7 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha var conversation *models.Conversation created := false if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error { - customerID, err := s.ensureExternalCustomer(ctx.Tx, externalInfo) + customerID, err := CustomerService.EnsureExternalCustomer(ctx.Tx, externalInfo) if err != nil { return err } @@ -170,43 +170,6 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha return s.Get(conversation.ID), nil } -func (s *conversationService) ensureExternalCustomer(db *gorm.DB, externalInfo openidentity.ExternalInfo) (int64, error) { - externalSource := externalInfo.ExternalSource - externalID := strings.TrimSpace(externalInfo.ExternalID) - if strings.TrimSpace(string(externalSource)) == "" || externalID == "" { - return 0, errorsx.Unauthorized("外部用户标识不能为空") - } - now := time.Now() - if identity := repositories.CustomerIdentityRepository.GetBy(db, externalSource, externalID); identity != nil { - _ = repositories.CustomerRepository.Updates(db, identity.CustomerID, map[string]any{ - "last_active_at": now, - "updated_at": now, - }) - return identity.CustomerID, nil - } - - customer := &models.Customer{ - Name: s.buildDefaultSubject(externalInfo), - LastActiveAt: &now, - Status: enums.StatusOk, - AuditFields: utils.BuildAuditFields(nil), - } - if err := repositories.CustomerRepository.Create(db, customer); err != nil { - return 0, err - } - identity := &models.CustomerIdentity{ - CustomerID: customer.ID, - ExternalSource: externalSource, - ExternalID: externalID, - Status: enums.StatusOk, - AuditFields: utils.BuildAuditFields(nil), - } - if err := repositories.CustomerIdentityRepository.Create(db, identity); err != nil { - return 0, err - } - return customer.ID, nil -} - func (s *conversationService) AssignConversation(req request.AssignConversationRequest, operator *dto.AuthPrincipal) error { if operator == nil { return errorsx.Unauthorized("未登录或登录已过期") diff --git a/internal/services/customer_service.go b/internal/services/customer_service.go index bca65bb..6f9d2d9 100644 --- a/internal/services/customer_service.go +++ b/internal/services/customer_service.go @@ -8,6 +8,7 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/enums" "cs-agent/internal/pkg/errorsx" + "cs-agent/internal/pkg/openidentity" "cs-agent/internal/pkg/utils" "cs-agent/internal/repositories" "strings" @@ -110,6 +111,49 @@ func (s *customerService) CountByCompanyIDs(companyIDs []int64) map[int64]int64 return repositories.CustomerRepository.CountByCompanyIDs(sqls.DB(), companyIDs, int(enums.StatusDeleted)) } +func (s *customerService) EnsureExternalCustomer(db *gorm.DB, externalInfo openidentity.ExternalInfo) (int64, error) { + externalSource := externalInfo.ExternalSource + externalID := strings.TrimSpace(externalInfo.ExternalID) + if strings.TrimSpace(string(externalSource)) == "" || externalID == "" { + return 0, errorsx.Unauthorized("外部用户标识不能为空") + } + now := time.Now() + if identity := repositories.CustomerIdentityRepository.GetBy(db, externalSource, externalID); identity != nil { + _ = repositories.CustomerRepository.Updates(db, identity.CustomerID, map[string]any{ + "last_active_at": now, + "updated_at": now, + }) + return identity.CustomerID, nil + } + + customer := &models.Customer{ + Name: buildExternalCustomerName(externalInfo), + LastActiveAt: &now, + Status: enums.StatusOk, + AuditFields: utils.BuildAuditFields(nil), + } + if err := repositories.CustomerRepository.Create(db, customer); err != nil { + return 0, err + } + if err := repositories.CustomerIdentityRepository.Create(db, &models.CustomerIdentity{ + CustomerID: customer.ID, + ExternalSource: externalSource, + ExternalID: externalID, + Status: enums.StatusOk, + AuditFields: utils.BuildAuditFields(nil), + }); err != nil { + return 0, err + } + return customer.ID, nil +} + +func buildExternalCustomerName(externalInfo openidentity.ExternalInfo) string { + if strs.IsNotBlank(externalInfo.ExternalName) { + return externalInfo.ExternalName + } + return "访客" + hashUUID(externalInfo.ExternalID) +} + func (s *customerService) CreateCustomer(req request.CreateCustomerRequest, operator *dto.AuthPrincipal) (*models.Customer, error) { if operator == nil { return nil, errorsx.Unauthorized("未登录或登录已过期")