2022-08-12 08:51:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\User;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Helpers\Lock;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
|
|
|
|
|
|
|
class Drop extends Model
|
|
|
|
{
|
|
|
|
use HasFactory, Lock;
|
|
|
|
|
2022-08-13 09:47:10 +00:00
|
|
|
protected $cache_key, $cache;
|
|
|
|
|
2022-08-12 08:51:55 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'payment', 'amount', 'user_id', 'type'
|
|
|
|
];
|
|
|
|
|
2022-08-13 09:47:10 +00:00
|
|
|
// casts
|
|
|
|
protected $casts = [
|
|
|
|
'amount' => 'double',
|
|
|
|
'total' => 'double',
|
|
|
|
'rate' => 'integer',
|
|
|
|
'status' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2022-08-12 08:51:55 +00:00
|
|
|
// user
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// before create
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
self::creating(function ($drops) {
|
|
|
|
// if not admin auth guard
|
|
|
|
if (!auth()->guard('admin')->check()) {
|
|
|
|
$drops->user_id = auth()->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-09 13:04:09 +00:00
|
|
|
$rate = config('drops.rate') ;
|
2022-08-12 08:51:55 +00:00
|
|
|
$drops->total = $drops->amount * $rate;
|
2022-08-30 09:20:45 +00:00
|
|
|
|
|
|
|
$this->cache_key = 'user_' . $drops->user_id;
|
|
|
|
|
|
|
|
// if cache has user
|
|
|
|
|
|
|
|
// if (Cache::has($this->cache_key)) {
|
|
|
|
// // if user is not instances of Model
|
|
|
|
// $user = Cache::get($this->cache_key);
|
|
|
|
// if ($user instanceof User) {
|
|
|
|
// $this->await($this->cache_key, function () use ($user) {
|
|
|
|
// $user->save();
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // delete cache
|
|
|
|
// Cache::forget($this->cache_key);
|
|
|
|
// }
|
2022-08-12 08:51:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// created
|
|
|
|
self::created(function ($drops) {
|
|
|
|
$drop = new self();
|
2022-08-13 09:47:10 +00:00
|
|
|
$drop->await('user_' . $drops->user_id, function () use ($drops) {
|
|
|
|
$cache = Cache::tags(['users']);
|
|
|
|
$drops->load('user');
|
|
|
|
$cache_key = 'user_' . $drops->user_id;
|
|
|
|
|
|
|
|
// if cache has user
|
|
|
|
if ($cache->has($cache_key)) {
|
|
|
|
$user = $cache->get($cache_key);
|
|
|
|
if (!($user instanceof User)) {
|
|
|
|
$user = $drops->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->drops += $drops->total;
|
|
|
|
|
|
|
|
|
|
|
|
$cache->put($cache_key, $user, 600);
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
} else {
|
|
|
|
$drops->user->drops += $drops->total;
|
|
|
|
$drops->user->save();
|
|
|
|
}
|
2022-08-12 08:51:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|