From 35c93ef9895f728c6f8aa316b6ca684de3090e4a Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Wed, 17 Dec 2025 13:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/StatisticsController.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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,