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.

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