From b0b78c82ab514d6fc00b69c1d721c93e7cf4653e Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 4 Jun 2026 11:43:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/command/GancaoSyncLogisticsRoute.php | 72 ++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/server/app/command/GancaoSyncLogisticsRoute.php b/server/app/command/GancaoSyncLogisticsRoute.php index 5b7da05b..61cc28ca 100755 --- a/server/app/command/GancaoSyncLogisticsRoute.php +++ b/server/app/command/GancaoSyncLogisticsRoute.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace app\command; +use app\common\model\tcm\PrescriptionOrder; use app\common\service\ExpressTrackingService; use app\common\service\gancao\GancaoLogisticsRouteService; use app\common\service\gancao\GancaoScmRecipelService; @@ -24,7 +25,8 @@ use think\console\Output; * 使用方法: * php think gancao:sync-logistics 默认拉 2000 单(跳过已完成/已签收) * php think gancao:sync-logistics --limit=500 自定义拉取上限 - * php think gancao:sync-logistics --order-id=123 只跑指定订单 + * php think gancao:sync-logistics --order-id=1424 只跑指定订单(数字 id) + * php think gancao:sync-logistics --order-id=PO20260530165158645023 或 PO 业务单号 * php think gancao:sync-logistics -t SF1234567890 按快递单号走快递100 查询并落库 * php think gancao:sync-logistics --detail 打印每单详细 * @@ -42,7 +44,7 @@ class GancaoSyncLogisticsRoute extends Command $this->setName('gancao:sync-logistics') ->setDescription('同步甘草订单的物流路由(GET_TASK_ROUTE_LIST)到本地物流追踪表') ->addOption('limit', 'l', Option::VALUE_OPTIONAL, '本次最多处理多少条订单(跳过已完成/已签收)', 2000) - ->addOption('order-id', null, Option::VALUE_OPTIONAL, '只同步指定 prescription_order.id', null) + ->addOption('order-id', null, Option::VALUE_OPTIONAL, '只同步指定订单:prescription_order.id 或 PO 业务单号 order_no', null) ->addOption('tracking-number', 't', Option::VALUE_REQUIRED, '按快递单号走快递100 查询并落库') ->addOption('detail', 'd', Option::VALUE_NONE, '打印每单详细结果'); } @@ -50,8 +52,19 @@ class GancaoSyncLogisticsRoute extends Command protected function execute(Input $input, Output $output) { $limit = max(1, (int) $input->getOption('limit')); - $onlyOrderId = $input->getOption('order-id'); - $onlyOrderId = $onlyOrderId !== null ? (int) $onlyOrderId : null; + $onlyOrderId = null; + $onlyOrderNo = ''; + $orderIdArg = $input->getOption('order-id'); + if ($orderIdArg !== null && trim((string) $orderIdArg) !== '') { + $resolved = self::resolvePrescriptionOrderId((string) $orderIdArg); + if ($resolved === null) { + $output->error('未找到订单:' . trim((string) $orderIdArg) . '(--order-id 支持数字 id 或 PO 业务单号)'); + + return 1; + } + $onlyOrderId = $resolved['id']; + $onlyOrderNo = $resolved['order_no']; + } $trackingNumber = trim((string) $input->getOption('tracking-number')); $verbose = (bool) $input->getOption('detail'); @@ -73,7 +86,11 @@ class GancaoSyncLogisticsRoute extends Command if ($trackingNumber !== '') { $output->writeln('开始查询...(快递100,tracking_number=' . $trackingNumber . ')'); } else { - $output->writeln('开始同步...(limit=' . $limit . ($onlyOrderId ? ', order_id=' . $onlyOrderId : '') . ')'); + if ($onlyOrderId !== null) { + $output->writeln('开始同步...(指定单 order_id=' . $onlyOrderId . ($onlyOrderNo !== '' ? ', order_no=' . $onlyOrderNo : '') . ')'); + } else { + $output->writeln('开始同步...(limit=' . $limit . ')'); + } } try { @@ -84,10 +101,16 @@ class GancaoSyncLogisticsRoute extends Command $stats['reconcile_lines'] = []; } else { $stats = GancaoLogisticsRouteService::syncBatch($limit, $onlyOrderId); - $recon = ExpressTrackingService::reconcileAssistantReleaseForShippedPrescriptionOrders(2000); - $stats['reconcile_cleared'] = (int) ($recon['cleared'] ?? 0); - $stats['reconcile_scanned'] = (int) ($recon['scanned'] ?? 0); - $stats['reconcile_lines'] = $recon['lines'] ?? []; + if ($onlyOrderId !== null) { + $stats['reconcile_cleared'] = 0; + $stats['reconcile_scanned'] = 0; + $stats['reconcile_lines'] = []; + } else { + $recon = ExpressTrackingService::reconcileAssistantReleaseForShippedPrescriptionOrders(2000); + $stats['reconcile_cleared'] = (int) ($recon['cleared'] ?? 0); + $stats['reconcile_scanned'] = (int) ($recon['scanned'] ?? 0); + $stats['reconcile_lines'] = $recon['lines'] ?? []; + } } } catch (\Throwable $e) { $output->error('同步异常:' . $e->getMessage()); @@ -142,4 +165,35 @@ class GancaoSyncLogisticsRoute extends Command return 0; } + + /** + * @return array{id:int, order_no:string}|null + */ + private static function resolvePrescriptionOrderId(string $raw): ?array + { + $raw = trim($raw); + if ($raw === '') { + return null; + } + + $q = PrescriptionOrder::whereNull('delete_time'); + if (preg_match('/^\d+$/', $raw) === 1) { + $id = (int) $raw; + if ($id <= 0) { + return null; + } + $row = (clone $q)->where('id', $id)->field('id,order_no')->find(); + } else { + $row = (clone $q)->where('order_no', $raw)->field('id,order_no')->find(); + } + + if (!$row) { + return null; + } + + return [ + 'id' => (int) $row->id, + 'order_no' => (string) $row->order_no, + ]; + } }