This commit is contained in:
Your Name
2026-04-07 18:13:03 +08:00
parent a780356908
commit fdf714f833
397 changed files with 15086 additions and 1043 deletions
+115
View File
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace app\command;
use app\common\service\ExpressTrackingService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
/**
* 同步现有订单快递单号到物流追踪表
*
* 使用方法:
* php think express:sync
*/
class SyncTrackingNumbers extends Command
{
protected function configure()
{
$this->setName('express:sync')
->setDescription('同步现有订单快递单号到物流追踪表');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('开始同步现有订单快递单号...');
$startTime = microtime(true);
try {
// 查询所有有快递单号的订单
$orders = Db::name('tcm_prescription_order')
->where('tracking_number', '<>', '')
->whereNull('delete_time')
->field([
'id',
'tracking_number',
'express_company',
'recipient_name',
'recipient_phone',
'shipping_address',
])
->select()
->toArray();
$total = count($orders);
$success = 0;
$skipped = 0;
$failed = 0;
$output->writeln("找到 {$total} 个有快递单号的订单");
foreach ($orders as $order) {
try {
// 检查是否已存在
$exists = Db::name('express_tracking')
->where('tracking_number', $order['tracking_number'])
->whereNull('delete_time')
->count();
if ($exists > 0) {
$skipped++;
$output->writeln("跳过: {$order['tracking_number']} (已存在)");
continue;
}
// 创建追踪记录
$result = ExpressTrackingService::createOrUpdate([
'order_id' => $order['id'],
'order_type' => 'prescription',
'tracking_number' => $order['tracking_number'],
'express_company' => $order['express_company'] ?: 'auto',
'recipient_phone' => $order['recipient_phone'],
'recipient_name' => $order['recipient_name'],
'recipient_address' => $order['shipping_address'],
]);
if ($result) {
$success++;
$output->writeln("成功: {$order['tracking_number']}");
} else {
$failed++;
$output->writeln("失败: {$order['tracking_number']}");
}
} catch (\Throwable $e) {
$failed++;
$output->error("错误: {$order['tracking_number']} - {$e->getMessage()}");
}
}
$duration = round(microtime(true) - $startTime, 2);
$output->writeln('');
$output->writeln('========================================');
$output->writeln('同步完成!');
$output->writeln('========================================');
$output->writeln("总数: {$total}");
$output->writeln("成功: {$success}");
$output->writeln("跳过: {$skipped}");
$output->writeln("失败: {$failed}");
$output->writeln("耗时: {$duration}");
$output->writeln('');
$output->writeln('现在可以运行定时任务测试:');
$output->writeln(' php think express:auto-update');
return 0;
} catch (\Throwable $e) {
$output->error("同步失败: " . $e->getMessage());
return 1;
}
}
}