From ae01384f84903b30e843b5fcd1c22ae99601f5b5 Mon Sep 17 00:00:00 2001 From: fomichev Date: Thu, 29 May 2025 12:59:00 +0300 Subject: [PATCH] =?utf8?q?=D0=9C=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?utf8?q?=D1=86=D0=B8=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20calc?= =?utf8?q?ulateMedianSalesForProductsWithoutHistoryExtended?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/CategoryPlanController.php | 2 +- erp24/services/StorePlanService.php | 38 +++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/erp24/controllers/CategoryPlanController.php b/erp24/controllers/CategoryPlanController.php index d5989dc7..0947df5f 100644 --- a/erp24/controllers/CategoryPlanController.php +++ b/erp24/controllers/CategoryPlanController.php @@ -348,7 +348,7 @@ class CategoryPlanController extends Controller { $species ); - $weightedResults = StorePlanService::calculateWeightedSalesForProductsWithoutHistory( + $weightedResults = StorePlanService::calculateMedianSalesForProductsWithoutHistoryExtended( $storeId, $selectedMonth, $selectedYear, diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index 5757b9b2..c732738d 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -455,7 +455,7 @@ class StorePlanService /** - * Метод вычисляет взвешенное значение продаж для товаров без истории. + * Метод вычисляет значение продаж для товаров без истории. * * @param int $storeId Идентификатор магазина. * @param string $selectedMonth Выбранный месяц в формате "mm" (целевой месяц). @@ -468,54 +468,50 @@ class StorePlanService * * @return array Возвращает массив, где ключ – GUID товара, а значение – рассчитанное взвешенное значение продаж. */ - public static function calculateWeightedSalesForProductsWithoutHistory($storeId, $selectedMonth, $selectedYear, $productsWithoutHistory) + public static function calculateMedianSalesForProductsWithoutHistoryExtended($storeId, $selectedMonth, $selectedYear, $productsWithoutHistory) { - $t0 = hrtime(true); $targetDate = strtotime("{$selectedYear}-{$selectedMonth}-01"); $periods = self::getPeriods($targetDate, 3); - $weightedResults = []; - $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды - Yii::warning( "Init (periods): {$initTime} ms\n"); + $medianSalesResults = []; + foreach ($productsWithoutHistory as $product) { $guid = $product['guid']; - $t1 = hrtime(true); + + $similarProductIds = self::getSimilarProductIDs($guid); if (empty($similarProductIds)) { - $weightedResults[$guid] = 0; + $medianSalesResults[$guid] = 0; continue; } - $dur = (hrtime(true) - $t1) / 1e6; - Yii::warning( "getSimilarProductIDs for product {$guid} {$storeId}: {$dur} ms\n"); + $medianSales = []; $salesValuesForEachMonth = []; - $t2 = hrtime(true); + foreach ($periods as $periodKey => $monthInfo) { list($median, $salesValues) = self::calculateMedianSalesForPeriod($storeId, $similarProductIds, $monthInfo); $medianSales[$periodKey] = $median; $salesValuesForEachMonth[$periodKey] = $salesValues; } - $dur = (hrtime(true) - $t2) / 1e6; - Yii::warning("calculateMedianSalesForPeriod for product {$guid} {$storeId}: {$dur} ms\n"); - $t3 = hrtime(true); $median3 = self::calculateMedianSalesOverPeriods($storeId, $similarProductIds, $periods); - $weightedResults[$guid] = [ + $medianSalesResults[$guid] = [ + 'store_id' => $storeId, + 'category' => $product['category'], + 'subcategory' => $product['subcategory'], + 'species' => $product['species'], 'weightedValue' => $median3, 'medianSales' => $medianSales, 'salesValues' => $salesValuesForEachMonth, ]; - $dur = (hrtime(true) - $t3) / 1e6; - Yii::warning( "computeWeightedValue for product {$guid} {$storeId}: {$dur} ms\n"); - } - $totalTime = (hrtime(true) - $t0) / 1e6; - Yii::warning( "Total calculateWeightedSalesForProductsWithoutHistory: {$totalTime} ms\n"); + } - return $weightedResults; + return $medianSalesResults; } + /** * Возвращает среднюю цену $productId для месяца, отстоящего на $offset месяцев * от заданного $year-$month, в контексте $storeId. -- 2.39.5