CaiWuXiTong/app/Console/Commands/SyncDingOnlineDaifaPrice.php
limeng 7a6fc997f3 perf(线上代发价格): 成本核查改为全量预计算缓存,页面打开不再现算 (#862)
#844 已去掉整表合并;但成本核查仍按“筛选条件+同步时间”缓存 10 分钟,生产 cron 每轮同步都会刷新同步时间,
等于每次打开页面都要把全部钉钉行逐批关联聚水潭订单重算一遍,页面依旧打不开。
- 核查结果按数据指纹(行数/最新创建时间/金额合计)缓存全量结果,最长 6 小时;筛选只取行键再查缓存
- 数据变化时先用旧结果,响应结束后后台重算(加锁防并发)
- 每日同步命令完成后预计算成本核查
涉及:只读查询与缓存;不涉及迁移、权限和上游写入。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VANWYsNUbu2piMtCKymoyd
2026-11-04 15:16:06 +08:00

61 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\EcommerceFinance\Repositories\DingOnlineDaifaPriceMirrorRepository;
use Modules\EcommerceFinance\Services\DingPayableWorkflowMirrorSynchronizer;
use Throwable;
class SyncDingOnlineDaifaPrice extends Command
{
protected $signature = 'business-mirror:sync-ding-online-daifa-price
{--full : 执行或继续首次全量同步}
{--batch= : 每批流程数量}
{--max-batches= : 本轮最多批次数,0 表示直到完成}';
protected $description = '同步钉钉线上代发业务报销流程到共享业务镜像库';
public function handle(DingPayableWorkflowMirrorSynchronizer $synchronizer): int
{
if (! (bool) config('upstream_mirrors.refresh_enabled', false) && ! $this->option('full')) {
return self::SUCCESS;
}
try {
$result = $synchronizer
->forConfigKey('ding_online_daifa_price')
->sync(
(bool) $this->option('full'),
$this->option('batch') === null ? null : (int) $this->option('batch'),
$this->option('max-batches') === null ? null : (int) $this->option('max-batches'),
);
} catch (Throwable $exception) {
Log::error('钉钉线上代发价格流程镜像同步失败。', ['exception' => $exception::class]);
$this->error('钉钉线上代发价格流程同步失败,已保留上一版共享数据。');
return self::FAILURE;
}
$this->info(sprintf(
'%s:本轮 %d 批,累计处理 %d 条明细。',
$result['status'] === 'succeeded' ? '同步完成' : '同步进行中',
$result['batches'],
$result['rows'],
));
// 同步完成后预先算好成本核查结果,页面打开时直接读缓存。
if ($result['status'] === 'succeeded') {
try {
$this->info(sprintf('成本核查已预计算:%d 行。', app(DingOnlineDaifaPriceMirrorRepository::class)->warmAuditCache()));
} catch (Throwable $exception) {
report($exception);
$this->warn('成本核查预计算失败,页面打开时会重新计算。');
}
}
return self::SUCCESS;
}
}