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.

110 lines
2.4 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. /**
  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 $visible = [
  52. 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'sales as total_sales'
  53. ];
  54. protected function boot(): void
  55. {
  56. parent::boot();
  57. self::addGlobalScope('normal', function (Builder $builder) {
  58. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  59. });
  60. }
  61. public function scopeOrderByDefault($query, $sort)
  62. {
  63. return $query->orderBy('sort', $sort)->orderBy('id', $sort);
  64. }
  65. public function scopeOrderBySales($query, $sort)
  66. {
  67. return $query->orderBy('sales', $sort);
  68. }
  69. public function scopeOrderByPrice($query, $sort)
  70. {
  71. return $query->orderBy('price', $sort);
  72. }
  73. public function getMonthSalesAttribute()
  74. {
  75. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  76. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  77. }
  78. public function getCartNumAttribute()
  79. {
  80. return (integer)$this->shopCartService->check($this->id);
  81. }
  82. public function getIsEffectiveAttribute()
  83. {
  84. return 1;
  85. }
  86. public function getNoneffectiveNoteAttribute()
  87. {
  88. return '';
  89. }
  90. public function store()
  91. {
  92. return $this->belongsTo(Store::class, 'store_id', 'id');
  93. }
  94. }