diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index 22e9d28..e7dd704 100755 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -515,27 +515,20 @@ class StatisticsController extends CommonController foreach ($productItem as $item) { foreach ($factor as $factor_item) { - // 基础查询:当前项目下、当前病区床位、指定产品子项、指定月份的子订单 - $baseQuery = OrderItems::where('product_item_id', $item->id) + $query = OrderItems::where('product_item_id', $item->id) ->whereIn("bed_id", $bedIds) ->where('paid_at', 'like', '%' . $month . '%'); - // 精确匹配包含指定 factor_item_id 的子订单(使用 LIKE + 参数绑定,兼容现有 JSON 结构) + // 使用 MySQL 5.7 的 JSON_SEARCH 精确匹配包含指定 factor_item_id 的子订单 $factorItemId = (int) $factor_item->id; - $baseQuery->where(function ($q) use ($factorItemId) { - // 匹配 ... "factor_item_id":1, ... 或 ... "factor_item_id":1} - $q->whereRaw("factors LIKE CONCAT('%', '\"factor_item_id\":', ?, ',%')", [$factorItemId]) - ->orWhereRaw("factors LIKE CONCAT('%', '\"factor_item_id\":', ?, '}%')", [$factorItemId]); - }); + $query->whereRaw("JSON_SEARCH(factors, 'one', ?, NULL, '$[*].factor_item_id') IS NOT NULL", [$factorItemId]); // 1)列对应的真实总收入:该因子 + 产品子项 + 病区 + 月份 下所有子订单的 total 之和 - $totalQuery = clone $baseQuery; - $total = (float) $totalQuery->sum('total'); + $total = (float) $query->sum('total'); - // 2)表头展示的价格:使用该列下“实际发生过的正价子订单”的最小单日价格,保证接近真实下单价格 - $priceQuery = clone $baseQuery; - $price = (float) $priceQuery->where('total', '>', 0)->min('total'); - $totalPrice = $price > 0 ? $price : 0; + // 2)表头展示的价格:按“公式”计算,保证不是 0(近似真实下单价) + // 价格 = 产品子项基础价 + 因子价格 + $totalPrice = (float) $item->price + (float) $factor_item->price; $list[] = [ 'name' => $totalPrice . '元/天',