集中订单状态常量

This commit is contained in:
yml
2026-06-09 20:01:37 +08:00
parent 609b49f92b
commit 0990652f8b
7 changed files with 128 additions and 68 deletions
+27 -27
View File
@@ -21,7 +21,7 @@ func (r *Repository) SubmitCheckout(userID uint64, orderID uint64, req SubmitChe
if order.RenterID != userID {
return ErrPermissionDenied
}
if (order.Status != "renting" && order.Status != "overdue") || (order.HandoffStatus != "received" && order.HandoffStatus != "return_overdue") {
if (order.Status != orderStatusRenting && order.Status != orderStatusOverdue) || (order.HandoffStatus != handoffStatusReceived && order.HandoffStatus != handoffStatusReturnOverdue) {
return ErrCheckoutCannotSubmit
}
hasOpen, err := hasOpenCheckout(tx, order.ID)
@@ -41,16 +41,16 @@ func (r *Repository) SubmitCheckout(userID uint64, orderID uint64, req SubmitChe
if err := tx.Create(&record).Error; err != nil {
return err
}
checkout, err := buildCheckout(order, order.RenterID, "submitted", req.Content, req.EvidenceURLS, req.ConsumableAmountCent, req.CoinConsumedM, req.OtherAmountCent, 0, false)
checkout, err := buildCheckout(order, order.RenterID, checkoutStatusSubmitted, req.Content, req.EvidenceURLS, req.ConsumableAmountCent, req.CoinConsumedM, req.OtherAmountCent, 0, false)
if err != nil {
return err
}
if err := tx.Create(&checkout).Error; err != nil {
return err
}
order.Status = "pending_checkout_confirm"
order.HandoffStatus = "pending_owner_checkout"
order.SettlementStatus = "pending"
order.Status = orderStatusPendingCheckoutConfirm
order.HandoffStatus = handoffStatusPendingOwnerCheckout
order.SettlementStatus = settlementStatusPending
orderID := order.ID
if err := notification.Append(tx, notification.Entry{
UserID: order.OwnerID,
@@ -88,12 +88,12 @@ func (r *Repository) ConfirmCheckout(userID uint64, orderID uint64) error {
if order.OwnerID != userID {
return ErrPermissionDenied
}
if order.Status != "pending_checkout_confirm" || order.HandoffStatus != "pending_owner_checkout" {
if order.Status != orderStatusPendingCheckoutConfirm || order.HandoffStatus != handoffStatusPendingOwnerCheckout {
return ErrCheckoutCannotConfirm
}
var checkout model.OrderCheckout
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
Where("order_id = ? AND status = ?", order.ID, "submitted").
Where("order_id = ? AND status = ?", order.ID, checkoutStatusSubmitted).
Order("id DESC").
First(&checkout).Error; err != nil {
return err
@@ -104,7 +104,7 @@ func (r *Repository) ConfirmCheckout(userID uint64, orderID uint64) error {
Update("confirmed_by_owner_at", now).Error; err != nil {
return err
}
checkout.Status = "accepted"
checkout.Status = checkoutStatusAccepted
checkout.OwnerAdjustedAt = &now
action, err := r.finalizeCheckout(tx, &order, &checkout, "号主已确认结账,订单完成。")
refund = action
@@ -127,22 +127,22 @@ func (r *Repository) CounterCheckout(userID uint64, orderID uint64, req CounterC
if order.OwnerID != userID {
return ErrPermissionDenied
}
if order.Status != "pending_checkout_confirm" || order.HandoffStatus != "pending_owner_checkout" {
if order.Status != orderStatusPendingCheckoutConfirm || order.HandoffStatus != handoffStatusPendingOwnerCheckout {
return ErrCheckoutCannotCounter
}
var checkout model.OrderCheckout
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
Where("order_id = ? AND status = ?", order.ID, "submitted").
Where("order_id = ? AND status = ?", order.ID, checkoutStatusSubmitted).
Order("id DESC").
First(&checkout).Error; err != nil {
return err
}
next, err := buildCheckout(order, checkout.InitiatedBy, "countered", checkout.Content, req.EvidenceURLS, req.ConsumableAmountCent, req.CoinConsumedM, req.OtherAmountCent, req.DepositDeductAmountCent, true)
next, err := buildCheckout(order, checkout.InitiatedBy, checkoutStatusCountered, checkout.Content, req.EvidenceURLS, req.ConsumableAmountCent, req.CoinConsumedM, req.OtherAmountCent, req.DepositDeductAmountCent, true)
if err != nil {
return err
}
now := time.Now()
checkout.Status = "countered"
checkout.Status = checkoutStatusCountered
checkout.RentAmountCent = next.RentAmountCent
checkout.OwnerRentAmountCent = next.OwnerRentAmountCent
checkout.PlatformFeeCent = next.PlatformFeeCent
@@ -156,9 +156,9 @@ func (r *Repository) CounterCheckout(userID uint64, orderID uint64, req CounterC
checkout.OwnerAdjustmentReason = req.Reason
checkout.OwnerAdjustedAt = &now
checkout.EvidenceURLS = next.EvidenceURLS
order.Status = "pending_checkout_accept"
order.HandoffStatus = "pending_renter_checkout"
order.SettlementStatus = "pending"
order.Status = orderStatusPendingCheckoutAccept
order.HandoffStatus = handoffStatusPendingRenterCheckout
order.SettlementStatus = settlementStatusPending
orderID := order.ID
if err := notification.Append(tx, notification.Entry{
UserID: order.RenterID,
@@ -205,18 +205,18 @@ func (r *Repository) AcceptCheckout(userID uint64, orderID uint64) error {
if order.RenterID != userID {
return ErrPermissionDenied
}
if order.Status != "pending_checkout_accept" || order.HandoffStatus != "pending_renter_checkout" {
if order.Status != orderStatusPendingCheckoutAccept || order.HandoffStatus != handoffStatusPendingRenterCheckout {
return ErrCheckoutCannotConfirm
}
var checkout model.OrderCheckout
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
Where("order_id = ? AND status = ?", order.ID, "countered").
Where("order_id = ? AND status = ?", order.ID, checkoutStatusCountered).
Order("id DESC").
First(&checkout).Error; err != nil {
return err
}
now := time.Now()
checkout.Status = "accepted"
checkout.Status = checkoutStatusAccepted
checkout.RenterConfirmedAt = &now
action, err := r.finalizeCheckout(tx, &order, &checkout, "租客已确认修正结账,订单完成。")
refund = action
@@ -239,9 +239,9 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che
return nil, err
}
now := time.Now()
order.Status = "completed"
order.HandoffStatus = "returned"
order.SettlementStatus = "settled"
order.Status = orderStatusCompleted
order.HandoffStatus = handoffStatusReturned
order.SettlementStatus = settlementStatusSettled
order.SettledAt = &now
order.OwnerSettledAt = &now
archiveListingAfterCheckout(&listing, &account)
@@ -257,7 +257,7 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che
Direction: "in",
AmountCent: settlement.OwnerRentIncomeCent,
BalanceType: "available",
BizType: "owner_income",
BizType: walletBizOwnerIncome,
BizNo: order.OrderNo,
Remark: "订单结账租金收入",
})
@@ -269,7 +269,7 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che
Direction: "in",
AmountCent: settlement.DepositCompensationCent,
BalanceType: "available",
BizType: "deposit_compensation",
BizType: walletBizDepositCompensation,
BizNo: order.OrderNo,
Remark: "订单结账押金赔付",
})
@@ -283,7 +283,7 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che
var refund *refundAction
renterRefundTotalCent := settlement.RentRefundCent + settlement.DepositRefundCent
if renterRefundTotalCent > 0 {
action, err := r.prepareRefund(order, renterRefundTotalCent, "checkout_refund", "结账退款原路退还")
action, err := r.prepareRefund(order, renterRefundTotalCent, refundBizCheckout, "结账退款原路退还")
if err != nil {
return nil, err
}
@@ -332,16 +332,16 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che
// 完成后的账号先下架,避免已完成订单对应的账号重新出现在公开首页。
func archiveListingAfterCheckout(listing *model.RentalListing, account *model.GameAccount) {
listing.Status = "offline"
listing.Status = listingStatusOffline
listing.InTransaction = false
listing.PublishedAt = nil
account.Status = "offline"
account.Status = accountStatusOffline
}
func hasOpenCheckout(tx *gorm.DB, orderID uint64) (bool, error) {
var count int64
err := tx.Model(&model.OrderCheckout{}).
Where("order_id = ? AND status IN ?", orderID, []string{"submitted", "countered", "accepted", "disputed"}).
Where("order_id = ? AND status IN ?", orderID, []string{checkoutStatusSubmitted, checkoutStatusCountered, checkoutStatusAccepted, checkoutStatusDisputed}).
Count(&count).Error
return count > 0, err
}