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.

97 lines
2.1 KiB

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. /**
  13. */
  14. class Goods extends Model
  15. {
  16. use SoftDeletes;
  17. const ON_SALE_YES = 1;
  18. const ON_SALE_NO = 2;
  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. ];
  50. protected function boot(): void
  51. {
  52. parent::boot();
  53. self::addGlobalScope('normal', function (Builder $builder) {
  54. $builder->where(['on_sale' => self::ON_SALE_YES]);
  55. });
  56. }
  57. public function scopeOrderByDefault($query, $sort)
  58. {
  59. return $query->orderBy('sort', $sort)->orderBy('id', $sort);
  60. }
  61. public function scopeOrderBySales($query, $sort)
  62. {
  63. return $query->orderBy('sales', $sort);
  64. }
  65. public function scopeOrderByPrice($query, $sort)
  66. {
  67. return $query->orderBy('price', $sort);
  68. }
  69. public function getMonthSalesAttribute()
  70. {
  71. return 1;
  72. // $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  73. // return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  74. }
  75. public function getCartNumAttribute()
  76. {
  77. return (integer)$this->shopCartService->check($this->id);
  78. }
  79. public function store()
  80. {
  81. return $this->belongsTo(Store::class, 'store_id', 'id');
  82. }
  83. }