diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index 3686ebd..aa336b0 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -145,8 +145,8 @@ class OtherController extends CommonController // 高层次人才 // 获取人才培训课程 $list['cover_rencai_total'] = CourseSign::rencai(); - // 重点上市公司 - $list['cover_stock_total'] = CourseSign::shangshi(); + // 苏州重点上市公司 + $list['cover_stock_total'] = CourseSign::suzhouStock(); // 培养人次1 $start_date = CourseType::START_DATE; // 默认结束日期一年以后 @@ -1687,13 +1687,13 @@ class OtherController extends CommonController break; case 'cover_stock_total': - // 重点上市公司明细 - 使用模型方法 - $companiesData = CourseSign::shangshi($start_date, $end_date, $course_ids, true); + // 苏州重点上市公司明细 - 使用模型方法 + $companiesData = CourseSign::suzhouStock($start_date, $end_date, $course_ids, true); foreach ($companiesData as $item) { $company = $item['company']; $users = $item['users'] ?? []; - // 公司基本信息(只在第一行使用) + // 公司基本信息 $companyInfo = [ 'company_name' => $company->company_name, 'company_legal_representative' => $company->company_legal_representative ?? '', @@ -1717,8 +1717,7 @@ class OtherController extends CommonController 'course_count' => 0, ]); } else { - // 每个学员一行 - $isFirstRow = true; + // 每个学员一行,每行都显示完整的公司信息 foreach ($users as $userInfo) { $user = $userInfo['user'] ?? null; $courses = $userInfo['courses'] ?? []; @@ -1739,35 +1738,13 @@ class OtherController extends CommonController $courseCount = count($courses); } - if ($isFirstRow) { - // 第一行:显示公司信息 - $data[] = array_merge($companyInfo, [ - 'user_name' => $userInfo['user_name'] ?? ($user->name ?? ''), - 'course_names' => $courseNames, - 'course_types' => $courseTypes, - 'course_count' => $courseCount, - ]); - $isFirstRow = false; - } else { - // 后续行:公司信息为空 - $data[] = [ - 'company_name' => '', - 'company_legal_representative' => '', - 'company_date' => '', - 'stock_date' => '', - 'company_address' => '', - 'company_city' => '', - 'company_area' => '', - 'company_tag' => '', - 'business_scope' => '', - 'contact_phone' => '', - 'contact_mail' => '', - 'user_name' => $userInfo['user_name'] ?? ($user->name ?? ''), - 'course_names' => $courseNames, - 'course_types' => $courseTypes, - 'course_count' => $courseCount, - ]; - } + // 每个学员一行,每行都显示完整的公司信息 + $data[] = array_merge($companyInfo, [ + 'user_name' => $userInfo['user_name'] ?? ($user->name ?? ''), + 'course_names' => $courseNames, + 'course_types' => $courseTypes, + 'course_count' => $courseCount, + ]); } } } @@ -1788,7 +1765,7 @@ class OtherController extends CommonController 'course_types' => '课程体系', 'course_count' => '报名课程数', ]; - $filename = '重点上市公司明细'; + $filename = '苏州重点上市公司明细'; break; default: diff --git a/app/Models/CourseSign.php b/app/Models/CourseSign.php index 50ad16a..01889a6 100755 --- a/app/Models/CourseSign.php +++ b/app/Models/CourseSign.php @@ -831,6 +831,125 @@ class CourseSign extends SoftDeletesModel } } + /** + * 苏州重点上市公司(统计或列表) + * 在上市公司基础上,限定为苏州地区的公司 + * 条件:user表的company_address包含"苏州",或关联公司的company_address包含"苏州",或company_city包含"苏州" + * @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 suzhouStock($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(); + + // 获取这些公司中标记为上市的公司 + $companies = Company::approvedStudents()->whereIn('id', $companyIds) + ->where('company_market', 1) + ->get() + ->keyBy('id'); + + // 筛选苏州地区的公司 + $suzhouCompanyIds = []; + foreach ($courseSigns as $courseSign) { + if (!$courseSign->user || !$courseSign->user->company) { + continue; + } + + $companyId = $courseSign->user->company->id; + // 只处理上市公司的记录 + if (!isset($companies[$companyId])) { + continue; + } + + $user = $courseSign->user; + $company = $user->company; + + // 判断是否为苏州地区:user表的company_address包含"苏州",或关联公司的company_address包含"苏州",或company_city包含"苏州" + $isSuzhou = false; + if ($user->company_address && strpos($user->company_address, '苏州') !== false) { + $isSuzhou = true; + } elseif ($company->company_address && strpos($company->company_address, '苏州') !== false) { + $isSuzhou = true; + } elseif ($company->company_city && strpos($company->company_city, '苏州') !== false) { + $isSuzhou = true; + } + + if ($isSuzhou) { + $suzhouCompanyIds[$companyId] = true; + } + } + + // 只保留苏州地区的上市公司 + $suzhouCompanies = $companies->filter(function ($company) use ($suzhouCompanyIds) { + return isset($suzhouCompanyIds[$company->id]); + }); + + if ($retList) { + // 返回详细列表:主表是公司,子数据是学员信息 + $result = []; + foreach ($courseSigns as $courseSign) { + if (!$courseSign->user || !$courseSign->user->company) { + continue; + } + + $companyId = $courseSign->user->company->id; + // 只处理苏州上市公司的记录 + if (!isset($suzhouCompanyIds[$companyId])) { + continue; + } + + $company = $suzhouCompanies[$companyId] ?? $courseSign->user->company; + + // 如果公司还没有在结果中,初始化 + 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 count($suzhouCompanyIds); + } + } + /** * 今年上市公司(统计或列表) * @param string|null $start_date 开始日期