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.

164 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. declare (strict_types=1);
  3. namespace App\Model\v3;
  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. use App\Constants\v3\Goods as GoodsConstants;
  15. use Hyperf\Utils\Collection;
  16. /**
  17. */
  18. class Goods extends Model
  19. {
  20. use SoftDeletes;
  21. /**
  22. * @Inject
  23. * @var ShopCartServiceInterface
  24. */
  25. protected $shopCartService;
  26. /**
  27. * @Inject
  28. * @var AttachmentServiceInterface
  29. */
  30. protected $attachmentService;
  31. /**
  32. * The table associated with the model.
  33. *
  34. * @var string
  35. */
  36. protected $table = 'lanzu_goods';
  37. /**
  38. * The attributes that are mass assignable.
  39. *
  40. * @var array
  41. */
  42. protected $fillable = [
  43. 'id',
  44. 'market_id',
  45. 'store_id',
  46. 'name',
  47. 'category_id',
  48. 'goods_unit',
  49. 'price',
  50. 'original_price',
  51. 'inventory',
  52. 'restrict_num',
  53. 'start_num',
  54. 'spec',
  55. 'tags',
  56. 'remark',
  57. 'on_sale',
  58. 'is_infinite'
  59. ];
  60. /**
  61. * The attributes that should be cast to native types.
  62. *
  63. * @var array
  64. */
  65. protected $casts = [
  66. 'details_imgs' => 'array',
  67. 'spec' => 'array',
  68. 'tags' => 'array',
  69. ];
  70. protected $appends = [
  71. 'month_sales',
  72. 'cart_num',
  73. 'is_effective',
  74. 'noneffective_note',
  75. 'details_imgs_url',
  76. 'goods_name'
  77. ];
  78. protected function boot(): void
  79. {
  80. parent::boot();
  81. self::addGlobalScope('normal', function (Builder $builder) {
  82. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  83. });
  84. }
  85. public function scopeOrderByDefault($query, $sort)
  86. {
  87. return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
  88. }
  89. public function scopeOrderBySales($query, $sort)
  90. {
  91. return $query->orderBy($this->getTable().'.sales', $sort);
  92. }
  93. public function scopeOrderByPrice($query, $sort)
  94. {
  95. return $query->orderBy($this->getTable().'.price', $sort);
  96. }
  97. public function getCoverImgAttribute($value)
  98. {
  99. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  100. }
  101. public function getMonthSalesAttribute()
  102. {
  103. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  104. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_1_'.$this->id);
  105. }
  106. public function getCartNumAttribute()
  107. {
  108. return 0;
  109. // $userId = $this->request->user->id ?? 0;
  110. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  111. }
  112. public function getIsEffectiveAttribute()
  113. {
  114. return 1;
  115. }
  116. public function getNoneffectiveNoteAttribute()
  117. {
  118. return '';
  119. }
  120. public function getDetailsImgsUrlAttribute()
  121. {
  122. $details_imgs = $this->details_imgs;
  123. return collect($details_imgs)->map(function($item) {
  124. return $this->attachmentService->switchImgToAliOss($item);
  125. });
  126. }
  127. public function getGoodsNameAttribute()
  128. {
  129. return $this->attributes['name'];
  130. }
  131. public function getNameAttribute($value)
  132. {
  133. return $value . ' ' . $this->attributes['goods_unit'];
  134. }
  135. public function store()
  136. {
  137. return $this->belongsTo(Store::class, 'store_id', 'id');
  138. }
  139. public function carts()
  140. {
  141. return $this->morphMany(ShoppingCart::class, 'goods');
  142. }
  143. }