diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index 8201d76..ec7ad64 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -162,6 +162,46 @@ class OtherController extends CommonController ->where('start_time', 'like', '%' . date('Y-m') . '%') ->get(); + // 苏州区域数据 + $suzhouArea = Company::approvedStudents()->where('company_city', '苏州市')->groupBy('company_area') + ->whereNotNull('company_area') + ->get(['company_area']); + $suzhou = []; + foreach ($suzhouArea as $item) { + $suzhou[] = [ + 'area' => $item->company_area, + 'total' => User::whereHas('company', function ($query) use ($item) { + $query->where('company_area', $item->company_area); + })->where('is_schoolmate', 1)->count() + ]; + } + + // 全国数据 + $countryArea = Company::approvedStudents()->groupBy('company_city')->whereNotNull('company_city')->get(['company_city']); + $country = []; + foreach ($countryArea as $item) { + $total = User::whereHas('company', function ($query) use ($item) { + $query->where('company_city', $item->company_city); + })->where('is_schoolmate', 1)->count(); + if (empty($total)) { + continue; + } + $country[] = [ + 'area' => $item->company_city, + 'total' => User::whereHas('company', function ($query) use ($item) { + $query->where('company_city', $item->company_city); + })->where('is_schoolmate', 1)->count() + ]; + } + + // 时间轴 + $time_axis = TimeEvent::orderBy('sort', 'asc')->get(); + // 动态信息 + $article['xiaoyou'] = Article::where('type', 1)->limit(7)->orderBy('sort', 'desc')->get(); + $article['yejie'] = Article::where('type', 2)->limit(7)->orderBy('sort', 'desc')->get(); + $article['supply_demands'] = SupplyDemand::limit(7)->orderBy('created_at', 'desc')->get(); + + // 课程体系统计 // 1. 从配置表读取配置列表 $yearConfigs = CourseTypeDataOverviewConfig::where('status', true) ->orderBy('sort', 'asc') @@ -184,18 +224,33 @@ class OtherController extends CommonController return clone $item; }); + // 收集所有课程类型的课程ID(用于计算去重总计) + $allCourseIds = collect(); + // 对每个 CourseType 进行统计 foreach ($courseTypes as $courseType) { // 历史已开设期数 $historyCourse = HistoryCourse::whereHas('typeDetail', function ($query) use ($courseType) { $query->where('name', 'like', '%' . $courseType->name . '%'); })->get(); - // 课程 - $courses = Course::where('type', $courseType->id)->where('is_chart', 1)->get(); + // 课程(使用配置的日期范围) + $courses = Course::where('type', $courseType->id) + ->where('is_chart', 1) + ->where(function ($query) use ($configStartDate, $configEndDate) { + // 开始结束日期的筛选。or查询 + if ($configStartDate && $configEndDate) { + $query->whereBetween('start_date', [$configStartDate, $configEndDate]) + ->orWhereBetween('end_date', [$configStartDate, $configEndDate]); + } + }) + ->get(); + // 收集课程ID + $allCourseIds = $allCourseIds->merge($courses->pluck('id')); + // 历史课程期数 $courseType->history_course_periods_total = $historyCourse->count(); - // 现在课程数据 - $courseType->now_course_periods_total = Course::where('type', $courseType->id)->where('is_chart', 1)->count(); + // 现在课程数据(使用配置的日期范围) + $courseType->now_course_periods_total = $courses->count(); // 历史课程培养人数去重 $courseType->history_course_signs_total = $historyCourse->sum('course_type_signs_pass_unique'); @@ -210,46 +265,45 @@ class OtherController extends CommonController // 将统计数据直接组合到配置对象中 $config->courseTypes = $courseTypes; - } - - // 苏州区域数据 - $suzhouArea = Company::approvedStudents()->where('company_city', '苏州市')->groupBy('company_area') - ->whereNotNull('company_area') - ->get(['company_area']); - $suzhou = []; - foreach ($suzhouArea as $item) { - $suzhou[] = [ - 'area' => $item->company_area, - 'total' => User::whereHas('company', function ($query) use ($item) { - $query->where('company_area', $item->company_area); - })->where('is_schoolmate', 1)->count() - ]; - } - // 全国数据 - $countryArea = Company::approvedStudents()->groupBy('company_city')->whereNotNull('company_city')->get(['company_city']); - $country = []; - foreach ($countryArea as $item) { - $total = User::whereHas('company', function ($query) use ($item) { - $query->where('company_city', $item->company_city); - })->where('is_schoolmate', 1)->count(); - if (empty($total)) { - continue; - } - $country[] = [ - 'area' => $item->company_city, - 'total' => User::whereHas('company', function ($query) use ($item) { - $query->where('company_city', $item->company_city); - })->where('is_schoolmate', 1)->count() - ]; + // 计算去重总计数量 + // 1. 现在课程的去重学员数(所有课程类型,在配置的日期范围内) + $nowCourseSignsUniqueTotal = CourseSign::courseSignsTotalByUnique($configStartDate, $configEndDate, 1, $allCourseIds->unique(), false, false); + + // 2. 历史课程的去重学员数(在配置的日期范围内,针对所有课程类型) + // 收集所有课程类型的名称,用于匹配历史课程 + $courseTypeNames = $allCourseTypes->pluck('name'); + $historyCourseSignsUniqueTotal = HistoryCourse::whereHas('typeDetail', function ($query) use ($courseTypeNames) { + // 使用名称匹配所有课程类型(与循环中的逻辑保持一致) + $query->where(function ($q) use ($courseTypeNames) { + foreach ($courseTypeNames as $name) { + $q->orWhere('name', 'like', '%' . $name . '%'); + } + }); + }) + ->where(function ($query) use ($configStartDate, $configEndDate) { + // 开始结束日期的筛选。or查询 + if ($configStartDate && $configEndDate) { + $query->whereBetween('start_time', [$configStartDate, $configEndDate]) + ->orWhereBetween('end_time', [$configStartDate, $configEndDate]); + } + }) + ->sum('course_type_signs_pass_unique'); + + // 3. 总计 = 现在课程去重人数 + 历史课程去重人数 + $config->course_signs_unique_total = $nowCourseSignsUniqueTotal + $historyCourseSignsUniqueTotal; + // 去重总计 + $config->course_signs_unique_total = Course::where('type', $courseType->id) + ->where('is_chart', 1) + ->where(function ($query) use ($configStartDate, $configEndDate) { + // 开始结束日期的筛选。or查询 + if ($configStartDate && $configEndDate) { + $query->whereBetween('start_date', [$configStartDate, $configEndDate]) + ->orWhereBetween('end_date', [$configStartDate, $configEndDate]); + } + })->count(); } - // 时间轴 - $time_axis = TimeEvent::orderBy('sort', 'asc')->get(); - // 动态信息 - $article['xiaoyou'] = Article::where('type', 1)->limit(7)->orderBy('sort', 'desc')->get(); - $article['yejie'] = Article::where('type', 2)->limit(7)->orderBy('sort', 'desc')->get(); - $article['supply_demands'] = SupplyDemand::limit(7)->orderBy('created_at', 'desc')->get(); return $this->success(compact('list', 'suzhou', 'country', 'monthCourses', 'time_axis', 'article', 'yearConfigs')); } diff --git a/app/Http/Controllers/Web/CompanyController.php b/app/Http/Controllers/Web/CompanyController.php index 1d06e55..6b89091 100644 --- a/app/Http/Controllers/Web/CompanyController.php +++ b/app/Http/Controllers/Web/CompanyController.php @@ -59,14 +59,41 @@ class CompanyController extends Controller ], 404); } + // 获取当前用户的公司名称(更新前的) + $oldCompanyName = $user->company_name; + + // 更新当前用户 $user->company_name = $request->company_name; $user->company_id = null; // 设置 company_id 为 null $user->save(); + // 查找所有具有相同公司名称的用户(排除已匹配到公司的,即 company_id > 0) + $sameCompanyUsers = User::where('company_name', $oldCompanyName) + ->where(function ($query) { + $query->whereNull('company_id') + ->orWhere('company_id', '<=', 0); + }) + ->whereHas('courseSigns', function ($query) { + $query->where('status', 1); // 审核通过 + }) + ->get(); + + // 批量更新所有相同公司名称的用户 + $updateCount = 0; + foreach ($sameCompanyUsers as $sameUser) { + $sameUser->company_name = $request->company_name; + $sameUser->company_id = null; + $sameUser->save(); + $updateCount++; + } + return response()->json([ 'code' => 200, - 'msg' => '更新成功', - 'data' => $user + 'msg' => '更新成功,共更新 ' . $updateCount . ' 条记录', + 'data' => [ + 'user' => $user, + 'updated_count' => $updateCount + ] ]); } catch (\Exception $e) { return response()->json([