Files
cunkebao_v3/Server/application/command/CheckAndFixWechatAccountCommand.php
2025-11-20 16:07:57 +08:00

160 lines
5.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use app\common\service\WechatAccountHealthScoreService;
/**
* 检测和修复s2_wechat_account表中wechatId和alias不一致的问题
* 同时更新isModifiedAlias字段和健康分
*/
class CheckAndFixWechatAccountCommand extends Command
{
protected function configure()
{
$this->setName('wechat:check-and-fix')
->setDescription('检测和修复s2_wechat_account表中wechatId和alias不一致的问题并更新健康分');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("开始检测和修复s2_wechat_account表...");
try {
// 1. 检测wechatId和alias不一致的记录
$output->writeln("步骤1: 检测wechatId和alias不一致的记录...");
$inconsistentAccounts = $this->findInconsistentAccounts();
$output->writeln("发现 " . count($inconsistentAccounts) . " 条不一致记录");
if (empty($inconsistentAccounts)) {
$output->writeln("没有发现不一致的记录,任务完成!");
return;
}
// 2. 修复不一致的记录
$output->writeln("步骤2: 修复不一致的记录...");
$fixedCount = $this->fixInconsistentAccounts($inconsistentAccounts);
$output->writeln("已修复 " . $fixedCount . " 条记录");
// 3. 更新isModifiedAlias字段
$output->writeln("步骤3: 更新isModifiedAlias字段...");
$updatedCount = $this->updateModifiedAliasFlag();
$output->writeln("已更新 " . $updatedCount . " 条记录的isModifiedAlias字段");
// 4. 重新计算健康分
$output->writeln("步骤4: 重新计算健康分...");
$healthScoreService = new WechatAccountHealthScoreService();
$accountIds = array_column($inconsistentAccounts, 'id');
$stats = $healthScoreService->batchCalculateAndUpdate($accountIds, 100);
$output->writeln("健康分计算完成:成功 " . $stats['success'] . " 条,失败 " . $stats['failed'] . "");
if (!empty($stats['errors'])) {
$output->writeln("错误详情:");
foreach ($stats['errors'] as $error) {
$output->writeln(" 账号ID {$error['accountId']}: {$error['error']}");
}
}
$output->writeln("任务完成!");
} catch (\Exception $e) {
$output->writeln("错误: " . $e->getMessage());
$output->writeln($e->getTraceAsString());
}
}
/**
* 查找wechatId和alias不一致的记录
*
* @return array
*/
private function findInconsistentAccounts()
{
// 查找wechatId和alias不一致的记录
// 条件wechatId不为空alias不为空且wechatId != alias
$accounts = Db::table('s2_wechat_account')
->where('isDeleted', 0)
->where('wechatId', '<>', '')
->where('alias', '<>', '')
->whereRaw('wechatId != alias')
->field('id, wechatId, alias, nickname, isModifiedAlias')
->select();
return $accounts ?: [];
}
/**
* 修复不一致的记录
* 策略从s2_wechat_friend表中查找最新的alias值来更新
*
* @param array $accounts 不一致的账号列表
* @return int 修复数量
*/
private function fixInconsistentAccounts($accounts)
{
$fixedCount = 0;
foreach ($accounts as $account) {
$wechatId = $account['wechatId'];
// 从s2_wechat_friend表中查找最新的alias值
$latestAlias = Db::table('s2_wechat_friend')
->where('wechatId', $wechatId)
->where('alias', '<>', '')
->order('updateTime', 'desc')
->value('alias');
// 如果找到了最新的alias则更新
if (!empty($latestAlias) && $latestAlias !== $account['alias']) {
Db::table('s2_wechat_account')
->where('id', $account['id'])
->update([
'alias' => $latestAlias,
'updateTime' => time()
]);
$fixedCount++;
}
}
return $fixedCount;
}
/**
* 更新isModifiedAlias字段
* 如果wechatId和alias不一致则标记为已修改
*
* @return int 更新数量
*/
private function updateModifiedAliasFlag()
{
// 更新isModifiedAlias字段wechatId != alias 的记录标记为1
$updatedCount = Db::table('s2_wechat_account')
->where('isDeleted', 0)
->where('wechatId', '<>', '')
->where('alias', '<>', '')
->whereRaw('wechatId != alias')
->update([
'isModifiedAlias' => 1,
'updateTime' => time()
]);
// 更新isModifiedAlias字段wechatId == alias 的记录标记为0
Db::table('s2_wechat_account')
->where('isDeleted', 0)
->where('wechatId', '<>', '')
->where('alias', '<>', '')
->whereRaw('wechatId = alias')
->update([
'isModifiedAlias' => 0,
'updateTime' => time()
]);
return $updatedCount;
}
}