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.

136 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Model\v3;
  3. use App\Constants\v3\Goods as GoodsConstants;
  4. use App\Constants\v3\OssThumbnail;
  5. use App\Constants\v3\SsdbKeys;
  6. use App\Model\Model;
  7. use App\Service\v3\Interfaces\AttachmentServiceInterface;
  8. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  9. use App\TaskWorker\SSDBTask;
  10. use Hyperf\Database\Model\Builder;
  11. use Hyperf\Database\Model\SoftDeletes;
  12. use Hyperf\Utils\ApplicationContext;
  13. use Hyperf\Di\Annotation\Inject;
  14. class GoodsActivity extends Model
  15. {
  16. use SoftDeletes;
  17. /**
  18. * @Inject
  19. * @var ShopCartServiceInterface
  20. */
  21. protected $shopCartService;
  22. /**
  23. * @Inject
  24. * @var AttachmentServiceInterface
  25. */
  26. protected $attachmentService;
  27. protected $table = 'lanzu_goods_activity';
  28. protected $casts = [
  29. 'details_imgs' => 'array',
  30. 'spec' => 'array',
  31. 'tags' => 'array',
  32. ];
  33. protected $appends = [
  34. 'month_sales',
  35. 'cart_num',
  36. 'is_effective',
  37. 'noneffective_note',
  38. 'total_seconds',
  39. 'details_imgs_url',
  40. 'activity_type'
  41. ];
  42. // protected $visible = [
  43. // 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'spec', 'tags', 'sales',
  44. // 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds','store_id'
  45. // ];
  46. protected function boot(): void
  47. {
  48. parent::boot();
  49. self::addGlobalScope('normal', function (Builder $builder) {
  50. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  51. });
  52. }
  53. public function getActivityTypeAttribute()
  54. {
  55. return 2;
  56. }
  57. public function getMonthSalesAttribute()
  58. {
  59. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  60. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_2_'.$this->id);
  61. }
  62. public function getCartNumAttribute()
  63. {
  64. return 0;
  65. // $userId = $this->request->user->id ?? 0;
  66. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  67. }
  68. public function getIsEffectiveAttribute()
  69. {
  70. if ($this->attributes['expire_time'] < time() || ($this->attributes['inventory'] <= 0 && $this->attributes['is_infinite'] != 1)) {
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. public function getNoneffectiveNoteAttribute()
  76. {
  77. $msg = '';
  78. if ($this->attributes['expire_time'] < time()) {
  79. $msg = '已结束';
  80. } elseif ($this->attributes['inventory'] <= 0 && $this->attributes['is_infinite'] != 1) {
  81. $msg = '已抢光';
  82. }
  83. return $msg;
  84. }
  85. public function getTotalSecondsAttribute()
  86. {
  87. return $this->attributes['expire_time'] - time();
  88. }
  89. public function getCoverImgAttribute($value)
  90. {
  91. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  92. }
  93. public function getNameAttribute($value)
  94. {
  95. return $value . ' ' . $this->attributes['goods_unit'];
  96. }
  97. public function store()
  98. {
  99. return $this->belongsTo(Store::class, 'store_id', 'id');
  100. }
  101. public function carts()
  102. {
  103. return $this->morphMany(ShoppingCart::class, 'goods');
  104. }
  105. public function getDetailsImgsUrlAttribute()
  106. {
  107. $details_imgs = $this->details_imgs;
  108. return collect($details_imgs)->map(function($item) {
  109. return $this->attachmentService->switchImgToAliOss($item);
  110. });
  111. }
  112. }