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.

84 lines
2.0 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 App\TaskWorker\SSDBTask;
  8. use Hyperf\Database\Model\Builder;
  9. use Hyperf\Database\Model\SoftDeletes;
  10. use Hyperf\Utils\ApplicationContext;
  11. use Hyperf\Di\Annotation\Inject;
  12. class GoodsActivity extends Model
  13. {
  14. use SoftDeletes;
  15. /**
  16. * @Inject
  17. * @var ShopCartServiceInterface
  18. */
  19. protected $shopCartService;
  20. protected $table = 'lanzu_goods_activity';
  21. protected $casts = [
  22. 'details_imgs' => 'array',
  23. 'spec' => 'array',
  24. 'tags' => 'array',
  25. ];
  26. protected $appends = [
  27. 'month_sales',
  28. 'cart_num',
  29. 'is_effective',
  30. 'noneffective_note',
  31. 'total_seconds',
  32. ];
  33. protected $visible = [
  34. 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'spec', 'tags', 'sales',
  35. 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds',
  36. ];
  37. protected function boot(): void
  38. {
  39. parent::boot();
  40. self::addGlobalScope('normal', function (Builder $builder) {
  41. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  42. });
  43. }
  44. public function getMonthSalesAttribute()
  45. {
  46. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  47. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  48. }
  49. public function getCartNumAttribute()
  50. {
  51. return (integer)$this->shopCartService->check($this->id);
  52. }
  53. public function getIsEffectiveAttribute()
  54. {
  55. return 2;
  56. }
  57. public function getNoneffectiveNoteAttribute()
  58. {
  59. return '已抢光';
  60. }
  61. public function getTotalSecondsAttribute()
  62. {
  63. return $this->attributes['expire_time'] - time();
  64. }
  65. public function store()
  66. {
  67. return $this->belongsTo(Store::class, 'store_id', 'id');
  68. }
  69. }