master
cody 2 months ago
parent 99754b777d
commit 55f4629c37

@ -16,7 +16,7 @@ class UpdateUserTalentTags extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'update:user-talent-tags'; protected $signature = 'update:user-talent-tags {--clean-duplicates : 清理已存在的重复标签}';
/** /**
* The console command description. * The console command description.
@ -32,6 +32,11 @@ class UpdateUserTalentTags extends Command
*/ */
public function handle() public function handle()
{ {
// 如果指定了清理重复标签选项,先执行清理
if ($this->option('clean-duplicates')) {
$this->cleanDuplicateTags();
}
$this->info('开始读取Excel文件并更新用户人才标签...'); $this->info('开始读取Excel文件并更新用户人才标签...');
// Excel文件路径 // Excel文件路径
@ -127,12 +132,14 @@ class UpdateUserTalentTags extends Command
$existingTagsArray = []; $existingTagsArray = [];
if (!empty($existingTags)) { if (!empty($existingTags)) {
// 将现有标签转换为数组 // 将现有标签转换为数组,并先进行去重处理(防止数据库中已有重复数据)
$existingTagsArray = array_filter(array_map('trim', explode(',', $existingTags))); $existingTagsArray = array_filter(array_map('trim', explode(',', $existingTags)));
// 对现有标签数组进行去重(防止数据库中已存在重复标签)
$existingTagsArray = array_values(array_unique($existingTagsArray));
} }
// 检查新标签是否已存在 // 检查新标签是否已存在(使用严格比较,确保大小写敏感)
if (in_array($talentTag, $existingTagsArray)) { if (in_array($talentTag, $existingTagsArray, true)) {
$this->info("第 {$rowNum} 行: 用户 '{$userName}' (ID: {$user->id}) 已存在标签 '{$talentTag}',跳过"); $this->info("第 {$rowNum} 行: 用户 '{$userName}' (ID: {$user->id}) 已存在标签 '{$talentTag}',跳过");
$this->info(" 当前人才标签: {$oldTalentTagsDisplay}"); $this->info(" 当前人才标签: {$oldTalentTagsDisplay}");
$skippedCount++; $skippedCount++;
@ -141,7 +148,8 @@ class UpdateUserTalentTags extends Command
// 添加新标签 // 添加新标签
$existingTagsArray[] = $talentTag; $existingTagsArray[] = $talentTag;
$existingTagsArray = array_values(array_unique($existingTagsArray)); // 去重并重新索引 // 再次去重并重新索引(双重保险,确保不会有重复)
$existingTagsArray = array_values(array_unique($existingTagsArray));
$newTalentTags = implode(',', $existingTagsArray); $newTalentTags = implode(',', $existingTagsArray);
// 更新用户 // 更新用户
@ -179,4 +187,61 @@ class UpdateUserTalentTags extends Command
return 1; return 1;
} }
} }
/**
* 清理所有用户中重复的人才标签
*/
protected function cleanDuplicateTags()
{
$this->info('开始清理重复的人才标签...');
$users = User::whereNotNull('talent_tags')
->where('talent_tags', '!=', '')
->get();
$cleanedCount = 0;
$totalCleaned = 0;
DB::beginTransaction();
try {
foreach ($users as $user) {
$originalTags = $user->talent_tags;
// 将标签转换为数组
$tagsArray = array_filter(array_map('trim', explode(',', $originalTags)));
// 去重
$uniqueTagsArray = array_values(array_unique($tagsArray));
// 如果去重后数量减少,说明有重复
if (count($tagsArray) > count($uniqueTagsArray)) {
$cleanedTags = implode(',', $uniqueTagsArray);
$user->talent_tags = $cleanedTags;
$user->save();
$this->info("用户 ID {$user->id} ({$user->name}): 清理重复标签");
$this->info(" 从: {$originalTags}");
$this->info(" 到: {$cleanedTags}");
$cleanedCount++;
$totalCleaned += (count($tagsArray) - count($uniqueTagsArray));
}
}
DB::commit();
$this->info('');
$this->info('========================================');
$this->info('清理完成!');
$this->info("清理了 {$cleanedCount} 个用户的重复标签");
$this->info("共移除 {$totalCleaned} 个重复标签");
$this->info('========================================');
$this->info('');
} catch (\Exception $e) {
DB::rollBack();
$this->error('清理重复标签时发生错误: ' . $e->getMessage());
throw $e;
}
}
} }

Loading…
Cancel
Save