feat(order): 统一网站自交接订单24小时自动完成倒计时

This commit is contained in:
yml
2026-11-30 17:05:49 +08:00
parent 295c88dd2f
commit 2992885a6d
32 changed files with 996 additions and 23 deletions
@@ -231,6 +231,12 @@ func (r *Repository) AdminResetHandoff(ctx context.Context, adminID uint64, orde
order.HandoffStatus = target.HandoffStatus
if target.RefreshStageTime {
order.HandoffStartedAt = &now
if !isPlatformHandoffOrder(order) && !isPlatformSettlementOrder(order) && order.HandoffDeadlineAt != nil {
if timeoutMinutes := autoCompleteHandoffTimeoutMinutesForOrder(tx, order); timeoutMinutes > 0 {
deadline := now.Add(time.Duration(timeoutMinutes) * time.Minute)
order.HandoffDeadlineAt = &deadline
}
}
}
if target.RecalculateEnd {
durationHours := estimateOrderDurationHours(order.AccountSnapshot)
@@ -503,6 +509,13 @@ func (r *Repository) AdminRejectRefund(ctx context.Context, orderID uint64, acti
if order.HandoffStatus == handoffStatusCancelled || order.HandoffStatus == handoffStatusRenterConfirmTimeout {
order.HandoffStatus = handoffStatusPendingOwner
}
if !isPlatformHandoffOrder(*order) && !isPlatformSettlementOrder(*order) && order.HandoffDeadlineAt != nil {
if timeoutMinutes := autoCompleteHandoffTimeoutMinutesForOrder(tx, *order); timeoutMinutes > 0 {
now := time.Now()
deadline := now.Add(time.Duration(timeoutMinutes) * time.Minute)
order.HandoffDeadlineAt = &deadline
}
}
order.SettlementStatus = settlementStatusUnsettled
order.SettledAt = nil
reserveListingForOrder(listing)
+5 -2
View File
@@ -97,6 +97,7 @@ type OrderDTO struct {
Checkout *CheckoutDTO `json:"checkout,omitempty"`
AdminActions *AdminActionsDTO `json:"admin_actions,omitempty"`
PaymentDeadlineAt *time.Time `json:"payment_deadline_at,omitempty"`
HandoffDeadlineAt *time.Time `json:"handoff_deadline_at,omitempty"`
DistributionSiteID *uint64 `json:"distribution_site_id,omitempty"`
SourceTrackCode string `json:"source_track_code,omitempty"`
CreatedAt time.Time `json:"created_at"`
@@ -122,6 +123,7 @@ type UserOrderListItemDTO struct {
HandoffStatus string `json:"handoff_status"`
CanHide bool `json:"can_hide,omitempty"`
PaymentDeadlineAt *time.Time `json:"payment_deadline_at,omitempty"`
HandoffDeadlineAt *time.Time `json:"handoff_deadline_at,omitempty"`
DistributionSiteID *uint64 `json:"distribution_site_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
@@ -147,8 +149,9 @@ type AdminOrderListItemDTO struct {
HandoffStatus string `json:"handoff_status"`
HandoffMode string `json:"handoff_mode"`
SettlementMode string `json:"settlement_mode"`
SettlementStatus string `json:"settlement_status"`
CreatedAt time.Time `json:"created_at"`
SettlementStatus string `json:"settlement_status"`
HandoffDeadlineAt *time.Time `json:"handoff_deadline_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
// OwnerIncomeAmountCent 为号主实际到手金额(分),仅在已结账/已仲裁订单上返回;
// 用指针区分「未结账(null)」与「到手 0 元」。平台代管订单取线下结算金额,
// 其他订单取号主钱包最终入账合计。
@@ -415,6 +415,12 @@ func (r *Repository) ConfirmPaidFromChannelTx(tx *gorm.DB, orderID uint64) (uint
order.Status = orderStatusPendingHandoff
order.HandoffStatus = handoffStatusPendingOwner
order.HandoffStartedAt = &now
if !isPlatformHandoffOrder(*order) && !isPlatformSettlementOrder(*order) {
if timeoutMinutes := autoCompleteHandoffTimeoutMinutesForOrder(tx, *order); timeoutMinutes > 0 {
deadline := now.Add(time.Duration(timeoutMinutes) * time.Minute)
order.HandoffDeadlineAt = &deadline
}
}
if err := markAssetsRented(tx, listing, account); err != nil {
return 0, err
}
@@ -151,6 +151,7 @@ func (row orderRow) toAdminDTO() OrderDTO {
DepositHeldAt: row.DepositHeldAt,
DepositHoldReleasedAt: row.DepositHoldReleasedAt,
AdminActions: adminActionsForOrder(row.RentalOrder),
HandoffDeadlineAt: effectiveHandoffDeadlineAt(row.RentalOrder),
DistributionSiteID: row.DistributionSiteID,
SourceTrackCode: row.SourceTrackCode,
CreatedAt: row.CreatedAt,
@@ -215,6 +216,7 @@ func (row orderRow) toUserListItem(userID uint64, paymentDeadlineAt *time.Time)
Status: row.Status,
HandoffStatus: row.HandoffStatus,
PaymentDeadlineAt: paymentDeadlineAt,
HandoffDeadlineAt: effectiveHandoffDeadlineAt(row.RentalOrder),
DistributionSiteID: row.DistributionSiteID,
CreatedAt: row.CreatedAt,
}
@@ -241,10 +243,22 @@ func (row orderRow) toAdminListItem() AdminOrderListItemDTO {
HandoffMode: effectiveHandoffMode(row.RentalOrder),
SettlementMode: effectiveSettlementMode(row.RentalOrder),
SettlementStatus: row.SettlementStatus,
HandoffDeadlineAt: effectiveHandoffDeadlineAt(row.RentalOrder),
CreatedAt: row.CreatedAt,
}
}
func effectiveHandoffDeadlineAt(order model.RentalOrder) *time.Time {
if order.Status != orderStatusPendingHandoff || order.HandoffDeadlineAt == nil {
return nil
}
if isPlatformHandoffOrder(order) || isPlatformSettlementOrder(order) {
return nil
}
deadline := *order.HandoffDeadlineAt
return &deadline
}
func effectiveHandoffMode(order model.RentalOrder) string {
if order.HandoffMode != "" {
return order.HandoffMode
@@ -144,6 +144,10 @@ func orderListingTerms(snapshot datatypes.JSON) (days int, dailyLossM float64) {
return days, roundQuantity(dailyLossM)
}
func EstimateOrderDurationHours(snapshot datatypes.JSON) int {
return estimateOrderDurationHours(snapshot)
}
func estimateOrderDurationHours(snapshot datatypes.JSON) int {
hafCoinM := readOrderSnapshotCoinM(snapshot)
if hafCoinM <= 0 {
+24
View File
@@ -462,6 +462,30 @@ func pendingPaymentTimeoutMinutes(tx *gorm.DB) int {
return value
}
func autoCompleteHandoffTimeoutMinutesForOrder(tx *gorm.DB, order model.RentalOrder) int {
var row model.SystemConfig
query := tx.Where("`key` = ?", "handoff.auto_complete_timeout_minutes")
tenantID := order.BuyerTenantID
if tenantID == 0 && tx.Statement != nil && tx.Statement.Context != nil {
tenantID = tenancy.IDFromContext(tx.Statement.Context)
}
if tenantID == 0 {
tenantID = order.SourceTenantID
}
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
err := query.First(&row).Error
if err != nil {
return defaultAutoCompleteHandoffTimeoutMinutes
}
value, err := strconv.Atoi(row.Value)
if err != nil || value < 0 {
return defaultAutoCompleteHandoffTimeoutMinutes
}
return value
}
func applyPaymentDeadline(dto *OrderDTO, order model.RentalOrder, timeoutMinutes int) {
if dto == nil || order.Status != orderStatusPendingPayment || timeoutMinutes <= 0 {
return
+4 -1
View File
@@ -64,7 +64,10 @@ type Repository struct {
paymentProbe ChannelPaymentProbe
}
const defaultPendingPaymentTimeoutMinutes = 15
const (
defaultPendingPaymentTimeoutMinutes = 15
defaultAutoCompleteHandoffTimeoutMinutes = 1440
)
func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
repo := &Repository{db: db}
@@ -3127,3 +3127,153 @@ func TestCheckoutInvitationRebateUsesActualPlatformProfit(t *testing.T) {
})
}
}
func TestConfirmPaidSetsUnifiedHandoffDeadlineForOwnerAndSkipsPlatform(t *testing.T) {
db := setupOrderTestDB(t)
repo := NewRepository(db)
if err := db.Create(&model.SystemConfig{
TenantID: 1,
Key: "handoff.auto_complete_timeout_minutes",
Value: "720",
}).Error; err != nil {
t.Fatalf("写入系统配置失败: %v", err)
}
owner := model.User{Phone: "13800009101", TenantID: 1}
renter := model.User{Phone: "13900009101", TenantID: 1}
if err := db.Create(&owner).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&renter).Error; err != nil {
t.Fatal(err)
}
makeOrder := func(suffix, handoffMode string) model.RentalOrder {
acc := model.GameAccount{
OwnerID: owner.ID,
Status: accountStatusPublished,
ServerRegion: "国服",
LoginPlatform: "steam",
Title: "自动交接测试账号-" + suffix,
}
if err := db.Create(&acc).Error; err != nil {
t.Fatal(err)
}
lst := model.RentalListing{
ListingNo: "LST-DL-" + suffix,
OwnerID: owner.ID,
AccountID: acc.ID,
Status: listingStatusPublished,
ReviewStatus: listingReviewStatusApproved,
InTransaction: true,
PriceCent: 1000,
HandoffMode: handoffMode,
}
if err := db.Create(&lst).Error; err != nil {
t.Fatal(err)
}
ord := model.RentalOrder{
BuyerTenantID: 1,
SourceTenantID: 1,
OrderNo: "ORD-DL-" + suffix,
ListingID: lst.ID,
AccountID: acc.ID,
OwnerID: owner.ID,
RenterID: renter.ID,
RentAmountCent: 1000,
Status: orderStatusPendingPayment,
HandoffStatus: handoffStatusNone,
HandoffMode: handoffMode,
}
if err := db.Create(&ord).Error; err != nil {
t.Fatal(err)
}
return ord
}
ownerOrder := makeOrder("OWN", handoffModeOwner)
platformOrder := makeOrder("PLT", handoffModePlatform)
beforePaid := time.Now()
if _, err := repo.ConfirmPaidFromChannelTx(db, ownerOrder.ID); err != nil {
t.Fatalf("ConfirmPaidFromChannelTx(owner) error = %v", err)
}
if _, err := repo.ConfirmPaidFromChannelTx(db, platformOrder.ID); err != nil {
t.Fatalf("ConfirmPaidFromChannelTx(platform) error = %v", err)
}
var savedOwner, savedPlatform model.RentalOrder
_ = db.First(&savedOwner, ownerOrder.ID)
_ = db.First(&savedPlatform, platformOrder.ID)
if savedOwner.HandoffDeadlineAt == nil {
t.Fatal("owner order HandoffDeadlineAt should be set after payment")
}
expectedMin := beforePaid.Add(719 * time.Minute)
expectedMax := time.Now().Add(721 * time.Minute)
if savedOwner.HandoffDeadlineAt.Before(expectedMin) || savedOwner.HandoffDeadlineAt.After(expectedMax) {
t.Fatalf("HandoffDeadlineAt = %v, want within ~720m [%v, %v]", savedOwner.HandoffDeadlineAt, expectedMin, expectedMax)
}
if savedPlatform.HandoffDeadlineAt != nil {
t.Fatalf("platform order HandoffDeadlineAt = %v, want nil", savedPlatform.HandoffDeadlineAt)
}
// 号主提交交接后,统一倒计时不重置(沿用原 HandoffDeadlineAt)
initialDeadline := *savedOwner.HandoffDeadlineAt
if _, err := repo.SubmitHandoff(t.Context(), owner.ID, ownerOrder.ID, SubmitHandoffRequest{Content: "已提交账号"}); err != nil {
t.Fatalf("SubmitHandoff() error = %v", err)
}
_ = db.First(&savedOwner, ownerOrder.ID)
if savedOwner.HandoffDeadlineAt == nil || !savedOwner.HandoffDeadlineAt.Equal(initialDeadline) {
t.Fatalf("HandoffDeadlineAt changed after SubmitHandoff: got %v, want %v", savedOwner.HandoffDeadlineAt, initialDeadline)
}
// 详情和列表在待交接阶段透出 handoff_deadline_at,平台代管订单与已收号订单不透出
userDTO, err := repo.FindForUser(t.Context(), renter.ID, ownerOrder.ID)
if err != nil {
t.Fatalf("FindForUser() error = %v", err)
}
if userDTO.HandoffDeadlineAt == nil {
t.Fatal("expected FindForUser to expose handoff_deadline_at during pending_handoff")
}
platformDTO, err := repo.FindForUser(t.Context(), renter.ID, platformOrder.ID)
if err != nil {
t.Fatalf("FindForUser(platform) error = %v", err)
}
if platformDTO.HandoffDeadlineAt != nil {
t.Fatalf("expected platform order DTO handoff_deadline_at nil, got %v", platformDTO.HandoffDeadlineAt)
}
// 列表接口也应在待交接阶段透出 handoff_deadline_at,平台代管订单不透出
listResult, err := repo.ListForUser(t.Context(), renter.ID, 1, 20)
if err != nil {
t.Fatalf("ListForUser() error = %v", err)
}
foundOwnerInList := false
for _, item := range listResult.Items {
if item.ID == ownerOrder.ID {
foundOwnerInList = true
if item.HandoffDeadlineAt == nil {
t.Fatal("expected ListForUser to expose handoff_deadline_at during pending_handoff")
}
}
if item.ID == platformOrder.ID && item.HandoffDeadlineAt != nil {
t.Fatalf("expected platform order list item handoff_deadline_at nil, got %v", item.HandoffDeadlineAt)
}
}
if !foundOwnerInList {
t.Fatal("expected ownerOrder in ListForUser result")
}
if err := repo.ConfirmReceive(t.Context(), renter.ID, ownerOrder.ID); err != nil {
t.Fatalf("ConfirmReceive() error = %v", err)
}
afterReceiveDTO, err := repo.FindForUser(t.Context(), renter.ID, ownerOrder.ID)
if err != nil {
t.Fatalf("FindForUser() after receive error = %v", err)
}
if afterReceiveDTO.HandoffDeadlineAt != nil {
t.Fatalf("expected handoff_deadline_at cleared from DTO once renting, got %v", afterReceiveDTO.HandoffDeadlineAt)
}
}