From 649e756cbfc86a5b691869cdbb7642647dd9502f Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Sat, 17 Jan 2026 15:52:18 +0800 Subject: [PATCH] update --- .../Controllers/Admin/OtherController.php | 165 ++---------------- app/Models/CourseSign.php | 93 ++++++++++ 2 files changed, 103 insertions(+), 155 deletions(-) diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index c6344d1..750bb8c 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -165,8 +165,8 @@ class OtherController extends CommonController ->where('start_time', 'like', '%' . date('Y-m') . '%') ->get(); - // 苏州区域数据 - 使用 CourseSign::area 方法实现数据一致 - $areasData = CourseSign::area($start_date, $end_date, null, null, true, true); + // 苏州区域数据 - 使用 CourseSign::getAreaData 方法实现数据一致 + $areasData = CourseSign::getAreaData($start_date, $end_date, 1, null, true, 'statistics'); $suzhou = []; foreach ($areasData as $item) { $suzhou[] = [ @@ -735,26 +735,8 @@ class OtherController extends CommonController // 当前课程数据(已去重) $currentData = []; foreach ($users as $user) { - // 获取该学员报名的课程列表 - 使用与getStudentList完全一致的逻辑 - $userCourseSigns = CourseSign::where(function ($query) use ($course_ids) { - // status = 1 - $query->where('status', 1); - // course_ids筛选 - if ($course_ids && $course_ids->isNotEmpty()) { - $query->whereIn('course_id', $course_ids); - } - }) - ->whereHas('course', function ($query) use ($start_date, $end_date) { - $query->where('is_chart', 1); - // 开始结束日期的筛选。or查询 - if ($start_date && $end_date) { - $query->where(function ($q) use ($start_date, $end_date) { - $q->whereBetween('start_date', [$start_date, $end_date]) - ->orWhereBetween('end_date', [$start_date, $end_date]); - }); - } - }) - ->whereNotIn('status', [4, 5, 6]) + // 获取该学员报名的课程列表 - 使用getStudentList方法确保与统计逻辑一致 + $userCourseSigns = CourseSign::getStudentList($start_date, $end_date, 1, $course_ids) ->where('user_id', $user->id) ->with(['course.typeDetail']) ->get(); @@ -933,65 +915,9 @@ class OtherController extends CommonController break; case 'areas': - // 区域明细 - 使用与coursesHome统计逻辑一致的CourseSign::area方法 + // 区域明细 - 使用 CourseSign::getAreaData 方法,统一统计和导出逻辑 // 第一个sheet:普通学员区域明细 - $courseSignList = CourseSign::getStudentList($start_date, $end_date, 1, $courses->pluck('id')); - - // 从数据字典获取区域列表(与CourseSign::area逻辑一致) - $suzhouAreas = ParameterDetail::where('parameter_id', 5) - ->where('status', 1) - ->orderBy('sort', 'asc') - ->pluck('value') - ->toArray(); - - $areasData = []; - foreach ($suzhouAreas as $area) { - // 按照CourseSign::area的逻辑获取该区域下的CourseSign - if ($area === '高新区') { - $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) { - $query->whereHas('company', function ($query) { - $query->where('company_city', '苏州市') - ->where(function ($q) { - $q->where('company_area', '高新区') - ->orWhere('company_area', '虎丘区'); - }); - }); - })->get(); - } elseif ($area === '苏州市外') { - // 苏州市外:统计除了苏州之外的所有数据 - $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) { - $query->whereHas('company', function ($query) { - $query->where('company_city', '!=', '苏州市'); - }); - })->get(); - } else { - // 其他区域:正常统计 - $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) use ($area) { - $query->whereHas('company', function ($query) use ($area) { - $query->where('company_city', '苏州市') - ->where('company_area', 'like', '%' . $area . '%'); - }); - })->get(); - } - - // 加载关联关系 - $areaCourseSigns->load(['user.company', 'course.typeDetail']); - - // 将该区域下的学员明细添加到导出数据(一行一个学员) - foreach ($areaCourseSigns as $sign) { - $areasData[] = [ - 'company_name' => $sign->user->company->company_name ?? '', - 'company_area' => $sign->user->company->company_area ?? '', - 'company_city' => $sign->user->company->company_city ?? '', - 'company_province' => $sign->user->company->company_province ?? '', - 'company_address' => $sign->user->company->company_address ?? '', - 'user_name' => $sign->user->name ?? '', - 'mobile' => $sign->user->mobile ?? '', - 'course_type' => $sign->course->typeDetail->name ?? '', - 'course_name' => $sign->course->name ?? '', - ]; - } - } + $areasData = CourseSign::getAreaData($start_date, $end_date, 1, $courses->pluck('id'), false, 'export'); $fields = [ 'company_name' => '企业名称', @@ -1005,61 +931,8 @@ class OtherController extends CommonController 'course_name' => '课程名称', ]; - // 第二个sheet:校友区域明细 - 使用CourseSign::area方法,is_schoolmate=true - $courseSignListSchoolmate = CourseSign::getStudentList($start_date, $end_date, 1, $courses->pluck('id')); - // 添加校友筛选条件 - $courseSignListSchoolmate->whereHas('user', function ($query) { - $query->where('is_schoolmate', 1); - }); - - $areasSchoolmateData = []; - foreach ($suzhouAreas as $area) { - // 按照CourseSign::area的逻辑获取该区域下的CourseSign(校友) - if ($area === '高新区') { - $areaCourseSigns = (clone $courseSignListSchoolmate)->whereHas('user', function ($query) { - $query->whereHas('company', function ($query) { - $query->where('company_city', '苏州市') - ->where(function ($q) { - $q->where('company_area', '高新区') - ->orWhere('company_area', '虎丘区'); - }); - }); - })->get(); - } elseif ($area === '苏州市外') { - // 苏州市外:统计除了苏州之外的所有数据 - $areaCourseSigns = (clone $courseSignListSchoolmate)->whereHas('user', function ($query) { - $query->whereHas('company', function ($query) { - $query->where('company_city', '!=', '苏州市'); - }); - })->get(); - } else { - // 其他区域:正常统计 - $areaCourseSigns = (clone $courseSignListSchoolmate)->whereHas('user', function ($query) use ($area) { - $query->whereHas('company', function ($query) use ($area) { - $query->where('company_city', '苏州市') - ->where('company_area', 'like', '%' . $area . '%'); - }); - })->get(); - } - - // 加载关联关系 - $areaCourseSigns->load(['user.company', 'course.typeDetail']); - - // 将该区域下的校友明细添加到导出数据(一行一个学员) - foreach ($areaCourseSigns as $sign) { - $areasSchoolmateData[] = [ - 'company_name' => $sign->user->company->company_name ?? '', - 'company_area' => $sign->user->company->company_area ?? '', - 'company_city' => $sign->user->company->company_city ?? '', - 'company_province' => $sign->user->company->company_province ?? '', - 'company_address' => $sign->user->company->company_address ?? '', - 'user_name' => $sign->user->name ?? '', - 'mobile' => $sign->user->mobile ?? '', - 'course_type' => $sign->course->typeDetail->name ?? '', - 'course_name' => $sign->course->name ?? '', - ]; - } - } + // 第二个sheet:校友区域明细 + $areasSchoolmateData = CourseSign::getAreaData($start_date, $end_date, 1, $courses->pluck('id'), true, 'export'); $sheets = [ new SheetExport($areasData, $fields, '区域明细'), @@ -1186,26 +1059,8 @@ class OtherController extends CommonController // 跟班学员明细 - 使用模型方法 $users = CourseSign::genban($start_date, $end_date, $course_ids, true); foreach ($users as $user) { - // 获取该学员的课程报名记录 - $userCourseSigns = CourseSign::where(function ($query) use ($course_ids) { - // status = 1 - $query->where('status', 1); - // course_ids筛选 - if ($course_ids && $course_ids->isNotEmpty()) { - $query->whereIn('course_id', $course_ids); - } - }) - ->whereHas('course', function ($query) use ($start_date, $end_date) { - $query->where('is_chart', 1); - // 开始结束日期的筛选。or查询 - if ($start_date && $end_date) { - $query->where(function ($q) use ($start_date, $end_date) { - $q->whereBetween('start_date', [$start_date, $end_date]) - ->orWhereBetween('end_date', [$start_date, $end_date]); - }); - } - }) - ->whereNotIn('status', [4, 5, 6]) + // 获取该学员的课程报名记录 - 使用getStudentList方法确保与统计逻辑一致 + $userCourseSigns = CourseSign::getStudentList($start_date, $end_date, 1, $course_ids) ->where('user_id', $user->id) ->with(['course.typeDetail']) ->get(); diff --git a/app/Models/CourseSign.php b/app/Models/CourseSign.php index b83e40a..7bda817 100755 --- a/app/Models/CourseSign.php +++ b/app/Models/CourseSign.php @@ -600,6 +600,99 @@ class CourseSign extends SoftDeletesModel } } + /** + * 获取区域数据(统计或导出) + * @param string $start_date 开始日期 + * @param string $end_date 结束日期 + * @param int|null $status 状态,1表示审核通过 + * @param array|null $course_ids 课程ID数组 + * @param bool $is_schoolmate 是否只统计校友 + * @param string $returnType 返回类型:'statistics' 返回统计数据,'export' 返回导出数据 + * @return array + */ + public static function getAreaData($start_date, $end_date, $status = 1, $course_ids = null, $is_schoolmate = false, $returnType = 'statistics') + { + $courseSignList = self::getStudentList($start_date, $end_date, $status, $course_ids); + + // 如果只统计校友,添加校友筛选条件 + if ($is_schoolmate) { + $courseSignList->whereHas('user', function ($query) { + $query->where('is_schoolmate', 1); + }); + } + + // 从数据字典获取区域列表 + $suzhouAreas = ParameterDetail::where('parameter_id', 5) + ->where('status', 1) + ->orderBy('sort', 'asc') + ->pluck('value') + ->toArray(); + + $result = []; + foreach ($suzhouAreas as $area) { + // 根据区域获取对应的CourseSign记录 + if ($area === '高新区') { + // 高新区:统计高新区+虎丘区 + $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) { + $query->whereHas('company', function ($query) { + $query->where('company_city', '苏州市') + ->where(function ($q) { + $q->where('company_area', '高新区') + ->orWhere('company_area', '虎丘区'); + }); + }); + })->get(); + } elseif ($area === '苏州市外') { + // 苏州市外:统计除了苏州之外的所有数据 + $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) { + $query->whereHas('company', function ($query) { + $query->where('company_city', '!=', '苏州市'); + }); + })->get(); + } else { + // 其他区域:正常统计 + $areaCourseSigns = (clone $courseSignList)->whereHas('user', function ($query) use ($area) { + $query->whereHas('company', function ($query) use ($area) { + $query->where('company_city', '苏州市') + ->where('company_area', 'like', '%' . $area . '%'); + }); + })->get(); + } + + if ($returnType === 'statistics') { + // 返回统计数据 + $result[] = [ + 'area' => $area, + // 未去重 + 'total' => $areaCourseSigns->count(), + // 已去重 + 'total_unique' => User::whereIn('id', $areaCourseSigns->pluck('user_id'))->groupBy('mobile')->get()->count(), + ]; + } else { + // 返回导出数据(详细的学员列表) + // 加载关联关系 + $areaCourseSigns->load(['user.company', 'course.typeDetail']); + + // 将该区域下的学员明细添加到结果(一行一个学员) + foreach ($areaCourseSigns as $sign) { + $result[] = [ + 'company_name' => $sign->user->company->company_name ?? '', + 'company_area' => $sign->user->company->company_area ?? '', + 'company_city' => $sign->user->company->company_city ?? '', + 'company_province' => $sign->user->company->company_province ?? '', + 'company_address' => $sign->user->company->company_address ?? '', + 'user_name' => $sign->user->name ?? '', + 'mobile' => $sign->user->mobile ?? '', + 'course_type' => $sign->course->typeDetail->name ?? '', + 'course_name' => $sign->course->name ?? '', + ]; + } + } + } + + return $result; + } + /** * 元和员工参人员 */