From 59a863adcfdc007f74bafe27a87bd07848c9dcf8 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Sat, 17 Jan 2026 16:25:36 +0800 Subject: [PATCH] update --- .../Controllers/Admin/CourseController.php | 240 ++++++++++-------- 1 file changed, 138 insertions(+), 102 deletions(-) diff --git a/app/Http/Controllers/Admin/CourseController.php b/app/Http/Controllers/Admin/CourseController.php index 2ecc06c..6854257 100755 --- a/app/Http/Controllers/Admin/CourseController.php +++ b/app/Http/Controllers/Admin/CourseController.php @@ -63,119 +63,155 @@ class CourseController extends BaseController $query->whereNotIn('status', [4, 5]); } ])->withCount([ - 'courseSigns as sign_pass_total' => function ($query) { - $query->where('status', 1)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_wait_total' => function ($query) { - $query->where('status', 0)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_fault_total' => function ($query) { - $query->where('status', 2)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_prepare_total' => function ($query) { - $query->where('status', 3)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_cancel_total' => function ($query) { - $query->where('status', 4)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_give_up_total' => function ($query) { - $query->where('status', 5)->whereHas('user'); - } - ])->withCount([ - 'courseSigns as sign_black_total' => function ($query) { - $query->where('status', 6)->whereHas('user'); - } - ])->where(function ($query) use ($all) { - if (isset($all['has_course_forms']) && !empty($all['has_course_forms'])) { - $query->whereHas('courseForms'); - } - if (isset($all['start_date'])) { - $query->where('start_date', '>=', $all['start_date']); - } - if (isset($all['end_date'])) { - $query->where('end_date', '<=', $all['end_date']); - } - if (isset($all['course_type_id'])) { - $course_type_id = explode(',', $all['course_type_id']); - $query->whereIn('type', $course_type_id); - } - if (isset($all['filter']) && !empty($all['filter'])) { - foreach ($all['filter'] as $condition) { - $key = $condition['key'] ?? null; - $op = $condition['op'] ?? null; - $value = $condition['value'] ?? null; - if (!isset($key) || !isset($op) || !isset($value)) { - continue; - } - if ($key == 'start_date') { - list($from, $to) = explode(',', $value); - $query->where(function ($q) use ($from, $to) { - $q->whereBetween('start_date', [$from, $to])->orWhereBetween('end_date', [$from, $to]); - }); - continue; - } - - // 等于 - if ($op == 'eq') { - $query->where($key, $value); - } - // 不等于 - if ($op == 'neq') { - $query->where($key, '!=', $value); - } - // 大于 - if ($op == 'gt') { - $query->where($key, '>', $value); - } - // 大于等于 - if ($op == 'egt') { - $query->where($key, '>=', $value); - } - // 小于 - if ($op == 'lt') { - $query->where($key, '<', $value); - } - // 小于等于 - if ($op == 'elt') { - $query->where($key, '<=', $value); - } - // 模糊搜索 - if ($op == 'like') { - $query->where($key, 'like', '%' . $value . '%'); - } - // 否定模糊搜索 - if ($op == 'notlike') { - $query->where($key, 'not like', '%' . $value . '%'); - } - // 范围搜索 - if ($op == 'range') { - list($from, $to) = explode(',', $value); - if (empty($from) || empty($to)) { + 'courseSigns as sign_pass_total' => function ($query) { + $query->where('status', 1)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_wait_total' => function ($query) { + $query->where('status', 0)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_fault_total' => function ($query) { + $query->where('status', 2)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_prepare_total' => function ($query) { + $query->where('status', 3)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_cancel_total' => function ($query) { + $query->where('status', 4)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_give_up_total' => function ($query) { + $query->where('status', 5)->whereHas('user'); + } + ])->withCount([ + 'courseSigns as sign_black_total' => function ($query) { + $query->where('status', 6)->whereHas('user'); + } + ])->where(function ($query) use ($all) { + if (isset($all['has_course_forms']) && !empty($all['has_course_forms'])) { + $query->whereHas('courseForms'); + } + if (isset($all['start_date'])) { + $query->where('start_date', '>=', $all['start_date']); + } + if (isset($all['end_date'])) { + $query->where('end_date', '<=', $all['end_date']); + } + if (isset($all['course_type_id'])) { + $course_type_id = explode(',', $all['course_type_id']); + $query->whereIn('type', $course_type_id); + } + if (isset($all['filter']) && !empty($all['filter'])) { + foreach ($all['filter'] as $condition) { + $key = $condition['key'] ?? null; + $op = $condition['op'] ?? null; + $value = $condition['value'] ?? null; + if (!isset($key) || !isset($op) || !isset($value)) { continue; } - $query->whereBetween($key, [$from, $to]); - } - if ($op == 'in') { - $array = explode(',', $value); - $query->whereIn($key, $array); + if ($key == 'start_date') { + list($from, $to) = explode(',', $value); + $query->where(function ($q) use ($from, $to) { + $q->whereBetween('start_date', [$from, $to])->orWhereBetween('end_date', [$from, $to]); + }); + continue; + } + + // 等于 + if ($op == 'eq') { + $query->where($key, $value); + } + // 不等于 + if ($op == 'neq') { + $query->where($key, '!=', $value); + } + // 大于 + if ($op == 'gt') { + $query->where($key, '>', $value); + } + // 大于等于 + if ($op == 'egt') { + $query->where($key, '>=', $value); + } + // 小于 + if ($op == 'lt') { + $query->where($key, '<', $value); + } + // 小于等于 + if ($op == 'elt') { + $query->where($key, '<=', $value); + } + // 模糊搜索 + if ($op == 'like') { + $query->where($key, 'like', '%' . $value . '%'); + } + // 否定模糊搜索 + if ($op == 'notlike') { + $query->where($key, 'not like', '%' . $value . '%'); + } + // 范围搜索 + if ($op == 'range') { + list($from, $to) = explode(',', $value); + if (empty($from) || empty($to)) { + continue; + } + $query->whereBetween($key, [$from, $to]); + } + if ($op == 'in') { + $array = explode(',', $value); + $query->whereIn($key, $array); + } } } - } - }); + }); $list = $list->orderBy($all['sort_name'] ?? 'sign_status', $all['sort_type'] ?? 'asc'); if (isset($all['is_export']) && !empty($all['is_export'])) { $list = $list->limit(5000)->get()->toArray(); return Excel::download(new CommonExport($list, $all['export_fields'] ?? ''), $all['file_name'] ?? '' . date('YmdHis') . '.xlsx'); } else { - // 输出 + // 在分页之前,克隆查询构建器用于计算总和 + $totalStatsQuery = clone $list; + + // 输出分页数据 $list = $list->paginate($all['page_size'] ?? 20); + + // 计算所有符合条件的数据的统计总和(不仅仅是当前页) + // 移除排序,获取所有数据 + $totalStatsQuery->getQuery()->orders = null; + + // 获取所有符合条件的数据并计算总和 + $allCourses = $totalStatsQuery->get(); + $totalStats = [ + 'course_signs_count' => 0, + 'sign_pass_total' => 0, + 'sign_wait_total' => 0, + 'sign_fault_total' => 0, + 'sign_prepare_total' => 0, + 'sign_cancel_total' => 0, + 'sign_give_up_total' => 0, + 'sign_black_total' => 0, + ]; + + foreach ($allCourses as $course) { + $totalStats['course_signs_count'] += $course->course_signs_count ?? 0; + $totalStats['sign_pass_total'] += $course->sign_pass_total ?? 0; + $totalStats['sign_wait_total'] += $course->sign_wait_total ?? 0; + $totalStats['sign_fault_total'] += $course->sign_fault_total ?? 0; + $totalStats['sign_prepare_total'] += $course->sign_prepare_total ?? 0; + $totalStats['sign_cancel_total'] += $course->sign_cancel_total ?? 0; + $totalStats['sign_give_up_total'] += $course->sign_give_up_total ?? 0; + $totalStats['sign_black_total'] += $course->sign_black_total ?? 0; + } + + // 将分页数据转换为数组,并在最外层追加统计总和 + $result = $list->toArray(); + $result['statistics_total'] = $totalStats; + + return $this->success($result); } - return $this->success($list); } /**