feat: 客服封存订单功能(号主失联场景)

新增客服「封存订单」动作,用于号主失联、无法联系的场景:单事务内
完成关闭订单、全量原路退款、永久封存关联商品、解散发布群、双方通知
与审计日志。

- 新增 listing 终态 sealed(封存),区别于可恢复的 offline
- listingLockedForOwnerMutation 锁定 sealed,号主无法再编辑/提审/上架
- 解散群聊按 listing_id 查发布群(listing_group)置 archived,
  阻断所有成员发言并留系统消息
- 新增 order.AdminSeal 及 /admin/orders/:id/seal 路由
- 前端:卖家页 PC/移动端 sealed 视为终态禁用操作,客服后台新增
  「封存订单」按钮与二次确认

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
yml2213
2026-06-29 22:52:20 +08:00
co-authored by Claude Opus 4.8
parent 8a11c2517e
commit be5d322c10
16 changed files with 217 additions and 4 deletions
+14
View File
@@ -22,3 +22,17 @@ func (a *ListingChatCreatorAdapter) EnsureListingConversation(tx *gorm.DB, listi
}
return conv.ID, nil
}
// OrderChatArchiverAdapter 适配器,实现 order.OrderChatArchiver 接口,
// 供订单封存时在同一事务内解散关联的发布群。
type OrderChatArchiverAdapter struct{}
func NewOrderChatArchiverAdapter() *OrderChatArchiverAdapter {
return &OrderChatArchiverAdapter{}
}
// ArchiveListingConversation 将商品关联的发布群标记为已归档(解散),
// 写入一条系统消息提示群聊已被客服解散。会话不存在时静默跳过。
func (a *OrderChatArchiverAdapter) ArchiveListingConversation(tx *gorm.DB, listingID uint64, reason string) error {
return ArchiveListingConversation(tx, listingID, reason)
}
+28
View File
@@ -2,6 +2,7 @@ package chat
import (
"context"
"errors"
"fmt"
"gorm.io/gorm"
"hfb_sys/backend/internal/model"
@@ -366,3 +367,30 @@ func applyAdminChatStageFilter(db *gorm.DB, stage string, principal Principal) {
return
}
}
// ArchiveListingConversation 将商品关联的发布群标记为已归档(解散),并写入系统提示。
// 用于客服封存订单时同事务解散群聊;发布群不存在时静默跳过,不阻断封存流程。
// 归档后 SendMessage 的 status 校验会阻断所有成员继续发言。
func ArchiveListingConversation(tx *gorm.DB, listingID uint64, reason string) error {
var conversation model.ChatConversation
err := tx.Where("listing_id = ?", listingID).First(&conversation).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
if conversation.Status == "archived" {
return nil
}
if err := tx.Model(&model.ChatConversation{}).
Where("id = ?", conversation.ID).
Update("status", "archived").Error; err != nil {
return err
}
content := "群聊已由客服解散,订单已封存。"
if trimmed := strings.TrimSpace(reason); trimmed != "" {
content += "原因:" + trimmed
}
return sendSystemMessage(tx, conversation.ID, content)
}
+2 -1
View File
@@ -370,5 +370,6 @@ func listingLockedForOwnerMutation(listing *model.RentalListing) bool {
if listing == nil {
return true
}
return listing.Status == "rented" || listing.Status == "completed" || listing.InTransaction
return listing.Status == "rented" || listing.Status == "completed" ||
listing.Status == "sealed" || listing.InTransaction
}
@@ -440,6 +440,100 @@ func (r *Repository) AdminRejectRefund(ctx context.Context, orderID uint64, acti
})
}
// AdminSeal 客服封存订单:关闭订单并全额原路退款,将关联商品/账号置为终态 sealed
// (号主不可再编辑、提审、上架),同时在同一事务内解散关联群聊。
// 适用于号主失联、无法继续交接,需要终止并永久封存该商品的场景。
func (r *Repository) AdminSeal(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
var refund *refundAction
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
assets, err := r.lockOrderAssets(tx, orderID)
if err != nil {
return err
}
order := assets.Order
listing := assets.Listing
account := assets.Account
if isTerminalStatus(order.Status) {
return ErrOrderCannotComplete
}
beforeOrderStatus := order.Status
beforeHandoffStatus := order.HandoffStatus
beforeSettlementStatus := order.SettlementStatus
beforeListingStatus := listing.Status
beforeAccountStatus := account.Status
now := time.Now()
order.Status = orderStatusClosed
order.HandoffStatus = handoffStatusAdminClosed
order.SettlementStatus = settlementStatusClosed
order.SettledAt = &now
sealAssets(listing, account)
if beforeOrderStatus != orderStatusPendingPayment {
totalCent := order.RentAmountCent + order.DepositAmountCent
action, err := r.prepareRefund(order, totalCent, refundBizAdminSeal, "客服封存订单原路退款")
if err != nil {
return err
}
refund = action
}
if r.chatArchiver != nil {
if err := r.chatArchiver.ArchiveListingConversation(tx, listing.ID, req.Reason); err != nil {
return err
}
}
if err := notification.Append(tx,
notification.Entry{
UserID: order.RenterID,
Type: "order_admin",
Title: "订单已由客服封存",
Content: "客服已封存订单,退款将原路退回您的支付账户。原因:" + req.Reason,
BizType: "order",
BizID: &order.ID,
},
notification.Entry{
UserID: order.OwnerID,
Type: "order_admin",
Title: "订单已由客服封存",
Content: "客服已封存订单,关联商品已永久封存,不可再次编辑或上架。原因:" + req.Reason,
BizType: "order",
BizID: &order.ID,
},
); err != nil {
return err
}
if err := appendAuditLog(tx, adminID, "order.admin_seal", "order", order.ID, meta, map[string]any{
"order_id": order.ID,
"order_no": order.OrderNo,
"listing_id": order.ListingID,
"account_id": order.AccountID,
"reason": req.Reason,
"before_order_status": beforeOrderStatus,
"after_order_status": order.Status,
"before_handoff_status": beforeHandoffStatus,
"after_handoff_status": order.HandoffStatus,
"before_settlement_status": beforeSettlementStatus,
"after_settlement_status": order.SettlementStatus,
"before_listing_status": beforeListingStatus,
"after_listing_status": listing.Status,
"before_account_status": beforeAccountStatus,
"after_account_status": account.Status,
}); err != nil {
return err
}
if err := tx.Save(order).Error; err != nil {
return err
}
if err := tx.Save(listing).Error; err != nil {
return err
}
return tx.Save(account).Error
})
if err != nil {
return err
}
r.startRefundBestEffort(ctx, refund)
return nil
}
func isTerminalStatus(status string) bool {
return status == orderStatusCompleted || status == orderStatusCancelled || status == orderStatusClosed
}
+7
View File
@@ -63,6 +63,13 @@ func archiveAssets(listing *model.RentalListing, account *model.GameAccount) {
account.Status = accountStatusOffline
}
func sealAssets(listing *model.RentalListing, account *model.GameAccount) {
listing.Status = listingStatusSealed
listing.InTransaction = false
listing.PublishedAt = nil
account.Status = accountStatusSealed
}
func completeAssets(listing *model.RentalListing, account *model.GameAccount) {
listing.Status = listingStatusCompleted
listing.InTransaction = false
@@ -50,17 +50,20 @@ const (
listingStatusOffline = "offline"
listingStatusCompleted = "completed"
listingStatusAbnormal = "abnormal"
listingStatusSealed = "sealed"
accountStatusPublished = "published"
accountStatusRented = "rented"
accountStatusOffline = "offline"
accountStatusAbnormal = "abnormal"
accountStatusSealed = "sealed"
walletBizOwnerIncome = "owner_income"
walletBizDepositCompensation = "deposit_compensation"
refundBizCancel = "cancel_refund"
refundBizAdminClose = "admin_close_refund"
refundBizAdminSeal = "admin_seal_refund"
refundBizAdmin = "admin_refund"
refundBizCheckout = "checkout_refund"
)
@@ -62,6 +62,10 @@ func (h *Handler) AdminClose(c *gin.Context) {
h.adminAction(c, h.service.AdminClose, gin.H{"closed": true})
}
func (h *Handler) AdminSeal(c *gin.Context) {
h.adminAction(c, h.service.AdminSeal, gin.H{"sealed": true})
}
func (h *Handler) AdminMarkAbnormal(c *gin.Context) {
h.adminAction(c, h.service.AdminMarkAbnormal, gin.H{"abnormal": true})
}
@@ -21,8 +21,15 @@ type OrderChatNotifier interface {
NotifyNewConversation(conversationID uint64)
}
// OrderChatArchiver 由 chat 模块适配实现,在订单封存时于同一事务内解散关联群聊。
// 实际聊天群按 listing 关联(发布群 listing_group),故传 listingID。
type OrderChatArchiver interface {
ArchiveListingConversation(tx *gorm.DB, listingID uint64, reason string) error
}
type Dependencies struct {
ChatNotifier OrderChatNotifier
ChatArchiver OrderChatArchiver
RefundStarter RefundStarter
}
@@ -36,6 +43,7 @@ type refundAction struct {
type Repository struct {
db *gorm.DB
chatNotifier OrderChatNotifier
chatArchiver OrderChatArchiver
refundStarter RefundStarter
}
@@ -45,6 +53,7 @@ func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
repo := &Repository{db: db}
if len(deps) > 0 {
repo.chatNotifier = deps[0].ChatNotifier
repo.chatArchiver = deps[0].ChatArchiver
repo.refundStarter = deps[0].RefundStarter
}
return repo
+10
View File
@@ -196,6 +196,16 @@ func (s *Service) AdminMarkAbnormal(ctx context.Context, adminID uint64, orderID
return s.repo.AdminMarkAbnormal(ctx, adminID, orderID, req, meta)
}
func (s *Service) AdminSeal(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
if orderID == 0 || req.Reason == "" {
return ErrOrderCannotComplete
}
return s.repo.AdminSeal(ctx, adminID, orderID, req, meta)
}
func (s *Service) AdminResetHandoff(ctx context.Context, adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
if s.repo == nil {
return ErrDependencyUnavailable