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.

191 lines
4.4 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
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. 'activity_type'
  80. ];
  81. protected function boot(): void
  82. {
  83. parent::boot();
  84. self::addGlobalScope('normal', function (Builder $builder) {
  85. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  86. });
  87. }
  88. public function getActivityTypeAttribute()
  89. {
  90. return 1;
  91. }
  92. public function scopeOrderByDefault($query, $sort)
  93. {
  94. return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
  95. }
  96. public function scopeOrderBySales($query, $sort)
  97. {
  98. return $query->orderBy($this->getTable().'.sales', $sort);
  99. }
  100. public function scopeOrderByPrice($query, $sort)
  101. {
  102. return $query->orderBy($this->getTable().'.price', $sort);
  103. }
  104. public function getCoverImgAttribute($value)
  105. {
  106. if(!empty($value)) {
  107. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  108. }else{
  109. return '';
  110. }
  111. }
  112. public function getMonthSalesAttribute()
  113. {
  114. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  115. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_1_'.$this->id);
  116. }
  117. public function getCartNumAttribute()
  118. {
  119. return 0;
  120. // $userId = $this->request->user->id ?? 0;
  121. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  122. }
  123. public function getIsEffectiveAttribute()
  124. {
  125. return 1;
  126. }
  127. public function getNoneffectiveNoteAttribute()
  128. {
  129. return '';
  130. }
  131. public function getDetailsImgsUrlAttribute()
  132. {
  133. $details_imgs = $this->details_imgs;
  134. return collect($details_imgs)->map(function($item) {
  135. return $this->attachmentService->switchImgToAliOss($item);
  136. });
  137. }
  138. public function getGoodsNameAttribute()
  139. {
  140. return $this->attributes['name'];
  141. }
  142. public function getNameAttribute($value)
  143. {
  144. return $value . ' ' . $this->attributes['goods_unit'];
  145. }
  146. public function store()
  147. {
  148. return $this->belongsTo(Store::class, 'store_id', 'id');
  149. }
  150. public function carts()
  151. {
  152. return $this->morphMany(ShoppingCart::class, 'goods');
  153. }
  154. public function setTagsAttribute($value)
  155. {
  156. $this->attributes['tags'] = json_encode(json_decode($value, true));
  157. }
  158. public function setSpecAttribute($value)
  159. {
  160. $this->attributes['spec'] = json_encode(json_decode($value, true));
  161. }
  162. public function banner()
  163. {
  164. return $this->hasMany(GoodsBanner::class, 'goods_id','id');
  165. }
  166. }