You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
6.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Models\Company;
use App\Models\User;
use Illuminate\Console\Command;
/**
* 对比 studyis_schoolmate=1, address=上海, is_chart=1与 home-v2 country 上海市 的人数差异,
* 输出各自 User id 及「仅 study 有 / 仅 home-v2 有」的明细,便于修正。
*/
class DiffStudyHomeV2Country extends Command
{
protected $signature = 'diff:study-home-v2-country
{--address=上海 : 地址关键词,与 study 的 address 对应}';
protected $description = '对比 study(上海+校友+is_chart) 与 home-v2 country 上海市 的 User 差异';
public function handle()
{
$address = $this->option('address');
// 1) studyis_schoolmate=1, address=上海, is_chart=1courses_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();
}
}