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.

127 lines
2.9 KiB

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