1c-bitrix

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
class CSaleManager
{
private $IBLOCK_ID = 12;
private $SKU_IBLOCK_ID = 13;
private static $_instance;
private function __construct()
{
CModule::IncludeModule("iblock");
CModule::IncludeModule("catalog");
}
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new CSaleManager();
}
return self::$_instance;
}
public function updateSaleRedis($is_agent = false)
{
set_time_limit(0);
ini_set("memory_limit", "2048M");
define("TIME_START", microtime(true));
$result = array();
$log = "";
$agent = "CSaleManager::getInstance()->updateSaleRedis(" . true . ");"; // то, что должно возвращаться агенту
/**
* Получаем активные товары
*
* "CATALOG_GROUP_1" - BASE, Розничная цена
* "CATALOG_GROUP_2" - Закупка
* "CATALOG_GROUP_3" - Розница - Базовый
*/
$arSelect = Array("ID", "IBLOCK_ID", "NAME", "CODE", "CATALOG_GROUP_2", "CATALOG_GROUP_3");
$arFilter = Array("IBLOCK_ID" => $this->IBLOCK_ID, "ACTIVE_DATE" => "Y", "ACTIVE" => "Y", "SECTION_GLOBAL_ACTIVE" => "Y");
$res = CIBlockElement::GetList(Array("ID" => "DESC"), $arFilter, false, false, $arSelect);
while ($arFields = $res->GetNext()) {
$result["products"][$arFields["ID"]] = $arFields;
$result["PIDs"][] = $arFields["ID"];
}
/* торговые предложения товаров */
$result["PRICES"] = CIBlockPriceTools::GetCatalogPrices($this->IBLOCK_ID, array("BASE", "Розница", "Закупка"));
$arOffers = CIBlockPriceTools::GetOffersArray(
array("IBLOCK_ID" => $this->IBLOCK_ID),
$result["PIDs"],
array(
"SORT" => "ASC",
"ID" => "DESC",
), // сортировка по полям
array(), // поля для вывода
array(), // свойства для вывода
0, // кол-во торг. предложений
$result["PRICES"], // типы цен
true
);
// проверяем кол-во ТП
// чтобы исключить отсутствующие товары
foreach ($arOffers as $offer) {
// товар
$pid = $offer["PROPERTY_121_VALUE"];
if (!isset($result["ITEMS"][$pid])) {
$result["ITEMS"][$pid] = $result["products"][$pid];
}
$result["ITEMS"][$pid]["offer_counts"]++;
if ($offer["CATALOG_QUANTITY"] <= 0)
continue;
// ТП
$resOffer = CIBlockElement::GetList(
Array("ID" => "DESC"), array("ID" => $offer["ID"], "IBLOCK_ID" => $this->SKU_IBLOCK_ID), false, array("nTopCount" => 1), array("ID","IBLOCK_ID","PROPERTY_CML2_SIZE","PROPERTY_CML2_COLOR", "PROPERTY_CML2_ARTICLE")
);
if($f_offer = $resOffer->GetNext()){
$offer = array_merge($offer,$f_offer);
$result["ITEMS"][$pid]["OFFERS"][] = $offer;
}
}
// отбор актуальных ТП по условиям из настроек
// и сохранение в Redis
$quantity = COption::GetOptionInt("sale_catalog", "quantity");
$iRedis = RedisRun::getInstance();
foreach ($result["ITEMS"] as $p_id => $pOffers) {
if (count($pOffers["OFFERS"]) != $quantity || $pOffers["offer_counts"]<=1) {
unset($result["ITEMS"][$p_id]);
continue;
}
$iRedis->hSet("sale::itemcard", $p_id, json_encode($pOffers));
}
$countProducts = count($result["ITEMS"]);
// логирование времени начала и конца выборки
define("TIME_END", microtime(true));
$log .= "count: " . $countProducts . "\n";
$log .= "start select offers: " . ConvertTimeStamp(TIME_START, "FULL") . "\n";
$log .= "end select offers: " . ConvertTimeStamp(TIME_END, "FULL") . "\n";
DebugInfo::log("sale_catalog", $log);
$iRedis->set("sm:init:list", json_encode($result["ITEMS"]));
if ($is_agent == true) {
return $agent;
} else {
return $countProducts;
}
}
}