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.

115 lines
2.5 KiB

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