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.

118 lines
2.9 KiB

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