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.

148 lines
3.5 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
  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. /**
  44. * The attributes that should be cast to native types.
  45. *
  46. * @var array
  47. */
  48. protected $casts = [
  49. 'details_imgs' => 'array',
  50. 'spec' => 'array',
  51. 'tags' => 'array',
  52. ];
  53. protected $appends = [
  54. 'month_sales',
  55. 'cart_num',
  56. 'is_effective',
  57. 'noneffective_note',
  58. 'details_imgs_url',
  59. 'goods_name'
  60. ];
  61. protected function boot(): void
  62. {
  63. parent::boot();
  64. self::addGlobalScope('normal', function (Builder $builder) {
  65. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  66. });
  67. }
  68. public function scopeOrderByDefault($query, $sort)
  69. {
  70. return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
  71. }
  72. public function scopeOrderBySales($query, $sort)
  73. {
  74. return $query->orderBy($this->getTable().'.sales', $sort);
  75. }
  76. public function scopeOrderByPrice($query, $sort)
  77. {
  78. return $query->orderBy($this->getTable().'.price', $sort);
  79. }
  80. public function getCoverImgAttribute($value)
  81. {
  82. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  83. }
  84. public function getMonthSalesAttribute()
  85. {
  86. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  87. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_1_'.$this->id);
  88. }
  89. public function getCartNumAttribute()
  90. {
  91. return 0;
  92. // $userId = $this->request->user->id ?? 0;
  93. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  94. }
  95. public function getIsEffectiveAttribute()
  96. {
  97. return 1;
  98. }
  99. public function getNoneffectiveNoteAttribute()
  100. {
  101. return '';
  102. }
  103. public function getDetailsImgsUrlAttribute()
  104. {
  105. $details_imgs = $this->details_imgs;
  106. return collect($details_imgs)->map(function($item) {
  107. return $this->attachmentService->switchImgToAliOss($item);
  108. });
  109. }
  110. public function getGoodsNameAttribute()
  111. {
  112. return $this->attributes['name'];
  113. }
  114. public function getNameAttribute($value)
  115. {
  116. return $value . ' ' . $this->attributes['goods_unit'];
  117. }
  118. public function store()
  119. {
  120. return $this->belongsTo(Store::class, 'store_id', 'id');
  121. }
  122. public function carts()
  123. {
  124. return $this->morphMany(ShoppingCart::class, 'goods');
  125. }
  126. }