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.

113 lines
2.8 KiB

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(['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').'_'.$this->id);
  56. }
  57. public function getCartNumAttribute()
  58. {
  59. $userId = $this->request->user->id ?? 0;
  60. return $userId ? (integer)$this->shopCartService->check($userId, $this->id,1) : 0;
  61. }
  62. public function getIsEffectiveAttribute()
  63. {
  64. return 2;
  65. }
  66. public function getNoneffectiveNoteAttribute()
  67. {
  68. return '已抢光';
  69. }
  70. public function getTotalSecondsAttribute()
  71. {
  72. return $this->attributes['expire_time'] - time();
  73. }
  74. public function getCoverImgAttribute($value)
  75. {
  76. return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_600_Q90);
  77. }
  78. public function store()
  79. {
  80. return $this->belongsTo(Store::class, 'store_id', 'id');
  81. }
  82. public function carts()
  83. {
  84. return $this->morphMany(ShoppingCart::class, 'goods');
  85. }
  86. public function getDetailsImgsUrlAttribute()
  87. {
  88. $details_imgs = $this->details_imgs;
  89. return collect($details_imgs)->map(function($item) {
  90. return $this->attachmentService->switchImgToAliOss($item);
  91. });
  92. }
  93. }