Esempio n. 1
0
 public function actionDirection()
 {
     $date_id = false;
     $airline = ReceivedAirline::find()->select(['iata'])->asArray()->all();
     if ($airline) {
         $count = 0;
         $update = 0;
         $error = 0;
         $count_iata = count($airline);
         $this->stdout('Get IATA: ' . $count_iata . PHP_EOL, Console::FG_GREEN);
         foreach ($airline as $value) {
             $this->stdout('[' . $count_iata . '] Request API: Country IATA: ' . $value['iata'], Console::FG_GREEN);
             try {
                 $data = TravelPayoutsApi::getAirlineDirections($value['iata'], 1000);
             } catch (Exception $e) {
                 $error++;
                 $this->stdout($value['iata'] . ' ', Console::FG_RED);
                 print_r($e);
                 die;
             }
             $this->stdout(', Get: ' . count($data) . ' Directions.' . PHP_EOL, Console::FG_GREEN);
             foreach ($data as $cities => $popular) {
                 $iata = explode('-', $cities);
                 $top = ReceivedDirection::find()->where(['iata_code' => $value['iata'], 'city_from_code' => $iata[0], 'city_to_code' => $iata[1]])->one();
                 if ($top) {
                     if ($top->popularity != $popular) {
                         $this->stdout($cities . ' ', Console::FG_BLUE);
                         $top->popularity = $popular;
                         if (!$top->save()) {
                             print_r($top->attributes);
                             print_r($top->errors);
                             return Controller::EXIT_CODE_ERROR;
                         }
                         $update++;
                     }
                 } else {
                     if ($date_id === false) {
                         $received_date = new ReceivedDate();
                         $received_date->created_at = date("Y-m-d H:i:s");
                         $received_date->received = 0;
                         /** @var ActiveRecord $received_date */
                         if (!$received_date->save()) {
                             print_r($received_date->attributes);
                             print_r($received_date->errors);
                             return Controller::EXIT_CODE_ERROR;
                         }
                         $date_id = Yii::$app->db->lastInsertID;
                         $this->stdout(' [create new received date: ' . $received_date->created_at . '] ', Console::FG_CYAN);
                     }
                     $top = new ReceivedDirection();
                     $top->iata_code = $value['iata'];
                     $top->city_from_code = $iata[0];
                     $top->city_to_code = $iata[1];
                     $top->popularity = $popular;
                     $top->date_id = $date_id;
                     /**
                      * todo проблема - в базе не существуют города или неправильный развер ключа, например вместо кода написано "Москва"
                      */
                     try {
                         if (!$top->save()) {
                             $this->stdout($cities . ' ', Console::FG_RED);
                             print_r($top->attributes);
                             print_r($top->errors);
                             $error++;
                         } else {
                             $count++;
                             $this->stdout($cities . ' ', Console::FG_YELLOW);
                         }
                     } catch (Exception $e) {
                         $error++;
                         $this->stdout($cities . ' ', Console::FG_RED);
                     }
                 }
             }
             if (!empty($received_date)) {
                 $received_date->received = $count;
                 $received_date->updated = $update;
                 $received_date->errors = $error;
                 $received_date->save();
             }
             $this->stdout(PHP_EOL . 'Added: ' . $count . ' Updates: ' . $update . ' Error ' . $error . PHP_EOL, Console::FG_BLUE);
             $count_iata--;
         }
         if (!empty($received_date)) {
             $received_date->received = $count;
             $received_date->updated = $update;
             $received_date->errors = $error;
             $received_date->save();
         }
         $this->stdout(PHP_EOL . 'Added: ' . $count . ' Updates: ' . $update . ' Error ' . $error . PHP_EOL, Console::FG_BLUE);
         return Controller::EXIT_CODE_NORMAL;
     }
     $this->stdout(PHP_EOL . 'No ReceivedAirline database' . PHP_EOL, Console::FG_RED);
     return Controller::EXIT_CODE_ERROR;
 }
Esempio n. 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getIata()
 {
     return $this->hasOne(ReceivedAirline::className(), ['iata' => 'iata_code']);
 }