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.

94 lines
2.1 KiB

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