fix: 种菜明细每日同步不再受标签检查阻断 #1644

Open
qiuxiang wants to merge 1 commits from fix/qiuxiang-planting-daily-resilience into main
5 changed files with 47 additions and 17 deletions

View File

@ -9,7 +9,7 @@ use RuntimeException;
use Throwable;
/**
* 每日编排:先核对标签覆盖,再同步种菜报销工单,最后重算业绩明细种菜金额。
* 每日编排:先同步种菜报销工单,再核对标签覆盖,最后重算业绩明细种菜金额。
*/
class StoreReceiptPlantingDailySynchronizer
{
@ -37,15 +37,16 @@ class StoreReceiptPlantingDailySynchronizer
throw new InvalidArgumentException('公司 ID 无效。');
}
$coverage = $this->labelCoverage->assertCovered(
$fromDate->toDateString(),
$toDate->toDateString(),
$companyId,
);
$months = $this->months($months, $toDate);
$this->assertWorkOrderSourceReady();
if ($dryRun) {
$coverage = $this->labelCoverage->assertCovered(
$fromDate->toDateString(),
$toDate->toDateString(),
$companyId,
);
return [
'status' => 'ready',
'from' => $fromDate->toDateString(),
@ -65,6 +66,24 @@ class StoreReceiptPlantingDailySynchronizer
);
}
try {
$coverage = $this->labelCoverage->assertCovered(
$fromDate->toDateString(),
$toDate->toDateString(),
$companyId,
);
} catch (Throwable $exception) {
return [
'status' => 'failed',
'error' => '种菜明细已同步,但标签覆盖检查失败:' . $exception->getMessage(),
'from' => $fromDate->toDateString(),
'to' => $toDate->toDateString(),
'months' => $months,
'planting_sync' => $plantingSync,
'dry_run' => false,
];
}
return [
'status' => 'ready_to_refresh',
'from' => $fromDate->toDateString(),

View File

@ -49,6 +49,11 @@ class SyncStoreReceiptPlantingDaily extends Command
return self::FAILURE;
}
if (($result['status'] ?? '') === 'failed') {
$this->outputResult($result);
return self::FAILURE;
}
if ((bool) $this->option('dry-run')) {
$this->outputResult($result);
return self::SUCCESS;

View File

@ -223,9 +223,9 @@ CRON_TZ=Asia/Shanghai
# 聚水潭推广费用:每天汇总最近 7 天,覆盖迟到改单并供财务经营报表直接读取。
10 3 * * * root /usr/bin/timeout 1800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-promotion-expenses --days=7 --quiet >/dev/null 2>&1
# 聚水潭订单标签明细:先核对 2026-10-01 起逐日覆盖,补历史缺口,再刷新最近 7 天。
10 6 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh jst-order-labels:sync-daily --from=2026-10-01 --company=1 --quiet >/dev/null 2>&1
# 业绩明细种菜金额:每天 09:30 核查标签覆盖,同步本月/上月种菜报销并重算种菜金额。
30 9 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh store-receipt:planting-daily --from=2026-10-01 --company=1 --quiet >/dev/null 2>&1
10 6 * * * root CAIWU_BATCH_LOCK_WAIT=10800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh jst-order-labels:sync-daily --from=2026-10-01 --company=1 --quiet >> /opt/caiwuxitong/runtime/sync-logs/jst-order-labels-daily.log 2>&1
# 种菜明细与业绩种菜金额:每天 09:30 先同步本月/上月工单明细,再做标签覆盖和业绩重算。
30 9 * * * root CAIWU_BATCH_LOCK_WAIT=10800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh store-receipt:planting-daily --from=2026-10-01 --company=1 --quiet >> /opt/caiwuxitong/runtime/sync-logs/planting-daily.log 2>&1
# 商品月加权与成本:先算月加权单价,再同步商品成本,避免成本先于价格生成。
0 18 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh finance:monthly-weighted-product-cost:auto >/dev/null 2>&1
# 店铺违规信息预警:每天 05:30 把副本库抓取结果追平到统一表(本地补充字段不覆盖)。

View File

@ -227,9 +227,9 @@ CRON_TZ=Asia/Shanghai
# 聚水潭推广费用:每天汇总最近 7 天,覆盖迟到改单并供财务经营报表直接读取。
10 3 * * * root /usr/bin/timeout 1800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-promotion-expenses --days=7 --quiet >/dev/null 2>&1
# 聚水潭订单标签明细:先核对 2026-10-01 起逐日覆盖,补历史缺口,再刷新最近 7 天。
10 6 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh jst-order-labels:sync-daily --from=2026-10-01 --company=1 --quiet >/dev/null 2>&1
# 业绩明细种菜金额:每天 09:30 核查标签覆盖,同步本月/上月种菜报销并重算种菜金额。
30 9 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh store-receipt:planting-daily --from=2026-10-01 --company=1 --quiet >/dev/null 2>&1
10 6 * * * root CAIWU_BATCH_LOCK_WAIT=10800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh jst-order-labels:sync-daily --from=2026-10-01 --company=1 --quiet >> /opt/caiwuxitong/runtime/sync-logs/jst-order-labels-daily.log 2>&1
# 种菜明细与业绩种菜金额:每天 09:30 先同步本月/上月工单明细,再做标签覆盖和业绩重算。
30 9 * * * root CAIWU_BATCH_LOCK_WAIT=10800 /bin/sh /opt/caiwuxitong/run-finance-batch.sh store-receipt:planting-daily --from=2026-10-01 --company=1 --quiet >> /opt/caiwuxitong/runtime/sync-logs/planting-daily.log 2>&1
# 商品月加权与成本:先算月加权单价,再同步商品成本,避免成本先于价格生成。
0 18 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh finance:monthly-weighted-product-cost:auto >/dev/null 2>&1
# 店铺违规信息预警:每天 05:30 把副本库抓取结果追平到统一表(本地补充字段不覆盖)。

View File

@ -86,14 +86,17 @@ class StoreReceiptPlantingDailySynchronizerTest extends TestCase
$this->assertSame(['2026-10', '2026-11'], $result['months']);
}
public function test_blocks_recalculation_when_label_coverage_is_not_ready(): void
public function test_syncs_planting_details_before_label_coverage_failure_blocks_recalculation(): void
{
$coverage = Mockery::mock(JstOrderLabelDailySynchronizer::class);
$coverage->shouldReceive('assertCovered')
->once()
->andThrow(new \RuntimeException('历史数据检查未通过'));
$workOrders = Mockery::mock(PlantingWorkOrderSynchronizer::class);
$workOrders->shouldNotReceive('sync');
$workOrders->shouldReceive('sync')
->once()
->with('2026-10-01', '2026-10-31', 1)
->andReturn(['orders' => 10, 'inserted' => 10]);
$synchronizer = new StoreReceiptPlantingDailySynchronizer(
app(DatabaseManager::class),
@ -101,8 +104,11 @@ class StoreReceiptPlantingDailySynchronizerTest extends TestCase
$workOrders,
);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('历史数据检查未通过');
$synchronizer->sync('2026-10-01', '2026-10-31', 1, ['2026-10']);
$result = $synchronizer->sync('2026-10-01', '2026-10-31', 1, ['2026-10']);
$this->assertSame('failed', $result['status']);
$this->assertSame(10, $result['planting_sync']['2026-10']['inserted']);
$this->assertStringContainsString('种菜明细已同步', $result['error']);
$this->assertStringContainsString('历史数据检查未通过', $result['error']);
}
}