songweizong 4 months ago
commit 232ebbf63d

@ -428,12 +428,8 @@ class StatisticsController extends CommonController
public function huli(Request $request) public function huli(Request $request)
{ {
$projects = $this->_checkProjects(); $projects = (new StatisticsController())->_checkProjects();
if (!$projects->count()) { $defaultProjectsId = ($projects[0]->id) ?? '';
return $this->error($this->noProjects);
}
$firstProject = $projects->first();
$defaultProjectsId = $firstProject ? $firstProject->id : '';
$project_id = $request->get('project_id', $defaultProjectsId); $project_id = $request->get('project_id', $defaultProjectsId);
$month = request()->month ?? date("Y-m"); $month = request()->month ?? date("Y-m");
@ -454,9 +450,9 @@ class StatisticsController extends CommonController
$buildingId = []; $buildingId = [];
if ($hushizhang) { if ($hushizhang) {
$user = auth()->user(); $user = auth()->user();
$areaId = AdminAreaLink::where(function ($query) use ($project_id) { $areaId = AdminAreaLink::where(function ($qeury) use ($project_id) {
if ($project_id) { if ($project_id) {
$query->where('project_id', $project_id); $qeury->where('project_id', $project_id);
} }
})->where('admin_id', $user->id)->pluck('area_id'); })->where('admin_id', $user->id)->pluck('area_id');
} elseif ($yuanfang) { } elseif ($yuanfang) {
@ -478,20 +474,16 @@ class StatisticsController extends CommonController
$data->appends($request->all())->render(); $data->appends($request->all())->render();
$product = Product::where('project_id', $project_id)->first(); $product = Product::where('project_id', $project_id)->first();
if (!$product) {
return $this->error("该项目下没有找到产品");
}
$productItem = ProductItems::where('product_id', $product->id)->get(); $productItem = ProductItems::where('product_id', $product->id)->get();
$factor = FactorItems::where('factor_id', $product->statistic_factor_id)->get(); $factor = FactorItems::where('factor_id', $product->statistic_factor_id)->get();
$sumOrderTotal = 0; $sumOrderTotal = 0;
foreach ($data as $item) { foreach ($data as $item) {
// 获取所有床位id // 获取所有床位id
$bedIds = Bed::where('area_id', $item->id)->pluck('id'); $bedIds = Bed::where('area_id', $item->id)->pluck('id');
// 总和 - 使用DATE_FORMAT精确匹配月份 // 总和
$item->order_total = OrderItems::whereIn('product_item_id', $productItem->pluck('id')) $item->order_total = OrderItems::whereIn('product_item_id', $productItem->pluck('id'))
->whereIn("bed_id", $bedIds) ->whereIn("bed_id", $bedIds)
->whereNotNull('paid_at') ->where('paid_at', 'like', '%' . $month . '%')
->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'")
->sum('total'); ->sum('total');
$sumOrderTotal += $item->order_total; $sumOrderTotal += $item->order_total;
// 子项 // 子项
@ -499,15 +491,12 @@ class StatisticsController extends CommonController
} }
// 获取所有列 // 获取所有列
$lie = []; $lie = [];
if ($data->total() > 0) { if (isset($data[0]->lies)) {
$firstDataItem = $data->items()[0] ?? null; $lie = array_column($data[0]->lies, 'name');
if ($firstDataItem && isset($firstDataItem->lies)) {
$lie = array_column($firstDataItem->lies, 'name');
}
} }
$months = $this->_getMonths(); $months = $this->_getMonths();
return view($this->bladePath . ".huli", compact("sumOrderTotal", "data", "month", "lie", "projects", "project_id", "months")); return view($this->bladePath . ".huli", compact("sumOrderTotal", "data", "month", "lie", "projects", "project_id"));
} }
/** /**
@ -516,23 +505,38 @@ class StatisticsController extends CommonController
public function getLies($bedIds, $productItem, $factor, $month) public function getLies($bedIds, $productItem, $factor, $month)
{ {
$list = []; $list = [];
// 修复如果床位ID为空直接返回空数组避免 whereIn 空数组导致的SQL错误
if (empty($bedIds)) {
return $list;
}
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) $query = OrderItems::where('product_item_id', $item->id)
->whereIn("bed_id", $bedIds) ->whereIn("bed_id", $bedIds)
->whereRaw("factors like '%\"factor_item_id\": {$factor_item->id}%'") ->where('paid_at', 'like', '%' . $month . '%');
->whereNotNull('paid_at')
->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'"); // 修复使用参数绑定防止SQL注入并使用更精确的JSON匹配
// 确保 factor_item_id 是整数类型防止SQL注入
$factorItemId = (int) $factor_item->id;
// 使用更精确的LIKE匹配模式避免误匹配如 1 匹配到 10、11 等)
// 匹配模式:%"factor_item_id":数字, 或 %"factor_item_id":数字}
// 使用 CONCAT 和参数绑定确保完全安全
$query->where(function ($q) use ($factorItemId) {
// 使用 CONCAT 函数构建模式完全参数化防止SQL注入
$q->whereRaw("factors LIKE CONCAT('%', '\"factor_item_id\":', ?, ',%')", [$factorItemId])
->orWhereRaw("factors LIKE CONCAT('%', '\"factor_item_id\":', ?, '}%')", [$factorItemId]);
});
// 如果MySQL版本 >= 5.7也可以使用JSON函数更精确
// $query->whereRaw("JSON_SEARCH(factors, 'one', ?, NULL, '$[*].factor_item_id') IS NOT NULL", [$factorItemId]);
$total = $query->sum('total'); $total = $query->sum('total');
$count = $query->count();
// 如果有实际订单数据,使用平均单价;否则使用理论价格 // 修复:明确计算价格总和,避免运算符优先级问题
if ($count > 0 && $total > 0) { $totalPrice = (float) $item->price + (float) $factor_item->price;
$totalPrice = round($total / $count, 2);
} else {
$totalPrice = $item->price + $factor_item->price;
}
$list[] = [ $list[] = [
'name' => $totalPrice . '元/天', 'name' => $totalPrice . '元/天',
@ -562,7 +566,7 @@ class StatisticsController extends CommonController
$month = request()->month ?? date("Y-m"); $month = request()->month ?? date("Y-m");
$months = $this->_getMonths(); $months = $this->_getMonths();
// 当月天数1 // 当月天数
$days = date('t', strtotime($month)); $days = date('t', strtotime($month));
$area = Area::withCount('beds')->where('project_id', $project_id)->get(); $area = Area::withCount('beds')->where('project_id', $project_id)->get();
$beds = Bed::whereIn('area_id', $area->pluck('id'))->where('project_id', $project_id)->get(); $beds = Bed::whereIn('area_id', $area->pluck('id'))->where('project_id', $project_id)->get();

Loading…
Cancel
Save