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.

181 lines
4.2 KiB

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