You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.5 KiB
116 lines
2.5 KiB
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace App\Model\v3;
|
|
|
|
use App\Constants\v3\SsdbKeys;
|
|
use App\Model\Model;
|
|
use App\Service\v3\Interfaces\ShopCartServiceInterface;
|
|
use App\TaskWorker\SSDBTask;
|
|
use Hyperf\Database\Model\Builder;
|
|
use Hyperf\Database\Model\SoftDeletes;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use App\Constants\v3\Goods as GoodsConstants;
|
|
|
|
/**
|
|
*/
|
|
class Goods extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var ShopCartServiceInterface
|
|
*/
|
|
protected $shopCartService;
|
|
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'lanzu_goods';
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [];
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'details_imgs' => 'array',
|
|
'spec' => 'array',
|
|
'tags' => 'array',
|
|
];
|
|
|
|
protected $appends = [
|
|
'month_sales',
|
|
'cart_num',
|
|
'is_effective',
|
|
'noneffective_note',
|
|
];
|
|
|
|
protected function boot(): void
|
|
{
|
|
parent::boot();
|
|
self::addGlobalScope('normal', function (Builder $builder) {
|
|
$builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
|
|
});
|
|
}
|
|
|
|
public function scopeOrderByDefault($query, $sort)
|
|
{
|
|
return $query->orderBy('sort', $sort)->orderBy('id', $sort);
|
|
}
|
|
|
|
public function scopeOrderBySales($query, $sort)
|
|
{
|
|
return $query->orderBy('sales', $sort);
|
|
}
|
|
|
|
public function scopeOrderByPrice($query, $sort)
|
|
{
|
|
return $query->orderBy('price', $sort);
|
|
}
|
|
|
|
public function getCoverImgAttribute($value)
|
|
{
|
|
if(strripos($value,"http") === false){
|
|
return config('alioss.img_host').'/'.$value;
|
|
}else{
|
|
return $value;
|
|
}
|
|
}
|
|
|
|
public function getMonthSalesAttribute()
|
|
{
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
|
|
return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
|
|
}
|
|
|
|
public function getCartNumAttribute()
|
|
{
|
|
return (integer)$this->shopCartService->check($this->id);
|
|
}
|
|
|
|
public function getIsEffectiveAttribute()
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
public function getNoneffectiveNoteAttribute()
|
|
{
|
|
return '';
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
return $this->belongsTo(Store::class, 'store_id', 'id');
|
|
}
|
|
|
|
}
|