107 lines
2.3 KiB
PHP
107 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Platform extends Model
|
|
{
|
|
/**
|
|
* 可批量赋值的属性
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'shop_auth_id',
|
|
'platform_product_id',
|
|
'platform_sku_id',
|
|
'title',
|
|
'description',
|
|
'price',
|
|
'original_price',
|
|
'stock',
|
|
'sold',
|
|
'images',
|
|
'specs',
|
|
'status',
|
|
'sync_status',
|
|
'last_sync_at',
|
|
'sync_error',
|
|
];
|
|
|
|
/**
|
|
* 应进行类型转换的属性
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'images' => 'array',
|
|
'specs' => 'array',
|
|
'price' => 'decimal:2',
|
|
'original_price' => 'decimal:2',
|
|
'stock' => 'integer',
|
|
'sold' => 'integer',
|
|
'last_sync_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* 关联店铺授权
|
|
*
|
|
* @return BelongsTo
|
|
*/
|
|
public function shopAuth(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ShopAuth::class);
|
|
}
|
|
|
|
/**
|
|
* 关联SKU绑定
|
|
*/
|
|
public function skuBindings(): HasMany
|
|
{
|
|
return $this->hasMany(PlatformSkuBinding::class, 'platform_sku_id');
|
|
}
|
|
|
|
/**
|
|
* 获取当前生效的绑定
|
|
*/
|
|
public function activeBinding(): HasOne
|
|
{
|
|
return $this->hasOne(PlatformSkuBinding::class, 'platform_sku_id')->where('is_active', true);
|
|
}
|
|
|
|
/**
|
|
* 获取状态标签
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
return match($this->status) {
|
|
'on_sale' => '在售',
|
|
'off_sale' => '下架',
|
|
'deleted' => '已删除',
|
|
default => '未知',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取同步状态标签
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getSyncStatusLabelAttribute(): string
|
|
{
|
|
return match($this->sync_status) {
|
|
'pending' => '待同步',
|
|
'syncing' => '同步中',
|
|
'synced' => '已同步',
|
|
'failed' => '同步失败',
|
|
default => '未知',
|
|
};
|
|
}
|
|
}
|