修正后台订单号主实际收入
This commit is contained in:
@@ -61,11 +61,20 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminOrderQuery) (*Pag
|
||||
return nil, err
|
||||
}
|
||||
|
||||
checkouts, err := r.latestAdminCheckouts(ctx, rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]OrderDTO, 0, len(rows))
|
||||
paymentTimeoutMinutes := pendingPaymentTimeoutMinutes(db)
|
||||
for _, row := range rows {
|
||||
dto := row.toAdminDTO()
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, paymentTimeoutMinutes)
|
||||
if checkout, ok := checkouts[row.ID]; ok && shouldAttachAdminCheckout(row) {
|
||||
checkoutDTO := toCheckoutAdminDTO(checkout)
|
||||
refreshCheckoutSettlementDTO(&checkoutDTO, row.RentalOrder, checkout)
|
||||
dto.Checkout = &checkoutDTO
|
||||
}
|
||||
items = append(items, dto)
|
||||
}
|
||||
|
||||
@@ -77,6 +86,41 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminOrderQuery) (*Pag
|
||||
}, nil
|
||||
}
|
||||
|
||||
// latestAdminCheckouts loads one latest checkout per order so the admin order
|
||||
// list can display finalized income without issuing one query per row.
|
||||
func (r *Repository) latestAdminCheckouts(ctx context.Context, rows []orderRow) (map[uint64]model.OrderCheckout, error) {
|
||||
result := make(map[uint64]model.OrderCheckout, len(rows))
|
||||
if len(rows) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
orderIDs := make([]uint64, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
orderIDs = append(orderIDs, row.ID)
|
||||
}
|
||||
latest := r.db.WithContext(ctx).
|
||||
Table("order_checkouts").
|
||||
Select("MAX(id) AS id").
|
||||
Where("order_id IN ?", orderIDs).
|
||||
Group("order_id")
|
||||
var checkouts []model.OrderCheckout
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("order_checkouts AS oc").
|
||||
Joins("JOIN (?) AS latest ON latest.id = oc.id", latest).
|
||||
Find(&checkouts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, checkout := range checkouts {
|
||||
result[checkout.OrderID] = checkout
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func shouldAttachAdminCheckout(row orderRow) bool {
|
||||
return shouldAttachCheckout(row.Status) ||
|
||||
row.SettlementStatus == settlementStatusSettled ||
|
||||
row.SettlementStatus == settlementStatusArbitrated
|
||||
}
|
||||
|
||||
func applyAdminOrderFilters(db *gorm.DB, query AdminOrderQuery) *gorm.DB {
|
||||
if query.Status != "" {
|
||||
db = db.Where("o.status = ?", query.Status)
|
||||
|
||||
Reference in New Issue
Block a user