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.

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