修复商品价格读取实时价格日志

商品价格改为读取当前价格调整日志表,并补充查询映射与回归测试。
This commit is contained in:
WuYongJie 2026-08-14 10:10:34 +08:00
parent 730a0ad1b5
commit 7a3857e10f
2 changed files with 33 additions and 26 deletions

View File

@ -11,16 +11,16 @@ use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
class ProductPriceRepository
{
private const SOURCE = 'jiansun';
private const TABLE = '[dbo].[aaronprice]';
private const TABLE = '[dbo].[pm_priceAdjustment_log]';
private const PRICE_TYPES = [
'pricing' => '定价',
'repricing' => '变价',
'pricing' => '0',
'repricing' => '1',
];
private const COST_REDUCTION = [
'yes' => '是',
'no' => '否',
'yes' => '1',
'no' => '0',
];
private readonly UpstreamReadOnlyQuery $query;
@ -39,31 +39,31 @@ class ProductPriceRepository
$bindings = [];
if (($filters['barcode'] ?? '') !== '') {
$where[] = "[条形码] LIKE ? ESCAPE '\\'";
$where[] = "[skucode] LIKE ? ESCAPE '\\'";
$bindings[] = $this->contains((string) $filters['barcode']);
}
$priceType = self::PRICE_TYPES[(string) ($filters['price_type'] ?? '')] ?? null;
if ($priceType !== null) {
$where[] = '[价格类型] = ?';
$where[] = '[priceType] = ?';
$bindings[] = $priceType;
}
$costReduction = self::COST_REDUCTION[(string) ($filters['cost_reduction'] ?? '')] ?? null;
if ($costReduction !== null) {
$where[] = '[是否采购降本] = ?';
$where[] = '[isCostReduction] = ?';
$bindings[] = $costReduction;
}
if (($filters['effective_from'] ?? '') !== '') {
$where[] = 'TRY_CONVERT(date, [生效开始时间]) >= ?';
$where[] = '[effectStartTime] >= CAST(? AS date)';
$bindings[] = (string) $filters['effective_from'];
}
if (($filters['effective_to'] ?? '') !== '') {
$where[] = 'TRY_CONVERT(date, [生效开始时间]) <= ?';
$where[] = '[effectStartTime] < DATEADD(day, 1, CAST(? AS date))';
$bindings[] = (string) $filters['effective_to'];
}
@ -71,21 +71,22 @@ class ProductPriceRepository
$offset = ($page - 1) * $limit;
$fetch = $limit + 1;
$sql = 'SELECT '
. '[条形码] AS barcode, '
. '[价格类型] AS price_type, '
. '[变价类型] AS change_type, '
. '[是否采购降本] AS cost_reduction, '
. 'TRY_CONVERT(decimal(18,2), NULLIF([变价前价格], \'\')) AS price_before, '
. 'TRY_CONVERT(decimal(18,2), NULLIF([变价后价格], \'\')) AS price_after, '
. 'TRY_CONVERT(decimal(18,2), NULLIF([差价], \'\')) AS price_diff, '
. '[申请人] AS applicant, '
. '[备注] AS remark, '
. 'CONVERT(varchar(19), TRY_CONVERT(datetime2, [生效开始时间]), 120) AS effective_start, '
. 'CONVERT(varchar(19), TRY_CONVERT(datetime2, [生效结束时间]), 120) AS effective_end, '
. 'CONVERT(varchar(19), TRY_CONVERT(datetime2, [流程完成时间]), 120) AS completed_at '
. '[skucode] AS barcode, '
. "CASE [priceType] WHEN '0' THEN N'定价' WHEN '1' THEN N'变价' ELSE NULLIF([priceType], '') END AS price_type, "
. "CASE [priceChangeType] WHEN '0' THEN N'普通变价' WHEN '1' THEN N'清货变价' "
. "WHEN '2' THEN N'阶段变价' WHEN '4' THEN N'打折入库' ELSE NULLIF([priceChangeType], '') END AS change_type, "
. "CASE [isCostReduction] WHEN '1' THEN N'是' WHEN '0' THEN N'否' ELSE NULL END AS cost_reduction, "
. '[originalPrice] AS price_before, '
. '[afterPrice] AS price_after, '
. '[diffPrice] AS price_diff, '
. '[applyUser] AS applicant, '
. '[remark] AS remark, '
. 'CONVERT(varchar(19), [effectStartTime], 120) AS effective_start, '
. 'CONVERT(varchar(19), [effectEndTime], 120) AS effective_end, '
. 'CONVERT(varchar(19), COALESCE([updateTime], [createTime]), 120) AS completed_at '
. 'FROM ' . self::TABLE
. $whereSql
. ' ORDER BY TRY_CONVERT(datetime2, [流程完成时间]) DESC, [条形码] ASC'
. ' ORDER BY COALESCE([updateTime], [createTime]) DESC, [Id] DESC, [skucode] ASC'
. " OFFSET {$offset} ROWS FETCH NEXT {$fetch} ROWS ONLY";
return ['rows' => array_map([$this, 'normalize'], $this->query->select($sql, $bindings))];

View File

@ -15,11 +15,17 @@ class ProductPriceRepositoryTest extends TestCase
$query->expects($this->once())->method('select')->willReturnCallback(
function (string $sql, array $bindings): array {
UpstreamReadOnlyQuery::assertReadOnlySql($sql);
$this->assertStringContainsString('[dbo].[aaronprice]', $sql);
$this->assertStringContainsString('[条形码] LIKE ?', $sql);
$this->assertStringContainsString('[dbo].[pm_priceAdjustment_log]', $sql);
$this->assertStringContainsString('[skucode] LIKE ?', $sql);
$this->assertStringContainsString("CASE [priceType] WHEN '0' THEN N'定价' WHEN '1' THEN N'变价'", $sql);
$this->assertStringContainsString("WHEN '1' THEN N'清货变价'", $sql);
$this->assertStringContainsString("WHEN '2' THEN N'阶段变价'", $sql);
$this->assertStringContainsString("WHEN '4' THEN N'打折入库'", $sql);
$this->assertStringContainsString("CASE [isCostReduction] WHEN '1' THEN N'是' WHEN '0' THEN N'否'", $sql);
$this->assertStringContainsString('[effectStartTime] < DATEADD(day, 1, CAST(? AS date))', $sql);
$this->assertStringContainsString('OFFSET 2 ROWS FETCH NEXT 3 ROWS ONLY', $sql);
$this->assertSame(
['%ABC\\%\\_%', '定价', '是', '2026-07-01', '2026-07-31'],
['%ABC\\%\\_%', '0', '1', '2026-07-01', '2026-07-31'],
$bindings,
);