131 lines
2.5 KiB
PHP
131 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\JsonArray;
|
|
use App\Casts\Price;
|
|
use App\Endpoint\Api\Filters\ProductSkuFilter;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use EloquentFilter\Filterable;
|
|
|
|
class ProductSpu extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
use Filterable;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $attributes = [
|
|
'weight' => 0,
|
|
'stock' => 0,
|
|
'sales' => 0,
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'images' => JsonArray::class,
|
|
// 'sell_price' => Price::class,
|
|
// 'market_price' => Price::class,
|
|
// 'cost_price' => Price::class,
|
|
// 'vip_price' => Price::class,
|
|
'attrs' => JsonArray::class,
|
|
'release_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'subtitle',
|
|
'category_id',
|
|
'codcovere',
|
|
'images',
|
|
'sell_price',
|
|
'market_price',
|
|
'cost_price',
|
|
'vip_price',
|
|
'media',
|
|
'weight',
|
|
'attrs',
|
|
'stock',
|
|
'sales',
|
|
'release_at',
|
|
'sales_value',
|
|
'cover',
|
|
'description'
|
|
];
|
|
|
|
public function modelFilter()
|
|
{
|
|
return ProductSkuFilter::class;
|
|
}
|
|
|
|
public function skus()
|
|
{
|
|
return $this->hasMany(ProductSku::class, 'spu_id');
|
|
}
|
|
|
|
public function specs()
|
|
{
|
|
return $this->hasMany(ProductSpuSpec::class, 'spu_id');
|
|
}
|
|
|
|
public function features()
|
|
{
|
|
return $this->belongsToMany(ProductFeature::class, 'product_spu_features', 'feature_id', 'spu_id');
|
|
}
|
|
|
|
/**
|
|
* 确认此主商品是否有 sku
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function hasSku(): bool
|
|
{
|
|
return $this->skus()->exists();
|
|
}
|
|
|
|
/**
|
|
* 获取商品售价
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getSellPriceFormatAttribute()
|
|
{
|
|
return bcdiv($this->attributes['sell_price'], 100, 2);
|
|
}
|
|
|
|
/**
|
|
* 获取商品会员价
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getVipPriceFormatAttribute()
|
|
{
|
|
if (is_null($price = $this->attributes['vip_price'])) {
|
|
return '';
|
|
}
|
|
|
|
return bcdiv($price, 100, 2);
|
|
}
|
|
|
|
/**
|
|
* 获取商品市场价
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getMarketPriceFormatAttribute()
|
|
{
|
|
if (is_null($price = $this->attributes['market_price'])) {
|
|
return '';
|
|
}
|
|
|
|
return bcdiv($price, 100, 2);
|
|
}
|
|
}
|