133 lines
4.4 KiB
Go
133 lines
4.4 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"hfb_sys/backend/internal/model"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
const (
|
|
autoWelcomeConfigKey = "chat.auto_welcome_message"
|
|
defaultAutoWelcomeMessage = "欢迎加入订单群聊!如有任何问题,请随时沟通。"
|
|
listingGroupWelcomeConfigKey = "chat.listing_group_welcome"
|
|
defaultListingGroupWelcome = "欢迎加入账号群!请号主扫描下方二维码加入企业微信群,方便客服与您及时联系。"
|
|
)
|
|
|
|
func (r *Repository) UpdateRemark(ctx context.Context, principal Principal, conversationID uint64, remark string) error {
|
|
if principal.Type != "admin" {
|
|
return ErrPermissionDenied
|
|
}
|
|
db := r.db.WithContext(ctx)
|
|
var conversation model.ChatConversation
|
|
if err := db.Select("id").First(&conversation, conversationID).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return ErrConversationNotFound
|
|
}
|
|
return err
|
|
}
|
|
state := model.ChatAdminConversationState{
|
|
ConversationID: conversationID,
|
|
AdminUserID: principal.ID,
|
|
Remark: strings.TrimSpace(remark),
|
|
}
|
|
return db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "conversation_id"}, {Name: "admin_user_id"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"remark", "updated_at"}),
|
|
}).Create(&state).Error
|
|
}
|
|
func (r *Repository) ListQuickReplies(ctx context.Context, adminID uint64) ([]QuickReplyDTO, error) {
|
|
var replies []model.ChatQuickReply
|
|
err := r.db.WithContext(ctx).Where("admin_user_id = ? OR admin_user_id = 0", adminID).
|
|
Order("admin_user_id DESC, sort_order ASC, id ASC").
|
|
Find(&replies).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]QuickReplyDTO, len(replies))
|
|
for i, reply := range replies {
|
|
result[i] = QuickReplyDTO{
|
|
ID: reply.ID,
|
|
AdminUserID: reply.AdminUserID,
|
|
Title: reply.Title,
|
|
Content: reply.Content,
|
|
SortOrder: reply.SortOrder,
|
|
IsGlobal: reply.AdminUserID == 0,
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
func (r *Repository) CreateQuickReply(ctx context.Context, adminID uint64, req CreateQuickReplyRequest) (*QuickReplyDTO, error) {
|
|
ownerID := adminID
|
|
if req.IsGlobal {
|
|
ownerID = 0
|
|
}
|
|
reply := model.ChatQuickReply{
|
|
AdminUserID: ownerID,
|
|
Title: req.Title,
|
|
Content: req.Content,
|
|
SortOrder: req.SortOrder,
|
|
}
|
|
if err := r.db.WithContext(ctx).Create(&reply).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &QuickReplyDTO{
|
|
ID: reply.ID,
|
|
AdminUserID: reply.AdminUserID,
|
|
Title: reply.Title,
|
|
Content: reply.Content,
|
|
SortOrder: reply.SortOrder,
|
|
IsGlobal: reply.AdminUserID == 0,
|
|
}, nil
|
|
}
|
|
func (r *Repository) UpdateQuickReply(ctx context.Context, adminID uint64, replyID uint64, req UpdateQuickReplyRequest) error {
|
|
query := r.db.WithContext(ctx).Model(&model.ChatQuickReply{}).Where("id = ? AND (admin_user_id = ? OR admin_user_id = 0)", replyID, adminID)
|
|
updates := map[string]interface{}{}
|
|
if req.Title != "" {
|
|
updates["title"] = req.Title
|
|
}
|
|
if req.Content != "" {
|
|
updates["content"] = req.Content
|
|
}
|
|
if req.SortOrder != nil {
|
|
updates["sort_order"] = *req.SortOrder
|
|
}
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
return query.Updates(updates).Error
|
|
}
|
|
func (r *Repository) DeleteQuickReply(ctx context.Context, adminID uint64, replyID uint64) error {
|
|
return r.db.WithContext(ctx).Where("id = ? AND admin_user_id = ?", replyID, adminID).
|
|
Delete(&model.ChatQuickReply{}).Error
|
|
}
|
|
func (r *Repository) GetAutoWelcomeMessage(ctx context.Context) string {
|
|
var cfg model.SystemConfig
|
|
if err := r.db.WithContext(ctx).Where("`key` = ?", autoWelcomeConfigKey).First(&cfg).Error; err != nil {
|
|
return defaultAutoWelcomeMessage
|
|
}
|
|
return cfg.Value
|
|
}
|
|
func (r *Repository) UpdateAutoWelcomeMessage(ctx context.Context, message string) error {
|
|
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
|
Where("`key` = ?", autoWelcomeConfigKey).
|
|
Update("value", message).Error
|
|
}
|
|
|
|
func (r *Repository) GetListingGroupWelcomeMessage(ctx context.Context) string {
|
|
var cfg model.SystemConfig
|
|
if err := r.db.WithContext(ctx).Where("`key` = ?", listingGroupWelcomeConfigKey).First(&cfg).Error; err != nil {
|
|
return defaultListingGroupWelcome
|
|
}
|
|
return cfg.Value
|
|
}
|
|
|
|
func (r *Repository) UpdateListingGroupWelcomeMessage(ctx context.Context, message string) error {
|
|
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
|
Where("`key` = ?", listingGroupWelcomeConfigKey).
|
|
Update("value", message).Error
|
|
}
|