fix(顾家过账): 品牌货号缺失时匹配档案货号 (#1191)

商品编码优先匹配品牌货号;缺失时按供应商档案条码回退货号;两级都缺失时保留聚水潭原货号。
This commit is contained in:
chenxi 2026-11-16 11:16:38 +08:00
parent 5cf5a9bd5b
commit 861bc4db71
6 changed files with 99 additions and 43 deletions

View File

@ -84,16 +84,9 @@ class KukaPostingBasePageData
$companyId,
$decodedRows->pluck('item_number')->filter()->unique()->values()->all(),
);
$brandCodes = $decodedRows
->pluck('product_code')
->merge($decodedRows->pluck('item_number'))
->filter()
->unique()
->values()
->all();
$brandItems = $this->repository->brandItemMaps(
$companyId,
$brandCodes,
$decodedRows->pluck('product_code')->filter()->unique()->values()->all(),
);
$quotations = $this->repository->effectiveQuotations(
$companyId,
@ -106,11 +99,6 @@ class KukaPostingBasePageData
$brandItemNumber = trim((string) (
$brandItems[$row['product_code']]->brand_item_number ?? ''
));
if ($brandItemNumber === '') {
$brandItemNumber = trim((string) (
$brandItems[$row['item_number']]->brand_item_number ?? ''
));
}
if ($brandItemNumber !== '') {
$values['货号'] = $brandItemNumber;

View File

@ -33,7 +33,9 @@ class KukaPostingMaterialMapSynchronizer
* direct: int,
* archive: int,
* brand_sku: int,
* brand_item_mappings: int
* brand_item_mappings: int,
* brand_sku_mappings: int,
* archive_item_mappings: int
* }
*/
public function sync(int $companyId = 1, ?string $period = null, bool $dryRun = false): array
@ -61,10 +63,8 @@ class KukaPostingMaterialMapSynchronizer
[$direct, $archive, $brandSku] = $this->sourceCandidates($items);
$rows = $this->resolveRows($companyId, $items, $quotations, $direct, $archive, $brandSku);
$stats = $this->stats($rows);
// normalizedSku 同时覆盖聚水潭商品编码和原始货号,按唯一查询键一次同步。
$brandCodes = array_values(array_unique(array_merge($items, $identifiers['product_codes'])));
sort($brandCodes, SORT_STRING);
$brandItemRows = $this->brandItemRows($companyId, $brandCodes);
$displayItemResult = $this->displayItemRows($companyId, $identifiers['product_codes']);
$brandItemRows = $displayItemResult['rows'];
if (! $dryRun) {
$connection->transaction(function () use ($connection, $companyId, $rows, $brandItemRows): void {
@ -88,6 +88,8 @@ class KukaPostingMaterialMapSynchronizer
return ['dry_run' => $dryRun] + $stats + [
'brand_item_mappings' => count($brandItemRows),
'brand_sku_mappings' => $displayItemResult['brand_sku'],
'archive_item_mappings' => $displayItemResult['archive_item'],
];
} catch (Throwable $exception) {
if (! $dryRun) {
@ -136,31 +138,35 @@ class KukaPostingMaterialMapSynchronizer
}
/**
* @param list<string> $brandCodes
* @return list<array<string,mixed>>
* @param list<string> $productCodes
* @return array{
* rows:list<array<string,mixed>>,
* brand_sku:int,
* archive_item:int
* }
*/
private function brandItemRows(int $companyId, array $brandCodes): array
private function displayItemRows(int $companyId, array $productCodes): array
{
if ($brandCodes === []) {
return [];
if ($productCodes === []) {
return ['rows' => [], 'brand_sku' => 0, 'archive_item' => 0];
}
$query = $this->queries->forSourceWithTimeout(self::SOURCE_KEY, 15);
$rows = [];
$now = now();
foreach (array_chunk($brandCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach (array_chunk($productCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->brandItemSql(count($chunk)), $chunk) as $sourceRow) {
$brandCode = trim((string) ($sourceRow['product_code'] ?? ''));
$productCode = trim((string) ($sourceRow['product_code'] ?? ''));
$brandItemNumber = trim((string) ($sourceRow['brand_item_number'] ?? ''));
if ($brandCode === '' || $brandItemNumber === '') {
if ($productCode === '' || $brandItemNumber === '') {
continue;
}
$sourceImportedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
'company_id' => $companyId,
'product_code' => $brandCode,
'product_code' => $productCode,
'brand_item_number' => $brandItemNumber,
'source_imported_at' => $sourceImportedAt === '' ? null : $sourceImportedAt,
'synced_at' => $now,
@ -168,7 +174,39 @@ class KukaPostingMaterialMapSynchronizer
}
}
return $rows;
$brandSkuCount = count($rows);
$brandProductCodes = array_fill_keys(array_column($rows, 'product_code'), true);
$missingProductCodes = array_values(array_filter(
$productCodes,
static fn (string $productCode): bool => ! isset($brandProductCodes[$productCode]),
));
$archiveItemCount = 0;
foreach (array_chunk($missingProductCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->archiveItemSql(count($chunk)), $chunk) as $sourceRow) {
$productCode = trim((string) ($sourceRow['product_code'] ?? ''));
$archiveItemNumber = trim((string) ($sourceRow['archive_item_number'] ?? ''));
if ($productCode === '' || $archiveItemNumber === '') {
continue;
}
$sourceUpdatedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
'company_id' => $companyId,
'product_code' => $productCode,
'brand_item_number' => $archiveItemNumber,
'source_imported_at' => $sourceUpdatedAt === '' ? null : $sourceUpdatedAt,
'synced_at' => $now,
];
$archiveItemCount++;
}
}
return [
'rows' => $rows,
'brand_sku' => $brandSkuCount,
'archive_item' => $archiveItemCount,
];
}
/** @return array<string,string> */
@ -282,6 +320,20 @@ class KukaPostingMaterialMapSynchronizer
. 'LIMIT 5000';
}
private function archiveItemSql(int $count): string
{
$placeholders = implode(', ', array_fill(0, $count, '?'));
return 'SELECT barcode AS product_code, '
. 'TRIM(sku) AS archive_item_number, '
. 'COALESCE("updatedAt", "createdAt") AS source_imported_at '
. 'FROM public."SupplyChainProductArchive" '
. 'WHERE barcode IN (' . $placeholders . ') '
. 'AND COALESCE(TRIM(sku), \'\') <> \'\' '
. 'ORDER BY barcode '
. 'LIMIT 5000';
}
/**
* @param list<string> $items
* @param array<string,true> $quoteMaterials

View File

@ -14,7 +14,7 @@ class SyncKukaPostingMaterialMap extends Command
{--dry-run : 只读取和校验,不覆盖镜像}
{--json : 输出 JSON}';
protected $description = '按货号从家具产品开发数据库匹配唯一物料编号';
protected $description = '按货号匹配物料编号,并按商品编码匹配品牌货号及货号回退';
public function handle(KukaPostingMaterialMapSynchronizer $synchronizer): int
{
@ -38,7 +38,7 @@ class SyncKukaPostingMaterialMap extends Command
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
} else {
$this->info(sprintf(
'物料映射%s完成:货号 %d,唯一匹配 %d,未匹配 %d,冲突 %d;直接物料编号 %d,档案条码 %d,品牌SKU %d;商品编码/货号品牌映射 %d。',
'物料映射%s完成:货号 %d,唯一匹配 %d,未匹配 %d,冲突 %d;直接物料编号 %d,档案条码 %d,品牌SKU %d;页面货号映射 %d(品牌 %d,档案回退 %d)。',
$result['dry_run'] ? '校验' : '同步',
$result['items'],
$result['matched'],
@ -48,6 +48,8 @@ class SyncKukaPostingMaterialMap extends Command
$result['archive'],
$result['brand_sku'],
$result['brand_item_mappings'],
$result['brand_sku_mappings'],
$result['archive_item_mappings'],
));
}

View File

@ -17,7 +17,7 @@
| 状态 | 说明 |
| --- | --- |
| 本次接入 | 顾家过账基础数据读取聚水潭“销售主题分析(新版)→ 商品利润 → 明细(订单商品)”10 月导出,仅保留天猫、京东、拼多多和抖音的 12 家 HX 旗舰店,写入 `kuka_posting_base_rows`;导入摘要写入 `kuka_posting_base_imports`。只保存前 40 个聚水潭原始字段,后续成本映射、报价和过账分类不混入基础数据。XLSX 通过 `business-mirror:import-kuka-posting-base` 流式读取,校验 12 家店铺均有数据后,在单一事务内整月替换;文件缺失、字段错误或中断时保留上一版。当前 2026 年 10 月导出为 13,949 行。 |
| 本次接入 | `furniture3d_prod` PostgreSQL 只读库用于顾家发货明细映射:物料编号优先使用 `public.BarcodePrintImportRecord.updates.materialNo`,其次用 `public.SupplyChainProductArchive.sku` 对应聚水潭“货号”并校验候选 `barcode`,最后按 `updates.brandSkuCode` 对应报价单物料描述前缀回退;命中后必须存在报价单生效 `MaterialNumber`。同一物料编号允许多个货号共用,只要求单个货号内部候选唯一。页面“货号”先用聚水潭“商品编码”对应 `BarcodePrintImportRecord.normalizedSku`,未命中时再用原始“货号”对应 `normalizedSku`,统一取最新非空 `updates.brandSkuCode` 作为品牌货号;两级都未命中时保留聚水潭原货号,物料编号和成本仍按原货号计算。紧凑结果分别写入 `kuka_posting_material_map_rows` 和 `kuka_posting_brand_item_map_rows`,不复制整库、不在页面直连上游;无命中或仍有多个报价候选时保留空值,不猜测。每日 05:50 执行 `business-mirror:sync-kuka-posting-material-map`。 |
| 本次接入 | `furniture3d_prod` PostgreSQL 只读库用于顾家发货明细映射:物料编号优先使用 `public.BarcodePrintImportRecord.updates.materialNo`,其次用 `public.SupplyChainProductArchive.sku` 对应聚水潭“货号”并校验候选 `barcode`,最后按 `updates.brandSkuCode` 对应报价单物料描述前缀回退;命中后必须存在报价单生效 `MaterialNumber`。同一物料编号允许多个货号共用,只要求单个货号内部候选唯一。页面“货号”先按聚水潭“商品编码”匹配 `BarcodePrintImportRecord.normalizedSku`,取最新非空 `updates.brandSkuCode` 作为品牌货号;没有品牌货号时按商品编码匹配 `SupplyChainProductArchive.barcode`,取 `sku` 作为货号;两级都未命中时保留聚水潭原货号。物料编号和成本仍按原货号计算。紧凑结果分别写入 `kuka_posting_material_map_rows` 和 `kuka_posting_brand_item_map_rows`,不复制整库、不在页面直连上游;无命中或仍有多个报价候选时保留空值,不猜测。每日 05:50 执行 `business-mirror:sync-kuka-posting-material-map`。 |
| 本次接入 | 钉钉考勤 `attendance/list` 按财务中心在职成员同步到 `ding_attendance_records`,保留打卡记录 ID、用户、工作日期、上下班类型、打卡结果、审批标识、审批实例和打卡时间;每 15 分钟回看 35 天,按 7 天窗口和 50 人上限分页。钉钉排班通过 `getScheduleByRange` 同步到 `ding_attendance_schedule_records`,保留逐日排班 ID、用户、考勤组、班次、是否休息、变更状态和完整源数据;本机脚本为 `scripts/sync-ding-attendance-schedules.py`,导入命令为 `business-mirror:import-ding-attendance-schedules`,默认覆盖最近 95 天,查询不完整时拒绝覆盖旧镜像。日志漏写扣分只统计钉钉排班明确为工作日的日期,并继续排除没有日志但有“请假 / 休息 / 调休”等审批标记的日期;排班或考勤镜像未完成时扣分暂不计算,周五、周末或法定班次调整均以公司真实排班为准。依赖权限 `qyapi_attendance_isv_query_result`、`qyapi_get_attendance_data`。 |
| 已完成 | `operations_replica/rpt.Bulk_Price_Limit` 已进入类型化表 `product_price_mirror_rows`,商品价格页面已读取镜像并支持完整分页。 |
| 已完成 | `operations_replica/rpt.jst_sku` 已进入 `ops_jst_sku`,普通商品资料页面固定读取 `sku_type = normal` 的共享镜像,并支持稳定分页和业务筛选。 |

View File

@ -180,7 +180,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
@unlink($file);
}
public function test_brand_item_number_uses_product_code_then_item_number(): void
public function test_display_item_uses_brand_then_archive_then_original_item(): void
{
$file = $this->workbook();
@ -200,8 +200,8 @@ class KukaPostingBasePageTest extends FeatureTestCase
]);
$connection->table('kuka_posting_brand_item_map_rows')->insert([
'company_id' => $this->company->id,
'product_code' => 'ITEM-01',
'brand_item_number' => 'BRAND-FALLBACK-01',
'product_code' => 'SKU-01',
'brand_item_number' => 'ARCHIVE-FALLBACK-01',
'source_imported_at' => now(),
'synced_at' => now(),
]);
@ -246,7 +246,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
'store_name' => '京东-HX旗舰店101',
]))
->assertOk()
->assertSeeText('BRAND-FALLBACK-01')
->assertSeeText('ARCHIVE-FALLBACK-01')
->assertDontSeeText('ITEM-01');
$this->get(route('business.pages.show', [

View File

@ -87,9 +87,22 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
}
$query = Mockery::mock(UpstreamReadOnlyQuery::class);
$fallbackBindings = [];
$query->shouldReceive('select')
->times(3)
->andReturnUsing(static function (string $sql): array {
->times(4)
->andReturnUsing(static function (string $sql, array $bindings = []) use (&$fallbackBindings): array {
if (str_contains($sql, 'AS archive_item_number')) {
$fallbackBindings[] = $bindings;
return [
[
'product_code' => 'PRODUCT-B',
'archive_item_number' => 'ARCHIVE-ITEM-B',
'source_imported_at' => '2026-10-03 10:00:00',
],
];
}
if (str_contains($sql, 'DISTINCT ON')) {
return [
[
@ -102,11 +115,6 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
'brand_item_number' => 'BRAND-DISPLAY-G',
'source_imported_at' => '2026-10-02 10:00:00',
],
[
'product_code' => 'ITEM-B',
'brand_item_number' => 'BRAND-FALLBACK-B',
'source_imported_at' => '2026-10-03 10:00:00',
],
];
}
@ -148,6 +156,8 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
'archive' => 1,
'brand_sku' => 1,
'brand_item_mappings' => 3,
'brand_sku_mappings' => 2,
'archive_item_mappings' => 1,
], $result);
$rows = $connection->table('kuka_posting_material_map_rows')
@ -172,9 +182,13 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
->pluck('brand_item_number', 'product_code')
->all();
$this->assertSame([
'ITEM-B' => 'BRAND-FALLBACK-B',
'PRODUCT-A' => 'BRAND-A',
'PRODUCT-B' => 'ARCHIVE-ITEM-B',
'PRODUCT-G' => 'BRAND-DISPLAY-G',
], $brandItems);
$this->assertCount(1, $fallbackBindings);
$this->assertContains('PRODUCT-B', $fallbackBindings[0]);
$this->assertNotContains('PRODUCT-A', $fallbackBindings[0]);
$this->assertNotContains('PRODUCT-G', $fallbackBindings[0]);
}
}