From 3ce42d564ee845fee151ef2a122275f000f2fe3c Mon Sep 17 00:00:00 2001 From: chenxi Date: Thu, 26 Nov 2026 13:06:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=9A=E7=BB=A9=E6=98=8E=E7=BB=86?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=8E=E8=87=AA=E5=8A=A8=E9=87=8D=E7=AE=97?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=88=90=E6=9C=AC=20(#1448)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/SyncStoreReceiptOrderMatches.php | 39 ++++++ docs/商品成本占位行规则清单.md | 2 + .../Services/ProductCostAutoRefresh.php | 128 ++++++++++++++++++ .../ProductCostAutoRefreshTest.php | 50 +++++++ 4 files changed, 219 insertions(+) create mode 100644 modules/EcommerceFinance/Services/ProductCostAutoRefresh.php create mode 100644 tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php diff --git a/app/Console/Commands/SyncStoreReceiptOrderMatches.php b/app/Console/Commands/SyncStoreReceiptOrderMatches.php index 0c911c9f..b926a1ee 100644 --- a/app/Console/Commands/SyncStoreReceiptOrderMatches.php +++ b/app/Console/Commands/SyncStoreReceiptOrderMatches.php @@ -12,6 +12,7 @@ use App\Business\PlantingOrderLabelLookup; use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap; use Modules\EcommerceFinance\Services\AccountMappingStore; use Modules\EcommerceFinance\Services\LedgerRemarkFilter; +use Modules\EcommerceFinance\Services\ProductCostAutoRefresh; use Modules\EcommerceFinance\Services\StoreReportMirrorSynchronizer; use RuntimeException; use Throwable; @@ -226,6 +227,13 @@ class SyncStoreReceiptOrderMatches extends Command if ($result === self::SUCCESS && ! (bool) $this->option('planting-only')) { $this->refreshStoreReportPaidSales(); } + if ( + $result === self::SUCCESS + && ! (bool) $this->option('dry-run') + && (int) $this->option('limit') === 0 + ) { + $this->refreshProductCosts(); + } return $result; } @@ -1076,6 +1084,37 @@ class SyncStoreReceiptOrderMatches extends Command : sprintf(' 店铺维度报表销量汇总:业绩明细无变化,沿用 %d 行。', $result['stored_rows'])); } + /** 业绩明细写入后,按同一账期和平台自动重算商品成本。 */ + private function refreshProductCosts(): void + { + try { + $result = app(ProductCostAutoRefresh::class)->refresh( + trim((string) $this->argument('month')), + (bool) $this->option('planting-only') + ? '' + : trim((string) $this->option('platform')), + ); + } catch (Throwable $exception) { + $this->warn('商品成本自动更新失败:' . $exception->getMessage()); + + return; + } + + if ($result['status'] === 'skipped') { + return; + } + if ($result['status'] !== 'succeeded') { + $this->warn('商品成本自动更新失败,请查看日志或稍后手动重跑。'); + + return; + } + + $this->line(sprintf( + ' 商品成本自动更新:已完成 %d 个平台。', + count($result['platforms']), + )); + } + /** 只回填「种菜金额 / 标签」:按种菜明细表与聚水潭种菜标签的并集判断。 */ private function backfillPlantingAmounts(string $reportMonth): int diff --git a/docs/商品成本占位行规则清单.md b/docs/商品成本占位行规则清单.md index 11d2b3b3..37d6e914 100644 --- a/docs/商品成本占位行规则清单.md +++ b/docs/商品成本占位行规则清单.md @@ -6,6 +6,8 @@ 本清单是商品成本同步的固定校验口径。每月重跑时先完成商品月加权平均单价,再执行商品成本同步;同一规则不得在单月任务中临时改变。 +业绩明细 `store-receipt:order-match:sync` 写入成功后,会自动重算同一账期、同一平台的商品成本;种菜全平台回填会按该账期实际存在的平台逐一重算。调试用的 `--dry-run` 和 `--limit` 不触发自动重算。 + 仓库映射表新增或修改后,如需只刷新仓库简称且不改动数量、单价和成本金额,使用: ```text diff --git a/modules/EcommerceFinance/Services/ProductCostAutoRefresh.php b/modules/EcommerceFinance/Services/ProductCostAutoRefresh.php new file mode 100644 index 00000000..391bd0e8 --- /dev/null +++ b/modules/EcommerceFinance/Services/ProductCostAutoRefresh.php @@ -0,0 +1,128 @@ + 'taobao-alipay', + '淘系微信' => 'taobao-wechat', + '拼多多' => 'pinduoduo', + '得物' => 'dewu', + '京东网银钱包' => 'jd-wallet', + '京东账单' => 'jd-transactions', + '京喜账单' => 'jingxi', + '抖音' => 'douyin', + ]; + + /** @param null|Closure(list): array{success:bool,output:string} $runner */ + public function __construct(private readonly ?Closure $runner = null) + { + } + + /** + * @return array{status:string,platforms:list,results:list>} + */ + public function refresh(string $month, string $platform = ''): array + { + if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + throw new InvalidArgumentException('商品成本自动更新月份格式应为 YYYY-MM。'); + } + + $keys = $platform !== '' + ? [$platform] + : $this->platformKeysForMonth($month); + if ($keys === []) { + return ['status' => 'skipped', 'platforms' => [], 'results' => []]; + } + + $results = []; + foreach ($keys as $key) { + $command = [ + PHP_BINARY ?: 'php', + 'artisan', + 'store-receipt:product-cost:sync', + $month, + '--platform=' . $key, + '--chunk=1000', + ]; + $result = $this->run($command); + $results[] = ['platform' => $key] + $result; + if (! $result['success']) { + Log::warning('业绩明细更新后商品成本自动重算失败。', [ + 'month' => $month, + 'platform' => $key, + ]); + } + } + + return [ + 'status' => collect($results)->every(static fn (array $result): bool => $result['success']) + ? 'succeeded' + : 'failed', + 'platforms' => $keys, + 'results' => $results, + ]; + } + + /** @return list */ + private function platformKeysForMonth(string $month): array + { + $labels = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')) + ->table('store_receipt_order_match_rows') + ->where('report_month', $month . '-01') + ->whereNotNull('platform') + ->where('platform', '!=', '') + ->distinct() + ->orderBy('platform') + ->pluck('platform') + ->all(); + + $keys = []; + foreach ($labels as $label) { + $key = self::PLATFORM_KEYS[trim((string) $label)] ?? null; + if ($key !== null) { + $keys[$key] = $key; + } + } + + return array_values($keys); + } + + /** @param list $command + * @return array{success:bool,output:string} + */ + protected function run(array $command): array + { + if ($this->runner !== null) { + return ($this->runner)($command); + } + + try { + $process = new Process($command, base_path()); + $process->setTimeout(7200); + $process->run(); + + return [ + 'success' => $process->isSuccessful(), + 'output' => mb_substr( + trim($process->getOutput() . "\n" . $process->getErrorOutput()), + 0, + 4000, + ), + ]; + } catch (Throwable $exception) { + return ['success' => false, 'output' => $exception->getMessage()]; + } + } +} diff --git a/tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php b/tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php new file mode 100644 index 00000000..910668cf --- /dev/null +++ b/tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php @@ -0,0 +1,50 @@ + true, 'output' => 'ok']; + }, + ); + + $result = $service->refresh('2026-10', 'taobao-alipay'); + + $this->assertSame('succeeded', $result['status']); + $this->assertSame(['taobao-alipay'], $result['platforms']); + $this->assertCount(1, $calls); + $this->assertContains('store-receipt:product-cost:sync', $calls[0]); + $this->assertContains('2026-10', $calls[0]); + $this->assertContains('--platform=taobao-alipay', $calls[0]); + } + + public function test_it_reports_a_failed_product_cost_refresh(): void + { + $service = new ProductCostAutoRefresh( + static fn (array $command): array => ['success' => false, 'output' => 'failed'], + ); + + $result = $service->refresh('2026-10', 'pinduoduo'); + + $this->assertSame('failed', $result['status']); + $this->assertFalse($result['results'][0]['success']); + } + + public function test_it_rejects_an_invalid_month(): void + { + $this->expectException(InvalidArgumentException::class); + + (new ProductCostAutoRefresh())->refresh('2026-10-01', 'taobao-alipay'); + } +}