CaiWuXiTong/modules/EcommerceFinance/Console/SyncJushuitanShipping.php
limeng 2a1b663da2 feat(发货数据): 合并/明细双模式,还原工作平台 导出/导出明细
- 同步改为"一个发货单×每件商品一行"(明细底座);新增 pkg_count 列存"1/N"件数
- 体积=数量×长×宽×高;包裹级(实称重量/运费)仅落首件,合并求和不重复
- 仓储 paginate/exportRows 支持 mode:
  合并=按 source_id 归并(条码/商品ID逗号拼接、数量/重量/体积/金额/运费求和、长宽高取最大、其余取值);
  明细=每件一行
- 页面加"合并/明细"切换 + 两个导出(导出/导出明细) + 件数列
- 同步内存加固(1.5G、关本地查询日志、每批释放+gc),避免全量 OOM

实测:包裹199602/明细222311,多件包裹11667,补差0;
7件样例合并体积=各件求和、运费不重复、明细每行1/7。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhRy6EiUwf
2026-09-21 07:49:26 +00:00

53 lines
2.2 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 Modules\EcommerceFinance\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
/**
* 发货数据同步:从经营分析副本库(operations_replica)直读聚水潭发货明细,清洗进本地展示表。
*
* php artisan jushuitan:sync-shipping --from=2026-09-01 --to=2026-09-17 # 按发货时间窗口拉全量
* php artisan jushuitan:sync-shipping --days=3 # 滚动最近 3 天(每日调度用)
* php artisan jushuitan:sync-shipping # 最近 2000 条(快速看效果)
*
* 按 id 去重 upsert,任何时候补拉早期区间都不会重复。
*/
class SyncJushuitanShipping extends Command
{
protected $signature = 'jushuitan:sync-shipping {--from=} {--to=} {--days=} {--limit=2000}';
protected $description = '从经营分析副本库直读聚水潭发货明细,清洗写入 caiwu 发货数据表';
public function handle(): int
{
@ini_set('memory_limit', '1536M');
@set_time_limit(0);
$from = $this->option('from') ? Carbon::parse($this->option('from')) : null;
$to = $this->option('to') ? Carbon::parse($this->option('to')) : null;
// --days=N:滚动最近 N 天(含今天)。显式 --from/--to 优先。
if (! $from && ! $to && $this->option('days')) {
$days = max((int) $this->option('days'), 1);
$from = now()->startOfDay()->subDays($days - 1);
$to = now()->startOfDay()->addDay();
}
$limit = (int) ($this->option('limit') ?: 2000);
$range = ($from && $to) ? $from->toDateString() . ' ~ ' . $to->toDateString() : '最近 ' . $limit . ' 条';
$this->info('开始从副本库同步发货明细(' . $range . ')...');
try {
$result = ShippingReplicaSynchronizer::make()->sync($from, $to, $limit);
} catch (\Throwable $e) {
$this->error('同步失败:' . $e->getMessage());
return self::FAILURE;
}
$this->info('完成:写入 ' . ($result['processed'] ?? 0) . ' 条发货明细。');
return self::SUCCESS;
}
}