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.
214 lines
5.0 KiB
214 lines
5.0 KiB
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace App\Model\v3;
|
|
|
|
use App\Constants\v3\OssThumbnail;
|
|
use App\Constants\v3\SsdbKeys;
|
|
use App\Model\Model;
|
|
use App\Service\v3\Interfaces\AttachmentServiceInterface;
|
|
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;
|
|
use Hyperf\Utils\Collection;
|
|
|
|
/**
|
|
*/
|
|
class Goods extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var ShopCartServiceInterface
|
|
*/
|
|
protected $shopCartService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var AttachmentServiceInterface
|
|
*/
|
|
protected $attachmentService;
|
|
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'lanzu_goods';
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'id',
|
|
'market_id',
|
|
'store_id',
|
|
'name',
|
|
'category_id',
|
|
'goods_category_id',
|
|
'goods_unit',
|
|
'price',
|
|
'original_price',
|
|
'inventory',
|
|
'restrict_num',
|
|
'start_num',
|
|
'spec',
|
|
'tags',
|
|
'remark',
|
|
'on_sale',
|
|
'is_infinite',
|
|
'cover_img'
|
|
];
|
|
/**
|
|
* 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',
|
|
'details_imgs_url',
|
|
'goods_name',
|
|
'activity_type'
|
|
];
|
|
|
|
protected function boot(): void
|
|
{
|
|
parent::boot();
|
|
self::addGlobalScope('normal', function (Builder $builder) {
|
|
$builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
|
|
});
|
|
}
|
|
|
|
public function getActivityTypeAttribute()
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
public function scopeOrderByDefault($query, $sort)
|
|
{
|
|
return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
|
|
}
|
|
|
|
public function scopeOrderBySales($query, $sort)
|
|
{
|
|
return $query->orderBy($this->getTable().'.sales', $sort);
|
|
}
|
|
|
|
public function scopeOrderByPrice($query, $sort)
|
|
{
|
|
return $query->orderBy($this->getTable().'.price', $sort);
|
|
}
|
|
|
|
public function getCoverImgAttribute($value)
|
|
{
|
|
if(!empty($value)) {
|
|
return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
|
|
}else{
|
|
return '';
|
|
}
|
|
}
|
|
|
|
public function getMonthSalesAttribute()
|
|
{
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
|
|
return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_1_'.$this->id);
|
|
}
|
|
|
|
public function getCartNumAttribute()
|
|
{
|
|
return 0;
|
|
// $userId = $this->request->user->id ?? 0;
|
|
// return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
|
|
}
|
|
|
|
public function getIsEffectiveAttribute()
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
public function getNoneffectiveNoteAttribute()
|
|
{
|
|
return '';
|
|
}
|
|
|
|
public function getDetailsImgsUrlAttribute()
|
|
{
|
|
$details_imgs = $this->details_imgs;
|
|
|
|
return collect($details_imgs)->map(function($item) {
|
|
return $this->attachmentService->switchImgToAliOss($item);
|
|
});
|
|
}
|
|
|
|
public function getGoodsNameAttribute()
|
|
{
|
|
return $this->attributes['name'];
|
|
}
|
|
|
|
public function getNameAttribute($value)
|
|
{
|
|
return $value . ' ' . $this->attributes['goods_unit'];
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
return $this->belongsTo(Store::class, 'store_id', 'id');
|
|
}
|
|
|
|
public function carts()
|
|
{
|
|
return $this->morphMany(ShoppingCart::class, 'goods');
|
|
}
|
|
|
|
public function setTagsAttribute($value)
|
|
{
|
|
$this->attributes['tags'] = json_encode(json_decode($value, true));
|
|
}
|
|
|
|
public function setSpecAttribute($value)
|
|
{
|
|
$this->attributes['spec'] = json_encode(json_decode($value, true));
|
|
}
|
|
|
|
public function banner()
|
|
{
|
|
return $this->hasMany(GoodsBanner::class, 'goods_id','id');
|
|
}
|
|
|
|
/**
|
|
* add:2022-04-11,获取产品毛重,主要用于计算顺丰运费
|
|
*/
|
|
public function getWeightAttribute($value): int
|
|
{
|
|
if (empty($value) && !empty($this->attributes['goods_unit'])) {
|
|
$goods_unit = $this->attributes['goods_unit'];
|
|
switch (true) {
|
|
case preg_match('/(\d{2,})\D*±(\d{2,})/', $goods_unit, $matches):
|
|
array_shift($matches);
|
|
$value = (int)max($matches);
|
|
break;
|
|
case preg_match_all('/(\d{2,})[g|克]/', $goods_unit, $matches):
|
|
$value = (int)max($matches[1]);
|
|
break;
|
|
default:
|
|
$value = 0;
|
|
}
|
|
}
|
|
return $value ?: 300; // 如果获取不到重量,默认为300g
|
|
}
|
|
}
|