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.

79 lines
1.8 KiB

5 years ago
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\TaskWorker\SSDBTask;
  7. use Hyperf\Database\Model\Builder;
  8. use Hyperf\Database\Model\SoftDeletes;
  9. use Hyperf\Utils\ApplicationContext;
  10. use App\Constants\v3\Store as StoreConstants;
  11. /**
  12. */
  13. class Store extends Model
  14. {
  15. use SoftDeletes;
  16. /**
  17. * The table associated with the model.
  18. *
  19. * @var string
  20. */
  21. protected $table = 'lanzu_store';
  22. /**
  23. * The attributes that are mass assignable.
  24. *
  25. * @var array
  26. */
  27. protected $fillable = [];
  28. /**
  29. * The attributes that should be cast to native types.
  30. *
  31. * @var array
  32. */
  33. protected $casts = [];
  34. protected $appends = [
  35. 'month_sales'
  36. ];
  37. protected function boot(): void
  38. {
  39. parent::boot();
  40. self::addGlobalScope('normal', function (Builder $builder) {
  41. $builder->where(['is_open' => StoreConstants::IS_OPEN_YES, 'status' => StoreConstants::STATUS_PASS]);
  42. });
  43. }
  44. public function scopeOrderByDefault($query, $sort)
  45. {
  46. return $query->orderBy('sort', $sort)->orderBy('id', $sort);
  47. }
  48. public function scopeOrderBySales($query, $sort)
  49. {
  50. return $query->orderBy('sales', $sort);
  51. }
  52. public function getMonthSalesAttribute() {
  53. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  54. return (integer)$ssdb->exec('get', SsdbKeys::STORE_MONTH_SALES.date('YM').'_'.$this->id);
  55. }
  56. public function goods()
  57. {
  58. return $this->hasMany(Goods::class, 'store_id', 'id');
  59. }
  60. public function shoppingCart()
  61. {
  62. return $this->hasMany(ShoppingCart::class, 'store_id', 'id');
  63. }
  64. public function market()
  65. {
  66. return $this->belongsTo(Market::class, 'market_id', 'id');
  67. }
  68. }