新增
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* 物流自动更新定时任务
|
||||
*
|
||||
* 使用方法:
|
||||
* php think express:auto-update
|
||||
*
|
||||
* 配置crontab(每10分钟执行一次):
|
||||
* 0,10,20,30,40,50 * * * * cd /path/to/server && php think express:auto-update >> /dev/null 2>&1
|
||||
*/
|
||||
class ExpressAutoUpdate extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('express:auto-update')
|
||||
->setDescription('自动更新物流追踪信息');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始自动更新物流信息...');
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
try {
|
||||
$result = ExpressTrackingService::autoUpdateBatch(50);
|
||||
|
||||
$duration = round(microtime(true) - $startTime, 2);
|
||||
|
||||
$output->writeln("更新完成!");
|
||||
$output->writeln("总数: {$result['total']}");
|
||||
$output->writeln("成功: {$result['success']}");
|
||||
$output->writeln("失败: {$result['failed']}");
|
||||
$output->writeln("耗时: {$duration}秒");
|
||||
|
||||
return 0;
|
||||
} catch (\Throwable $e) {
|
||||
$output->error("更新失败: " . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user