- 新增 order_checkouts 表,支持结账明细(消耗/押金扣除/退回/号主入账) - 租客发起结账 → 号主确认 → 已完成(正常路径) - 号主修改结账 → 租客确认修正 → 已完成(修正路径) - 结账阶段任一方发起争议 → 后台仲裁 → 已完成/已关闭/异常(争议路径) - 争议模块适配结账争议类型,仲裁结果新增 mark_abnormal - 超时任务适配新的 pending_checkout_confirm 状态 - 前端结账明细面板、发起结账/确认/修正/拒绝表单全部实现 - 移除旧的 SubmitReturn/ConfirmReturn 接口
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
package admindashboard
|
|
|
|
import (
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) *Repository {
|
|
return &Repository{db: db}
|
|
}
|
|
|
|
func (r *Repository) Summary() (*DashboardDTO, error) {
|
|
now := time.Now()
|
|
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
metrics := MetricsDTO{}
|
|
pending := PendingDTO{}
|
|
|
|
if err := r.db.Model(&model.User{}).Count(&metrics.TotalUsers).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.User{}).Where("realname_status = ?", "verified").Count(&metrics.VerifiedUsers).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalListing{}).Count(&metrics.TotalListings).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalListing{}).Where("status = ? AND review_status = ?", "published", "approved").Count(&metrics.PublishedListings).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalOrder{}).Count(&metrics.TotalOrders).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalOrder{}).Where("status = ?", "renting").Count(&metrics.RentingOrders).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalOrder{}).Where("created_at >= ?", today).Count(&metrics.TodayOrders).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.WalletLedger{}).
|
|
Select("COALESCE(SUM(amount), 0)").
|
|
Where("created_at >= ?", today).
|
|
Scan(&metrics.TodayLedgerAmount).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalListing{}).Where("review_status = ?", "pending").Count(&pending.ListingReviews).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.Dispute{}).Where("status IN ?", []string{"open", "processing"}).Count(&pending.Disputes).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalOrder{}).Where("status = ? AND handoff_status IN ?", "pending_handoff", []string{"pending_owner", "pending_renter_confirm"}).Count(&pending.PendingHandoffs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.db.Model(&model.RentalOrder{}).Where("status IN ?", []string{"pending_checkout_confirm", "pending_checkout_accept"}).Count(&pending.PendingReturnConfirms).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
recentOrders, err := r.recentOrders()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
recentDisputes, err := r.recentDisputes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &DashboardDTO{
|
|
Metrics: metrics,
|
|
Pending: pending,
|
|
RecentOrders: recentOrders,
|
|
RecentDisputes: recentDisputes,
|
|
GeneratedAt: now,
|
|
}, nil
|
|
}
|
|
|
|
func (r *Repository) recentOrders() ([]RecentOrderDTO, error) {
|
|
var rows []RecentOrderDTO
|
|
err := r.db.Table("rental_orders AS o").
|
|
Select("o.id, o.order_no, a.title, o.renter_id, o.owner_id, o.status, o.rent_amount, o.deposit_amount, o.created_at").
|
|
Joins("JOIN game_accounts AS a ON a.id = o.account_id").
|
|
Order("o.id DESC").
|
|
Limit(8).
|
|
Scan(&rows).Error
|
|
return rows, err
|
|
}
|
|
|
|
func (r *Repository) recentDisputes() ([]RecentDisputeDTO, error) {
|
|
var rows []RecentDisputeDTO
|
|
err := r.db.Table("disputes AS d").
|
|
Select("d.id, d.order_id, o.order_no, a.title, d.type, d.status, d.initiator_id, d.created_at").
|
|
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
|
|
Joins("JOIN game_accounts AS a ON a.id = o.account_id").
|
|
Order("d.id DESC").
|
|
Limit(8).
|
|
Scan(&rows).Error
|
|
return rows, err
|
|
}
|