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.

180 lines
4.2 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
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_category_id',
  49. 'goods_unit',
  50. 'price',
  51. 'original_price',
  52. 'inventory',
  53. 'restrict_num',
  54. 'start_num',
  55. 'spec',
  56. 'tags',
  57. 'remark',
  58. 'on_sale',
  59. 'is_infinite'
  60. ];
  61. /**
  62. * The attributes that should be cast to native types.
  63. *
  64. * @var array
  65. */
  66. protected $casts = [
  67. 'details_imgs' => 'array',
  68. 'spec' => 'array',
  69. 'tags' => 'array',
  70. ];
  71. protected $appends = [
  72. 'month_sales',
  73. 'cart_num',
  74. 'is_effective',
  75. 'noneffective_note',
  76. 'details_imgs_url',
  77. 'goods_name'
  78. ];
  79. protected function boot(): void
  80. {
  81. parent::boot();
  82. self::addGlobalScope('normal', function (Builder $builder) {
  83. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  84. });
  85. }
  86. public function scopeOrderByDefault($query, $sort)
  87. {
  88. return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
  89. }
  90. public function scopeOrderBySales($query, $sort)
  91. {
  92. return $query->orderBy($this->getTable().'.sales', $sort);
  93. }
  94. public function scopeOrderByPrice($query, $sort)
  95. {
  96. return $query->orderBy($this->getTable().'.price', $sort);
  97. }
  98. public function getCoverImgAttribute($value)
  99. {
  100. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  101. }
  102. public function getMonthSalesAttribute()
  103. {
  104. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  105. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_1_'.$this->id);
  106. }
  107. public function getCartNumAttribute()
  108. {
  109. return 0;
  110. // $userId = $this->request->user->id ?? 0;
  111. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  112. }
  113. public function getIsEffectiveAttribute()
  114. {
  115. return 1;
  116. }
  117. public function getNoneffectiveNoteAttribute()
  118. {
  119. return '';
  120. }
  121. public function getDetailsImgsUrlAttribute()
  122. {
  123. $details_imgs = $this->details_imgs;
  124. return collect($details_imgs)->map(function($item) {
  125. return $this->attachmentService->switchImgToAliOss($item);
  126. });
  127. }
  128. public function getGoodsNameAttribute()
  129. {
  130. return $this->attributes['name'];
  131. }
  132. public function getNameAttribute($value)
  133. {
  134. return $value . ' ' . $this->attributes['goods_unit'];
  135. }
  136. public function store()
  137. {
  138. return $this->belongsTo(Store::class, 'store_id', 'id');
  139. }
  140. public function carts()
  141. {
  142. return $this->morphMany(ShoppingCart::class, 'goods');
  143. }
  144. public function setTagsAttribute($value)
  145. {
  146. $this->attributes['tags'] = json_encode(json_decode($value, true));
  147. }
  148. public function setSpecAttribute($value)
  149. {
  150. $this->attributes['spec'] = json_encode(json_decode($value, true));
  151. }
  152. public function banner()
  153. {
  154. return $this->hasMany(GoodsBanner::class, 'goods_id','id');
  155. }
  156. }