param null string IScorable model throws Exception public function cal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @param null|string|IScorable $model
* @throws \Exception
*/
public function calculateRanks($model = null)
{
if (is_string($model)) {
$model = app()->make($model);
}
if ($model && !$model instanceof IScorable) {
throw new \Exception('Invalid model passed into the method', 500);
}
if (!$model) {
app()->call(ScoreStat::class, ['model' => Item::class], 'calculateRanks');
app()->call(ScoreStat::class, ['model' => User::class], 'calculateRanks');
app()->call(ScoreStat::class, ['model' => Company::class], 'calculateRanks');
return;
}
$type = $model->scores()->getMorphClass();
$query = DB::table($this->getTable());
$query->where('scorable_type', '=', $type);
$query->select($this->primaryKey);
$props = Score::SCORE_LIST;
foreach ($props as $prop) {
$query->selectRaw("row_number() over (ORDER BY {$prop} desc, {$prop}_count) as {$prop}_rank");
}
$query->selectRaw("row_number() over (ORDER BY total desc) as total_rank");
$query->where('rank_at', '<=', Carbon::now()->subMinute())->orWhereNull('rank_at');
$bulkCollection = $query->get();
DB::beginTransaction();
foreach ($bulkCollection as $bulk) {
DB::table($this->getTable())
->where($this->primaryKey, '=', $bulk->scorable_id)
->update(collect($bulk)->except('scorable_id')->toArray() + ['rank_at' => $this->freshTimestamp()]);
}
DB::commit();
}