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.

137 lines
3.2 KiB

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_new';
  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. ];
  60. protected function boot(): void
  61. {
  62. parent::boot();
  63. self::addGlobalScope('normal', function (Builder $builder) {
  64. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  65. });
  66. }
  67. public function scopeOrderByDefault($query, $sort)
  68. {
  69. return $query->orderBy('sort', $sort)->orderBy('id', $sort);
  70. }
  71. public function scopeOrderBySales($query, $sort)
  72. {
  73. return $query->orderBy('sales', $sort);
  74. }
  75. public function scopeOrderByPrice($query, $sort)
  76. {
  77. return $query->orderBy('price', $sort);
  78. }
  79. public function getCoverImgAttribute($value)
  80. {
  81. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  82. }
  83. public function getMonthSalesAttribute()
  84. {
  85. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  86. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_'.$this->id);
  87. }
  88. public function getCartNumAttribute()
  89. {
  90. return 0;
  91. // $userId = $this->request->user->id ?? 0;
  92. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  93. }
  94. public function getIsEffectiveAttribute()
  95. {
  96. return 1;
  97. }
  98. public function getNoneffectiveNoteAttribute()
  99. {
  100. return '';
  101. }
  102. public function getDetailsImgsUrlAttribute()
  103. {
  104. $details_imgs = $this->details_imgs;
  105. return collect($details_imgs)->map(function($item) {
  106. return $this->attachmentService->switchImgToAliOss($item);
  107. });
  108. }
  109. public function store()
  110. {
  111. return $this->belongsTo(Store::class, 'store_id', 'id');
  112. }
  113. public function carts()
  114. {
  115. return $this->morphMany(ShoppingCart::class, 'goods');
  116. }
  117. }