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.

96 lines
2.3 KiB

  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. ];
  39. // protected $visible = [
  40. // 'id', 'cover_img', 'name', 'original_price', 'price', 'inventory', 'store_id', 'spec', 'tags', 'sales',
  41. // 'month_sales', 'cart_num', 'is_effective', 'noneffective_note', 'total_seconds','store_id'
  42. // ];
  43. protected function boot(): void
  44. {
  45. parent::boot();
  46. self::addGlobalScope('normal', function (Builder $builder) {
  47. $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]);
  48. });
  49. }
  50. public function getMonthSalesAttribute()
  51. {
  52. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  53. return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('Ym').'_'.$this->id);
  54. }
  55. public function getCartNumAttribute()
  56. {
  57. return (integer)$this->shopCartService->check($this->id);
  58. }
  59. public function getIsEffectiveAttribute()
  60. {
  61. return 2;
  62. }
  63. public function getNoneffectiveNoteAttribute()
  64. {
  65. return '已抢光';
  66. }
  67. public function getTotalSecondsAttribute()
  68. {
  69. return $this->attributes['expire_time'] - time();
  70. }
  71. public function getCoverImgAttribute($value)
  72. {
  73. return $this->attachmentService->switchImgToAliOss($value);
  74. }
  75. public function store()
  76. {
  77. return $this->belongsTo(Store::class, 'store_id', 'id');
  78. }
  79. }