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.

93 lines
2.3 KiB

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