|
|
<?php
namespace App\Model\v3;
use App\Constants\v3\Goods as GoodsConstants;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;
class GoodsActivity extends Model{
use SoftDeletes;
/** * @Inject * @var ShopCartServiceInterface */ protected $shopCartService;
/** * @Inject * @var AttachmentServiceInterface */ protected $attachmentService;
protected $table = 'lanzu_goods_activity';
protected $casts = [ 'details_imgs' => 'array', 'spec' => 'array', 'tags' => 'array', ];
protected $appends = [ 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds', 'details_imgs_url', ];
// 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(['on_sale' => GoodsConstants::ON_SALE_YES]); }); }
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() { $userId = $this->request->user->id ?? 0; return $userId ? (integer)$this->shopCartService->check($userId, $this->id) : 0; }
public function getIsEffectiveAttribute() { return 2; }
public function getNoneffectiveNoteAttribute() { return '已抢光'; }
public function getTotalSecondsAttribute() { return $this->attributes['expire_time'] - time(); }
public function getCoverImgAttribute($value) { return $this->attachmentService->switchImgToAliOss($value); }
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; $img_host = config('alioss.img_host').'/';
return collect($details_imgs)->map(function($item) use ($img_host){ return $img_host . $item; }); }
public function getNameAttribute() { return $this->attributes['name'].' '.$this->attributes['goods_unit']; }}
|