'array', 'spec' => 'array', 'tags' => 'array', ]; protected $appends = [ 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds', 'details_imgs_url', 'activity_type' ]; // protected $visible = [ // 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'spec', 'tags', 'sales', // 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds','store_id' // ]; 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 2; } public function getMonthSalesAttribute() { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_2_'.$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() { if ($this->attributes['expire_time'] < time() || ($this->attributes['inventory'] <= 0 && $this->attributes['is_infinite'] != 1)) { return 1; } return 0; } public function getNoneffectiveNoteAttribute() { $msg = ''; // 获取冻结的库存 $inventoryFrozen = $this->goodsInventoryService->getSold(2, $this->attributes['id']); if ($this->attributes['expire_time'] < time()) { $msg = '已结束'; } elseif (($this->attributes['inventory'] <= $inventoryFrozen || $this->attributes['inventory'] <= 0) && $this->attributes['is_infinite'] != 1) { $msg = '已抢光'; } return $msg; } public function getTotalSecondsAttribute() { return $this->attributes['expire_time'] - time(); } public function getCoverImgAttribute($value) { return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90); } 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 getDetailsImgsUrlAttribute() { $details_imgs = $this->details_imgs; return collect($details_imgs)->map(function($item) { return $this->attachmentService->switchImgToAliOss($item); }); } }