修复仲裁退款并新增免押额度

This commit is contained in:
yml
2026-06-08 21:00:39 +08:00
parent 830cfcf33d
commit 0b419f5084
22 changed files with 559 additions and 72 deletions
+54 -1
View File
@@ -173,6 +173,11 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
}
rentHours := internalOrderHours
pricing := buildOrderPricing(listing, account)
depositOriginalAmount := roundMoney(listing.DepositAmount)
paidDepositAmount, waivedDepositAmount, err := r.depositAmountsForOrder(tx, renterID, depositOriginalAmount)
if err != nil {
return err
}
order := model.RentalOrder{
OrderNo: orderNo,
ListingID: listing.ID,
@@ -182,7 +187,9 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
EstimatedDurationHours: rentHours,
RentAmount: pricing.RentAmount,
OwnerRentAmount: pricing.OwnerRentAmount,
DepositAmount: listing.DepositAmount,
DepositAmount: paidDepositAmount,
DepositOriginalAmount: depositOriginalAmount,
DepositWaivedAmount: waivedDepositAmount,
PlatformFee: pricing.PlatformFee,
AccountSnapshot: snapshot,
Status: "pending_payment",
@@ -218,6 +225,43 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
return r.FindForUser(renterID, createdID)
}
func (r *Repository) depositAmountsForOrder(tx *gorm.DB, renterID uint64, originalDeposit float64) (float64, float64, error) {
originalDeposit = roundMoney(originalDeposit)
if originalDeposit <= 0 {
return 0, 0, nil
}
var user model.User
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, renterID).Error; err != nil {
return 0, 0, err
}
used, err := activeDepositFreeUsed(tx, renterID)
if err != nil {
return 0, 0, err
}
paidDeposit, waivedDeposit := calculateDepositWaiver(originalDeposit, user.DepositFreeQuota, used)
return paidDeposit, waivedDeposit, nil
}
func activeDepositFreeUsed(tx *gorm.DB, renterID uint64) (float64, error) {
var used float64
err := tx.Model(&model.RentalOrder{}).
Where("renter_id = ? AND status NOT IN ?",
renterID,
[]string{"completed", "cancelled", "closed"},
).
Select("COALESCE(SUM(deposit_waived_amount), 0)").
Scan(&used).Error
return roundMoney(used), err
}
func calculateDepositWaiver(originalDeposit float64, quota float64, used float64) (float64, float64) {
originalDeposit = roundMoney(originalDeposit)
remaining := maxMoney(roundMoney(quota)-roundMoney(used), 0)
waived := minMoney(originalDeposit, remaining)
paid := maxMoney(originalDeposit-waived, 0)
return roundMoney(paid), roundMoney(waived)
}
// Pay 保留旧接口兼容,但真实付款必须走 payment 模块的渠道支付入口。
func (r *Repository) Pay(userID uint64, orderID uint64) error {
return ErrChannelPaymentRequired
@@ -1424,6 +1468,8 @@ func (row orderRow) toAdminDTO() OrderDTO {
RentAmount: &rentAmount,
OwnerRentAmount: &ownerRentAmount,
DepositAmount: row.DepositAmount,
DepositOriginalAmount: effectiveDepositOriginalAmount(row.RentalOrder),
DepositWaivedAmount: row.DepositWaivedAmount,
PlatformFee: &platformFee,
AccountSnapshot: row.AccountSnapshot,
Status: row.Status,
@@ -1454,6 +1500,13 @@ func toHandoffDTO(record model.HandoffRecord) HandoffRecordDTO {
}
}
func effectiveDepositOriginalAmount(order model.RentalOrder) float64 {
if order.DepositOriginalAmount > 0 {
return order.DepositOriginalAmount
}
return order.DepositAmount
}
func toCheckoutAdminDTO(checkout model.OrderCheckout) CheckoutDTO {
rentAmount := checkout.RentAmount
ownerRentAmount := checkout.OwnerRentAmount