144 lines
7.1 KiB
PHP
144 lines
7.1 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace app\command;
|
|||
|
|
|
|||
|
|
use support\Command;
|
|||
|
|
use support\Db;
|
|||
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
|||
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|||
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|||
|
|
use Symfony\Component\Console\Input\InputOption;
|
|||
|
|
|
|||
|
|
class UpdateInquiryLogFromCrmCustomerCommand extends SymfonyCommand
|
|||
|
|
{
|
|||
|
|
protected static $defaultName = 'inquiry:update-from-crm-customer';
|
|||
|
|
protected static $defaultDescription = '将crm_customer表的部分字段更新到inquiry_log表';
|
|||
|
|
|
|||
|
|
protected function configure()
|
|||
|
|
{
|
|||
|
|
$this->setHelp('该命令将crm_customer表的user_id更新到inquiry_log表的recipient字段,将obtain字段更新到t_time字段。')
|
|||
|
|
->addOption('dry-run', null, InputOption::VALUE_NONE, '仅显示将要执行的更新操作,不实际执行')
|
|||
|
|
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, '限制处理的记录数量', 5000);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|||
|
|
{
|
|||
|
|
$isDryRun = $input->getOption('dry-run');
|
|||
|
|
$limit = (int) $input->getOption('limit');
|
|||
|
|
|
|||
|
|
if ($isDryRun) {
|
|||
|
|
$output->writeln('<info>DRY RUN 模式 - 不会实际执行更新操作</info>');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$output->writeln('<info>开始更新 inquiry_log 表...</info>');
|
|||
|
|
$output->writeln("<info>处理限制: {$limit} 条记录</info>");
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 使用事务确保数据一致性
|
|||
|
|
DB::transaction(function () use ($output, $isDryRun, $limit) {
|
|||
|
|
// 查询需要更新的记录
|
|||
|
|
$customers = Db::table('crm_customer')
|
|||
|
|
->select('id', 'sn', 'user_id', 'obtain','created_at')
|
|||
|
|
->whereNotNull('sn')
|
|||
|
|
->where('sn', '<>', '')
|
|||
|
|
->offset($limit)
|
|||
|
|
->limit(4000)
|
|||
|
|
->orderBy('id', 'asc')
|
|||
|
|
->get();
|
|||
|
|
|
|||
|
|
$output->writeln("<info>找到 " . count($customers) . " 条客户记录需要处理</info>");
|
|||
|
|
|
|||
|
|
$updatedCount = 0;
|
|||
|
|
$skippedCount = 0;
|
|||
|
|
$defaultTimestamp = strtotime('2025-08-26 00:00:00');
|
|||
|
|
|
|||
|
|
$output->writeln("<info>找到 " . count($customers) . " 条客户记录需要处理</info>");
|
|||
|
|
$step = [];
|
|||
|
|
$updateId = [];
|
|||
|
|
foreach ($customers as $customer) {
|
|||
|
|
$output->writeln("<info>处理客户 ID: {$customer->id}, SN: {$customer->sn}, User ID: {$customer->user_id}, obtain: {$customer->obtain}, created_at: {$customer->created_at}</info>");
|
|||
|
|
// 查找匹配的 inquiry_log 记录
|
|||
|
|
$inquiryLog = Db::table('inquiry_log')
|
|||
|
|
->where('order_no', $customer->sn)
|
|||
|
|
->first(['id', 'recipient', 't_time']);
|
|||
|
|
|
|||
|
|
if (!$inquiryLog) {
|
|||
|
|
$step[] = $customer->id;
|
|||
|
|
$output->writeln("<comment>跳过客户 ID {$customer->id} (sn: {$customer->sn}) - 未找到对应的 inquiry_log 记录</comment>");
|
|||
|
|
$skippedCount++;
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
$output->writeln("<info>$inquiryLog->id</info>");
|
|||
|
|
|
|||
|
|
// 准备更新数据
|
|||
|
|
$updateData = [];
|
|||
|
|
|
|||
|
|
// 更新 recipient 字段 (user_id)
|
|||
|
|
if ($customer->user_id != $inquiryLog->recipient) {
|
|||
|
|
$updateData['recipient'] = $customer->user_id;
|
|||
|
|
$output->writeln("<comment>客户 ID {$customer->id}: recipient 将从 {$inquiryLog->recipient} 更新为 {$customer->user_id}</comment>");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(empty($customer->obtain)&& !empty($customer->user_id)){
|
|||
|
|
if($customer->user_id == $inquiryLog->recipient){
|
|||
|
|
$obtain = $inquiryLog->t_time;
|
|||
|
|
}else{
|
|||
|
|
$obtain = strtotime('2025-08-01 10:00:00');
|
|||
|
|
$obtain = $customer->created_at > $obtain?$customer->created_at:$obtain;
|
|||
|
|
}
|
|||
|
|
Db::table('crm_customer')
|
|||
|
|
->where('id', $customer->id)
|
|||
|
|
->update(['obtain' => $obtain]);
|
|||
|
|
$output->writeln("<info>更新obtain $obtain</info>");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新 t_time 字段 (obtain)
|
|||
|
|
$newTTime = $customer->obtain > 0 ? $customer->obtain : $defaultTimestamp;
|
|||
|
|
if ($newTTime != $inquiryLog->t_time && !empty($updateData['recipient'])) {
|
|||
|
|
$updateData['t_time'] = $newTTime;
|
|||
|
|
$timeStr = date('Y-m-d H:i:s', $newTTime);
|
|||
|
|
$output->writeln("<comment>客户 ID {$customer->id}: t_time 将从 " . ($inquiryLog->t_time ? date('Y-m-d H:i:s', $inquiryLog->t_time) : 'NULL') . " 更新为 {$timeStr}</comment>");
|
|||
|
|
}
|
|||
|
|
if($customer->user_id == 0 && $inquiryLog->recipient != 0){
|
|||
|
|
$updateData['recipient'] = 0;
|
|||
|
|
$updateData['t_time'] = time();
|
|||
|
|
$updateData['status'] = 1;
|
|||
|
|
$updateData['r_time'] = time();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果有需要更新的字段,执行更新
|
|||
|
|
if (!empty($updateData)) {
|
|||
|
|
$updateId[] = $customer->id;
|
|||
|
|
if (!$isDryRun) {
|
|||
|
|
Db::table('inquiry_log')
|
|||
|
|
->where('id', $inquiryLog->id)
|
|||
|
|
->update($updateData);
|
|||
|
|
}
|
|||
|
|
$updatedCount++;
|
|||
|
|
$output->writeln("<info>已更新 inquiry_log ID {$inquiryLog->id}</info>");
|
|||
|
|
} else {
|
|||
|
|
$output->writeln("<comment>客户 ID {$customer->id} - 无需更新</comment>");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$output->writeln("<info>处理完成!</info>");
|
|||
|
|
$output->writeln("<info>总记录数: " . count($customers) . "</info>");
|
|||
|
|
$updateIdstr = implode(",", $updateId);
|
|||
|
|
$output->writeln("<info>更新记录数: {$updatedCount},更新id:$updateIdstr</info>");
|
|||
|
|
$stepstr = implode(",", $step);
|
|||
|
|
$output->writeln("<info>跳过记录数: {$skippedCount},跳过id: {$stepstr}</info>");
|
|||
|
|
|
|||
|
|
if ($isDryRun) {
|
|||
|
|
$output->writeln("<info>DRY RUN 模式 - 未实际执行任何更新操作</info>");
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
return self::SUCCESS;
|
|||
|
|
} catch (\Exception $e) {
|
|||
|
|
$output->writeln('<error>执行过程中发生错误: ' . $e->getMessage() . '</error>');
|
|||
|
|
$output->writeln('<error>错误位置: ' . $e->getFile() . ':' . $e->getLine() . '</error>');
|
|||
|
|
return self::FAILURE;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|