彻底优化数据库字段
This commit is contained in:
@@ -28,31 +28,81 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func listingOrderPrice(listing model.RentalListing) float64 {
|
||||
switch {
|
||||
case listing.Price > 0:
|
||||
return listing.Price
|
||||
case listing.PriceDaily > 0:
|
||||
return listing.PriceDaily
|
||||
case listing.PriceHourly > 0:
|
||||
return listing.PriceHourly * float64(internalOrderHours)
|
||||
case listing.PriceWeekly > 0:
|
||||
return listing.PriceWeekly / 7
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
type orderPricing struct {
|
||||
RentAmount float64
|
||||
OwnerRentAmount float64
|
||||
PlatformFee float64
|
||||
}
|
||||
|
||||
func orderDurationHours(order model.RentalOrder) int {
|
||||
if order.EstimatedDurationHours > 0 {
|
||||
return order.EstimatedDurationHours
|
||||
}
|
||||
if order.RentHours > 0 {
|
||||
return order.RentHours
|
||||
}
|
||||
return internalOrderHours
|
||||
}
|
||||
|
||||
func buildOrderPricing(listing model.RentalListing, account model.GameAccount) orderPricing {
|
||||
rentAmount := roundMoney(listing.Price)
|
||||
ownerRentAmount := readSnapshotPrice(account.AssetSummary, "seller_total_price")
|
||||
if ownerRentAmount <= 0 || ownerRentAmount > rentAmount {
|
||||
ownerRentAmount = rentAmount
|
||||
}
|
||||
platformFee := readSnapshotPrice(account.AssetSummary, "platform_markup_amount")
|
||||
if platformFee <= 0 || roundMoney(ownerRentAmount+platformFee) != rentAmount {
|
||||
platformFee = roundMoney(rentAmount - ownerRentAmount)
|
||||
}
|
||||
if platformFee < 0 {
|
||||
platformFee = 0
|
||||
}
|
||||
return orderPricing{
|
||||
RentAmount: rentAmount,
|
||||
OwnerRentAmount: roundMoney(ownerRentAmount),
|
||||
PlatformFee: roundMoney(platformFee),
|
||||
}
|
||||
}
|
||||
|
||||
func readSnapshotPrice(raw datatypes.JSON, key string) float64 {
|
||||
if len(raw) == 0 {
|
||||
return 0
|
||||
}
|
||||
var summary map[string]any
|
||||
if err := json.Unmarshal(raw, &summary); err != nil {
|
||||
return 0
|
||||
}
|
||||
breakdown, ok := summary["price_breakdown"].(map[string]any)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return readJSONNumber(breakdown[key])
|
||||
}
|
||||
|
||||
func readJSONNumber(value any) float64 {
|
||||
switch typed := value.(type) {
|
||||
case float64:
|
||||
return typed
|
||||
case float32:
|
||||
return float64(typed)
|
||||
case int:
|
||||
return float64(typed)
|
||||
case int64:
|
||||
return float64(typed)
|
||||
case json.Number:
|
||||
number, err := typed.Float64()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return number
|
||||
case string:
|
||||
number, err := strconv.ParseFloat(typed, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return number
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, error) {
|
||||
var createdID uint64
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -80,6 +130,7 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
|
||||
return err
|
||||
}
|
||||
rentHours := internalOrderHours
|
||||
pricing := buildOrderPricing(listing, account)
|
||||
order := model.RentalOrder{
|
||||
OrderNo: orderNo,
|
||||
ListingID: listing.ID,
|
||||
@@ -87,10 +138,10 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
|
||||
OwnerID: listing.OwnerID,
|
||||
RenterID: renterID,
|
||||
EstimatedDurationHours: rentHours,
|
||||
RentHours: rentHours,
|
||||
RentAmount: listingOrderPrice(listing),
|
||||
RentAmount: pricing.RentAmount,
|
||||
OwnerRentAmount: pricing.OwnerRentAmount,
|
||||
DepositAmount: listing.DepositAmount,
|
||||
PlatformFee: 0,
|
||||
PlatformFee: pricing.PlatformFee,
|
||||
AccountSnapshot: snapshot,
|
||||
Status: "pending_payment",
|
||||
HandoffStatus: "none",
|
||||
@@ -370,10 +421,7 @@ func (r *Repository) ConfirmReceive(userID uint64, orderID uint64) error {
|
||||
order.HandoffStatus = "received"
|
||||
durationHours := orderDurationHours(order)
|
||||
order.EstimatedDurationHours = durationHours
|
||||
order.RentStartAt = &now
|
||||
order.RentedAt = &now
|
||||
rentEnd := now.Add(time.Duration(durationHours) * time.Hour)
|
||||
order.RentEndAt = &rentEnd
|
||||
orderID := order.ID
|
||||
if err := notification.Append(tx, notification.Entry{
|
||||
UserID: order.OwnerID,
|
||||
@@ -983,13 +1031,15 @@ func buildCheckout(order model.RentalOrder, initiatedBy uint64, status string, c
|
||||
InitiatedBy: initiatedBy,
|
||||
Status: status,
|
||||
RentAmount: order.RentAmount,
|
||||
OwnerRentAmount: order.OwnerRentAmount,
|
||||
PlatformFee: order.PlatformFee,
|
||||
DepositAmount: order.DepositAmount,
|
||||
ConsumableAmount: roundMoney(consumableAmount),
|
||||
CoinConsumedM: roundMoney(coinConsumedM),
|
||||
OtherAmount: roundMoney(otherAmount),
|
||||
DepositDeductAmount: roundMoney(deductAmount),
|
||||
RenterRefundAmount: roundMoney(renterRefund),
|
||||
OwnerIncomeAmount: roundMoney(order.RentAmount + deductAmount),
|
||||
OwnerIncomeAmount: roundMoney(order.OwnerRentAmount + deductAmount),
|
||||
Content: content,
|
||||
EvidenceURLS: evidence,
|
||||
}, nil
|
||||
@@ -1068,15 +1118,7 @@ type orderRow struct {
|
||||
|
||||
func (row orderRow) toDTO() OrderDTO {
|
||||
rentedAt := row.RentedAt
|
||||
if rentedAt == nil {
|
||||
rentedAt = row.RentStartAt
|
||||
}
|
||||
durationHours := orderDurationHours(row.RentalOrder)
|
||||
rentEndAt := row.RentEndAt
|
||||
if rentEndAt == nil && rentedAt != nil && durationHours > 0 {
|
||||
calculatedEnd := rentedAt.Add(time.Duration(durationHours) * time.Hour)
|
||||
rentEndAt = &calculatedEnd
|
||||
}
|
||||
return OrderDTO{
|
||||
ID: row.ID,
|
||||
OrderNo: row.OrderNo,
|
||||
@@ -1091,9 +1133,8 @@ func (row orderRow) toDTO() OrderDTO {
|
||||
LoginPlatform: row.LoginPlatform,
|
||||
RentedAt: rentedAt,
|
||||
EstimatedDurationHours: durationHours,
|
||||
RentStartAt: row.RentStartAt,
|
||||
RentEndAt: rentEndAt,
|
||||
RentAmount: row.RentAmount,
|
||||
OwnerRentAmount: row.OwnerRentAmount,
|
||||
DepositAmount: row.DepositAmount,
|
||||
PlatformFee: row.PlatformFee,
|
||||
AccountSnapshot: row.AccountSnapshot,
|
||||
@@ -1126,6 +1167,8 @@ func toCheckoutDTO(checkout model.OrderCheckout) CheckoutDTO {
|
||||
InitiatedBy: checkout.InitiatedBy,
|
||||
Status: checkout.Status,
|
||||
RentAmount: checkout.RentAmount,
|
||||
OwnerRentAmount: checkout.OwnerRentAmount,
|
||||
PlatformFee: checkout.PlatformFee,
|
||||
DepositAmount: checkout.DepositAmount,
|
||||
ConsumableAmount: checkout.ConsumableAmount,
|
||||
CoinConsumedM: checkout.CoinConsumedM,
|
||||
|
||||
Reference in New Issue
Block a user