init
This commit is contained in:
106
web10/app/Models/Product.php
Normal file
106
web10/app/Models/Product.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'category_id',
|
||||
'name',
|
||||
'slug',
|
||||
'summary',
|
||||
'description',
|
||||
'cover',
|
||||
'screenshots',
|
||||
'video_url',
|
||||
'website_url',
|
||||
'pricing_type',
|
||||
'platforms',
|
||||
'is_featured',
|
||||
'featured_until',
|
||||
'is_sponsored',
|
||||
'sponsor_until',
|
||||
'sort',
|
||||
'hot_score',
|
||||
'hot_override',
|
||||
'view_count',
|
||||
'click_count',
|
||||
'status',
|
||||
'seo_title',
|
||||
'seo_description',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'screenshots' => 'array',
|
||||
'platforms' => 'array',
|
||||
'is_featured' => 'boolean',
|
||||
'featured_until' => 'datetime',
|
||||
'is_sponsored' => 'boolean',
|
||||
'sponsor_until' => 'datetime',
|
||||
];
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany(Tag::class)->withPivot('sort')->withTimestamps();
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class, 'target_id')
|
||||
->where('target_type', 'product');
|
||||
}
|
||||
|
||||
public function scopePublished($query)
|
||||
{
|
||||
return $query->where('status', 'published');
|
||||
}
|
||||
|
||||
public function scopeFeatured($query)
|
||||
{
|
||||
return $query->where('is_featured', true)
|
||||
->where(function ($inner) {
|
||||
$inner->whereNull('featured_until')
|
||||
->orWhere('featured_until', '>=', now());
|
||||
});
|
||||
}
|
||||
|
||||
public function getEffectiveHotScoreAttribute(): int
|
||||
{
|
||||
return $this->hot_override ?? $this->hot_score;
|
||||
}
|
||||
|
||||
public function refreshHotScore(): void
|
||||
{
|
||||
$this->hot_score = $this->calculateHotScore();
|
||||
$this->save();
|
||||
}
|
||||
|
||||
private function calculateHotScore(): int
|
||||
{
|
||||
if ($this->hot_override !== null) {
|
||||
return (int) $this->hot_override;
|
||||
}
|
||||
|
||||
$weightView = (int) SiteSetting::value('hot_weight_view', 1);
|
||||
$weightClick = (int) SiteSetting::value('hot_weight_click', 3);
|
||||
|
||||
return (int) $this->view_count * $weightView + (int) $this->click_count * $weightClick;
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::saving(function (Product $product): void {
|
||||
$product->hot_score = $product->calculateHotScore();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user