parfum/app/Models/Tier.php
2025-12-09 20:45:41 +07:00

51 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Dyrynda\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Veelasky\LaravelHashId\Eloquent\HashableId;
class Tier extends Model
{
use CascadeSoftDeletes, HasFactory, HashableId, SoftDeletes;
protected $guarded = ['id'];
protected $cascadeDeletes = ['memberships'];
protected function casts(): array
{
return [
'min_points' => 'int',
'max_points' => 'int',
];
}
public static function isOverlap(int $minPoints, ?int $maxPoints, ?int $ignoreId = null): bool
{
$maxPointsCheck = $maxPoints ?? PHP_INT_MAX;
return self::when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId))
->get()
->contains(function ($tier) use ($minPoints, $maxPointsCheck) {
$tierMax = $tier->max_points ?? PHP_INT_MAX;
return ($minPoints <= $tierMax) && ($maxPointsCheck >= $tier->min_points);
});
}
public function memberships(): HasMany
{
return $this->hasMany(Membership::class);
}
public function rewards(): HasMany
{
return $this->hasMany(TierReward::class);
}
}