66 lines
1.4 KiB
PHP
66 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\DrawPrizeType;
|
|
use App\Exceptions\BizException;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DrawActivityPrize extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $attributes = [
|
|
'type' => DrawPrizeType::None,
|
|
'weight' => 0,
|
|
'limited' => true,
|
|
'sort' => 0,
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'type' => DrawPrizeType::class,
|
|
'limited' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'draw_activity_id',
|
|
'name',
|
|
'icon',
|
|
'type',
|
|
'amount',
|
|
'weight',
|
|
'limited',
|
|
'stock',
|
|
'winnings',
|
|
'sort',
|
|
];
|
|
|
|
public static function booted()
|
|
{
|
|
static::updating(function ($drawActivityPrize) {
|
|
if ($drawActivityPrize->weight == 0 && $drawActivityPrize->activity->isPublished()) {
|
|
$weight = $drawActivityPrize->activity->prizes()->where('id', '!=', $drawActivityPrize->id)->sum('weight');
|
|
|
|
if ($weight == 0) {
|
|
throw new BizException('活动奖品总权重值不能为0');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public function activity()
|
|
{
|
|
return $this->belongsTo(DrawActivity::class, 'draw_activity_id');
|
|
}
|
|
}
|