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.

136 lines
3.1 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\SsdbKeys;
  5. use App\Model\Model;
  6. use App\Service\v3\Interfaces\AttachmentServiceInterface;
  7. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  8. use App\TaskWorker\SSDBTask;
  9. use Hyperf\Database\Model\Builder;
  10. use Hyperf\Database\Model\SoftDeletes;
  11. use Hyperf\Utils\ApplicationContext;
  12. use Hyperf\Di\Annotation\Inject;
  13. use App\Constants\v3\Goods as GoodsConstants;
  14. use Hyperf\Utils\Collection;
  15. /**
  16. */
  17. class Goods extends Model
  18. {
  19. use SoftDeletes;
  20. /**
  21. * @Inject
  22. * @var ShopCartServiceInterface
  23. */
  24. protected $shopCartService;
  25. /**
  26. * @Inject
  27. * @var AttachmentServiceInterface
  28. */
  29. protected $attachmentService;
  30. /**
  31. * The table associated with the model.
  32. *
  33. * @var string
  34. */
  35. protected $table = 'lanzu_goods_new';
  36. /**
  37. * The attributes that are mass assignable.
  38. *
  39. * @var array
  40. */
  41. protected $fillable = [];
  42. /**
  43. * The attributes that should be cast to native types.
  44. *
  45. * @var array
  46. */
  47. protected $casts = [
  48. 'details_imgs' => 'array',
  49. 'spec' => 'array',
  50. 'tags' => 'array',
  51. ];
  52. protected $appends = [
  53. 'month_sales',
  54. 'cart_num',
  55. 'is_effective',
  56. 'noneffective_note',
  57. 'details_imgs_url',
  58. ];
  59. protected function boot(): void
  60. {
  61. parent::boot();
  62. self::addGlobalScope('normal', function (Builder $builder) {
  63. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  64. });
  65. }
  66. public function scopeOrderByDefault($query, $sort)
  67. {
  68. return $query->orderBy('sort', $sort)->orderBy('id', $sort);
  69. }
  70. public function scopeOrderBySales($query, $sort)
  71. {
  72. return $query->orderBy('sales', $sort);
  73. }
  74. public function scopeOrderByPrice($query, $sort)
  75. {
  76. return $query->orderBy('price', $sort);
  77. }
  78. public function getCoverImgAttribute($value)
  79. {
  80. return $this->attachmentService->switchImgToAliOss($value);
  81. }
  82. public function getMonthSalesAttribute()
  83. {
  84. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  85. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_'.$this->id);
  86. }
  87. public function getCartNumAttribute()
  88. {
  89. $userId = $this->request->user->id ?? 0;
  90. return $userId ? (integer)$this->shopCartService->check($userId, $this->id) : 0;
  91. }
  92. public function getIsEffectiveAttribute()
  93. {
  94. return 1;
  95. }
  96. public function getNoneffectiveNoteAttribute()
  97. {
  98. return '';
  99. }
  100. public function getDetailsImgsUrlAttribute()
  101. {
  102. $details_imgs = $this->details_imgs;
  103. $img_host = config('alioss.img_host').'/';
  104. return collect($details_imgs)->map(function($item) use ($img_host){
  105. return $img_host . $item;
  106. });
  107. }
  108. public function store()
  109. {
  110. return $this->belongsTo(Store::class, 'store_id', 'id');
  111. }
  112. public function carts()
  113. {
  114. return $this->morphMany(ShoppingCart::class, 'goods');
  115. }
  116. }