public static function lazySave() { if (self::$needSave) { $connection = Yii::app()->db; $values = array(); $attrs = array('id', 'name', 'stars', 'cityId', 'countryId', 'rating', 'minPrice'); foreach (self::$hotelDbs as $hotelDb) { if ($hotelDb->id) { $vals = array(); foreach ($attrs as $attrName) { $attrVal = $hotelDb->getAttribute($attrName); if ($attrVal) { $vals[] = "'" . addslashes($attrVal) . "'"; } else { $vals[] = 'NULL'; } } $values[] = "(" . implode(',', $vals) . ")"; } } $sql = 'INSERT INTO hotel (' . implode(',', $attrs) . ') VALUES ' . implode(',', $values); //$sql .= " (".implode(',',$in).")"; $sql .= " ON DUPLICATE KEY UPDATE rating=VALUES(rating),minPrice=VALUES(minPrice)"; $command = $connection->createCommand($sql); $command->execute(); self::$needSave = false; } }
public function saveHotelDb() { $hotels = $this->getJsonObject(0); if ($hotels) { $city = City::getCityByPk($hotels['hotels'][0]['cityId']); $hotelNames = array(); foreach ($hotels['hotels'] as $hotelInfo) { $hotelNames[$hotelInfo['hotelName']] = $hotelInfo['hotelName']; } $ratingNames = HotelRating::model()->findByNames($hotelNames, $city); foreach ($hotels['hotels'] as $hotelInfo) { $hotelDb = HotelDb::lazySaveHotelDb(array('id' => $hotelInfo['hotelId'], 'name' => $hotelInfo['hotelName'], 'stars' => $hotelInfo['categoryId'], 'cityId' => $city->id, 'countryId' => $city->countryId, 'rating' => isset($ratingNames[$hotelInfo['hotelName']]) ? $ratingNames[$hotelInfo['hotelName']] : null, 'minPrice' => $hotelInfo['rubPrice'])); } HotelDb::lazySave(); } }
public function actionRTFlight($countryCode = '', $cityCodeFrom = '', $cityCodeTo = '') { if (!($country = $this->testCountry($countryCode))) { return false; } $this->morphy = Yii::app()->morphy; $countryUp = mb_strtoupper($country->localRu, 'utf-8'); $countryMorph = array('caseAcc' => $this->getCase($countryUp, 'ВН')); if (!$cityCodeFrom) { $currentCity = City::getCityByCode('LED'); } else { $currentCity = City::getCityByCode($cityCodeFrom); } $city = City::getCityByCode($cityCodeTo); $citiesFrom = array(); $this->addCityFrom($citiesFrom, 4466); // Moscow $this->addCityFrom($citiesFrom, 5185); // Spb $this->addCityFrom($citiesFrom, $currentCity->id); Yii::import('site.common.modules.hotel.models.*'); $hotelClient = new HotelBookClient(); /*$criteria = new CDbCriteria(); $criteria->addCondition('countAirports > 0'); $criteria->addCondition('countryId = '.$country->id); $cities = City::model()->findAll($criteria); $cityIds = array(); foreach($cities as $city){ $cityIds[$city->id] = $city->id; } if(isset($cityIds[$currentCity->id])){ unset($cityIds[$currentCity->id]); }*/ $criteria = new CDbCriteria(); $criteria->addCondition('`to` = ' . $city->id); $criteria->addCondition('`from` = ' . $currentCity->id); //$criteria->group = 'dateBack'; //$criteria->addCondition('`from` = '.$currentCity->id); $criteria->addCondition('`dateFrom` >= ' . date("'Y-m-d'")); $criteria->addCondition('`dateFrom` <= ' . date("'Y-m-d'", time() + 3600 * 24 * 18)); $criteria->addCondition('`dateBack` >= ' . date("'Y-m-d'")); $criteria->addCondition('`dateBack` <= ' . date("'Y-m-d'", time() + 3600 * 24 * 18)); //$criteria->addCondition("`dateBack` <> '0000-00-00'"); $criteria->order = 'priceBestPrice'; //$criteria->limit = 18; $flightCache = FlightCache::model()->findAll($criteria); /*$sortFc = array(); foreach($flightCache as $fc){ $k = strtotime($fc->dateFrom); $sortFc[$k] = $fc;//array('price'=>$fc->priceBestPrice,'date'=>$fc->dateFrom,'dateBack'=>$fc->dateBack); } ksort($sortFc);*/ //$sortFc = array_slice($sortFc,0,18); $minPrice = false; $maxPrice = false; $activeMin = null; $activeMax = null; foreach ($flightCache as $k => $fc) { $k = strtotime($fc->dateFrom); if (!$minPrice) { $minPrice = $fc->priceBestPrice; $maxPrice = $fc->priceBestPrice; } else { if ($fc->priceBestPrice < $minPrice) { $minPrice = $fc->priceBestPrice; $activeMin = $k; } if ($fc->priceBestPrice > $maxPrice) { $maxPrice = $fc->priceBestPrice; $activeMax = $k; } } } $sortFc = array(); foreach ($flightCache as $fc) { //$k = strtotime($fc->dateFrom); $sortFc[] = array('price' => $fc->priceBestPrice, 'date' => $fc->dateFrom, 'dateBack' => $fc->dateBack); } //print_r($flightCache);die(); $criteria = new CDbCriteria(); //$criteria->addInCondition('`cityId`',$cityIds); $criteria->addCondition('cityId = ' . $city->id); $criteria->limit = 15; $hotelCache = HotelDb::model()->findAll($criteria); $hotelsInfo = array(); foreach ($hotelCache as $hc) { $hotelInfo = $hotelClient->hotelDetail($hc->id); $hotelInfo->price = $hc->minPrice; $hotelInfo->hotelName = $hc->name; $hotelInfo->hotelId = $hc->id; $hotelsInfo[] = $hotelInfo; } $criteria = new CDbCriteria(); //$criteria->addInCondition('`to`',$cityIds); $criteria->addCondition('`from` = ' . $currentCity->id); $criteria->addCondition('`dateFrom` > \'' . date('Y-m-d') . "'"); $criteria->order = 'priceBestPrice'; $criteria->limit = 14; $criteria->group = '`to`'; $flightCacheFromCurrent = FlightCache::model()->findAll($criteria); $this->layout = 'static'; //$this->render('landing'); $this->render('rtflight', array('city' => $city, 'citiesFrom' => $citiesFrom, 'hotelsInfo' => $hotelsInfo, 'currentCity' => $currentCity, 'flightCache' => $sortFc, 'maxPrice' => $maxPrice, 'minPrice' => $minPrice, 'activeMin' => $activeMin, 'flightCacheFromCurrent' => $flightCacheFromCurrent)); }