Joywork/ajax/analytics/getAnalyticsPriceStats.php
2026-05-22 21:21:54 +03:00

179 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
$partOneNum = 1;
$partTwoNum = 1;
$partThreeNum = 1;
$script = '';
//создаем хранилище Ид в сессии
if (!isset($_SESSION['analytics'])) {
$_SESSION['analytics'] = [];
}
// аналитическая часть
if ($vsego >= 6) {
//инициализируем конкретный ИД в сессии
$_SESSION['analytics'][$objectId] = [];
$script .= '$("#averageCountNumber").html("Объектов в сравнении: ' . $vsego . '");';
$script .= '$("#analyticsMetersTitleWarning").hide();';
$script .= '$("#analyticsDempAllPriceLink").show();';
$script .= '$("#analyticsAvarageAllPriceLink").show();';
$script .= '$("#analyticsHighAllPriceLink").show();';
// 1) делим на 3 части
$partOneNum = $partTwoNum = intval ($vsego / 3);
$partThreeNum = $vsego - $partOneNum - $partTwoNum;
//делаем запросы для подсчета общей стоимости
$foundIdsOne = array();
$foundIdsTwo = array();
$foundIdsThree = array();
$partTwoEnd = $partOneNum + $partTwoNum;
//----------первая часть---------
$sqlOneSearchIds = "SELECT id FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 ORDER BY stoim ASC LIMIT 0, $partOneNum OPTION max_matches=1000000";
$resOneSearchIds = $db->query($sqlOneSearchIds);
if ($db->num_rows($resOneSearchIds) > 0) {
while ($objOne = $db->fetch_assoc($resOneSearchIds)) {
$foundIdsOne[] = $objOne['id'];
}
}
$sqlOneSearchSql = "SELECT min(stoim) as min_price, max(stoim) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundIdsOne) . ") ORDER BY stoim ASC OPTION max_matches=1000000";
$rezOneSearch = $db->query($sqlOneSearchSql);
$resultOne = $db->fetch_assoc($rezOneSearch);
$partOneMin = $resultOne['min_price'];
$partOneMax = $resultOne['max_price'];
$avarageOnePrice = ($partOneMax - $partOneMin)/2 + $partOneMin;
$script .= '$("#analyticsDempAllPrice").html("'. number_format($partOneMin, 0, ',', ' ') . ' - ' . number_format($partOneMax, 0, ',', ' ') . ' р");';
$script .= '$("#analyticsNowPrice").html("'. number_format($avarageOnePrice, 0, ',', ' ') . '");';
//----------вторая часть---------
$sqlTwoSearchIds = "SELECT id FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 ORDER BY stoim ASC LIMIT $partOneNum, $partTwoNum OPTION max_matches=1000000";
$resTwoSearchIds = $db->query($sqlTwoSearchIds);
if ($db->num_rows($resTwoSearchIds) > 0) {
while ($objTwo = $db->fetch_assoc($resTwoSearchIds)) {
$foundIdsTwo[] = $objTwo['id'];
}
}
$sqlTwoSearchSql = "SELECT min(stoim) as min_price, max(stoim) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundIdsTwo) . ") ORDER BY stoim ASC OPTION max_matches=1000000";
$rezTwoSearch = $db->query($sqlTwoSearchSql);
$resultTwo = $db->fetch_assoc($rezTwoSearch);
$partTwoMin = $resultTwo['min_price'];
$partTwoMax = $resultTwo['max_price'];
$avarageTwoPrice = ($partTwoMax - $partTwoMin)/2 + $partTwoMin;
$script .= '$("#analyticsAvarageAllPrice").html("'. number_format($partTwoMin, 0, ',', ' ') . ' - ' . number_format($partTwoMax, 0, ',', ' ') . ' р");';
$script .= '$("#analyticsThreeMonthPrice").html("'. number_format($avarageTwoPrice, 0, ',', ' ') . '");';
//----------третья часть---------
$sqlThreeSearchIds = "SELECT id FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 ORDER BY stoim ASC LIMIT $partTwoEnd, $partThreeNum OPTION max_matches=1000000";
$resThreeSearchIds = $db->query($sqlThreeSearchIds);
if ($db->num_rows($resThreeSearchIds) > 0) {
while ($objThree = $db->fetch_assoc($resThreeSearchIds)) {
$foundIdsThree[] = $objThree['id'];
}
}
$sqlThreeSearchSql = "SELECT min(stoim) as min_price, max(stoim) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundIdsThree) . ") ORDER BY stoim ASC OPTION max_matches=1000000";
$rezThreeSearch = $db->query($sqlThreeSearchSql);
$resultThree = $db->fetch_assoc($rezThreeSearch);
$partThreeMin = $resultThree['min_price'];
$partThreeMax = $resultThree['max_price'];
$avarageThreePrice = ($partThreeMax - $partThreeMin)/2 + $partThreeMin;
$script .= '$("#analyticsHighAllPrice").html("'. number_format($partThreeMin, 0, ',', ' ') . ' - ' . number_format($partThreeMax, 0, ',', ' ') . ' р");';
$script .= '$("#analyticsTwelveMonthPrice").html("'. number_format($avarageThreePrice, 0, ',', ' ') . '");';
//сохраняем ИД в сессии по этому объекту
$_SESSION['analytics'][$objectId]['low'] = $foundIdsOne;
$_SESSION['analytics'][$objectId]['average'] = $foundIdsTwo;
$_SESSION['analytics'][$objectId]['high'] = $foundIdsThree;
$field = 'ploshad';
$exp = 'stoim/ploshad';
//для участков берем влощадь участка в сотках
if ($obj['type'] == 7) {
$field = 'land_area';
$exp = 'stoim/land_area';
$script .= '$("#analyticsMetersTitle").html("Среднерыночная стоимость «Сотки»");';
} else {
$script .= '$("#analyticsMetersTitle").html("Среднерыночная стоимость «Квадратного&nbsp;метра»");';
}
//делаем запросы для подсчета стоимости за метр
$foundMetersIdsOne = array();
$foundMetersIdsTwo = array();
$foundMetersIdsThree = array();
//----------первая часть---------
$sqlOneSearchMetersIdsSql= "SELECT id, $exp as stoim_kv_m FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 AND $field > 0 ORDER BY stoim_kv_m ASC LIMIT 0, $partOneNum OPTION max_matches=1000000";
$resOneSearchMetersIds = $db->query($sqlOneSearchMetersIdsSql);
if ($db->num_rows($resOneSearchMetersIds) > 0) {
while ($objOne = $db->fetch_assoc($resOneSearchMetersIds)) {
$foundMetersIdsOne[] = $objOne['id'];
}
}
$sqlOneSearchMetersSql = "SELECT $exp as stoim_kv_m, min($exp) as min_price, max($exp) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundMetersIdsOne) . ") ORDER BY stoim_kv_m ASC OPTION max_matches=1000000";
$rezOneSearchMeters = $db->query($sqlOneSearchMetersSql);
$resultOneMeters = $db->fetch_assoc($rezOneSearchMeters);
$script .= '$("#analyticsDempMeters").html("'. number_format($resultOneMeters['min_price'], 0, ',', ' ') . ' - ' . number_format($resultOneMeters['max_price'], 0, ',', ' ') . ' р");';
//----------вторая часть---------
$sqlTwoSearchMetersIdsSql = "SELECT id, $exp as stoim_kv_m FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 AND $field > 0 ORDER BY stoim_kv_m ASC LIMIT $partOneNum, $partTwoNum OPTION max_matches=1000000";
$resTwoSearchMetersIds = $db->query($sqlTwoSearchMetersIdsSql);
if ($db->num_rows($resTwoSearchMetersIds) > 0) {
while ($objTwo = $db->fetch_assoc($resTwoSearchMetersIds)) {
$foundMetersIdsTwo[] = $objTwo['id'];
}
}
if (count($foundMetersIdsTwo) > 0) {
$sqlTwoSearchMetersSql = "SELECT $exp as stoim_kv_m, min($exp) as min_price, max($exp) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundMetersIdsTwo) . ") ORDER BY stoim_kv_m ASC OPTION max_matches=1000000";
$rezTwoSearchMeters = $db->query($sqlTwoSearchMetersSql);
$resultTwoMeters = $db->fetch_assoc($rezTwoSearchMeters);
$script .= '$("#analyticsAvarageMeters").html("' . number_format($resultTwoMeters['min_price'], 0, ',', ' ') . ' - ' . number_format($resultTwoMeters['max_price'], 0, ',', ' ') . ' р");';
} else {
$script .= '$("#analyticsAvarageMeters").html("-");';
}
//----------третья часть---------
$sqlThreeSearchMetersIdsSql = "SELECT id, $exp as stoim_kv_m FROM objects, objects_delta WHERE id in (" . implode(",", $foundIds) . ") AND stoim > 0 AND $field > 0 ORDER BY stoim_kv_m ASC LIMIT $partTwoEnd, $partThreeNum OPTION max_matches=1000000";
$resThreeSearchMetersIds = $db->query($sqlThreeSearchMetersIdsSql);
if ($db->num_rows($resThreeSearchMetersIds) > 0) {
while ($objThree = $db->fetch_assoc($resThreeSearchMetersIds)) {
$foundMetersIdsThree[] = $objThree['id'];
}
}
if (count($foundMetersIdsThree) > 0){
$sqlThreeSearchMetersSql = "SELECT $exp as stoim_kv_m, min($exp) as min_price, max($exp) as max_price FROM objects, objects_delta WHERE id in (" . implode(",", $foundMetersIdsThree) . ") ORDER BY stoim_kv_m ASC OPTION max_matches=1000000";
$rezThreeSearchMeters = $db->query($sqlThreeSearchMetersSql);
$resultThreeMeters = $db->fetch_assoc($rezThreeSearchMeters);
$script .= '$("#analyticsHighMeters").html("' . number_format($resultThreeMeters['min_price'], 0, ',', ' ') . ' - ' . number_format($resultThreeMeters['max_price'], 0, ',', ' ') . ' р");';
} else {
$script .= '$("#analyticsHighMeters").html("-");';
}
} else {
$script .= '$("#analyticsDempAllPrice").html("-");';
$script .= '$("#analyticsNowPrice").html("-");';
$script .= '$("#analyticsAvarageAllPrice").html("-");';
$script .= '$("#analyticsThreeMonthPrice").html("-");';
$script .= '$("#analyticsHighAllPrice").html("-");';
$script .= '$("#analyticsTwelveMonthPrice").html("-");';
$script .= '$("#analyticsDempMeters").html("-");';
$script .= '$("#analyticsAvarageMeters").html("-");';
$script .= '$("#analyticsHighMeters").html("-");';
$script .= '$("#analyticsMetersTitleWarning").show();';
$script .= '$("#analyticsDempAllPriceLink").hide();';
$script .= '$("#analyticsAvarageAllPriceLink").hide();';
$script .= '$("#analyticsHighAllPriceLink").hide();';
$script .= '$("#averageCountNumber").html("");';
}
if ($asScript) {
echo '<script type="text/javascript">' . $script . '</script>';
} else {
$resultArrayMap['script'] = $script;
}