master
cody 4 months ago
parent dbc99098a5
commit 6dc067414f

@ -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 . '元/天',

Loading…
Cancel
Save