feat(顾家过账): 增加货号品牌货号回退 (#1202)

品牌货号先按款式编码匹配,未命中再按原始货号回退;物料编号始终按原始货号匹配。
This commit is contained in:
chenxi 2026-11-16 17:25:26 +08:00
parent 01ef6181d9
commit 28e69e32ed
8 changed files with 177 additions and 90 deletions

View File

@ -96,17 +96,26 @@ class KukaPostingBasePageData
$companyId,
$decodedRows->pluck('style_code')->filter()->unique()->values()->all(),
);
$itemBrandItems = $this->repository->itemBrandItemMaps(
$companyId,
$decodedRows->pluck('item_number')->filter()->unique()->values()->all(),
);
$quotations = $this->repository->effectiveQuotations(
$companyId,
collect($maps)->pluck('material_number')->filter()->unique()->values()->all(),
);
$rows->setCollection($decodedRows->map(function (array $row) use ($maps, $brandItems, $quotations): object {
$rows->setCollection($decodedRows->map(function (array $row) use ($maps, $brandItems, $itemBrandItems, $quotations): object {
$values = $row['values'];
$materialNumber = trim((string) ($maps[$row['item_number']]->material_number ?? ''));
$quotation = $materialNumber === '' ? null : ($quotations[$materialNumber] ?? null);
$brandItemNumber = trim((string) (
$brandItems[$row['style_code']]->brand_item_number ?? ''
));
if ($brandItemNumber === '') {
$brandItemNumber = trim((string) (
$itemBrandItems[$row['item_number']]->brand_item_number ?? ''
));
}
if ($brandItemNumber !== '') {
$values['货号'] = $brandItemNumber;

View File

@ -138,6 +138,31 @@ class KukaPostingBaseRepository
->all();
}
/**
* @param list<string> $itemNumbers
* @return array<string,object>
*/
public function itemBrandItemMaps(int $companyId, array $itemNumbers): array
{
$itemNumbers = array_values(array_unique(array_filter(array_map(
static fn (mixed $value): string => trim((string) $value),
$itemNumbers,
))));
if ($itemNumbers === []
|| ! Schema::connection($this->connectionName())->hasTable('kuka_posting_item_brand_item_map_rows')) {
return [];
}
return $this->connection()
->table('kuka_posting_item_brand_item_map_rows')
->where('company_id', $companyId)
->whereIn('item_number', $itemNumbers)
->get(['item_number', 'brand_item_number'])
->filter(static fn (object $row): bool => trim((string) $row->brand_item_number) !== '')
->keyBy(static fn (object $row): string => (string) $row->item_number)
->all();
}
/**
* @param list<string> $materialNumbers
* @return array<string,object>

View File

@ -35,7 +35,7 @@ class KukaPostingMaterialMapSynchronizer
* brand_sku: int,
* brand_item_mappings: int,
* brand_sku_mappings: int,
* archive_item_mappings: int
* item_brand_mappings: int
* }
*/
public function sync(int $companyId = 1, ?string $period = null, bool $dryRun = false): array
@ -44,6 +44,7 @@ class KukaPostingMaterialMapSynchronizer
if (! Schema::connection($this->connectionName())->hasTable('kuka_posting_material_map_rows')
|| ! Schema::connection($this->connectionName())->hasTable('kuka_posting_brand_item_map_rows')
|| ! Schema::connection($this->connectionName())->hasTable('kuka_posting_item_brand_item_map_rows')
|| ! Schema::connection($this->connectionName())->hasTable('kuka_posting_base_rows')
|| ! Schema::connection($this->connectionName())->hasTable('kuka_posting_quotation_rows')) {
throw new RuntimeException('顾家物料匹配所需的数据表尚未建立,请先执行数据库迁移。');
@ -63,33 +64,43 @@ class KukaPostingMaterialMapSynchronizer
[$direct, $archive, $brandSku] = $this->sourceCandidates($items);
$rows = $this->resolveRows($companyId, $items, $quotations, $direct, $archive, $brandSku);
$stats = $this->stats($rows);
$displayItemResult = $this->displayItemRows($companyId, $identifiers['style_codes']);
$brandItemRows = $displayItemResult['rows'];
$displayItemResult = $this->displayItemRows(
$companyId,
$identifiers['style_codes'],
$items,
);
if (! $dryRun) {
$connection->transaction(function () use ($connection, $companyId, $rows, $brandItemRows): void {
$connection->transaction(function () use ($connection, $companyId, $rows, $displayItemResult): void {
$connection->table('kuka_posting_material_map_rows')
->where('company_id', $companyId)
->delete();
$connection->table('kuka_posting_brand_item_map_rows')
->where('company_id', $companyId)
->delete();
$connection->table('kuka_posting_item_brand_item_map_rows')
->where('company_id', $companyId)
->delete();
foreach (array_chunk($rows, self::INSERT_CHUNK) as $chunk) {
$connection->table('kuka_posting_material_map_rows')->insert($chunk);
}
foreach (array_chunk($brandItemRows, self::INSERT_CHUNK) as $chunk) {
foreach (array_chunk($displayItemResult['style_rows'], self::INSERT_CHUNK) as $chunk) {
$connection->table('kuka_posting_brand_item_map_rows')->insert($chunk);
}
foreach (array_chunk($displayItemResult['item_rows'], self::INSERT_CHUNK) as $chunk) {
$connection->table('kuka_posting_item_brand_item_map_rows')->insert($chunk);
}
});
$this->markState('succeeded', count($rows), null, $started);
}
return ['dry_run' => $dryRun] + $stats + [
'brand_item_mappings' => count($brandItemRows),
'brand_sku_mappings' => $displayItemResult['brand_sku'],
'archive_item_mappings' => $displayItemResult['archive_item'],
'brand_item_mappings' => count($displayItemResult['style_rows'])
+ count($displayItemResult['item_rows']),
'brand_sku_mappings' => count($displayItemResult['style_rows']),
'item_brand_mappings' => count($displayItemResult['item_rows']),
];
} catch (Throwable $exception) {
if (! $dryRun) {
@ -139,32 +150,29 @@ class KukaPostingMaterialMapSynchronizer
/**
* @param list<string> $styleCodes
* @param list<string> $itemNumbers
* @return array{
* rows:list<array<string,mixed>>,
* brand_sku:int,
* archive_item:int
* style_rows:list<array<string,mixed>>,
* item_rows:list<array<string,mixed>>
* }
*/
private function displayItemRows(int $companyId, array $styleCodes): array
private function displayItemRows(int $companyId, array $styleCodes, array $itemNumbers): array
{
if ($styleCodes === []) {
return ['rows' => [], 'brand_sku' => 0, 'archive_item' => 0];
}
$query = $this->queries->forSourceWithTimeout(self::SOURCE_KEY, 15);
$rows = [];
$styleRows = [];
$itemRows = [];
$now = now();
foreach (array_chunk($styleCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->brandItemSql(count($chunk)), $chunk) as $sourceRow) {
$styleCode = trim((string) ($sourceRow['style_code'] ?? ''));
$styleCode = trim((string) ($sourceRow['lookup_code'] ?? ''));
$brandItemNumber = trim((string) ($sourceRow['brand_item_number'] ?? ''));
if ($styleCode === '' || $brandItemNumber === '') {
continue;
}
$sourceImportedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
$styleRows[] = [
'company_id' => $companyId,
'product_code' => $styleCode,
'brand_item_number' => $brandItemNumber,
@ -174,38 +182,28 @@ class KukaPostingMaterialMapSynchronizer
}
}
$brandSkuCount = count($rows);
$brandStyleCodes = array_fill_keys(array_column($rows, 'product_code'), true);
$missingStyleCodes = array_values(array_filter(
$styleCodes,
static fn (string $styleCode): bool => ! isset($brandStyleCodes[$styleCode]),
));
$archiveItemCount = 0;
foreach (array_chunk($missingStyleCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->archiveItemSql(count($chunk)), $chunk) as $sourceRow) {
$styleCode = trim((string) ($sourceRow['style_code'] ?? ''));
$archiveItemNumber = trim((string) ($sourceRow['archive_item_number'] ?? ''));
if ($styleCode === '' || $archiveItemNumber === '') {
foreach (array_chunk($itemNumbers, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->brandItemSql(count($chunk)), $chunk) as $sourceRow) {
$itemNumber = trim((string) ($sourceRow['lookup_code'] ?? ''));
$brandItemNumber = trim((string) ($sourceRow['brand_item_number'] ?? ''));
if ($itemNumber === '' || $brandItemNumber === '') {
continue;
}
$sourceUpdatedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
$sourceImportedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$itemRows[] = [
'company_id' => $companyId,
'product_code' => $styleCode,
'brand_item_number' => $archiveItemNumber,
'source_imported_at' => $sourceUpdatedAt === '' ? null : $sourceUpdatedAt,
'item_number' => $itemNumber,
'brand_item_number' => $brandItemNumber,
'source_imported_at' => $sourceImportedAt === '' ? null : $sourceImportedAt,
'synced_at' => $now,
];
$archiveItemCount++;
}
}
return [
'rows' => $rows,
'brand_sku' => $brandSkuCount,
'archive_item' => $archiveItemCount,
'style_rows' => $styleRows,
'item_rows' => $itemRows,
];
}
@ -310,7 +308,7 @@ class KukaPostingMaterialMapSynchronizer
$placeholders = implode(', ', array_fill(0, $count, '?'));
return 'SELECT DISTINCT ON ("normalizedSku") '
. '"normalizedSku" AS style_code, '
. '"normalizedSku" AS lookup_code, '
. 'TRIM(updates->>\'brandSkuCode\') AS brand_item_number, '
. '"importedAt" AS source_imported_at '
. 'FROM public."BarcodePrintImportRecord" '
@ -320,20 +318,6 @@ class KukaPostingMaterialMapSynchronizer
. 'LIMIT 5000';
}
private function archiveItemSql(int $count): string
{
$placeholders = implode(', ', array_fill(0, $count, '?'));
return 'SELECT barcode AS style_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

@ -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(品牌 %d,档案回退 %d)。',
'物料映射%s完成:货号 %d,唯一匹配 %d,未匹配 %d,冲突 %d;直接物料编号 %d,档案条码 %d,品牌SKU %d;页面货号映射 %d(款式品牌 %d,货号品牌回退 %d)。',
$result['dry_run'] ? '校验' : '同步',
$result['items'],
$result['matched'],
@ -49,7 +49,7 @@ class SyncKukaPostingMaterialMap extends Command
$result['brand_sku'],
$result['brand_item_mappings'],
$result['brand_sku_mappings'],
$result['archive_item_mappings'],
$result['item_brand_mappings'],
));
}

View File

@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
private function connection(): string
{
return (string) config('upstream_mirrors.connection', 'business_mirror');
}
public function up(): void
{
$schema = Schema::connection($this->connection());
if ($schema->hasTable('kuka_posting_item_brand_item_map_rows')) {
return;
}
$schema->create('kuka_posting_item_brand_item_map_rows', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->unsignedBigInteger('company_id')->default(1);
$table->string('item_number', 191);
$table->string('brand_item_number', 191);
$table->dateTime('source_imported_at')->nullable();
$table->timestamp('synced_at');
$table->unique(
['company_id', 'item_number'],
'kuka_posting_item_brand_item_company_item_unique',
);
$table->index(
['company_id', 'brand_item_number'],
'kuka_posting_item_brand_item_company_brand_item',
);
});
}
public function down(): void
{
Schema::connection($this->connection())->dropIfExists('kuka_posting_item_brand_item_map_rows');
}
};

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`,取最新非空 `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`。 |
| 本次接入 | `furniture3d_prod` PostgreSQL 只读库用于顾家发货明细映射:物料编号优先使用 `public.BarcodePrintImportRecord.updates.materialNo`,其次用 `public.SupplyChainProductArchive.sku` 对应聚水潭“货号”并校验候选 `barcode`,最后按 `updates.brandSkuCode` 对应报价单物料描述前缀回退;命中后必须存在报价单生效 `MaterialNumber`。同一物料编号允许多个货号共用,只要求单个货号内部候选唯一。页面“货号”先用聚水潭“款式编码”匹配 `BarcodePrintImportRecord.normalizedSku` 并取最新非空 `updates.brandSkuCode`;款式编码未命中时再用原始“货号”匹配同一张表;两级都未命中时保留聚水潭原货号。因此同一款式编码下的多个原始货号共享款式品牌货号,个别只有货号品牌映射的记录也能回退命中。物料编号和成本始终按聚水潭原始货号计算。款式和货号的品牌紧凑结果分别写入 `kuka_posting_brand_item_map_rows`、`kuka_posting_item_brand_item_map_rows`,物料映射写入 `kuka_posting_material_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

@ -237,7 +237,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
@unlink($file);
}
public function test_display_item_uses_style_brand_and_item_material(): void
public function test_display_item_uses_style_then_item_brand_and_item_material(): void
{
$file = $this->workbook();
@ -266,6 +266,13 @@ class KukaPostingBasePageTest extends FeatureTestCase
'source_imported_at' => now(),
'synced_at' => now(),
]);
$connection->table('kuka_posting_item_brand_item_map_rows')->insert([
'company_id' => $this->company->id,
'item_number' => 'ITEM-02',
'brand_item_number' => 'BRAND-ITEM-FALLBACK-02',
'source_imported_at' => now(),
'synced_at' => now(),
]);
$connection->table('kuka_posting_material_map_rows')->insert([
'company_id' => $this->company->id,
'item_number' => 'ITEM-00',
@ -340,7 +347,16 @@ class KukaPostingBasePageTest extends FeatureTestCase
'store_name' => '京东-HX旗舰店102',
]))
->assertOk()
->assertSeeText('ITEM-02');
->assertSeeText('BRAND-ITEM-FALLBACK-02')
->assertDontSeeText('ITEM-02');
$this->get(route('business.pages.show', [
'section' => 'posting-data',
'page' => 'kuka-basic-data',
'store_name' => '京东-HX旗舰店103',
]))
->assertOk()
->assertSeeText('ITEM-03');
@unlink($file);
}

View File

@ -60,6 +60,14 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
$table->dateTime('source_imported_at')->nullable();
$table->timestamp('synced_at');
});
$schema->create('kuka_posting_item_brand_item_map_rows', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->unsignedBigInteger('company_id');
$table->string('item_number', 191);
$table->string('brand_item_number', 191);
$table->dateTime('source_imported_at')->nullable();
$table->timestamp('synced_at');
});
}
public function test_only_a_unique_active_quotation_match_is_selected(): void
@ -88,33 +96,30 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
}
$query = Mockery::mock(UpstreamReadOnlyQuery::class);
$fallbackBindings = [];
$query->shouldReceive('select')
->times(4)
->andReturnUsing(static function (string $sql, array $bindings = []) use (&$fallbackBindings): array {
if (str_contains($sql, 'AS archive_item_number')) {
$fallbackBindings[] = $bindings;
return [
[
'style_code' => 'STYLE-B',
'archive_item_number' => 'ARCHIVE-ITEM-B',
'source_imported_at' => '2026-10-03 10:00:00',
],
];
}
->andReturnUsing(static function (string $sql, array $bindings = []): array {
if (str_contains($sql, 'DISTINCT ON')) {
if (in_array('STYLE-A', $bindings, true)) {
return [
[
'lookup_code' => 'STYLE-A',
'brand_item_number' => 'BRAND-A',
'source_imported_at' => '2026-10-01 10:00:00',
],
[
'lookup_code' => 'STYLE-G',
'brand_item_number' => 'BRAND-DISPLAY-G',
'source_imported_at' => '2026-10-02 10:00:00',
],
];
}
return [
[
'style_code' => 'STYLE-A',
'brand_item_number' => 'BRAND-A',
'source_imported_at' => '2026-10-01 10:00:00',
],
[
'style_code' => 'STYLE-G',
'brand_item_number' => 'BRAND-DISPLAY-G',
'source_imported_at' => '2026-10-02 10:00:00',
'lookup_code' => 'ITEM-B',
'brand_item_number' => 'BRAND-ITEM-B',
'source_imported_at' => '2026-10-03 10:00:00',
],
];
}
@ -158,7 +163,7 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
'brand_sku' => 1,
'brand_item_mappings' => 3,
'brand_sku_mappings' => 2,
'archive_item_mappings' => 1,
'item_brand_mappings' => 1,
], $result);
$rows = $connection->table('kuka_posting_material_map_rows')
@ -184,12 +189,15 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
->all();
$this->assertSame([
'STYLE-A' => 'BRAND-A',
'STYLE-B' => 'ARCHIVE-ITEM-B',
'STYLE-G' => 'BRAND-DISPLAY-G',
], $brandItems);
$this->assertCount(1, $fallbackBindings);
$this->assertContains('STYLE-B', $fallbackBindings[0]);
$this->assertNotContains('STYLE-A', $fallbackBindings[0]);
$this->assertNotContains('STYLE-G', $fallbackBindings[0]);
$itemBrandItems = $connection->table('kuka_posting_item_brand_item_map_rows')
->orderBy('item_number')
->pluck('brand_item_number', 'item_number')
->all();
$this->assertSame([
'ITEM-B' => 'BRAND-ITEM-B',
], $itemBrandItems);
}
}