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.

48 lines
1.1 KiB

  1. <?php
  2. namespace App\Model\v3;
  3. use App\Constants\v3\Goods as GoodsConstants;
  4. use App\Constants\v3\SsdbKeys;
  5. use App\Model\Model;
  6. use Hyperf\Database\Model\Builder;
  7. use Hyperf\Utils\ApplicationContext;
  8. class GoodsActivity extends Model
  9. {
  10. protected $table = 'lanzu_goods_activity';
  11. protected $casts = [
  12. 'details_imgs' => 'array',
  13. 'spec' => 'array',
  14. 'tags' => 'array',
  15. ];
  16. protected $appends = [
  17. 'month_sales',
  18. 'cart_num',
  19. ];
  20. protected function boot(): void
  21. {
  22. parent::boot();
  23. self::addGlobalScope('normal', function (Builder $builder) {
  24. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  25. });
  26. }
  27. public function getMonthSalesAttribute()
  28. {
  29. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  30. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  31. }
  32. public function getCartNumAttribute()
  33. {
  34. return (integer)$this->shopCartService->check($this->id);
  35. }
  36. public function store()
  37. {
  38. return $this->belongsTo(Store::class, 'store_id', 'goods_id');
  39. }
  40. }