6
0
Fork 0
jiqu-library-server/app/Models/DrawActivityPrize.php

70 lines
1.5 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',
'other' => 'json'
];
/**
* @var array
*/
protected $fillable = [
'draw_activity_id',
'name',
'icon',
'type',
'amount',
'weight',
'limited',
'stock',
'winnings',
'sort',
'auto_send',
'price_type_id',
'other'
];
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');
}
}