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.

133 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Model\v3;
  3. use App\Constants\v3\Goods as GoodsConstants;
  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\Service\v3\Interfaces\ShopCartServiceInterface;
  9. use App\TaskWorker\SSDBTask;
  10. use Hyperf\Database\Model\Builder;
  11. use Hyperf\Database\Model\SoftDeletes;
  12. use Hyperf\Utils\ApplicationContext;
  13. use Hyperf\Di\Annotation\Inject;
  14. class GoodsActivity extends Model
  15. {
  16. use SoftDeletes;
  17. /**
  18. * @Inject
  19. * @var ShopCartServiceInterface
  20. */
  21. protected $shopCartService;
  22. /**
  23. * @Inject
  24. * @var AttachmentServiceInterface
  25. */
  26. protected $attachmentService;
  27. protected $table = 'lanzu_goods_activity';
  28. protected $casts = [
  29. 'details_imgs' => 'array',
  30. 'spec' => 'array',
  31. 'tags' => 'array',
  32. ];
  33. protected $appends = [
  34. 'month_sales',
  35. 'cart_num',
  36. 'is_effective',
  37. 'noneffective_note',
  38. 'total_seconds',
  39. 'details_imgs_url',
  40. ];
  41. // protected $visible = [
  42. // 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'spec', 'tags', 'sales',
  43. // 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds','store_id'
  44. // ];
  45. protected function boot(): void
  46. {
  47. parent::boot();
  48. self::addGlobalScope('normal', function (Builder $builder) {
  49. $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]);
  50. });
  51. }
  52. public function getMonthSalesAttribute()
  53. {
  54. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  55. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_2_'.$this->id);
  56. }
  57. public function getCartNumAttribute()
  58. {
  59. return 0;
  60. // $userId = $this->request->user->id ?? 0;
  61. // return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  62. }
  63. public function getIsEffectiveAttribute()
  64. {
  65. if ($this->attributes['expire_time'] < time() || ($this->attributes['inventory'] <= 0 && $this->attributes['is_infinite'] != 1)) {
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. public function getNoneffectiveNoteAttribute()
  71. {
  72. $msg = '';
  73. // 获取冻结的库存
  74. $inventoryFrozen = $this->goodsInventoryService->getSold(2, $this->attributes['id']);
  75. if ($this->attributes['expire_time'] < time()) {
  76. $msg = '已结束';
  77. } elseif ($this->attributes['inventory'] <= $inventoryFrozen && $this->attributes['is_infinite'] != 1) {
  78. $msg = '已抢光';
  79. }
  80. return $msg;
  81. }
  82. public function getTotalSecondsAttribute()
  83. {
  84. return $this->attributes['expire_time'] - time();
  85. }
  86. public function getCoverImgAttribute($value)
  87. {
  88. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  89. }
  90. public function getNameAttribute($value)
  91. {
  92. return $value . ' ' . $this->attributes['goods_unit'];
  93. }
  94. public function store()
  95. {
  96. return $this->belongsTo(Store::class, 'store_id', 'id');
  97. }
  98. public function carts()
  99. {
  100. return $this->morphMany(ShoppingCart::class, 'goods');
  101. }
  102. public function getDetailsImgsUrlAttribute()
  103. {
  104. $details_imgs = $this->details_imgs;
  105. return collect($details_imgs)->map(function($item) {
  106. return $this->attachmentService->switchImgToAliOss($item);
  107. });
  108. }
  109. }