refactor: update Conversation model to include CustomerName, adjust related logic and queries
This commit is contained in:
@@ -59,7 +59,7 @@ func (s *conversationService) ListConversations(userID int64, filter request.Age
|
||||
if strs.IsNotBlank(keyword) {
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
keywordLike := "%" + keyword + "%"
|
||||
cnd.Where("last_message_summary LIKE ? OR customer_id IN (SELECT id FROM t_customer WHERE name LIKE ?)", keywordLike, keywordLike)
|
||||
cnd.Where("customer_name LIKE ? OR last_message_summary LIKE ?", keywordLike, keywordLike)
|
||||
}
|
||||
|
||||
switch filter {
|
||||
@@ -113,8 +113,18 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
customerName := s.getCustomerName(ctx.Tx, customerID)
|
||||
if existing := s.getLatestNotFinishedByCustomerID(ctx.Tx, customerID); existing != nil {
|
||||
conversation = existing
|
||||
if customerName != "" && existing.CustomerName != customerName {
|
||||
if err := repositories.ConversationRepository.Updates(ctx.Tx, existing.ID, map[string]any{
|
||||
"customer_name": customerName,
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
conversation.CustomerName = customerName
|
||||
}
|
||||
return nil
|
||||
}
|
||||
created = true
|
||||
@@ -123,6 +133,7 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha
|
||||
AIAgentID: aiAgentID,
|
||||
ChannelID: channelID,
|
||||
CustomerID: customerID,
|
||||
CustomerName: customerName,
|
||||
Status: s.resolveInitialStatus(aiAgent.ServiceMode),
|
||||
ServiceMode: aiAgent.ServiceMode,
|
||||
Priority: 0,
|
||||
@@ -662,7 +673,14 @@ func (s *conversationService) BuildConversationSummary(conversation *models.Conv
|
||||
if strings.TrimSpace(conversation.LastMessageSummary) != "" {
|
||||
return conversation.LastMessageSummary
|
||||
}
|
||||
if customer := CustomerService.Get(conversation.CustomerID); customer != nil {
|
||||
return strings.TrimSpace(conversation.CustomerName)
|
||||
}
|
||||
|
||||
func (s *conversationService) getCustomerName(db *gorm.DB, customerID int64) string {
|
||||
if customerID <= 0 {
|
||||
return ""
|
||||
}
|
||||
if customer := repositories.CustomerRepository.Get(db, customerID); customer != nil {
|
||||
return strings.TrimSpace(customer.Name)
|
||||
}
|
||||
return ""
|
||||
@@ -739,6 +757,7 @@ func (s *conversationService) LinkConversationCustomer(conversationID, customerI
|
||||
now := time.Now()
|
||||
return repositories.ConversationRepository.Updates(ctx.Tx, conversationID, map[string]any{
|
||||
"customer_id": customerID,
|
||||
"customer_name": strings.TrimSpace(cust.Name),
|
||||
"update_user_id": operator.UserID,
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": now,
|
||||
|
||||
@@ -218,16 +218,22 @@ func (s *customerService) UpdateCustomer(req request.UpdateCustomerRequest, oper
|
||||
}
|
||||
}
|
||||
|
||||
return repositories.CustomerRepository.Updates(sqls.DB(), req.ID, map[string]any{
|
||||
"name": name,
|
||||
"gender": req.Gender,
|
||||
"company_id": req.CompanyID,
|
||||
"primary_mobile": strings.TrimSpace(req.PrimaryMobile),
|
||||
"primary_email": strings.TrimSpace(req.PrimaryEmail),
|
||||
"remark": strings.TrimSpace(req.Remark),
|
||||
"update_user_id": operator.UserID,
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": time.Now(),
|
||||
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
||||
now := time.Now()
|
||||
if err := repositories.CustomerRepository.Updates(ctx.Tx, req.ID, map[string]any{
|
||||
"name": name,
|
||||
"gender": req.Gender,
|
||||
"company_id": req.CompanyID,
|
||||
"primary_mobile": strings.TrimSpace(req.PrimaryMobile),
|
||||
"primary_email": strings.TrimSpace(req.PrimaryEmail),
|
||||
"remark": strings.TrimSpace(req.Remark),
|
||||
"update_user_id": operator.UserID,
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.syncConversationCustomerName(ctx.Tx, req.ID, name, operator, now)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -244,6 +250,21 @@ func (s *customerService) DeleteCustomer(id int64, operator dto.AuthPrincipal) e
|
||||
})
|
||||
}
|
||||
|
||||
func (s *customerService) syncConversationCustomerName(db *gorm.DB, customerID int64, name string, operator *dto.AuthPrincipal, now time.Time) error {
|
||||
if customerID <= 0 {
|
||||
return nil
|
||||
}
|
||||
updates := map[string]any{
|
||||
"customer_name": strings.TrimSpace(name),
|
||||
"updated_at": now,
|
||||
}
|
||||
if operator != nil {
|
||||
updates["update_user_id"] = operator.UserID
|
||||
updates["update_user_name"] = operator.Username
|
||||
}
|
||||
return repositories.ConversationRepository.UpdatesByCustomerID(db, customerID, updates)
|
||||
}
|
||||
|
||||
func (s *customerService) UpdateStatus(id int64, status int, operator *dto.AuthPrincipal) error {
|
||||
if operator == nil {
|
||||
return errorsx.Unauthorized("未登录或登录已过期")
|
||||
@@ -316,6 +337,9 @@ func (s *customerService) SaveCustomerProfile(req request.SaveCustomerProfileReq
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.syncConversationCustomerName(ctx.Tx, customerID, name, operator, now); err != nil {
|
||||
return err
|
||||
}
|
||||
out = repositories.CustomerRepository.Get(ctx.Tx, customerID)
|
||||
}
|
||||
return CustomerContactService.ReplaceAllForCustomerInTx(ctx, customerID, req.Contacts, operator)
|
||||
|
||||
Reference in New Issue
Block a user