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.

412 lines
12 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\CourseForm;
use App\Models\CourseSign;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class UpdateUserFromCourseSign extends Command
{
/**
* 限定的手机号列表
*
* @var array
*/
protected $allowedMobiles = [
'13858193624',
'18168183607',
'13771868287',
'18549832553',
'18817298535',
'15273825050',
'18501595925',
'15962590786',
'15320620860',
'13914062017',
'18845143850',
'13917783425',
'13003277587',
'13361107842',
'15241804640',
'13372167314',
'18911127892',
'13997379909',
'15962509586',
'13476809761',
'18906228428',
'13962193806',
'13401443377',
'15962258750',
'17356073468',
'15810534434',
'18662580218',
'13914082740',
'15850525270',
'18739970513',
'15951188321',
'13774360353',
'13913655117',
'13626235977',
'18913283047',
'13013780812',
'18706135861',
'18896939233',
'13598863511',
'15061626527',
'13671824049',
'18762554317',
'15295604750',
'13901541065',
'15995554646',
'13607695075',
'18120148214',
'18626276559',
'13390883999',
'15144140738',
'18906223828',
'18012609856',
'18521596109',
'13009135328',
'13914075750',
'13675755980',
'15651822712',
'18967310128',
'18626295150',
'17712684658',
'18362590815',
'15021008622',
'17303651730',
'17706139033',
'15851476110',
'18601100026',
'13310030818',
'18017665072',
'18912617316',
'13509081568',
'18153524752',
'15995415268',
'17558865852',
'13080603559',
'18964136910',
'13572101550',
'19517891657',
'17805246873',
'13018069212',
'18810901685',
'18616829901',
'13516009851',
'13621553499',
'18986133721',
'13402687760',
'13616211556',
'18505126656',
'13914067620',
'13817301718',
'17710782443',
'13914070504',
'13301575752',
'18994390903',
'18118152163',
'18913553211',
'18550011669',
'15895987328',
'13521086590',
'16601137982',
'18101546975',
'15150558910',
'15962175908',
'18913516806',
'13306136908',
'13720114794',
'13915590667',
'18818589103',
'15850298560',
'13621738712',
'15502129786',
'13862044718',
'13699289293',
'13776041671',
'13811803477',
'18606274785',
'13962124936',
'13504329035',
'18115687371',
'18662158968',
'18662338056',
'13913682588',
'18294402490',
'13270460314',
'18118133321',
'18616795057',
'18912640962',
'18014012006',
'15162647653',
'13466327309',
'15995818235',
'15026822942',
'15652776292',
'15995845671',
'15711294528',
'13656245273',
'13732612069',
'13912729192',
'13771714603',
'13818243299',
'13488771282',
'13584853248',
'15506142175',
'13205193071',
'13962331061',
'15501521761',
'13731218215',
'13812787096',
'15527157619',
'15506218920',
'18858085416',
'18915500725',
'13162792917',
'18911085890',
'15250177879',
'13962113299',
'13681023055',
'17621680840',
'18126448678',
'13776090472',
'18550233721',
'15862493869',
'13298612196',
'13771914071',
'13501238055',
'18913226262',
'18051731616',
'13552627464',
'13669830249',
'19951220702',
'13511594468',
'13942027696',
'15952970972',
'18606270513',
'18627127978',
'13614175299',
'17751459927',
'18994467326',
'18012603353',
'18626178698',
'18811760936',
'13776243855',
'15862467695',
'13913584479',
'15021956508',
'18362120000',
'13076748281',
'18962233636',
'13913101360',
'13812685231',
'13862064335',
'15506266777',
'13812869876',
'13771855571',
'13906132266',
'13701570032',
'15298877317',
'15150106699',
'13506139373',
'13812770216',
'13862019986',
'18662118000',
'18626261221',
'13806216733',
'15306209021',
'13656225381',
'15250974754',
'13451736589',
'13901544538',
'13776091313',
'13812605725',
'17746389584',
'13813662822',
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update_user_from_course_sign';
/**
* The console command description.
*
* @var string
*/
protected $description = '从报名表获取用户填写的信息填充到user数据表按手机号匹配';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 根据限定手机号列表查找所有用户
$users = User::whereIn('mobile', $this->allowedMobiles)->get();
if ($users->isEmpty()) {
return $this->error('没有找到符合条件的用户');
}
$this->info("找到 {$users->count()} 个用户需要处理");
$successCount = 0;
$failCount = 0;
foreach ($users as $user) {
$this->newLine();
$this->info("========== 处理用户:{$user->name} (手机号: {$user->mobile}, ID: {$user->id}) ==========");
try {
$this->processUser($user);
$successCount++;
} catch (\Exception $e) {
$this->error("处理用户 {$user->name} 时出错:" . $e->getMessage());
$failCount++;
}
}
$this->newLine();
return $this->info("全部处理完成!成功:{$successCount},失败:{$failCount}");
}
/**
* 处理单个用户
*/
private function processUser($user)
{
$user_id = $user->id;
// 获取该用户的所有报名记录
$courseSigns = CourseSign::where('user_id', $user_id)
->whereNotNull('data')
->get();
if ($courseSigns->isEmpty()) {
$this->info('该用户没有报名记录或报名记录中没有数据');
return;
}
$this->info("找到 {$courseSigns->count()} 条报名记录");
// 收集所有需要更新的用户字段
$userData = [];
$hasCompanyName = false;
foreach ($courseSigns as $courseSign) {
if (empty($courseSign->data) || !is_array($courseSign->data)) {
continue;
}
// 获取该课程的表单字段配置,建立 field -> belong_user_table 的映射关系
$courseForms = CourseForm::where('course_id', $courseSign->course_id)
->where('belong_user', 1) // 只获取属于用户信息的字段
->whereNotNull('belong_user_table') // 必须有对应的用户表字段
->where('belong_user_table', '!=', '') // 用户表字段不能为空
->get(['field', 'belong_user_table']);
if ($courseForms->isEmpty()) {
continue;
}
// 建立 field -> belong_user_table 的映射关系
$fieldMapping = [];
foreach ($courseForms as $form) {
$fieldMapping[$form->field] = $form->belong_user_table;
}
// 将 data 数组转换为以 field 为 key 的关联数组
$dataArray = [];
foreach ($courseSign->data as $item) {
if (isset($item['field']) && isset($item['value'])) {
$dataArray[$item['field']] = $item['value'];
}
}
// 提取属于用户信息的字段,使用 belong_user_table 作为用户表的字段名
foreach ($fieldMapping as $field => $userTableField) {
if (isset($dataArray[$field]) && $dataArray[$field] !== null && $dataArray[$field] !== '') {
// 如果字段已经在 $userData 中,且新值不为空,则更新(优先使用非空值)
if (!isset($userData[$userTableField]) || empty($userData[$userTableField])) {
$userData[$userTableField] = $dataArray[$field];
}
// 检查是否是公司名字
if ($userTableField === 'company_name') {
$hasCompanyName = true;
}
}
}
}
if (empty($userData)) {
$this->info('没有找到需要更新的用户信息');
return;
}
$this->info('准备更新以下字段:' . implode(', ', array_keys($userData)));
// 更新用户信息
// 根据 User::$coverFields 判断是否需要覆盖更新
foreach ($userData as $key => $value) {
if (!in_array($key, User::$coverFields)) {
// 追加更新(对于非覆盖字段)
$currentValue = $user->$key ?? '';
if (!empty($currentValue)) {
$tempArray = explode(',', $currentValue . ',' . $value);
$tempArray = array_unique(array_filter($tempArray));
$userData[$key] = implode(',', $tempArray);
}
}
}
// 检查公司名字是否发生变化
$oldCompanyName = $user->company_name;
$newCompanyName = $userData['company_name'] ?? null;
// 如果公司名字从无到有,或者发生变化,都需要更新
$companyNameChanged = $hasCompanyName && isset($newCompanyName) &&
(empty($oldCompanyName) || $oldCompanyName != $newCompanyName);
$user->fill($userData);
$user->save();
$this->info('用户信息更新成功');
// 如果公司名字发生变化或新增,调用 UpdateCompany 脚本
// if ($companyNameChanged) {
// if (empty($oldCompanyName)) {
// $this->info("检测到新增公司名字({$newCompanyName}),开始调用 UpdateCompany 脚本更新公司信息...");
// } else {
// $this->info("检测到公司名字发生变化({$oldCompanyName} -> {$newCompanyName}),开始调用 UpdateCompany 脚本更新公司信息...");
// }
// Artisan::call("update_company --user_id={$user_id}");
// $this->info('公司信息更新完成');
// } elseif ($hasCompanyName && isset($newCompanyName)) {
// $this->info("公司名字未发生变化({$newCompanyName}),跳过公司信息更新");
// }
$this->info('用户更新完成');
}
}