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.

56 lines
1.4 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 App\Service\v3\Interfaces\ShopCartServiceInterface;
  7. use Hyperf\Database\Model\Builder;
  8. use Hyperf\Utils\ApplicationContext;
  9. use App\TaskWorker\SSDBTask;
  10. use Hyperf\Di\Annotation\Inject;
  11. class GoodsActivity extends Model
  12. {
  13. /**
  14. * @Inject
  15. * @var ShopCartServiceInterface
  16. */
  17. protected $shopCartService;
  18. protected $table = 'lanzu_goods_activity';
  19. protected $casts = [
  20. 'details_imgs' => 'array',
  21. 'spec' => 'array',
  22. 'tags' => 'array',
  23. ];
  24. protected $appends = [
  25. 'month_sales',
  26. 'cart_num',
  27. ];
  28. protected function boot(): void
  29. {
  30. parent::boot();
  31. self::addGlobalScope('normal', function (Builder $builder) {
  32. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  33. });
  34. }
  35. public function getMonthSalesAttribute()
  36. {
  37. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  38. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  39. }
  40. public function getCartNumAttribute()
  41. {
  42. return (integer)$this->shopCartService->check($this->id);
  43. }
  44. public function store()
  45. {
  46. return $this->belongsTo(Store::class, 'store_id', 'id');
  47. }
  48. }