diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index 43f85a8..b18f2cb 100755 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -518,13 +518,22 @@ class StatisticsController extends CommonController $list = []; foreach ($productItem as $item) { foreach ($factor as $factor_item) { - $total = OrderItems::where('product_item_id', $item->id) + $query = OrderItems::where('product_item_id', $item->id) ->whereIn("bed_id", $bedIds) ->whereRaw("factors like '%\"factor_item_id\": {$factor_item->id}%'") ->whereNotNull('paid_at') - ->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'") - ->sum('total'); - $totalPrice = $item->price + $factor_item->price; + ->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'"); + + $total = $query->sum('total'); + $count = $query->count(); + + // 如果有实际订单数据,使用平均单价;否则使用理论价格 + if ($count > 0 && $total > 0) { + $totalPrice = round($total / $count, 2); + } else { + $totalPrice = $item->price + $factor_item->price; + } + $list[] = [ 'name' => $totalPrice . '元/天', 'total_price' => $totalPrice,