成本价兜底改按方案②:仅“聚水潭成本价记录”有生效记录时才判已定价

This commit is contained in:
chenxi 2026-10-04 15:35:24 +08:00
parent d736ab0fd3
commit 9526173fec

View File

@ -30,6 +30,7 @@ class ComputeProductMonthlyWeightedAverage extends Command
$work = $this->workPlatformPrices($connection, $codes);
$change = $this->priceChanges($connection, $codes);
$inbound = $this->inboundAggregate($connection, $month);
$costRecordSkus = $this->costPriceRecordSkus($connection, $codes);
$total = (clone $query)->count();
$computed = 0;
@ -57,6 +58,7 @@ class ComputeProductMonthlyWeightedAverage extends Command
$changePrice,
$deadline,
$inbound[$inboundKey] ?? null,
isset($costRecordSkus[$code]),
);
$updates[] = array_merge([
'report_month' => $month->toDateString(),
@ -161,6 +163,38 @@ class ComputeProductMonthlyWeightedAverage extends Command
return $map;
}
/**
* 有"聚水潭成本价记录"(operated_at 非空)的商品编码集合。
*
* @return array<string,bool>
*/
private function costPriceRecordSkus(string $connection, array $codes): array
{
$map = [];
if (! \Illuminate\Support\Facades\Schema::connection($connection)->hasTable('jushuitan_cost_price_history_rows')) {
return $map;
}
foreach (array_chunk($codes, 2000) as $chunk) {
$rows = DB::connection($connection)->table('jushuitan_cost_price_history_rows')
->whereNotNull('operated_at')
->where(function ($query) use ($chunk): void {
$query->whereIn('sku_id', $chunk)->orWhereIn('sku_id_normalized', $chunk);
})
->get(['sku_id', 'sku_id_normalized']);
foreach ($rows as $row) {
foreach ([$row->sku_id, $row->sku_id_normalized] as $value) {
$key = mb_strtoupper(trim((string) $value));
if ($key !== '') {
$map[$key] = true;
}
}
}
}
return $map;
}
private function priceChanges(string $connection, array $codes): array
{
$map = [];
@ -188,7 +222,7 @@ class ComputeProductMonthlyWeightedAverage extends Command
* @param array{price:float,time:?string}|null $change
* @return array<string,mixed>
*/
private function calculate(object $row, ?float $cost, ?array $work, ?array $change, CarbonImmutable $deadline, ?array $inbound = null): array
private function calculate(object $row, ?float $cost, ?array $work, ?array $change, CarbonImmutable $deadline, ?array $inbound = null, bool $hasCostRecord = false): array
{
$openingQuantity = $row->opening_quantity === null ? 0.0 : (float) $row->opening_quantity;
$purchaseQuantity = $row->purchase_quantity === null ? 0.0 : (float) $row->purchase_quantity;
@ -226,9 +260,9 @@ class ComputeProductMonthlyWeightedAverage extends Command
$adjusted = $changePrice;
$status = $newPricePending ? '未定价' : '已定价';
} elseif ($cost !== null) {
// 方案①:能取到成本价即视为"有生效开始日期" → 已定价。
// 方案②:只有"聚水潭成本价记录"里有生效记录(operated_at 非空)才算有生效日期 → 已定价。
$adjusted = $cost;
$status = '已定价';
$status = $hasCostRecord ? '已定价' : '未定价';
} else {
$adjusted = null;
$status = '待人工核查';