fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -107,56 +107,70 @@ func (r *Repository) operatingExpenseSummary(ctx context.Context, query Dashboar
|
||||
|
||||
// pickupSummary 线下提号统计,独立查询 admin_pickups 表,不混入正常订单口径。
|
||||
func (r *Repository) pickupSummary(ctx context.Context, query DashboardQuery) (*PickupSummaryDTO, error) {
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return &PickupSummaryDTO{}, nil
|
||||
}
|
||||
db := r.db.WithContext(ctx)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var row struct {
|
||||
SettledAmountCent int64
|
||||
ProfitAmountCent int64
|
||||
CompletedCount int64
|
||||
}
|
||||
if err := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
pickupQuery := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
Select(`COALESCE(SUM(settle_amount_cent), 0) AS settled_amount_cent, COALESCE(SUM(profit_amount_cent), 0) AS profit_amount_cent, COUNT(id) AS completed_count`).
|
||||
Where("status = ?", "completed").
|
||||
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&row).Error; err != nil {
|
||||
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID > 0 {
|
||||
pickupQuery = pickupQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := pickupQuery.Scan(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var adjustments struct {
|
||||
SettledAmountCent int64
|
||||
ProfitAmountCent int64
|
||||
}
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
adjustmentQuery := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`COALESCE(SUM(fa.settle_delta_cent), 0) AS settled_amount_cent,
|
||||
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
||||
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&adjustments).Error; err != nil {
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID > 0 {
|
||||
adjustmentQuery = adjustmentQuery.Where("fa.tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := adjustmentQuery.Scan(&adjustments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var inProgress int64
|
||||
if err := db.Table("admin_pickups").Where("status = ?", "picking_up").Count(&inProgress).Error; err != nil {
|
||||
inProgressQuery := db.Table("admin_pickups").Where("status = ?", "picking_up")
|
||||
if tenantID > 0 {
|
||||
inProgressQuery = inProgressQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := inProgressQuery.Count(&inProgress).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pending struct {
|
||||
AmountCent int64
|
||||
Count int64
|
||||
}
|
||||
if err := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_offline_settlement)").
|
||||
pendingQuery := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_offline_settlement)").
|
||||
Select(`COALESCE(SUM(settle_amount_cent), 0) AS amount_cent, COUNT(id) AS count`).
|
||||
Where("settlement_mode = ? AND offline_settlement_status = ? AND settle_amount_cent > 0", "platform_managed", "pending").
|
||||
Scan(&pending).Error; err != nil {
|
||||
Where("settlement_mode = ? AND offline_settlement_status = ? AND settle_amount_cent > 0", "platform_managed", "pending")
|
||||
if tenantID > 0 {
|
||||
pendingQuery = pendingQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := pendingQuery.Scan(&pending).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pendingAdjustments struct {
|
||||
AmountCent int64
|
||||
Count int64
|
||||
}
|
||||
if err := db.Table("admin_pickup_financial_adjustments").
|
||||
pendingAdjustmentQuery := db.Table("admin_pickup_financial_adjustments").
|
||||
Select(`COALESCE(SUM(settle_delta_cent), 0) AS amount_cent, COUNT(id) AS count`).
|
||||
Where("settlement_mode = ? AND status = ? AND settle_delta_cent > 0", "platform_managed", "pending").
|
||||
Scan(&pendingAdjustments).Error; err != nil {
|
||||
Where("settlement_mode = ? AND status = ? AND settle_delta_cent > 0", "platform_managed", "pending")
|
||||
if tenantID > 0 {
|
||||
pendingAdjustmentQuery = pendingAdjustmentQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := pendingAdjustmentQuery.Scan(&pendingAdjustments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PickupSummaryDTO{
|
||||
@@ -251,29 +265,29 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) (dash
|
||||
}
|
||||
|
||||
pickupProfits := make([]dailyPickupProfitRow, 0)
|
||||
if tenancy.IDFromContext(ctx) == 0 {
|
||||
if err := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
Select(`DATE_FORMAT(completed_at, '%Y-%m-%d') AS date,
|
||||
pickupProfitQuery := db.Table("admin_pickups USE INDEX (idx_admin_pickups_dashboard_completed)").
|
||||
Select(`DATE_FORMAT(completed_at, '%Y-%m-%d') AS date,
|
||||
COALESCE(SUM(profit_amount_cent), 0) AS profit_amount_cent,
|
||||
COUNT(id) AS completed_count`).
|
||||
Where("status = ?", "completed").
|
||||
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE_FORMAT(completed_at, '%Y-%m-%d')").
|
||||
Scan(&pickupProfits).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
Where("status = ?", "completed").
|
||||
Where("completed_at >= ? AND completed_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
pickupProfitQuery = pickupProfitQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := pickupProfitQuery.Group("DATE_FORMAT(completed_at, '%Y-%m-%d')").Scan(&pickupProfits).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
pickupAdjustments := make([]dailyPickupAdjustmentRow, 0)
|
||||
if tenancy.IDFromContext(ctx) == 0 {
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`DATE_FORMAT(fa.created_at, '%Y-%m-%d') AS date,
|
||||
pickupAdjustmentQuery := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`DATE_FORMAT(fa.created_at, '%Y-%m-%d') AS date,
|
||||
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
||||
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE_FORMAT(fa.created_at, '%Y-%m-%d')").
|
||||
Scan(&pickupAdjustments).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
pickupAdjustmentQuery = pickupAdjustmentQuery.Where("fa.tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := pickupAdjustmentQuery.Group("DATE_FORMAT(fa.created_at, '%Y-%m-%d')").Scan(&pickupAdjustments).Error; err != nil {
|
||||
return dashboardDailyResult{}, err
|
||||
}
|
||||
|
||||
// 取消提号按取消日统计,与「已付款订单数」中的提号创建口径区分开。
|
||||
@@ -604,15 +618,14 @@ func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([
|
||||
// dailyPickupCancelled 按取消日统计线下提号取消笔数。
|
||||
func (r *Repository) dailyPickupCancelled(ctx context.Context, query DashboardQuery) ([]dailyPickupCancelledRow, error) {
|
||||
rows := make([]dailyPickupCancelledRow, 0)
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return rows, nil
|
||||
}
|
||||
err := r.db.WithContext(ctx).Table("admin_pickups").
|
||||
db := r.db.WithContext(ctx).Table("admin_pickups").
|
||||
Select(`DATE_FORMAT(cancelled_at, '%Y-%m-%d') AS date, COUNT(id) AS count`).
|
||||
Where("status = ?", "cancelled").
|
||||
Where("cancelled_at >= ? AND cancelled_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE_FORMAT(cancelled_at, '%Y-%m-%d')").
|
||||
Scan(&rows).Error
|
||||
Where("cancelled_at >= ? AND cancelled_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
db = db.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
err := db.Group("DATE_FORMAT(cancelled_at, '%Y-%m-%d')").Scan(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
@@ -631,10 +644,13 @@ func (r *Repository) dailyPaidOrders(ctx context.Context, query DashboardQuery)
|
||||
AND legacy_order.distribution_site_id IS NOT NULL
|
||||
AND legacy_order.distribution_site_id > 0
|
||||
))`
|
||||
pickupTenantFilter = " AND 1 = 0"
|
||||
pickupTenantFilter = " AND tenant_id = ?"
|
||||
args = append(args, tenantID)
|
||||
}
|
||||
args = append(args, query.StartDate, query.EndDate)
|
||||
if tenantID > 0 {
|
||||
args = append(args, tenantID)
|
||||
}
|
||||
err := r.db.WithContext(ctx).Table(`(
|
||||
SELECT DATE_FORMAT(paid_at, '%Y-%m-%d') AS date, COUNT(DISTINCT order_id) AS paid_order_count
|
||||
FROM payment_orders USE INDEX (idx_payment_orders_biz_status_paid_at_order)
|
||||
|
||||
Reference in New Issue
Block a user