CaiWuXiTong/modules/EcommerceFinance/Services/Jushuitan/ShippingReplicaSynchronizer.php

950 lines
39 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\Services\Jushuitan;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
use Throwable;
/**
* 直读经营分析副本库(operations_replica)聚水潭发货明细,清洗写入 jst_oms_shipping。
* 对齐工作平台口径:明细=一个发货单×每件商品一行(导出明细);合并由查询层按包裹归并(导出)。
*
* 每行 = 一个发货单(包裹, express_details) × 其一件商品(OrderItem)。
* - source_id = 发货单 id(包裹)
* - sku_source_id = 商品明细 id(件)
* - pkg_count = '1/N'(同包裹件数)
* - 体积 volume_cm3 = 数量 × 长 × 宽 × 高(工作平台 Svolume 规则)
* - 包裹级字段(实称重量/运费)只落在首件,其余置空 —— 便于合并 SUM 不重复
* - 剔除补差虚拟单;并入自动发货仓(jst_Orders 已发货但不在发货明细表)
*/
class ShippingReplicaSynchronizer
{
private const SOURCE = 'operations_replica';
private const DATASET = 'jst.sctrade';
private const AUTOSHIP_OFFSET = 1000000000;
private const DEMO_SOURCE_IDS = [9000001, 9000002, 9000003, 9000004, 9000005, 9000006];
private const FLUSH = 300;
private const VIEW = 'rpt.Jst_SendData_View_extension';
private const OUTBOUND = 'rpt.jst_salesOutboundInfo';
private readonly \Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery $up;
private readonly \Illuminate\Database\ConnectionInterface $db;
/** @var array<int,array> */
private array $buffer = [];
/** @var array<string,array> 窗口内 dws 销售全量商品缓存 sourceno=>items */
private array $dwsCache = [];
public function __construct(UpstreamReadOnlyQueryFactory $factory)
{
$this->up = $factory->forSource(self::SOURCE);
$this->db = DB::connection((string) config('jushuitan.connection', 'jushuitan_local'));
$this->db->disableQueryLog();
}
public static function make(): self
{
return new self(app(UpstreamReadOnlyQueryFactory::class));
}
public function sync(?Carbon $from = null, ?Carbon $to = null, int $limit = 2000, ?callable $progress = null): array
{
$this->markRunning();
try {
$windowed = $from && $to;
$now = now();
$fromS = $windowed ? $from->format('Y-m-d H:i:s') : null;
$toS = $windowed ? $to->format('Y-m-d H:i:s') : null;
$this->db->table('jst_oms_shipping')->whereIn('source_id', self::DEMO_SOURCE_IDS)->delete();
if ($windowed) {
$this->db->table('jst_oms_shipping')
->where('ship_time', '>=', $fromS)->where('ship_time', '<', $toS)->delete();
}
$total = 0;
$auto = 0;
if ($windowed) {
$this->preloadDws($from, $to);
if ($progress) {
$progress('dws 预载完成:' . count($this->dwsCache) . ' 个 sourceno');
}
$total = $this->syncExpress(true, $fromS, $toS, $limit, $now, $progress);
$auto = $this->syncAutoShip($fromS, $toS, $now, $progress);
$this->dwsCache = [];
gc_collect_cycles();
$removed = $this->dedupAtPrefix();
if ($progress && $removed > 0) {
$progress('去除@重复单号:' . $removed . ' 条');
}
} else {
$total = $this->syncExpress(false, $fromS, $toS, $limit, $now, $progress);
}
$this->markSucceeded();
return ['processed' => $total, 'autoship' => $auto];
} catch (Throwable $e) {
$this->markFailed($e->getMessage());
throw $e;
}
}
/** 去除以 @ 开头、去@后已存在的重复单号(换单/重拉产生;工作平台只保留干净版)。 */
public function dedupAtPrefix(): int
{
$all = $this->db->table('jst_oms_shipping')
->whereNotNull('express_no')->where('express_no', '!=', '')
->distinct()->pluck('express_no')->all();
$set = [];
foreach ($all as $e) {
$set[(string) $e] = true;
}
$toDel = [];
foreach (array_keys($set) as $e) {
$e = (string) $e;
if ($e !== '' && $e[0] === '@' && isset($set[substr($e, 1)])) {
$toDel[] = $e;
}
}
$removed = 0;
foreach (array_chunk($toDel, 500) as $chunk) {
if ($chunk === []) {
continue;
}
$removed += (int) $this->db->table('jst_oms_shipping')->whereIn('express_no', $chunk)->delete();
}
return $removed;
}
/** 主口径:发货单(dlv 镜像) × 每件商品。 */
private function syncExpress(bool $windowed, ?string $fromS, ?string $toS, int $limit, $now, ?callable $progress = null): int
{
$batch = 1500;
$maxBatches = 5000;
$cols = 'e.id, e.so_id, e.inner_order_id, e.logistic_id, e.logistic_company_name, e.send_time, e.pay_time, '
. 'e.shop_name, e.platform_name, e.dis_co_name, e.warehouse_name, e.receiver_address, '
. 'e.weight, e.f_weight, e.buyer_message, e.order_labels, jo.l_id AS jo_lid, jo.send_date AS jo_send';
// 窗口模式:只按主键 e.id 翻页(有索引,毫秒级),send_time 窗口过滤放到 PHP,
// 避免每页都对无索引的 send_time 做全表扫描(那是"卡死"的真凶)。
$fromTs = $fromS !== null ? strtotime($fromS) : null;
$toTs = $toS !== null ? strtotime($toS) : null;
$total = 0;
$scanned = 0;
$lastId = null;
$entered = false;
for ($b = 0; $b < $maxBatches; $b++) {
$where = $windowed ? '1 = 1' : 'e.send_time IS NOT NULL';
$bind = [];
if ($lastId !== null) {
$where .= ' AND e.id < ?';
$bind[] = $lastId;
}
$sql = "SELECT TOP {$batch} {$cols} FROM rpt.jst_order_express_details e "
. 'LEFT JOIN rpt.jst_Orders jo ON jo.o_id = e.inner_order_id '
. "WHERE {$where} ORDER BY e.id DESC";
$rows = $this->up->select($sql, $bind);
if ($rows === []) {
break;
}
$cnt = count($rows);
$scanned += $cnt;
foreach ($rows as $r) {
$lastId = $r['id'];
}
$pageNewest = null;
$pageHit = false;
if ($windowed) {
$kept = [];
foreach ($rows as $r) {
$st = $r['send_time'] ?? null;
$ts = ($st !== null && $st !== '') ? strtotime((string) $st) : null;
if ($ts === null) {
$jd = $r['jo_send'] ?? null;
$ts = ($jd !== null && $jd !== '') ? strtotime((string) $jd) : null;
}
if ($ts !== null && ($pageNewest === null || $ts > $pageNewest)) {
$pageNewest = $ts;
}
if ($ts !== null && $ts >= $fromTs && $ts < $toTs) {
$kept[] = $r;
$pageHit = true;
$entered = true;
}
}
$rows = $kept;
}
$oids = [];
$soIds = [];
$logs = [];
foreach ($rows as $r) {
$so = (string) ($r['so_id'] ?? '');
if ($so !== '') {
$soIds[$so] = true;
}
$oid = (string) ($r['inner_order_id'] ?? '');
if ($oid !== '') {
$oids[$oid] = true;
}
$lg = (string) ($r['logistic_id'] ?? '');
if ($lg !== '') {
$logs[$lg] = true;
}
}
[$byOid, $bySo] = $this->loadAllItems(array_keys($oids), array_keys($soIds), ! $windowed);
$dws = [];
foreach (array_keys($soIds) as $mso) {
if (empty($bySo[$mso]) && ! empty($this->dwsCache[$mso])) {
$dws[$mso] = $this->dwsCache[$mso];
}
}
// 主口径:发货数据视图按出库单(io_id)取实发商品
$ioMap = $this->loadIoMap(array_keys($logs), array_keys($oids));
$ioIds = [];
foreach ($rows as $r) {
$io = $this->ioFor($ioMap, (string) ($r['logistic_id'] ?? ''), (string) ($r['inner_order_id'] ?? ''));
if ($io !== '') {
$ioIds[$io] = true;
}
}
$viewItems = $this->loadViewItems(array_keys($ioIds));
$skus = $this->loadSkus($this->collectSkuIds($byOid, $bySo, $dws, $viewItems));
$freight = $this->loadFreight(array_keys($soIds));
foreach ($rows as $r) {
$pkgId = (int) ($r['id'] ?? 0);
if ($pkgId === 0) {
continue;
}
if ($this->excludedByLabel($r['order_labels'] ?? null)) {
continue; // 线上发货(平台代发/云仓):工作平台不拉,对齐排除
}
$so = (string) ($r['so_id'] ?? '');
$oid = (string) ($r['inner_order_id'] ?? '');
$io = $this->ioFor($ioMap, (string) ($r['logistic_id'] ?? ''), $oid);
$items = ($io !== '' && ! empty($viewItems[$io]))
? $viewItems[$io]
: $this->itemsFor($byOid, $bySo, $dws, $oid, $so);
$tracking = $this->tracking((string) ($r['logistic_id'] ?? ''), (string) ($r['jo_lid'] ?? ''), $freight, $so);
$shipTime = $this->ts($r['send_time'] ?? null) ?? $this->ts($r['jo_send'] ?? null);
$pkg = $this->pkgFromExpress($r, $tracking, $shipTime);
$total += $this->emitPackage($pkgId, $pkg, $items, $skus, $freight, $so, $tracking, $now);
}
unset($rows, $byOid, $bySo, $dws, $skus, $freight);
if ($b % 20 === 0) {
gc_collect_cycles();
}
if ($progress && $b % 20 === 0) {
$progress(sprintf('发货:已扫描 %d 行,命中窗口 %d 件', $scanned, $total));
}
// 表远大于窗口时提前结束:已进入窗口,且本页整页都早于窗口(id≈时间倒序)。
if ($windowed && $entered && ! $pageHit && $pageNewest !== null && $fromTs !== null && $pageNewest < $fromTs) {
break;
}
if ($cnt < $batch) {
break;
}
if (! $windowed && $total >= $limit) {
break;
}
}
if ($progress) {
$progress(sprintf('发货完成:扫描 %d 行,写入 %d 件', $scanned, $total));
}
$this->flush();
return $total;
}
/** 并入自动发货仓:jst_Orders 已发货但不在发货明细表。 */
private function syncAutoShip(string $fromS, string $toS, $now, ?callable $progress = null): int
{
$batch = 400;
$maxBatches = 600;
$cols = 'o.Id AS rid, o.o_id, o.so_id, o.l_id, o.logistics_company, o.order_from, o.labels, '
. 'o.shop_name, o.send_date, o.pay_date, o.weight, o.buyer_message, '
. 'o.receiver_state, o.receiver_city, o.receiver_district, o.receiver_address, '
. 'o.receiver_name, o.receiver_mobile, o.receiver_phone';
$total = 0;
$lastRid = null;
for ($b = 0; $b < $maxBatches; $b++) {
$where = "o.status = 'Sent' AND o.send_date >= ? AND o.send_date < ? "
. 'AND NOT EXISTS (SELECT 1 FROM rpt.jst_order_express_details e WHERE e.inner_order_id = o.o_id)';
$bind = [$fromS, $toS];
if ($lastRid !== null) {
$where .= ' AND o.Id < ?';
$bind[] = $lastRid;
}
$sql = "SELECT TOP {$batch} {$cols} FROM rpt.jst_Orders o WHERE {$where} ORDER BY o.Id DESC";
$rows = $this->up->select($sql, $bind);
if ($rows === []) {
break;
}
$oids = [];
$soIds = [];
$logs = [];
foreach ($rows as $r) {
$so = (string) ($r['so_id'] ?? '');
if ($so !== '') {
$soIds[$so] = true;
}
$oid = (string) ($r['o_id'] ?? '');
if ($oid !== '') {
$oids[$oid] = true;
}
$lg = (string) ($r['l_id'] ?? '');
if ($lg !== '') {
$logs[$lg] = true;
}
$lastRid = $r['rid'];
}
[$byOid, $bySo] = $this->loadAllItems(array_keys($oids), array_keys($soIds), false);
$dws = [];
foreach (array_keys($soIds) as $mso) {
if (empty($bySo[$mso]) && ! empty($this->dwsCache[$mso])) {
$dws[$mso] = $this->dwsCache[$mso];
}
}
$ioMap = $this->loadIoMap(array_keys($logs), array_keys($oids));
$ioIds = [];
foreach ($rows as $r) {
$io = $this->ioFor($ioMap, (string) ($r['l_id'] ?? ''), (string) ($r['o_id'] ?? ''));
if ($io !== '') {
$ioIds[$io] = true;
}
}
$viewItems = $this->loadViewItems(array_keys($ioIds));
$skus = $this->loadSkus($this->collectSkuIds($byOid, $bySo, $dws, $viewItems));
$freight = $this->loadFreight(array_keys($soIds));
$cnt = count($rows);
foreach ($rows as $r) {
$oid = (string) ($r['o_id'] ?? '');
if ($oid === '') {
continue;
}
if ($this->excludedByLabel($r['labels'] ?? null)) {
continue; // 线上发货:对齐排除
}
$so = (string) ($r['so_id'] ?? '');
$io = $this->ioFor($ioMap, (string) ($r['l_id'] ?? ''), $oid);
$items = ($io !== '' && ! empty($viewItems[$io]))
? $viewItems[$io]
: $this->itemsFor($byOid, $bySo, $dws, $oid, $so);
$tracking = $this->tracking((string) ($r['l_id'] ?? ''), '', $freight, $so);
$shipTime = $this->ts($r['send_date'] ?? null);
$pkg = $this->pkgFromOrder($r, $tracking, $shipTime);
$pkgId = self::AUTOSHIP_OFFSET + (int) $oid;
$total += $this->emitPackage($pkgId, $pkg, $items, $skus, $freight, $so, $tracking, $now, true);
}
unset($rows, $byOid, $bySo, $dws, $skus, $freight);
if ($b % 30 === 0) {
gc_collect_cycles();
}
if ($cnt < $batch) {
break;
}
}
if ($progress) {
$progress(sprintf('代发仓完成:写入 %d 件', $total));
}
$this->flush();
return $total;
}
/**
* 输出一个包裹的所有明细行(每件一行;无商品则一行空商品)。
* @return int 写入行数
*/
private function emitPackage(int $pkgId, array $pkg, array $items, array $skus, array $freight, string $so, ?string $tracking, $now, bool $auto = false): int
{
$items = array_values(array_filter($items, fn ($it) => ! $this->isPricePatch($it['name'] ?? null)));
$n = count($items);
$f = $freight[$so . '|' . ($tracking ?? '')] ?? ($freight[$so . '|'] ?? []);
if ($n === 0) {
$this->push($pkgId, $pkgId, $this->buildRow($pkg, [], [], $f, true, '1/1', $now, $auto));
return 1;
}
$count = 0;
foreach ($items as $idx => $it) {
$first = ($idx === 0);
$sk = (! empty($it['sku']) && isset($skus[(string) $it['sku']])) ? $skus[(string) $it['sku']] : [];
$skuSid = (int) ($it['id'] ?? ($pkgId * 100 + $idx));
$this->push($pkgId, $skuSid, $this->buildRow($pkg, $it, $sk, $f, $first, '1/' . $n, $now, $auto));
$count++;
}
return $count;
}
/** 组装一件明细行。包裹级字段(实称重量/运费)仅首件携带。 */
private function buildRow(array $pkg, array $it, array $sk, array $f, bool $first, string $countStr, $now, bool $auto): array
{
$qty = (float) ($it['qty'] ?? 0);
$skuW = (float) (($it['weight'] ?? null) ?: ($sk['weight'] ?? 0));
$onlineW = $skuW > 0 ? round($skuW * ($qty > 0 ? $qty : 1), 4) : null;
$L = ($it['l'] ?? null) ?: ($sk['l'] ?? null);
$W = ($it['w'] ?? null) ?: ($sk['w'] ?? null);
$H = ($it['h'] ?? null) ?: ($sk['h'] ?? null);
$vol = ($it['vol'] ?? null) ? round((float) $it['vol'], 4) : (($L && $W && $H) ? round((float) $L * (float) $W * (float) $H, 4) : null);
$pkgWeight = (float) ($pkg['weight'] ?? 0) ?: null;
$freightW = (float) ($f['w'] ?? 0);
$settleW = $freightW > 0 ? $freightW : ($pkgWeight ?: $onlineW);
return $this->cleanPayload([
'express_no' => $pkg['tracking'],
'trade_no' => $pkg['so_id'],
'item_id' => $it['shopid'] ?? null,
'barcode' => $it['sku'] ?? null,
'product_name' => ($it['name'] ?? null) ?: ($sk['name'] ?? null),
'qty' => $it['qty'] ?? null,
'pkg_count' => $countStr,
'product_code' => ($it['iid'] ?? null) ?: ($sk['iid'] ?? null),
'express_name1' => $pkg['express_name1'],
'express_name2' => ($f['name'] ?? null) ?: $pkg['express_name1'],
'ship_time' => $pkg['ship_time'],
'pay_time' => $pkg['pay_time'],
'shop_name' => $pkg['shop_name'],
'platform' => $pkg['platform'],
'supplier_name' => ($it['supplier'] ?? null) ?: (($sk['supplier'] ?? null) ?: $pkg['dis_co_name']),
'warehouse_name' => $pkg['warehouse_name'] ?: ($it['warehouse'] ?? null),
'receiver_state' => $pkg['r_state'],
'receiver_city' => $pkg['r_city'],
'receiver_district' => $pkg['r_district'],
'receiver_address' => $pkg['r_address'],
'receiver_name' => $pkg['r_name'],
'receiver_mobile' => $pkg['r_mobile'],
'receiver_phone' => $pkg['r_phone'],
'online_weight' => $onlineW,
'actual_weight' => $first ? $pkgWeight : null,
'length_cm' => $L,
'width_cm' => $W,
'height_cm' => $H,
'volume_cm3' => $vol,
'weight_kg' => $first ? $settleW : null,
'settle_weight_kg' => $first ? $settleW : null,
'amount' => $it['amount'] ?? null,
'freight' => $first ? ($f['cost'] ?? null) : null,
'settle_freight' => $first ? ($f['cost'] ?? null) : null,
'buyer_memo' => $pkg['buyer_memo'],
'modified' => $pkg['ship_time'],
'synced_at' => $now,
]);
}
/** 发货明细表行 → 包裹级字段(省市区从地址拆) */
private function pkgFromExpress(array $r, ?string $tracking, ?string $shipTime): array
{
$addr = (string) ($r['receiver_address'] ?? '');
$parts = $addr !== '' ? array_values(array_filter(explode('-', $addr))) : [];
return [
'tracking' => $tracking,
'so_id' => $r['so_id'] ?? null,
'express_name1' => $r['logistic_company_name'] ?? null,
'ship_time' => $shipTime,
'pay_time' => $this->ts($r['pay_time'] ?? null),
'shop_name' => $r['shop_name'] ?? null,
'platform' => $r['platform_name'] ?? null,
'dis_co_name' => $r['dis_co_name'] ?? null,
'warehouse_name' => $r['warehouse_name'] ?? null,
'r_state' => $parts[0] ?? null,
'r_city' => $parts[1] ?? null,
'r_district' => $parts[2] ?? null,
'r_address' => $addr,
'r_name' => null,
'r_mobile' => null,
'r_phone' => null,
'buyer_memo' => $r['buyer_message'] ?? null,
'weight' => $r['weight'] ?? null,
];
}
/** jst_Orders 行(自动发货仓) → 包裹级字段 */
private function pkgFromOrder(array $r, ?string $tracking, ?string $shipTime): array
{
return [
'tracking' => $tracking,
'so_id' => $r['so_id'] ?? null,
'express_name1' => $r['logistics_company'] ?? null,
'ship_time' => $shipTime,
'pay_time' => $this->ts($r['pay_date'] ?? null),
'shop_name' => $r['shop_name'] ?? null,
'platform' => $r['order_from'] ?? null,
'dis_co_name' => null,
'warehouse_name' => null,
'r_state' => $r['receiver_state'] ?? null,
'r_city' => $r['receiver_city'] ?? null,
'r_district' => $r['receiver_district'] ?? null,
'r_address' => $r['receiver_address'] ?? '',
'r_name' => $r['receiver_name'] ?? null,
'r_mobile' => $r['receiver_mobile'] ?? null,
'r_phone' => $r['receiver_phone'] ?? null,
'buyer_memo' => $r['buyer_message'] ?? null,
'weight' => $r['weight'] ?? null,
];
}
private function tracking(string $logisticId, string $joLid, array $freight, string $so): ?string
{
if ($logisticId !== '') {
return $logisticId;
}
if ($joLid !== '') {
return $joLid;
}
$f = $freight[$so . '|'] ?? [];
$no = (string) ($f['no'] ?? '');
return $no !== '' ? $no : null;
}
/** @return array{0:array<string,array>,1:array<string,array>} [byOid, bySo] */
private function loadAllItems(array $oids, array $soIds, bool $loadBySoId = true): array
{
$byOid = [];
$bySo = [];
$sel = 'o_id, raw_so_id, sku_id, i_id, shop_i_id, name, qty, amount, send_warehouse, Id';
foreach (array_chunk($oids, 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
foreach ($this->up->select("SELECT {$sel} FROM rpt.jst_OrderItem WHERE o_id IN ({$ph}) ORDER BY Id", $chunk) as $r) {
$k = (string) ($r['o_id'] ?? '');
if ($k !== '') {
$byOid[$k][] = $this->itemArr($r);
}
}
}
// raw_so_id lacks a usable index in the production replica. Windowed sync already
// preloads DWS rows by sourceno, so querying this fallback can turn every batch into
// a full-table scan and exceed the upstream timeout. Keep it only for the small,
// non-windowed diagnostic mode where DWS is not preloaded.
if ($loadBySoId) {
foreach (array_chunk($soIds, 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
foreach ($this->up->select("SELECT {$sel} FROM rpt.jst_OrderItem WHERE raw_so_id IN ({$ph}) ORDER BY Id", $chunk) as $r) {
$k = (string) ($r['raw_so_id'] ?? '');
if ($k !== '') {
$bySo[$k][] = $this->itemArr($r);
}
}
}
}
return [$byOid, $bySo];
}
/** 开跑前按天预加载窗口内 dws 销售全量到内存,避免逐批扫表。 */
private function preloadDws(\Illuminate\Support\Carbon $from, \Illuminate\Support\Carbon $to): void
{
$this->dwsCache = [];
$seen = [];
$day = $from->copy()->subDay()->startOfDay();
$end = $to->copy();
while ($day < $end) {
$d = $day->format('Y-m-d');
$day = $day->addDay();
try {
$rows = $this->up->select("SELECT sourceno, item_id, skuoid, goods_count, sales_amount FROM rpt.dws_sn_sales_full_info_di WHERE stat_date = ?", [$d]);
} catch (Throwable) {
continue;
}
foreach ($rows as $r) {
$so = (string) ($r['sourceno'] ?? '');
$bc = (string) ($r['skuoid'] ?? '');
if ($so === '' || $bc === '') {
continue;
}
$k = $so.'|'.$bc;
if (isset($seen[$k])) {
continue;
}
$seen[$k] = true;
$this->dwsCache[$so][] = [
'id' => null, 'sku' => $bc, 'iid' => null, 'shopid' => $r['item_id'] ?? null,
'name' => null, 'qty' => $r['goods_count'] ?? null, 'amount' => $r['sales_amount'] ?? null, 'warehouse' => null,
];
}
unset($rows);
}
}
/** jst_OrderItem 缺失时的兜底:dws 销售全量按 sourceno 取商品(条码/商品ID/数量/金额)。 */
private function loadDwsItems(array $soIds): array
{
$out = [];
$seen = [];
foreach (array_chunk($soIds, 500) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
$sql = "SELECT sourceno, item_id, skuoid, goods_count, sales_amount FROM rpt.dws_sn_sales_full_info_di WHERE sourceno IN ({$ph})";
foreach ($this->up->select($sql, $chunk) as $r) {
$so = (string) ($r['sourceno'] ?? '');
$barcode = (string) ($r['skuoid'] ?? '');
if ($so === '' || $barcode === '') {
continue;
}
$k = $so.'|'.$barcode;
if (isset($seen[$k])) {
continue;
}
$seen[$k] = true;
$out[$so][] = [
'id' => null, 'sku' => $barcode, 'iid' => null, 'shopid' => $r['item_id'] ?? null,
'name' => null, 'qty' => $r['goods_count'] ?? null, 'amount' => $r['sales_amount'] ?? null, 'warehouse' => null,
];
}
}
return $out;
}
private function itemArr(array $r): array
{
return [
'id' => $r['Id'] ?? null, 'sku' => $r['sku_id'] ?? null, 'iid' => $r['i_id'] ?? null,
'shopid' => $r['shop_i_id'] ?? null, 'name' => $r['name'] ?? null,
'qty' => $r['qty'] ?? null, 'amount' => $r['amount'] ?? null, 'warehouse' => $r['send_warehouse'] ?? null,
];
}
/** 该包裹的商品:优先 o_id 精确,取不到回落 so_id。 */
private function itemsFor(array $byOid, array $bySo, array $dws, string $oid, string $so): array
{
if ($oid !== '' && ! empty($byOid[$oid])) {
return $byOid[$oid];
}
if ($so !== '' && ! empty($bySo[$so])) {
return $bySo[$so];
}
if ($so !== '' && ! empty($dws[$so])) {
return $dws[$so]; // jst_OrderItem 取不到时,用 dws 销售全量兜底
}
return [];
}
/** 快递单号/内部单号 -> 出库单 io_id(优先快递单号,精确到该次出库)。 */
private function loadIoMap(array $logisticIds, array $oIds): array
{
$byL = [];
$byO = [];
foreach (array_chunk(array_values(array_unique(array_filter($logisticIds))), 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
foreach ($this->up->select('SELECT l_id, io_id FROM ' . self::OUTBOUND . " WHERE l_id IN ({$ph})", $chunk) as $r) {
$l = (string) ($r['l_id'] ?? '');
if ($l !== '' && ! isset($byL[$l])) {
$byL[$l] = (string) ($r['io_id'] ?? '');
}
}
}
foreach (array_chunk(array_values(array_unique(array_filter($oIds))), 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
foreach ($this->up->select('SELECT o_id, io_id FROM ' . self::OUTBOUND . " WHERE o_id IN ({$ph})", $chunk) as $r) {
$o = (string) ($r['o_id'] ?? '');
if ($o !== '' && ! isset($byO[$o])) {
$byO[$o] = (string) ($r['io_id'] ?? '');
}
}
}
return ['byL' => $byL, 'byO' => $byO];
}
private function ioFor(array $ioMap, string $logisticId, string $oid): string
{
if ($logisticId !== '' && ! empty($ioMap['byL'][$logisticId])) {
return (string) $ioMap['byL'][$logisticId];
}
if ($oid !== '' && ! empty($ioMap['byO'][$oid])) {
return (string) $ioMap['byO'][$oid];
}
return '';
}
/** io_id -> 实发商品[itemArr](发货数据视图,按(io_id,条码)去重,type=0 之和优先)。 */
private function loadViewItems(array $ioIds): array
{
$agg = [];
foreach (array_chunk(array_values(array_unique(array_filter($ioIds))), 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
$sql = 'SELECT io_id, sku_ids, qty, type, sku_name, shop_i_id, wareHoursName, l, w, h, volume, weight, supplier_name FROM '
. self::VIEW . " WHERE io_id IN ({$ph})";
foreach ($this->up->select($sql, $chunk) as $r) {
$io = (string) ($r['io_id'] ?? '');
$skuIds = (string) ($r['sku_ids'] ?? '');
if ($io === '' || $skuIds === '') {
continue;
}
// 一行可能逗号拼接多个条码 —— 对齐工作平台导出明细:拆成每条码一件;
// 数量与体积按条码数均摊,保证合并求和后与整行值一致(工作平台合并体积"整箱算一次")。
$bcs = array_values(array_filter(array_map('trim', explode(',', $skuIds)), fn ($x) => $x !== ''));
if ($bcs === []) {
continue;
}
$n = count($bcs);
$isT0 = ((string) ($r['type'] ?? '') === '0');
$perQty = (float) ($r['qty'] ?? 0) / $n;
$rowVol = ($r['volume'] !== null && $r['volume'] !== '') ? (float) $r['volume'] : null;
$perVol = ($rowVol !== null) ? $rowVol / $n : null;
foreach ($bcs as $bc) {
$k = $io . '|' . mb_strtolower($bc);
if (! isset($agg[$k])) {
$agg[$k] = [
'io' => $io, 'sku' => $bc, 't0' => 0.0, 't0n' => 0, 'any' => 0.0, 'anyn' => 0,
't0vol' => 0.0, 't0voln' => 0, 'anyvol' => 0.0, 'anyvoln' => 0,
'shopid' => null, 'name' => null, 'wh' => null,
'l' => null, 'w' => null, 'h' => null,
'weight' => null, 'supplier' => null,
];
}
if ($isT0) {
$agg[$k]['t0'] += $perQty;
$agg[$k]['t0n']++;
if ($perVol !== null) {
$agg[$k]['t0vol'] += $perVol;
$agg[$k]['t0voln']++;
}
}
$agg[$k]['any'] += $perQty;
$agg[$k]['anyn']++;
if ($perVol !== null) {
$agg[$k]['anyvol'] += $perVol;
$agg[$k]['anyvoln']++;
}
// 名称/长宽高/供应商沿用整行值(共享):任意行回填一次
if (empty($agg[$k]['name']) && ! empty($r['sku_name'])) {
$agg[$k]['name'] = $r['sku_name'];
}
if (empty($agg[$k]['shopid']) && ! empty($r['shop_i_id'])) {
$agg[$k]['shopid'] = $r['shop_i_id'];
}
if (empty($agg[$k]['wh']) && ! empty($r['wareHoursName'])) {
$agg[$k]['wh'] = $r['wareHoursName'];
}
foreach (['l', 'w', 'h', 'weight'] as $ff) {
if (empty($agg[$k][$ff]) && ! empty($r[$ff])) {
$agg[$k][$ff] = $r[$ff];
}
}
if (empty($agg[$k]['supplier']) && ! empty($r['supplier_name'])) {
$agg[$k]['supplier'] = $r['supplier_name'];
}
}
}
}
$out = [];
foreach ($agg as $a) {
$qty = $a['t0n'] > 0 ? $a['t0'] : ($a['anyn'] > 0 ? $a['any'] / $a['anyn'] : 0);
$qty = (int) round($qty);
if ($qty < 1) {
$qty = 1;
}
// 体积:type=0 各条码分摊之和优先;否则取任一 type 的均值(去 type 重复)
$vol = $a['t0voln'] > 0
? round($a['t0vol'], 4)
: ($a['anyvoln'] > 0 ? round($a['anyvol'] / $a['anyvoln'], 4) : null);
$out[$a['io']][] = [
'id' => null, 'sku' => $a['sku'], 'iid' => null,
'shopid' => $a['shopid'], 'name' => $a['name'],
'qty' => $qty, 'amount' => null, 'warehouse' => $a['wh'],
'l' => $a['l'], 'w' => $a['w'], 'h' => $a['h'], 'vol' => $vol,
'weight' => $a['weight'], 'supplier' => $a['supplier'],
];
}
return $out;
}
private function collectSkuIds(array ...$maps): array
{
$ids = [];
foreach ($maps as $map) {
foreach ($map as $items) {
foreach ($items as $it) {
if (! empty($it['sku'])) {
$ids[(string) $it['sku']] = true;
}
}
}
}
return array_keys($ids);
}
private function push(int $sourceId, int $skuSourceId, array $payload): void
{
$payload['source_id'] = $sourceId;
$payload['sku_source_id'] = $skuSourceId;
$this->buffer[] = $payload;
if (count($this->buffer) >= self::FLUSH) {
$this->flush();
}
}
private function flush(): void
{
if ($this->buffer === []) {
return;
}
$this->db->table('jst_oms_shipping')->insertOrIgnore($this->buffer);
$this->buffer = [];
}
private function isPricePatch(?string $name): bool
{
if ($name === null || $name === '') {
return false;
}
return str_contains($name, '补收差价') || str_contains($name, '差价专用');
}
/** 线上发货(平台代发/云仓)标签:工作平台不拉取,caiwu 对齐排除。 */
private function excludedByLabel(?string $labels): bool
{
if ($labels === null || $labels === '') {
return false;
}
return str_contains($labels, '线上发货');
}
/** @return array<string,array> sku_id => 尺寸/重量/供应商 */
private function loadSkus(array $skuIds): array
{
$out = [];
foreach (array_chunk($skuIds, 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
foreach ($this->up->select("SELECT sku_id, l, w, h, pack_volume, weight, supplier_name, name, i_id FROM rpt.jst_sku WHERE sku_id IN ({$ph})", $chunk) as $r) {
$out[(string) $r['sku_id']] = [
'l' => $r['l'] ?? null, 'w' => $r['w'] ?? null, 'h' => $r['h'] ?? null,
'vol' => $r['pack_volume'] ?? null, 'weight' => $r['weight'] ?? null,
'supplier' => $r['supplier_name'] ?? null,
'name' => $r['name'] ?? null, 'iid' => $r['i_id'] ?? null,
];
}
}
return $out;
}
/** @return array<string,array> so_id|expressNo => 运费 (含 so_id| 兜底键) */
private function loadFreight(array $soIds): array
{
$out = [];
foreach (array_chunk($soIds, 800) as $chunk) {
if ($chunk === []) {
continue;
}
$ph = implode(',', array_fill(0, count($chunk), '?'));
$sql = 'SELECT so_id, expressNo, SUM(freightCost) AS cost, SUM(weight) AS w, '
. 'MAX(expressName) AS name, MAX(receiver_state) AS state '
. "FROM rpt.fys_jstFreightCostDetail WHERE so_id IN ({$ph}) GROUP BY so_id, expressNo";
foreach ($this->up->select($sql, $chunk) as $r) {
$expno = (string) ($r['expressNo'] ?? '');
$val = ['cost' => $r['cost'] ?? null, 'w' => $r['w'] ?? null, 'name' => $r['name'] ?? null, 'state' => $r['state'] ?? null, 'no' => $expno ?: null];
$out[((string) $r['so_id']) . '|' . $expno] = $val;
$out[((string) $r['so_id']) . '|'] ??= $val;
}
}
return $out;
}
private function cleanPayload(array $data): array
{
foreach ($data as $k => $v) {
if (is_string($v)) {
$data[$k] = $this->cleanStr($v);
}
}
return $data;
}
private function cleanStr(string $v): string
{
$v = str_replace("\0", '', $v);
$clean = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
if ($clean === false) {
$clean = mb_convert_encoding($v, 'UTF-8', 'UTF-8');
}
return $clean;
}
private function ts(mixed $v): ?string
{
if ($v === null || $v === '') {
return null;
}
try {
return Carbon::parse((string) $v)->format('Y-m-d H:i:s');
} catch (Throwable) {
return null;
}
}
private function markRunning(): void
{
$this->db->table('jst_sync_state')->updateOrInsert(
['dataset_key' => self::DATASET],
['status' => 'running', 'last_started_at' => now(), 'updated_at' => now(), 'created_at' => now()]
);
}
private function markSucceeded(): void
{
$this->db->table('jst_sync_state')->where('dataset_key', self::DATASET)->update([
'status' => 'succeeded', 'last_succeeded_at' => now(), 'last_error' => null, 'watermark' => now(), 'updated_at' => now(),
]);
}
private function markFailed(string $err): void
{
$this->db->table('jst_sync_state')->where('dataset_key', self::DATASET)->update([
'status' => 'failed', 'last_error' => mb_substr($err, 0, 1000), 'updated_at' => now(),
]);
}
}