From 9eff400b0fbdcc4709a7dd335330eccbed4f1933 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Fri, 28 Nov 2025 13:47:07 +0800 Subject: [PATCH] update --- .../Controllers/Admin/OtherController.php | 75 +++++++++++++++-- app/Models/CourseSign.php | 81 ++++++++++++++++--- 2 files changed, 140 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index f61d1f9..cb479e9 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -677,10 +677,17 @@ class OtherController extends CommonController break; case 'company_market_year_total': - // 今年上市公司明细 - 使用模型方法 - $companies = CourseSign::companyMarketYear($start_date, $end_date, $course_ids, true); - foreach ($companies as $company) { - $data[] = [ + // 今年上市公司明细 - 所有今年上市公司,关联学员、课程信息 + // 数据结构:主表是公司,子数据是学员信息 + // 导出时:公司信息只在第一行显示,后续行公司信息为空 + $companiesData = CourseSign::companyMarketYear($start_date, $end_date, $course_ids->toArray(), true); + + foreach ($companiesData as $item) { + $company = $item['company']; + $users = $item['users'] ?? []; + + // 公司基本信息(只在第一行使用) + $companyInfo = [ 'company_name' => $company->company_name, 'company_legal_representative' => $company->company_legal_representative ?? '', 'company_date' => $company->company_date ?? '', @@ -688,7 +695,60 @@ class OtherController extends CommonController 'company_address' => $company->company_address ?? '', 'company_city' => $company->company_city ?? '', 'company_area' => $company->company_area ?? '', + 'company_tag' => $company->company_tag ?? '', ]; + + if (empty($users)) { + // 如果没有学员报名记录,仍然导出公司基本信息 + $data[] = array_merge($companyInfo, [ + 'user_name' => '', + 'course_name' => '', + 'course_type' => '', + ]); + } else { + // 每个学员一行,多个课程合并显示 + $isFirstRow = true; + foreach ($users as $userInfo) { + $courses = $userInfo['courses'] ?? []; + + // 合并同一学员的多个课程:格式为"课程体系-课程名称,课程体系-课程名称" + $courseList = []; + foreach ($courses as $courseInfo) { + $courseType = $courseInfo['course_type'] ?? ''; + $courseName = $courseInfo['course_name'] ?? ''; + if ($courseType && $courseName) { + $courseList[] = $courseType . '-' . $courseName; + } elseif ($courseName) { + $courseList[] = $courseName; + } + } + $courseDisplay = implode("\r\n", $courseList); + + if ($isFirstRow) { + // 第一行:显示公司信息 + $data[] = array_merge($companyInfo, [ + 'user_name' => $userInfo['user_name'] ?? '', + 'course_name' => $courseDisplay, + 'course_type' => '', // 课程类型已合并到课程名称中 + ]); + $isFirstRow = false; + } else { + // 后续行:公司信息为空 + $data[] = [ + 'company_name' => '', + 'company_legal_representative' => '', + 'company_date' => '', + 'stock_date' => '', + 'company_address' => '', + 'company_city' => '', + 'company_area' => '', + 'company_tag' => '', + 'user_name' => $userInfo['user_name'] ?? '', + 'course_name' => $courseDisplay, + ]; + } + } + } } $fields = [ 'company_name' => '企业名称', @@ -696,8 +756,11 @@ class OtherController extends CommonController 'company_date' => '成立时间', 'stock_date' => '上市日期', 'company_address' => '地址', - 'company_city' => '营业范围', - 'company_area' => '联系电话', + 'company_city' => '所在城市', + 'company_area' => '所在区域', + 'company_tag' => '企业资质', + 'user_name' => '学员姓名', + 'course_name' => '课程信息', ]; $filename = '今年上市公司明细'; break; diff --git a/app/Models/CourseSign.php b/app/Models/CourseSign.php index b13ac8c..9243479 100755 --- a/app/Models/CourseSign.php +++ b/app/Models/CourseSign.php @@ -577,23 +577,84 @@ class CourseSign extends SoftDeletesModel /** * 今年上市公司(统计或列表) - * @param int|null $year 年份,不传则使用当前年份 - * @param bool $retList 是否返回列表,false返回数量,true返回列表 - * @return int|\Illuminate\Database\Eloquent\Collection + * @param string|null $start_date 开始日期 + * @param string|null $end_date 结束日期 + * @param array|null $course_ids 课程ID数组,不传则统计所有课程 + * @param bool $retList 是否返回列表,false返回数量,true返回列表(包含学员、课程信息) + * @return int|array */ - public static function companyMarketYear($start_date, $end_date, $course_ids, $retList = false) + public static function companyMarketYear($start_date = null, $end_date = null, $course_ids = null, $retList = false) { $courseSignsQuery = self::getStudentList($start_date, $end_date, 1, $course_ids); + $courseSigns = $courseSignsQuery->with(['user.company', 'course.typeDetail'])->get(); + + // 获取所有上市公司的ID + $companyIds = $courseSigns->pluck('user.company.id') + ->filter() + ->unique() + ->toArray(); + $year = date('Y'); - $list = Company::whereHas('users', function ($query) use ($courseSignsQuery) { - $query->whereIn('id', $courseSignsQuery->get()->pluck('user_id')); - })->where('company_market', 1)->whereYear('stock_date', $year)->get(); + // 获取这些公司中标记为上市的公司,且上市日期为今年 + $companies = Company::whereIn('id', $companyIds) + ->where('company_market', 1) + ->whereYear('stock_date', $year) + ->get() + ->keyBy('id'); + if ($retList) { - // 返回列表 - return $list; + // 返回详细列表:主表是公司,子数据是学员信息 + $result = []; + foreach ($courseSigns as $courseSign) { + if (!$courseSign->user || !$courseSign->user->company) { + continue; + } + + $companyId = $courseSign->user->company->id; + // 只处理上市公司的记录 + if (!isset($companies[$companyId])) { + continue; + } + + $company = $companies[$companyId]; + + // 如果公司还没有在结果中,初始化 + if (!isset($result[$companyId])) { + $result[$companyId] = [ + 'company' => $company, + 'users' => [], + ]; + } + + // 按学员分组,收集每个学员的课程信息 + $userId = $courseSign->user->id; + if (!isset($result[$companyId]['users'][$userId])) { + $result[$companyId]['users'][$userId] = [ + 'user' => $courseSign->user, + 'user_name' => $courseSign->user->name ?? '', + 'mobile' => $courseSign->user->mobile ?? '', + 'courses' => [], + ]; + } + + // 添加该学员的课程信息 + $result[$companyId]['users'][$userId]['courses'][] = [ + 'course_name' => $courseSign->course->name ?? '', + 'course_type' => $courseSign->course->typeDetail->name ?? '', + 'course_sign' => $courseSign, + ]; + } + + // 将 users 转换为数组(去掉 user_id 作为 key) + foreach ($result as $companyId => $item) { + $result[$companyId]['users'] = array_values($item['users']); + } + + // 转换为数组并返回 + return array_values($result); } else { // 返回统计数据 - return $list->count(); + return $companies->count(); } }