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