统一订单资产状态变更
This commit is contained in:
@@ -7,16 +7,18 @@ import (
|
||||
"hfb_sys/backend/internal/modules/notification"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (r *Repository) AdminClose(adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
||||
var refund *refundAction
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
order, listing, account, err := r.findOrderAssetsForAdminUpdate(tx, orderID)
|
||||
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
|
||||
}
|
||||
@@ -30,9 +32,7 @@ func (r *Repository) AdminClose(adminID uint64, orderID uint64, req AdminActionR
|
||||
order.HandoffStatus = handoffStatusAdminClosed
|
||||
order.SettlementStatus = settlementStatusClosed
|
||||
order.SettledAt = &now
|
||||
listing.Status = listingStatusOffline
|
||||
listing.InTransaction = false
|
||||
account.Status = accountStatusOffline
|
||||
archiveAssets(listing, account)
|
||||
if beforeOrderStatus != orderStatusPendingPayment {
|
||||
totalCent := order.RentAmountCent + order.DepositAmountCent
|
||||
action, err := r.prepareRefund(order, totalCent, refundBizAdminClose, "客服关闭订单原路退款")
|
||||
@@ -97,10 +97,13 @@ func (r *Repository) AdminClose(adminID uint64, orderID uint64, req AdminActionR
|
||||
|
||||
func (r *Repository) AdminMarkAbnormal(adminID uint64, orderID uint64, req AdminActionRequest, meta AuditMeta) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
order, listing, account, err := r.findOrderAssetsForAdminUpdate(tx, orderID)
|
||||
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
|
||||
}
|
||||
@@ -110,9 +113,7 @@ func (r *Repository) AdminMarkAbnormal(adminID uint64, orderID uint64, req Admin
|
||||
beforeAccountStatus := account.Status
|
||||
order.Status = orderStatusAbnormal
|
||||
order.HandoffStatus = handoffStatusAdminAbnormal
|
||||
listing.Status = listingStatusAbnormal
|
||||
listing.InTransaction = false
|
||||
account.Status = accountStatusAbnormal
|
||||
markAssetsAbnormal(listing, account)
|
||||
if err := notification.Append(tx,
|
||||
notification.Entry{
|
||||
UserID: order.RenterID,
|
||||
@@ -211,22 +212,6 @@ func (r *Repository) buildRefundStatusDTO(order *model.RentalOrder) *RefundStatu
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) findOrderAssetsForAdminUpdate(tx *gorm.DB, orderID uint64) (*model.RentalOrder, *model.RentalListing, *model.GameAccount, error) {
|
||||
var order model.RentalOrder
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, orderID).Error; err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
var listing model.RentalListing
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing, order.ListingID).Error; err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
var account model.GameAccount
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&account, order.AccountID).Error; err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return &order, &listing, &account, nil
|
||||
}
|
||||
|
||||
func isTerminalStatus(status string) bool {
|
||||
return status == orderStatusCompleted || status == orderStatusCancelled || status == orderStatusClosed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user