master
cody 3 months ago
parent 649e756cbf
commit 084ea9a8d7

@ -1348,7 +1348,7 @@ class OtherController extends CommonController
break; break;
case 'company_invested_after_enrollment_total': case 'company_invested_after_enrollment_total':
// 入学后被投企业明细 - 使用模型方法 // 入学后被投企业明细 - 使用模型方法,与其他导出学员信息保持一致
// 辅助函数:构建入学后被投企业数据 // 辅助函数:构建入学后被投企业数据
$buildAfterEnrollmentData = function ($start_date, $end_date, $course_ids) { $buildAfterEnrollmentData = function ($start_date, $end_date, $course_ids) {
$data = []; $data = [];
@ -1356,18 +1356,136 @@ class OtherController extends CommonController
foreach ($companiesAfterEnrollment as $item) { foreach ($companiesAfterEnrollment as $item) {
$company = $item['company']; $company = $item['company'];
$userNames = collect($item['users'])->pluck('name')->filter()->unique()->implode("\n\r"); $users = $item['users'] ?? [];
$data[] = [
// 公司基本信息(只在第一行使用)
$companyInfo = [
'company_name' => $company->company_name, 'company_name' => $company->company_name,
'company_legal_representative' => $company->company_legal_representative ?? '', 'company_legal_representative' => $company->company_legal_representative ?? '',
'company_date' => $company->company_date ?? '', 'company_date' => $company->company_date ?? '',
'invest_date' => $item['invest_date'] ?? '', 'invest_date' => $item['invest_date'] ?? '',
'first_sign_date' => $item['first_enrollment_date'] ?? '', 'first_sign_date' => $item['first_enrollment_date'] ?? '',
'user_names' => $userNames,
'company_address' => $company->company_address ?? '', 'company_address' => $company->company_address ?? '',
'company_city' => $company->company_city ?? '', 'company_city' => $company->company_city ?? '',
'company_area' => $company->company_area ?? '', 'company_area' => $company->company_area ?? '',
]; ];
if (empty($users)) {
// 如果没有学员,仍然导出公司基本信息
$data[] = array_merge($companyInfo, [
'user_name' => '',
'course_names' => '',
'course_types' => '',
'course_count' => 0,
]);
} else {
// 获取该公司的学员ID列表
$userIds = collect($users)->pluck('id')->toArray();
// 使用getStudentList获取学员的课程报名记录与统计逻辑一致
$userCourseSigns = CourseSign::getStudentList($start_date, $end_date, 1, $course_ids)
->whereIn('user_id', $userIds)
->with(['user', 'course.typeDetail'])
->get();
// 按学员分组
$usersData = [];
foreach ($userCourseSigns as $sign) {
if (!$sign->user) {
continue;
}
$userId = $sign->user_id;
if (!isset($usersData[$userId])) {
$usersData[$userId] = [
'user' => $sign->user,
'courseSigns' => [],
];
}
$usersData[$userId]['courseSigns'][] = $sign;
}
// 如果没有找到课程报名记录,仍然导出学员基本信息
if (empty($usersData)) {
foreach ($users as $user) {
$data[] = array_merge($companyInfo, [
'user_name' => $user->name ?? '',
'course_names' => '',
'course_types' => '',
'course_count' => 0,
]);
// 后续行的公司信息为空
$companyInfo = array_fill_keys(array_keys($companyInfo), '');
}
} else {
// 每个学员一行,公司信息只在第一行显示,后续行公司信息为空
$isFirstRow = true;
foreach ($usersData as $userData) {
$user = $userData['user'];
$courseSigns = collect($userData['courseSigns']);
// 获取课程名称列表,用中文顿号分隔
$courseNames = $courseSigns->pluck('course.name')->filter()->unique()->values()->implode('、');
// 获取课程体系列表,用中文顿号分隔
$courseTypes = $courseSigns->pluck('course.typeDetail.name')
->filter()
->unique()
->values()
->implode('、');
// 报名课程数
$courseCount = $courseSigns->count();
if ($isFirstRow) {
// 第一行:显示公司信息
$data[] = array_merge($companyInfo, [
'user_name' => $user->name ?? '',
'course_names' => $courseNames,
'course_types' => $courseTypes,
'course_count' => $courseCount,
]);
$isFirstRow = false;
} else {
// 后续行:公司信息为空
$data[] = [
'company_name' => '',
'company_legal_representative' => '',
'company_date' => '',
'invest_date' => '',
'first_sign_date' => '',
'company_address' => '',
'company_city' => '',
'company_area' => '',
'user_name' => $user->name ?? '',
'course_names' => $courseNames,
'course_types' => $courseTypes,
'course_count' => $courseCount,
];
}
}
// 处理没有课程报名记录的学员(如果有)
$processedUserIds = array_keys($usersData);
foreach ($users as $user) {
if (!in_array($user->id, $processedUserIds)) {
$data[] = [
'company_name' => '',
'company_legal_representative' => '',
'company_date' => '',
'invest_date' => '',
'first_sign_date' => '',
'company_address' => '',
'company_city' => '',
'company_area' => '',
'user_name' => $user->name ?? '',
'course_names' => '',
'course_types' => '',
'course_count' => 0,
];
}
}
}
}
} }
return $data; return $data;
@ -1385,10 +1503,13 @@ class OtherController extends CommonController
'company_date' => '成立时间', 'company_date' => '成立时间',
'invest_date' => '被投日期', 'invest_date' => '被投日期',
'first_sign_date' => '首次报名时间', 'first_sign_date' => '首次报名时间',
'user_names' => '学员姓名',
'company_address' => '地址', 'company_address' => '地址',
'company_city' => '所在城市', 'company_city' => '所在城市',
'company_area' => '所在区域', 'company_area' => '所在区域',
'user_name' => '学员姓名',
'course_names' => '课程名称',
'course_types' => '课程体系',
'course_count' => '报名课程数',
]; ];
// 创建多 sheet 导出 // 创建多 sheet 导出

Loading…
Cancel
Save