/** * Function for get list currencies details. * * @param array $currencyIds * @param string $type * @return boolean|Ambigous <NULL, mixed, unknown> */ public static function getCurrenciesDetails($currencyIds = array()) { if (!count($currencyIds)) { return false; } // Create cache id $cacheCurrency = 'exr_currency'; // For currencies if (Cache::has($cacheCurrency)) { $currencies = json_decode(Cache::get($cacheCurrency)); } else { $currencies = CurrencyDaily::whereRaw('created_at > NOW() - INTERVAL ? DAY', array(1))->orderBy('created_at', 'DESC')->with('currency')->get(); if (count($currencies)) { Cache::put($cacheCurrency, json_encode($currencies), EXR_CACHE_MINUTE); } } $newRecords = array(); foreach ($currencies as $item) { $currencyId = intval($item->currency_id); if (in_array($currencyId, $currencyIds)) { $newRecords[] = $item; unset($currencyIds[$currencyId]); } } return $newRecords; }
/** * Api function for get currency and listing by 7 days data. * * @param number $currencyId * @return boolean|Response */ public function getCurrenciesById($currencyId = 0) { $data = get(); $default = array('currency_id' => $currencyId); // Validator input params. $valids = Validator::make($default, ['currency_id' => 'required|numeric|min:1'], EXRHelper::getMessage()); // Check fails. if ($valids->fails()) { return Reponse::json(array('status' => 'error', 'data' => $valids->messages()), 500); } $cacheId = 'currency_detail_' . $currencyId; if (!Cache::has($cacheId)) { $records = CurrencyDaily::whereRaw('currency_id = ? AND created_at >= NOW() - INTERVAL ? WEEK', array($currencyId, DLN_LIMIT_WEEK))->orderBy('updated_at', 'ASC')->with('currency')->get(); $newRecords = array(); if (count($records)) { foreach ($records as $record) { switch ($record->type) { case 'gold': $multiple = 100000; break; default: $multiple = 1; break; } $record->buy = intval($record->buy * $multiple); $record->transfer = intval($record->transfer * $multiple); $record->sell = intval($record->sell * $multiple); $record->min_buy = intval($record->min_buy * $multiple); $record->max_buy = intval($record->max_buy * $multiple); $record->min_sell = intval($record->min_sell * $multiple); $record->max_sell = intval($record->max_sell * $multiple); $record->buy_change = intval($record->buy_change * $multiple); $record->sell_change = intval($record->sell_change * $multiple); $newRecords[] = $record; } } Cache::put($cacheId, json_encode($newRecords), EXR_CACHE_MINUTE); } else { $records = json_decode(Cache::get($cacheId, null)); } return Response::json(array('status' => 'success', 'data' => $records)); }
/** * Api function for crawl send notification to devices. * * @return Response */ public function getSendNotificationDevices() { $data = get(); // Valid get params. $valids = Validator::make($data, ['token' => 'required|in:' + EXR_TOKEN], EXRHelper::getMessage()); // Check valid. if ($valids->fails()) { return Response::json(array('status' => 'Error', 'data' => $valids->messages()), 403); } // Get Currency $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); if (!count($currencies)) { Currency::where('is_send', true)->update(array('is_send' => false)); $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); } foreach ($currencies as $currency) { $message = $tye = ''; if (Cache::has('exr_notification_msg_' . $currency->id)) { $data = Cache::get('exr_notification_msg_' . $currency->id); $message = isset($data['message']) ? $data['message'] : ''; $type = isset($data['type']) ? $data['type'] : ''; } else { // Get today currency $today = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) = CURDATE()', array($currency->id))->first(); if (!$today) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } $mimax = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) >= NOW() - INTERVAL ? WEEK', array($currency->id, EXR_LIMIT_WEEK))->select(DB::raw('MIN(buy) as min, MAX(buy) as max'))->first(); if ($mimax->min > $today->buy) { $message = sprintf(EXR_MIN_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $tye = 'currency_min'; } else { if ($mimax->max < $today->buy) { $message = sprintf(EXR_MAX_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $type = 'currency_max'; } else { $message = ''; $type = ''; } } if ($message) { Cache::put('exr_notification_msg_' . $currency->id, array('message' => $message, 'type' => $type), EXR_CACHE_MINUTE); } } if (!$message) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } // Get 1000 devices $records = Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ?', array($currency->id, $type, false))->take(EXR_LIMIT_DEVICES)->get(); if (!count($records)) { // Update is_send for currency when send all devices. $currency->is_send = true; $currency->save(); // Clear if crawl end devices. Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ? AND DATE(updated_at) != CURDATE()', array($currency->id, $type, true))->update(array('is_send', false)); } // Convert devices registations ids to array. $deviceIds = $records->lists('device_id'); $response = ''; if (count($deviceIds) <= EXR_LIMIT_DEVICES && !empty($message)) { // Send to notification to it $response = Notification::sendNtfsToDevices($message, $deviceIds); } } return Response::json(array('status' => 'success', 'data' => $response)); }