diff --git a/app/Console/Commands/SyncStoreReceiptOrderMatches.php b/app/Console/Commands/SyncStoreReceiptOrderMatches.php index ae2e7673..46330c8f 100644 --- a/app/Console/Commands/SyncStoreReceiptOrderMatches.php +++ b/app/Console/Commands/SyncStoreReceiptOrderMatches.php @@ -8,7 +8,6 @@ use Illuminate\Support\Facades\DB; use InvalidArgumentException; use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap; use Modules\EcommerceFinance\Services\LedgerRemarkFilter; -use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory; use Throwable; /** @@ -115,7 +114,7 @@ class SyncStoreReceiptOrderMatches extends Command private const REMARK_MULTI_PAY = '一单多笔'; - public function handle(UpstreamReadOnlyQueryFactory $factory): int + public function handle(): int { $month = trim((string) $this->argument('month')); if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { @@ -257,11 +256,7 @@ class SyncStoreReceiptOrderMatches extends Command } } - $upstream = $factory->forSource('operations_replica'); - $dwsFrom = $start->copy()->subDays(150)->toDateString(); - $dwsTo = $end->copy()->addDays(7)->toDateString(); - - /** @var array $orderAmount 订单侧按「店铺 + 核心单号」聚合的商品金额(仅 goods_count>0 的有效行) */ + /** @var array $orderAmount 订单侧按「店铺 + 核心单号」聚合的商品金额(商品金额求和) */ $orderAmount = []; /** @var array $orderSeen 订单侧出现过的「店铺 + 核心单号」(含 0 元行) */ $orderSeen = []; @@ -269,45 +264,59 @@ class SyncStoreReceiptOrderMatches extends Command $coresByCore = []; /** @var array $coreSeen 订单侧出现过的核心单号(含店铺名为空的行) */ $coreSeen = []; - $chunks = array_chunk(array_keys($cores), $chunkSize); - $totalChunks = count($chunks); + // 订单侧改用「订单原始数据」(企微客户订单 + 聚水潭订单),按线上订单号取商品金额合计。 + // 两个来源数据量大(企微 500 万单 / 690 万行),先用临时表收敛候选单号, + // 再用 STRAIGHT_JOIN 走 so_id / o_id 索引聚合,避免逐批 IN 查询退化成全表扫描。 + $mirror = DB::connection($connection); + $mirror->statement('DROP TEMPORARY TABLE IF EXISTS tmp_receipt_order_nos'); + $mirror->statement('CREATE TEMPORARY TABLE tmp_receipt_order_nos (no varchar(64) NOT NULL, PRIMARY KEY (no)) ENGINE=InnoDB'); - foreach ($chunks as $index => $chunk) { - $in = implode(',', array_map(static fn (string $no): string => "'" . str_replace("'", "''", $no) . "'", $chunk)); + $candidates = array_keys($cores); + foreach (array_chunk($candidates, 1000) as $batch) { + $mirror->table('tmp_receipt_order_nos')->insertOrIgnore( + array_map(static fn (string $no): array => ['no' => $no], $batch) + ); + } + $this->info('订单侧候选单号:' . count($candidates)); + $orderRows = []; + foreach ([ + ['ops_jst_qmcust_orders', 'ops_jst_qmcust_order_items'], + ['ops_jst_orders', 'ops_jst_order_item'], + ] as [$ordersTable, $itemsTable]) { try { - $rows = $upstream->select( - "SELECT sourceno, shop_name, sales_amount, goods_count FROM rpt.dws_sn_sales_full_info_di - WHERE sourceno IN ({$in}) AND stat_date >= '{$dwsFrom}' AND stat_date < '{$dwsTo}'" + $result = $mirror->select( + "SELECT o.so_id as so_id, o.shop_name as shop_name, SUM(i.amount) as amount + FROM tmp_receipt_order_nos t + STRAIGHT_JOIN {$ordersTable} o ON o.so_id = t.no + STRAIGHT_JOIN {$itemsTable} i ON i.o_id = o.o_id + GROUP BY o.so_id, o.shop_name" ); } catch (Throwable $exception) { - $this->error('订单侧查询失败(第 ' . ($index + 1) . ' 批):' . $exception->getMessage()); + $this->error('订单侧查询失败(' . $ordersTable . '):' . $exception->getMessage()); return self::FAILURE; } - - foreach ($rows as $row) { - $core = self::cleanOrderNo((string) $row['sourceno']); - if ($core === '') { - continue; - } - $shopKey = self::normalizeShop((string) ($row['shop_name'] ?? '')); - $key = $shopKey . '|' . $core; - $orderSeen[$key] = true; - $coreSeen[$core] = true; - if ($shopKey !== '') { - $coresByCore[$core][$shopKey] = true; - } - if ((int) ($row['goods_count'] ?? 0) <= 0) { - // 退款/冲减/0 元赠品行不计入商品金额,差额列另行体现。 - continue; - } - $orderAmount[$key] = ($orderAmount[$key] ?? 0.0) + (float) $row['sales_amount']; + foreach ($result as $row) { + $orderRows[] = (array) $row; } + } + $this->info('订单侧命中(店铺 × 订单)行数:' . count($orderRows)); - if (($index + 1) % 10 === 0 || $index + 1 === $totalChunks) { - $this->info(sprintf('订单侧进度 %d/%d 批', $index + 1, $totalChunks)); + foreach ($orderRows as $row) { + $core = self::cleanOrderNo((string) $row['so_id']); + if ($core === '') { + continue; } + $shopKey = self::normalizeShop((string) ($row['shop_name'] ?? '')); + $key = $shopKey . '|' . $core; + $orderSeen[$key] = true; + $coreSeen[$core] = true; + if ($shopKey !== '') { + $coresByCore[$core][$shopKey] = true; + } + // 口径:按「店铺 + 线上订单号」对商品金额求和(一单多品/多子订单在订单侧先合并)。 + $orderAmount[$key] = ($orderAmount[$key] ?? 0.0) + (float) $row['amount']; } // 只重建「本账期 + 本平台」的数据,避免把同月其它平台的结果一起清掉。