erp/app/Models/GoodsSku.php

110 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2022-07-28 16:06:15 +08:00
use App\Models\traits\Filter;
class GoodsSku extends Model
{
2022-07-28 16:06:15 +08:00
use Filter;
//查询字段
public $fieldSearchable = [
2022-07-28 17:08:05 +08:00
'sku_title',
2022-07-28 16:06:15 +08:00
'status',
2023-04-17 18:56:59 +08:00
'exclude_ids',
2023-07-26 13:58:52 +08:00
'external_sku_id',
'is_combination',
2024-10-30 15:15:54 +08:00
"create_time_start",
"create_time_end"
2022-07-28 16:06:15 +08:00
];
2022-08-09 20:05:19 +08:00
protected $fillable = [
'goods_id',
'title',
'sku_code',
'status',
'num',
'stock',
'cost',
'two_days_ago_num',
'yesterday_num',
'reference_price',
'reserve',
2023-04-17 18:56:59 +08:00
'thumb_url',
2023-04-22 15:56:53 +08:00
'external_sku_id',
'is_combination',
2024-03-18 14:12:57 +08:00
'name',
2024-07-30 15:30:32 +08:00
'sale_stock',
2024-08-30 16:15:40 +08:00
'attribute'
2022-08-09 20:05:19 +08:00
];
2023-11-21 16:27:14 +08:00
public static $STATUS_ON_SALE = 1;
public static $STATUS_DOWN = 0;
2022-07-28 17:08:05 +08:00
/**
* 获取状态
*
* @param string $value
* @return string
*/
public function getStatusAttribute($value)
{
$map = [
0 => '下架',
1 => '在售',
2 => '预警',
];
return $map[$value];
}
2022-10-25 22:08:55 +08:00
public function setThumbUrlAttribute($value)
{
$this->attributes['thumb_url'] = json_encode($value, 256);
}
public function getThumbUrlAttribute($value)
{
return json_decode($value, true);
}
public function getNameAttribute($value)
{
if(empty($value)){
return $this->attributes['title']??'';
}
return $value;
}
2022-07-28 13:46:08 +08:00
/**
* 此规格从属于一个商品
*/
public function goods()
{
2023-04-03 15:41:14 +08:00
return $this->belongsTo(Goods::class, 'goods_id', 'id');
2022-07-28 13:46:08 +08:00
}
2022-07-28 17:08:05 +08:00
/**
* 此规格每日记录
*/
public function daily()
{
return $this->hasOne(DailyStockRecord::class, 'sku_id', 'id');
}
2023-04-18 11:22:16 +08:00
public function combinationItems()
2023-04-20 20:43:49 +08:00
{
return $this->hasMany(CombinationGood::class, 'item_id', 'id');
}
public function combinationGoods()
2023-04-18 11:22:16 +08:00
{
return $this->hasMany(CombinationGood::class, 'goods_sku_id', 'id');
}
2023-04-20 20:43:49 +08:00
public function todayPrice()
{
return $this->hasOne(TodayPrice::class, 'external_sku_id', 'external_sku_id');
}
}