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.

84 lines
1.8 KiB

  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\SoftDeletes;
  9. use Hyperf\Utils\ApplicationContext;
  10. use Hyperf\Di\Annotation\Inject;
  11. /**
  12. */
  13. class Goods extends Model
  14. {
  15. use SoftDeletes;
  16. /**
  17. * @Inject
  18. * @var ShopCartServiceInterface
  19. */
  20. protected $shopCartService;
  21. /**
  22. * The table associated with the model.
  23. *
  24. * @var string
  25. */
  26. protected $table = 'lanzu_goods';
  27. /**
  28. * The attributes that are mass assignable.
  29. *
  30. * @var array
  31. */
  32. protected $fillable = [];
  33. /**
  34. * The attributes that should be cast to native types.
  35. *
  36. * @var array
  37. */
  38. protected $casts = [
  39. 'details_imgs' => 'array',
  40. 'spec' => 'array',
  41. 'tags' => 'array',
  42. ];
  43. protected $appends = [
  44. 'month_sales',
  45. 'cart_num',
  46. ];
  47. public function scopeOrderByDefault($query, $sort)
  48. {
  49. return $query->orderBy('id', $sort);
  50. }
  51. public function scopeOrderBySales($query, $sort)
  52. {
  53. return $query->orderBy('sales', $sort);
  54. }
  55. public function scopeOrderByPrice($query, $sort)
  56. {
  57. return $query->orderBy('price', $sort);
  58. }
  59. public function getMonthSalesAttribute()
  60. {
  61. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  62. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
  63. }
  64. public function getCartNumAttribute()
  65. {
  66. return (integer)$this->shopCartService->check($this->id);
  67. }
  68. public function store()
  69. {
  70. return $this->belongsTo(Store::class, 'store_id', 'id');
  71. }
  72. }