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.

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