diff --git a/app/Console/Commands/ComputeProductMonthlyWeightedAverage.php b/app/Console/Commands/ComputeProductMonthlyWeightedAverage.php index 064fe38f..55d98bf4 100644 --- a/app/Console/Commands/ComputeProductMonthlyWeightedAverage.php +++ b/app/Console/Commands/ComputeProductMonthlyWeightedAverage.php @@ -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 + */ + 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 */ - 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 = '待人工核查';