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.

112 lines
2.7 KiB

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