feat: 接入工作平台采购价格到普通商品资料

This commit is contained in:
limeng 2026-09-09 07:09:29 +08:00
parent e1d62f0d79
commit 99abdea2b2
11 changed files with 367 additions and 13 deletions

View File

@ -22,6 +22,7 @@ use Illuminate\Routing\Controller;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Carbon;
use Illuminate\Database\QueryException;
use Modules\EcommerceFinance\Exceptions\UpstreamSnapshotPendingException;
use Modules\EcommerceFinance\Repositories\AlibabaBillRepository;
@ -1777,6 +1778,11 @@ class Pages extends Controller
'initialized' => false,
'last_succeeded_at' => null,
],
'productCatalogWorkPlatformPriceSyncStatus' => $result['work_platform_price_sync_status'] ?? [
'code' => 'unavailable',
'initialized' => false,
'last_succeeded_at' => null,
],
];
} catch (Throwable $exception) {
report($exception);
@ -1808,7 +1814,8 @@ class Pages extends Controller
fputcsv($csv, [
'ID', '款式编码', '商品编码', '商品名称', '商品简称', '规格 / 颜色',
'品牌', '类目', '供应商', '销售价', '成本价', '普通商品采购价',
'1688代发单价', '数据来源', '市场价', '商品状态', '库存同步', '业务更新时间',
'工作平台价格', '工作平台价格生效日期', '1688代发单价',
'数据来源', '市场价', '商品状态', '库存同步', '业务更新时间',
]);
foreach ($this->productCatalog->exportRows($filters) as $row) {
@ -1835,6 +1842,8 @@ class Pages extends Controller
$row->sale_price,
$row->cost_price,
$row->purchase_price,
$row->work_platform_price ?? null,
$this->workPlatformPriceDate($row->work_platform_price_effect_time ?? null),
$row->purchase_order_unit_price,
implode(';', $row->data_sources),
$row->market_price,
@ -1851,6 +1860,19 @@ class Pages extends Controller
]);
}
private function workPlatformPriceDate(mixed $value): string
{
if (! is_string($value) || trim($value) === '') {
return '';
}
try {
return Carbon::parse($value)->format('Y-m-d');
} catch (Throwable) {
return '';
}
}
public function exportCourierInformation(Request $request): StreamedResponse
{
abort_unless($this->courierInformation->available() && $this->courierInformation->ready(), 503);
@ -1979,6 +2001,11 @@ class Pages extends Controller
'initialized' => false,
'last_succeeded_at' => null,
],
'productCatalogWorkPlatformPriceSyncStatus' => [
'code' => 'unavailable',
'initialized' => false,
'last_succeeded_at' => null,
],
];
}

View File

@ -48,6 +48,7 @@ return [
'operations_replica.rpt.fys_shopInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '店铺资料/店铺报表'],
'operations_replica.rpt.fys_TrademarkInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '商标资料'],
'operations_replica.rpt.jst_sku' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '商品资料/售后费用'],
'jiansun.dbo.pm_priceAdjustment' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '普通商品资料/工作平台价格'],
'operations_replica.rpt.jst_warehouseInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '售后费用'],
'operations_replica.rpt.jst_shopInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '预留聚水潭店铺维表'],

View File

@ -75,6 +75,12 @@ return [
'stale_after_minutes' => (int) env('COURIER_INFORMATION_MIRROR_STALE_MINUTES', 60),
],
// 工作平台采购价格按 SKU 补充到普通商品资料;页面只读取共享镜像。
'work_platform_prices' => [
'dataset_key' => 'jiansun.dbo.pm_priceAdjustment',
'target_table' => 'js_pm_price_adjustment',
],
/*
|--------------------------------------------------------------------------
| 上游原表镜像目录
@ -365,6 +371,21 @@ return [
'jiansun.dbo.fys_supplierGoods_sttlement_purchase' => ['source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'fys_supplierGoods_sttlement_purchase', 'target' => 'js_supplier_goods_purchase', 'keys' => ['Id'], 'watermark' => 'createTime'],
'jiansun.dbo.fys_supplierGoods_sttlement_sale' => ['source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'fys_supplierGoods_sttlement_sale', 'target' => 'js_supplier_goods_sale', 'keys' => ['Id'], 'watermark' => 'createTime'],
'jiansun.dbo.pm_purchaseIn' => ['source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'pm_purchaseIn', 'target' => 'js_pm_purchase_in', 'keys' => ['id'], 'watermark' => 'updateTime'],
// 工作平台采购价格:skuCode 对应 ops_jst_sku.sku_id。源表没有可靠更新时间,
// 以 Id 稳定追加,页面按 effectTime 取当前已生效的最新价格。
'jiansun.dbo.pm_priceAdjustment' => [
'source_key' => 'jiansun',
'schema' => 'dbo',
'object' => 'pm_priceAdjustment',
'target' => 'js_pm_price_adjustment',
'keys' => ['Id'],
'watermark' => null,
'columns' => ['Id', 'skuCode', 'purchasePrice', 'effectTime'],
'indexes' => [
['skuCode(64)', 'effectTime', 'Id'],
['effectTime', 'Id'],
],
],
// 工作平台 2.0:快递资料及其网点、打款账户。页面只读取共享镜像,
// 首次使用前需由刷新节点显式执行对应数据集的 --full 同步。

View File

@ -22,6 +22,7 @@
| 中央同步接入 | 店铺维度报表从 `jiansun.dbo.jst_originalBillInfo` 和 `dbo.fys_shopInfo_flow` 自动、可断点同步;不再依赖开发者电脑、SQLite 或人工 XLSX。 |
| 已登记,待首次同步 | `operations_replica/rpt.jst_originalBillInfo` 登记为通用原始账单镜像 `ops_jst_original_bill_info`;保留当前 19 个业务字段,按 `Id` 断点全量、按 `updateTime` 重叠增量。部署迁移只创建空表,不自动启动约 1400 万行的首次同步。 |
| 已登记,待首次同步 | 店铺收款账单的淘系支付宝账户已登记 `jiansun.dbo.alipay_Bill_Detail`,页面按共享镜像分页、筛选和汇总;首次全量完成前显示同步中状态。 |
| 本次接入 | 普通商品资料的工作平台采购价格接入 `jiansun.dbo.pm_priceAdjustment`(镜像 `js_pm_price_adjustment`):按 `skuCode` 关联 `jst_sku.sku_id`,取 `effectTime` 不晚于当前时间的最新 `purchasePrice`,并在页面和导出中标记“工作平台价格”;源表没有可靠更新时间,按 `Id` 稳定键追加,首次全量完成前只显示普通商品自身采购价。 |
## 经营分析副本库(operations_replica)
@ -60,6 +61,7 @@
| 封边条 | `dbo.pm_edgeBandData`、`dbo.pm_edgeBandApplyDetail`、`dbo.pm_edgeBandProfitInfo`、`dbo.finance_edgeBandDeductio`、`dbo.pm_averageUnitPrice` |
| 库存与货款对账 | `dbo.pm_boardInventoryDetail_snapshot`、`dbo.fys_supplierGoods_sttlement_purchase`、`dbo.fys_supplierGoods_sttlement_sale`、`dbo.pm_purchaseIn` |
| 采购单价 | `dbo.jst_purchase`、`dbo.jst_purchaseItem`(受控字段联表镜像,不复制其他采购业务字段) |
| 工作平台采购价格 | `dbo.pm_priceAdjustment`(`Id` 稳定键;按 `skuCode` 匹配 `jst_sku.sku_id`,页面取当前已生效的最新 `purchasePrice` / `effectTime`) |
| 快递资料 | `dbo.wp_express`、`dbo.wp_express_expNet`、`dbo.wp_express_account`、`dbo.wp_express_extension` |
| 聚水潭小额打款工单 | `dbo.jst_workOrderInfo`(稳定键 `Id`、增量字段 `updateTime`)与 `dbo.jst_small_payment_order_map`(稳定键 `woNo`、增量字段 `updated_at`) |
@ -78,6 +80,12 @@
9. 店铺维度报表是已批准的中央专用数据集:生产节点按 `Id` 断点推进并回看最近 50000 行;原始账单同步后增量维护按店铺/日期汇总的 `store_report_daily_rows`,页面不得现场扫描千万级原始账单。开发机不运行刷新 worker。
10. `operations_replica.rpt.jst_originalBillInfo` 是供多个业务功能复用的原始账单镜像。首次同步必须单独授权并在低峰期分批续传;部署、页面请求和普通开发机均不得自动启动。同步完成后,开发者通过固定 Repository 查询 `ops_jst_original_bill_info`,不得各自创建整表快照。
工作平台采购价格首次全量需在备份和负责人授权后单独执行:
```bash
php artisan business-mirror:sync-tables jiansun.dbo.pm_priceAdjustment --full
```
## 后续切换顺序
1. 公司、店铺、商标等小型维表。

View File

@ -21,6 +21,12 @@ class ProductCatalogMirrorRepository
public const PURCHASE_SOURCE_LABEL = '1688代发单价';
public const WORK_PLATFORM_SOURCE_LABEL = '工作平台价格';
public const WORK_PLATFORM_PRICE_DATASET_KEY = 'jiansun.dbo.pm_priceAdjustment';
private const WORK_PLATFORM_PRICE_TABLE = 'js_pm_price_adjustment';
private readonly ConnectionInterface $connection;
public function __construct(
@ -84,7 +90,14 @@ class ProductCatalogMirrorRepository
return ['code' => $code, 'lag_minutes' => $lagMinutes];
}
/** @return array{data:list<object>,total:int,purchase_price_sync_status:array{code:string,initialized:bool,last_succeeded_at:?string}} */
/**
* @return array{
* data:list<object>,
* total:int,
* purchase_price_sync_status:array{code:string,initialized:bool,last_succeeded_at:?string},
* work_platform_price_sync_status:array{code:string,initialized:bool,last_succeeded_at:?string}
* }
*/
public function paginate(array $filters, int $page, int $perPage): array
{
if (! $this->ready()) {
@ -103,11 +116,14 @@ class ProductCatalogMirrorRepository
$purchasePriceSyncStatus = $this->purchasePriceSyncStatus();
$this->attachPurchasePrices($rows, $purchasePriceSyncStatus['initialized']);
$workPlatformPriceSyncStatus = $this->workPlatformPriceSyncStatus();
$this->attachWorkPlatformPrices($rows, $workPlatformPriceSyncStatus['initialized']);
return [
'data' => $rows,
'total' => $total,
'purchase_price_sync_status' => $purchasePriceSyncStatus,
'work_platform_price_sync_status' => $workPlatformPriceSyncStatus,
];
}
@ -123,6 +139,7 @@ class ProductCatalogMirrorRepository
$batchSize = min(max($batchSize, 1), 5000);
$purchasePriceSyncStatus = $this->purchasePriceSyncStatus();
$workPlatformPriceSyncStatus = $this->workPlatformPriceSyncStatus();
$lastAutoid = null;
while (true) {
@ -140,6 +157,7 @@ class ProductCatalogMirrorRepository
}
$this->attachPurchasePrices($rows, $purchasePriceSyncStatus['initialized']);
$this->attachWorkPlatformPrices($rows, $workPlatformPriceSyncStatus['initialized']);
foreach ($rows as $row) {
yield $row;
}
@ -213,6 +231,100 @@ class ProductCatalogMirrorRepository
}
}
/**
* 工作平台采购价是普通商品资料的补充价格来源,不覆盖商品自身采购价。
* 只取已生效记录;同一 SKU 按生效时间和稳定键取最新一条。
*
* @param list<object> $rows
*/
private function attachWorkPlatformPrices(array $rows, bool $initialized): void
{
foreach ($rows as $row) {
$row->work_platform_price = null;
$row->work_platform_price_effect_time = null;
}
if ($rows === [] || ! $initialized) {
return;
}
$skuIds = collect($rows)
->pluck('sku_id')
->filter(static fn (mixed $value): bool => is_string($value) && trim($value) !== '')
->map(static fn (string $value): string => trim($value))
->unique()
->values()
->all();
if ($skuIds === []) {
return;
}
$now = now();
$candidates = $this->connection->table($this->workPlatformPriceTable())
->whereIn('skuCode', $skuIds)
->whereNotNull('purchasePrice')
->where(static function ($query) use ($now): void {
$query->whereNull('effectTime')->orWhere('effectTime', '<=', $now);
})
->orderBy('skuCode')
->orderByDesc('effectTime')
->orderByDesc('Id')
->get(['skuCode', 'purchasePrice', 'effectTime'])
->unique('skuCode')
->keyBy('skuCode');
foreach ($rows as $row) {
$price = $candidates->get(trim((string) ($row->sku_id ?? '')));
if ($price === null) {
continue;
}
$row->work_platform_price = $price->purchasePrice;
$row->work_platform_price_effect_time = $price->effectTime;
$row->data_sources[] = self::WORK_PLATFORM_SOURCE_LABEL;
}
}
/** @return array{code:string,initialized:bool,last_succeeded_at:?string} */
public function workPlatformPriceSyncStatus(): array
{
$schema = Schema::connection($this->connection->getName());
if (! $schema->hasTable('business_mirror_table_states')
|| ! $schema->hasTable($this->workPlatformPriceTable())) {
return ['code' => 'unavailable', 'initialized' => false, 'last_succeeded_at' => null];
}
$state = $this->connection->table('business_mirror_table_states')
->where('dataset_key', self::WORK_PLATFORM_PRICE_DATASET_KEY)
->first();
if ($state === null || $state->initial_completed_at === null) {
$code = match ((string) ($state->status ?? 'pending')) {
'running' => 'initializing',
'failed' => 'initial_failed',
default => 'pending',
};
return ['code' => $code, 'initialized' => false, 'last_succeeded_at' => null];
}
return [
'code' => 'ready',
'initialized' => true,
'last_succeeded_at' => ($state->last_succeeded_at ?? null) === null
? null
: (string) $state->last_succeeded_at,
];
}
private function workPlatformPriceTable(): string
{
return (string) config(
'upstream_mirrors.work_platform_prices.target_table',
self::WORK_PLATFORM_PRICE_TABLE,
);
}
/** @param array<string,mixed> $filters */
private function filteredQuery(array $filters): Builder
{

View File

@ -2,6 +2,7 @@
namespace Modules\EcommerceFinance\Services;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
@ -35,7 +36,10 @@ class BusinessMirrorSchemaManager
}
foreach ($this->validatedIndexes($dataset, $columns) as $position => $index) {
$table->index($index, $this->configuredIndexName($dataset->targetTable(), $index, $position));
$table->index(
$this->indexColumns($index),
$this->configuredIndexName($dataset->targetTable(), $index, $position),
);
}
});
@ -62,13 +66,20 @@ class BusinessMirrorSchemaManager
->all();
$missingIndexes = array_values(array_filter(
$configuredIndexes,
static fn (array $index): bool => ! in_array(array_map('strtolower', $index), $existingIndexes, true),
fn (array $index): bool => ! in_array(
array_map(fn (string $column): string => strtolower($this->indexBaseColumn($column)), $index),
$existingIndexes,
true,
),
));
if ($missingIndexes !== []) {
$schema->table($dataset->targetTable(), function (Blueprint $table) use ($dataset, $missingIndexes): void {
foreach ($missingIndexes as $position => $index) {
$table->index($index, $this->configuredIndexName($dataset->targetTable(), $index, $position));
$table->index(
$this->indexColumns($index),
$this->configuredIndexName($dataset->targetTable(), $index, $position),
);
}
});
}
@ -187,8 +198,16 @@ class BusinessMirrorSchemaManager
foreach ($dataset->indexDefinitions() as $index) {
foreach ($index as $column) {
if (! in_array(strtolower($column), $columnNames, true)
|| ! $this->isIndexable($columns, $column)) {
$base = $this->indexBaseColumn($column);
if (! in_array(strtolower($base), $columnNames, true)) {
throw new RuntimeException('business_mirror_index_column_unsupported:' . $dataset->key());
}
if ($base !== $column) {
continue;
}
if (! $this->isIndexable($columns, $base)) {
throw new RuntimeException('business_mirror_index_column_unsupported:' . $dataset->key());
}
}
@ -197,6 +216,23 @@ class BusinessMirrorSchemaManager
return $dataset->indexDefinitions();
}
/** @param list<string> $index @return list<string|Expression> */
private function indexColumns(array $index): array
{
return array_map(function (string $column): string|Expression {
if (preg_match('/^([A-Za-z_][A-Za-z0-9_]*)\((\d+)\)$/', $column, $matched) !== 1) {
return $column;
}
return DB::raw(sprintf('`%s`(%d)', $matched[1], (int) $matched[2]));
}, $index);
}
private function indexBaseColumn(string $column): string
{
return preg_replace('/\(\d+\)$/', '', $column) ?? $column;
}
private function configuredIndexName(string $table, array $columns, int $position): string
{
$signature = implode('_', $columns) . '_' . $position;

View File

@ -9,6 +9,11 @@
'initialized' => false,
'last_succeeded_at' => null,
];
$productCatalogWorkPlatformPriceSyncStatus = $content['productCatalogWorkPlatformPriceSyncStatus'] ?? [
'code' => 'unavailable',
'initialized' => false,
'last_succeeded_at' => null,
];
$hasFilters = collect($productCatalogFilters)->contains(
static fn ($value) => $value !== null && $value !== ''
);
@ -220,7 +225,7 @@
.product-catalog-table {
width: 100%;
min-width: 2120px;
min-width: 2320px;
border-collapse: collapse;
font-size: 0.78125rem;
}
@ -515,6 +520,25 @@
</div>
@endif
@if (!($productCatalogWorkPlatformPriceSyncStatus['initialized'] ?? false) && !$productCatalogError)
<div
class="product-catalog-notice"
role="status"
data-work-platform-price-sync-state="{{ $productCatalogWorkPlatformPriceSyncStatus['code'] ?? 'unavailable' }}"
>
<span class="material-icons-outlined text-base" aria-hidden="true">schedule</span>
<span>
@if (($productCatalogWorkPlatformPriceSyncStatus['code'] ?? '') === 'initializing')
工作平台价格正在执行首次同步,完成前当前仅显示普通商品采购价。
@elseif (($productCatalogWorkPlatformPriceSyncStatus['code'] ?? '') === 'initial_failed')
工作平台价格首次同步失败,当前仅显示普通商品采购价。
@else
工作平台价格尚未完成首次同步,当前仅显示普通商品采购价。
@endif
</span>
</div>
@endif
<div class="product-catalog-table-wrap">
<table class="product-catalog-table" aria-label="普通商品资料">
<thead>
@ -531,6 +555,8 @@
<th class="is-numeric">销售价</th>
<th class="is-numeric">成本价</th>
<th class="is-numeric">普通商品采购价</th>
<th class="is-numeric">工作平台价格</th>
<th>工作平台价格生效日期</th>
<th class="is-numeric">1688代发单价</th>
<th>数据来源</th>
<th class="is-numeric">市场价</th>
@ -566,6 +592,8 @@
<td class="is-numeric">{{ isset($row->{$priceField}) && $row->{$priceField} !== null ? number_format((float) $row->{$priceField}, 2) : '-' }}</td>
@endforeach
<td class="is-numeric">{{ isset($row->purchase_price) && $row->purchase_price !== null ? number_format((float) $row->purchase_price, 2) : '-' }}</td>
<td class="is-numeric" title="{{ $row->work_platform_price_effect_time ?? '' }}">{{ isset($row->work_platform_price) && $row->work_platform_price !== null ? number_format((float) $row->work_platform_price, 2) : '-' }}</td>
<td class="is-code">{{ !empty($row->work_platform_price_effect_time) ? \Illuminate\Support\Carbon::parse($row->work_platform_price_effect_time)->format('Y-m-d') : '-' }}</td>
<td class="is-numeric" title="{{ $row->purchase_order_finished_at ?? '' }}">{{ isset($row->purchase_order_unit_price) && $row->purchase_order_unit_price !== null ? number_format((float) $row->purchase_order_unit_price, 2) : '-' }}</td>
<td class="product-catalog-source-list">
@forelse ($row->data_sources ?? [] as $source)
@ -589,7 +617,7 @@
</tr>
@empty
<tr>
<td colspan="18" class="product-catalog-empty">
<td colspan="20" class="product-catalog-empty">
<span class="material-icons-outlined text-3xl block mb-2" aria-hidden="true">inventory_2</span>
@if ($productCatalogError)
数据暂时不可用。

View File

@ -58,10 +58,12 @@ class ProductCatalogPageTest extends FeatureTestCase
'sale_price' => 899,
'cost_price' => 500,
'purchase_price' => 480,
'work_platform_price' => 475,
'work_platform_price_effect_time' => '2026-08-11 00:00:00',
'purchase_order_unit_price' => 65,
'purchase_order_finished_at' => '2026-08-20 11:03:05',
'purchase_order_id' => 827380,
'data_sources' => ['商品及库存管理(普通商品资料)', '1688代发单价'],
'data_sources' => ['商品及库存管理(普通商品资料)', '工作平台价格', '1688代发单价'],
'market_price' => 1099,
'brand' => '北木',
'category' => '住宅家具',
@ -76,6 +78,11 @@ class ProductCatalogPageTest extends FeatureTestCase
'initialized' => true,
'last_succeeded_at' => '2026-08-21 12:00:00',
],
'work_platform_price_sync_status' => [
'code' => 'ready',
'initialized' => true,
'last_succeeded_at' => '2026-09-01 03:02:00',
],
]);
$this->app->instance(ProductCatalogMirrorRepository::class, $repository);
@ -105,6 +112,9 @@ class ProductCatalogPageTest extends FeatureTestCase
->assertSeeText('岩板餐桌')
->assertSeeText('899.00')
->assertSeeText('普通商品采购价')
->assertSeeText('工作平台价格')
->assertSeeText('475.00')
->assertSeeText('2026-08-11')
->assertSeeText('1688代发单价')
->assertSeeText('数据来源')
->assertSeeText('商品及库存管理(普通商品资料)')
@ -112,6 +122,7 @@ class ProductCatalogPageTest extends FeatureTestCase
->assertSeeText('65.00')
->assertSeeText('导出')
->assertDontSeeText('1688代发单价尚未完成首次同步')
->assertDontSeeText('工作平台价格尚未完成首次同步')
->assertSeeText('正常同步')
->assertDontSee('pic_url');
}
@ -146,8 +157,10 @@ class ProductCatalogPageTest extends FeatureTestCase
'sale_price' => 899,
'cost_price' => 500,
'purchase_price' => 480,
'work_platform_price' => 475,
'work_platform_price_effect_time' => '2026-08-11 00:00:00',
'purchase_order_unit_price' => 65,
'data_sources' => ['商品及库存管理(普通商品资料)', '1688代发单价'],
'data_sources' => ['商品及库存管理(普通商品资料)', '工作平台价格', '1688代发单价'],
'market_price' => 1099,
'enabled' => 1,
'stock_disabled' => 0,
@ -168,7 +181,9 @@ class ProductCatalogPageTest extends FeatureTestCase
$content = $response->streamedContent();
$this->assertStringStartsWith(chr(0xEF) . chr(0xBB) . chr(0xBF), $content);
$this->assertStringContainsString('数据来源', $content);
$this->assertStringContainsString('商品及库存管理(普通商品资料);1688代发单价', $content);
$this->assertStringContainsString('工作平台价格', $content);
$this->assertStringContainsString('475', $content);
$this->assertStringContainsString('商品及库存管理(普通商品资料);工作平台价格;1688代发单价', $content);
$this->assertStringContainsString('SKU-001', $content);
}

View File

@ -75,7 +75,7 @@ class BusinessDataPlacementTest extends TestCase
$upstream = require base_path('config/upstream_mirrors.php');
$businessData = require base_path('config/business_data.php');
$this->assertCount(52, $upstream['datasets']);
$this->assertCount(53, $upstream['datasets']);
$upstreamKeys = array_keys($upstream['datasets']);
$policyKeys = array_keys($businessData['datasets']);
sort($upstreamKeys);

View File

@ -174,6 +174,54 @@ class ProductCatalogMirrorRepositoryTest extends TestCase
$this->assertSame('pending', $result['purchase_price_sync_status']['code']);
}
public function test_it_attaches_the_latest_effective_work_platform_price_and_marks_the_source(): void
{
$this->markReady(1);
$this->insertProduct(1, ['sku_id' => 'SKU-WORK', 'purchase_price' => 55]);
$this->createWorkPlatformPriceTables(true);
$this->insertWorkPlatformPrice(10, [
'skuCode' => 'SKU-WORK',
'purchasePrice' => 60,
'effectTime' => '2026-08-18 00:00:00',
]);
$this->insertWorkPlatformPrice(11, [
'skuCode' => 'SKU-WORK',
'purchasePrice' => 65,
'effectTime' => '2026-08-20 00:00:00',
]);
$this->insertWorkPlatformPrice(12, [
'skuCode' => 'SKU-WORK',
'purchasePrice' => 99,
'effectTime' => '2099-01-01 00:00:00',
]);
$row = $this->repository->paginate($this->filters(), 1, 25)['data'][0];
$this->assertSame('55', (string) $row->purchase_price);
$this->assertSame('65', (string) $row->work_platform_price);
$this->assertStringStartsWith('2026-08-20', (string) $row->work_platform_price_effect_time);
$this->assertSame(
['商品及库存管理(普通商品资料)', '工作平台价格'],
$row->data_sources,
);
}
public function test_it_keeps_products_visible_when_work_platform_price_mirror_is_not_initialized(): void
{
$this->markReady(1);
$this->insertProduct(1, ['sku_id' => 'SKU-WORK-PENDING']);
$this->createWorkPlatformPriceTables(false);
$result = $this->repository->paginate($this->filters(), 1, 25);
$row = $result['data'][0];
$this->assertNull($row->work_platform_price);
$this->assertNull($row->work_platform_price_effect_time);
$this->assertSame(['商品及库存管理(普通商品资料)'], $row->data_sources);
$this->assertFalse($result['work_platform_price_sync_status']['initialized']);
$this->assertSame('pending', $result['work_platform_price_sync_status']['code']);
}
public function test_it_exports_all_filtered_rows_in_stable_batches_with_data_sources(): void
{
$this->markReady(3);
@ -370,6 +418,33 @@ class ProductCatalogMirrorRepositoryTest extends TestCase
]);
}
private function createWorkPlatformPriceTables(bool $initialized): void
{
Schema::connection('business_mirror')->create('js_pm_price_adjustment', function (Blueprint $table): void {
$table->unsignedBigInteger('Id')->primary();
$table->string('skuCode')->nullable();
$table->decimal('purchasePrice', 18, 2)->nullable();
$table->dateTime('effectTime')->nullable();
});
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
'dataset_key' => ProductCatalogMirrorRepository::WORK_PLATFORM_PRICE_DATASET_KEY,
'status' => 'succeeded',
'initial_completed_at' => $initialized ? '2026-09-01 03:00:00' : null,
'last_succeeded_at' => $initialized ? '2026-09-01 03:01:33' : null,
'mirror_row_count' => $initialized ? 2 : 0,
]);
}
private function insertWorkPlatformPrice(int $id, array $overrides = []): void
{
DB::connection('business_mirror')->table('js_pm_price_adjustment')->insert(array_merge([
'Id' => $id,
'skuCode' => 'SKU-' . $id,
'purchasePrice' => 50,
'effectTime' => '2026-08-01 00:00:00',
], $overrides));
}
private function createExclusionTable(): void
{
Schema::connection('business_mirror')->create('product_catalog_exclusions', function (Blueprint $table): void {

View File

@ -0,0 +1,31 @@
<?php
namespace Tests\Unit\EcommerceFinance;
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Tests\TestCase;
class WorkPlatformPriceConfigTest extends TestCase
{
public function test_work_platform_price_dataset_is_wired_to_the_read_only_source(): void
{
$dataset = (new BusinessMirrorDatasetRegistry())->get(
ProductCatalogMirrorRepository::WORK_PLATFORM_PRICE_DATASET_KEY,
);
$this->assertSame('jiansun.dbo.pm_priceAdjustment', $dataset->key());
$this->assertSame('jiansun', $dataset->sourceKey());
$this->assertSame('dbo', $dataset->sourceSchema());
$this->assertSame('pm_priceAdjustment', $dataset->sourceObject());
$this->assertSame('js_pm_price_adjustment', $dataset->targetTable());
$this->assertSame(['Id'], $dataset->keyColumns());
$this->assertNull($dataset->watermarkColumn());
$this->assertContains(['skuCode(64)', 'effectTime', 'Id'], $dataset->indexDefinitions());
$selection = array_map('strtolower', (new BusinessMirrorDatasetRegistry())->columnSelection($dataset->key()) ?? []);
foreach (['id', 'skucode', 'purchaseprice', 'effecttime'] as $column) {
$this->assertContains($column, $selection);
}
}
}