From f248e9b16341b6d44dd59bd9e7fe366a1879ca82 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Tue, 20 Jan 2026 19:04:35 +0800 Subject: [PATCH 1/2] update --- app/Http/Controllers/Admin/OtherController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index 907b990..25fb460 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -174,21 +174,21 @@ class OtherController extends CommonController ]; } - // 全国数据 + // 全国数据(与 study 的 address 口径一致:company_address 或 company_city 的 like 匹配,含「市」的取前部如 上海市→上海 以同时匹配 上海/上海市) $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); + $term = preg_replace('/市$/', '', $item->company_city) ?: $item->company_city; + $total = User::whereHas('company', function ($query) use ($term) { + $query->where('company_address', 'like', '%' . $term . '%') + ->orWhere('company_city', 'like', '%' . $term . '%'); })->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() + 'total' => $total, ]; } From 3241c25da9bb21470483bb2f31f1c71a555ae529 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Tue, 20 Jan 2026 19:11:52 +0800 Subject: [PATCH 2/2] update --- .../Commands/DiffStudyHomeV2Country.php | 137 ++++++++++++++++++ .../Controllers/Admin/OtherController.php | 20 ++- 2 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 app/Console/Commands/DiffStudyHomeV2Country.php diff --git a/app/Console/Commands/DiffStudyHomeV2Country.php b/app/Console/Commands/DiffStudyHomeV2Country.php new file mode 100644 index 0000000..d7056ab --- /dev/null +++ b/app/Console/Commands/DiffStudyHomeV2Country.php @@ -0,0 +1,137 @@ +option('address'); + + // 1) study:is_schoolmate=1, address=上海, is_chart=1;courses_start/end、status 为空(不限制课程日期与报名状态) + $studyQuery = User::query() + ->where('is_schoolmate', 1) + ->whereHas('company', function ($c) use ($address) { + $c->where('company_address', 'like', '%' . $address . '%') + ->orWhere('company_city', 'like', '%' . $address . '%'); + }) + ->whereHas('courseSigns', function ($cs) { + $cs->whereHas('course', function ($c) { + $c->where('is_chart', 1); + }); + }); + $studyIds = $studyQuery->pluck('id')->toArray(); + + // 2) home-v2 上海市 当前逻辑:address like + is_schoolmate + whereHas courseSigns(course is_chart=1)(与 study 完全一致) + $term = preg_replace('/市$/', '', '上海市') ?: '上海市'; + $homeV2Query = User::query() + ->where('is_schoolmate', 1) + ->whereHas('company', function ($q) use ($term) { + $q->where(function ($q2) use ($term) { + $q2->where('company_address', 'like', '%' . $term . '%') + ->orWhere('company_city', 'like', '%' . $term . '%'); + }); + }) + ->whereHas('courseSigns', function ($cs) { + $cs->whereHas('course', function ($c) { + $c->where('is_chart', 1); + }); + }); + $homeV2Ids = $homeV2Query->pluck('id')->toArray(); + + // 3) 仅用 address 匹配 + is_schoolmate(即与 home-v2 同样的 address 条件,不含 courseSigns) + $addressOnlyQuery = User::query() + ->where('is_schoolmate', 1) + ->whereHas('company', function ($q) use ($address) { + $q->where('company_address', 'like', '%' . $address . '%') + ->orWhere('company_city', 'like', '%' . $address . '%'); + }); + $addressOnlyIds = $addressOnlyQuery->pluck('id')->toArray(); + + $this->line('======== 人数 ========'); + $this->table( + ['口径', '人数'], + [ + ['study (address+is_schoolmate+courseSigns is_chart=1)', count($studyIds)], + ['home-v2 上海市 (address like+is_schoolmate, term=' . $term . ')', count($homeV2Ids)], + ['仅 address+is_schoolmate (不含 courseSigns)', count($addressOnlyIds)], + ] + ); + + $onlyStudy = array_values(array_diff($studyIds, $homeV2Ids)); + $onlyHomeV2 = array_values(array_diff($homeV2Ids, $studyIds)); + + $this->line('study ids 与 home-v2 上海市 ids 是否完全一致: ' . (count($onlyStudy) === 0 && count($onlyHomeV2) === 0 ? '是' : '否')); + + if (!empty($onlyStudy)) { + $this->line(''); + $this->line('【仅 study 有、home-v2 没有】 count=' . count($onlyStudy)); + $rows = User::with('company')->whereIn('id', $onlyStudy)->get(['id', 'name', 'mobile', 'is_schoolmate', 'company_id']); + $out = []; + foreach ($rows as $u) { + $c = $u->company; + $out[] = [ + $u->id, + $u->name, + $u->mobile, + $u->company_id, + $c ? ($c->company_city ?? '') : '', + $c ? ($c->company_address ?? '') : '', + $c ? (strpos((string)$c->company_address, $address) !== false || strpos((string)$c->company_city, $address) !== false ? 'Y' : 'N') : 'N', + ]; + } + $this->table(['user_id', 'name', 'mobile', 'company_id', 'company_city', 'company_address', 'match_' . $address . '?'], $out); + } + + if (!empty($onlyHomeV2)) { + $this->line(''); + $this->line('【仅 home-v2 有、study 没有】 count=' . count($onlyHomeV2)); + $this->line(implode(', ', $onlyHomeV2)); + } + + // 4) 检查 $countryArea 中是否有 上海市,以及 approvedStudents 与 普通 company 的差别 + $this->line(''); + $this->line('======== 补充:$countryArea 与 approvedStudents ========'); + $countryArea = Company::approvedStudents()->groupBy('company_city')->whereNotNull('company_city')->get(['company_city']); + $shCity = $countryArea->firstWhere('company_city', '上海市'); + $this->line('countryArea 中是否存在 company_city=上海市: ' . ($shCity ? '是' : '否')); + $hasShanghai = $countryArea->contains(fn ($i) => $i->company_city === '上海' || strpos((string)$i->company_city, '上海') !== false); + $this->line('countryArea 中是否有含「上海」的 company_city: ' . ($hasShanghai ? '是' : '否')); + + // 5) 若 study 多 1 人:检查该人 company 是否 whereHas 能匹配 (company_address or company_city) like 上海 + if (count($onlyStudy) === 1) { + $u = User::with('company')->find($onlyStudy[0]); + if ($u && $u->company) { + $c = $u->company; + $a = (string)($c->company_address ?? ''); + $city = (string)($c->company_city ?? ''); + $like = 'like \'%' . $term . '%\''; + $this->line(''); + $this->line('该 User 的 company: company_city=' . $city . ' company_address=' . $a); + $this->line('company_address ' . $like . ' => ' . (strpos($a, $term) !== false ? 'true' : 'false')); + $this->line('company_city ' . $like . ' => ' . (strpos($city, $term) !== false ? 'true' : 'false')); + $this->line('company 是否 approvedStudents: ' . ($this->companyIsApprovedStudents($c->id) ? 'Y' : 'N')); + } + } + + return 0; + } + + private function companyIsApprovedStudents($companyId): bool + { + return Company::approvedStudents()->where('id', $companyId)->exists(); + } +} diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index 25fb460..4ca66fb 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -174,15 +174,25 @@ class OtherController extends CommonController ]; } - // 全国数据(与 study 的 address 口径一致:company_address 或 company_city 的 like 匹配,含「市」的取前部如 上海市→上海 以同时匹配 上海/上海市) + // 全国数据(与 study 口径一致:address 用 company_address/company_city 的 like;且须有 courseSigns 且 course.is_chart=1,is_schoolmate=1) $countryArea = Company::approvedStudents()->groupBy('company_city')->whereNotNull('company_city')->get(['company_city']); $country = []; foreach ($countryArea as $item) { $term = preg_replace('/市$/', '', $item->company_city) ?: $item->company_city; - $total = User::whereHas('company', function ($query) use ($term) { - $query->where('company_address', 'like', '%' . $term . '%') - ->orWhere('company_city', 'like', '%' . $term . '%'); - })->where('is_schoolmate', 1)->count(); + $total = User::query() + ->where('is_schoolmate', 1) + ->whereHas('company', function ($query) use ($term) { + $query->where(function ($q) use ($term) { + $q->where('company_address', 'like', '%' . $term . '%') + ->orWhere('company_city', 'like', '%' . $term . '%'); + }); + }) + ->whereHas('courseSigns', function ($cs) { + $cs->whereHas('course', function ($c) { + $c->where('is_chart', 1); + }); + }) + ->count(); if (empty($total)) { continue; }