Example #1
0
 /**
  * Finds the StockBuy model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return StockBuy the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = StockBuy::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #2
0
 /**
  * 更新股票信息
  * @date   2016-02-26T22:46:57+0800
  * @author cnzhangxl@foxmail.com
  * @return [type]                   [description]
  */
 public function actionUpdateStockInfo()
 {
     $sleep = 2;
     $cache_time = 60;
     $cachename = 'console_update_stock_info';
     if (Yii::$app->cache->get($cachename)) {
         // exit;
     }
     while (true) {
         Yii::$app->cache->set($cachename, 1, $cache_time);
         $list = StockBuy::find()->groupBy('stock_code')->all();
         foreach ($list as $key => $stock_one) {
             $url = 'http://hq.sinajs.cn/list=' . $stock_one->stock_code;
             $return_arr = Common::reqUrl($url);
             if ($return_arr['content']) {
                 $Stock = Stock::find()->where(['stock_code' => $stock_one->stock_code])->one();
                 if ($Stock) {
                     $Stock->scenario = 'update';
                 } else {
                     $Stock = new Stock(['scenario' => 'create']);
                 }
                 $stock_info_arr = explode(',', $return_arr['content']);
                 if ($stock_info_arr) {
                     $Stock->stock_code = $stock_one->stock_code;
                     if (isset($stock_info_arr[1]) && $stock_info_arr[1]) {
                         $Stock->today_open = $stock_info_arr[1];
                     }
                     if (isset($stock_info_arr[2]) && $stock_info_arr[2]) {
                         $Stock->yesterday_closed = $stock_info_arr[2];
                     }
                     if (isset($stock_info_arr[3]) && $stock_info_arr[3]) {
                         $Stock->now_price = $stock_info_arr[3];
                     }
                     if (isset($stock_info_arr[4]) && $stock_info_arr[4]) {
                         $Stock->max_price = $stock_info_arr[4];
                     }
                     if (isset($stock_info_arr[5]) && $stock_info_arr[5]) {
                         $Stock->min_price = $stock_info_arr[5];
                     }
                     if (isset($stock_info_arr[8]) && $stock_info_arr[8]) {
                         $Stock->volume = round($stock_info_arr[8] / 100 / 10000, 4);
                     }
                     if (isset($stock_info_arr[9]) && $stock_info_arr[9]) {
                         $Stock->volume_price = round($stock_info_arr[9] / 10000, 4);
                     }
                     $buy_num = 0;
                     for ($i = 10; $i < 19; $i += 2) {
                         if (isset($stock_info_arr[$i]) && $stock_info_arr[$i]) {
                             $buy_num += $stock_info_arr[$i];
                         }
                     }
                     $sale_num = 0;
                     for ($i = 20; $i < 29; $i += 2) {
                         if (isset($stock_info_arr[$i]) && $stock_info_arr[$i]) {
                             $sale_num += $stock_info_arr[$i];
                         }
                     }
                     if ($buy_num + $sale_num > 0) {
                         $Stock->committee_ratio = round(($buy_num - $sale_num) / ($buy_num + $sale_num), 4);
                         $Stock->committee_difference = intval(($buy_num - $sale_num) / 100);
                     }
                     $Stock->updated_at = time();
                     $Stock->save();
                 }
             }
         }
         sleep($sleep);
     }
     return false;
 }