ProductPresell.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\store\product;
  12. use app\common\model\BaseModel;
  13. use app\common\model\system\merchant\Merchant;
  14. use app\common\repositories\store\coupon\StoreCouponRepository;
  15. use app\common\repositories\store\product\SpuRepository;
  16. class ProductPresell extends BaseModel
  17. {
  18. /**
  19. *
  20. * @return string
  21. * @author Qinii
  22. * @day 2020-10-12
  23. */
  24. public static function tablePk(): string
  25. {
  26. return 'product_presell_id';
  27. }
  28. /**
  29. *
  30. * @return string
  31. * @author Qinii
  32. * @day 2020-10-12
  33. */
  34. public static function tableName(): string
  35. {
  36. return 'store_product_presell';
  37. }
  38. public function product()
  39. {
  40. return $this->hasOne(Product::class,'product_id','product_id');
  41. }
  42. public function presellSku()
  43. {
  44. return$this->hasMany(ProductPresellSku::class,'product_presell_id','product_presell_id');
  45. }
  46. public function merchant()
  47. {
  48. return $this->hasOne(Merchant::class,'mer_id','mer_id');
  49. }
  50. /**
  51. * 状态
  52. * @return int
  53. * @author Qinii
  54. * @day 2020-10-14
  55. */
  56. public function getPresellStatusAttr()
  57. {
  58. $start_time = strtotime($this->start_time);
  59. $end_time = strtotime($this->end_time);
  60. $time = time();
  61. //已结束
  62. if($this->action_status == -1) return 2;
  63. //未开始
  64. if($start_time > $time) return 0;
  65. //进行中
  66. if($start_time <= $time && $end_time > $time) {
  67. if($this->product_status !== 1 || $this->status !==1 || $this->is_show !== 1) return 0;
  68. return 1;
  69. }
  70. //已结束
  71. if($end_time <= $time) {
  72. if($this->presell_type == 1 || ($this->presell_type == 2 && (strtotime($this->final_end_time) < $time))){
  73. $this->action_status = -1;
  74. $this->save();
  75. }
  76. app()->make(SpuRepository::class)->changeStatus($this->product_presell_id,2);
  77. return 2;
  78. }
  79. }
  80. public function getStarAttr()
  81. {
  82. return Spu::where('product_type',2)->where('activity_id',$this->product_presell_id)->value('star');
  83. }
  84. public function getUsStatusAttr()
  85. {
  86. return ($this->product_status == 1) ? ($this->status == 1 ? ( $this->is_show ? 1 : 0 ) : -1) : -1;
  87. }
  88. /**
  89. * 第一阶段 参与人数
  90. * @return mixed
  91. * @author Qinii
  92. * @day 2020-10-30
  93. */
  94. public function getTattendOneAttr()
  95. {
  96. $data['all'] = ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('one_take');
  97. $data['pay']= ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('one_pay');
  98. return $data;
  99. }
  100. /**
  101. * 第二阶段 参与人数
  102. * @return mixed
  103. * @author Qinii
  104. * @day 2020-10-30
  105. */
  106. public function getTattendTwoAttr()
  107. {
  108. $data['all'] = 0;
  109. $data['pay'] = 0;
  110. if($this->presell_type == 2){
  111. $data['all'] = ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('one_pay');
  112. $data['pay']= ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('two_pay');
  113. }
  114. return $data;
  115. }
  116. /**
  117. * 获取一张店铺优惠券
  118. * @return array|\think\Model|null
  119. * @author Qinii
  120. * @day 2020-10-30
  121. */
  122. public function getCouponAttr()
  123. {
  124. $make = app()->make(StoreCouponRepository::class);
  125. return $coupon = $make->validCouponQuery(0,0)->where('mer_id',$this->mer_id)->find();
  126. }
  127. public function getSelesAttr()
  128. {
  129. return ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('seles');
  130. }
  131. public function getStockAttr()
  132. {
  133. return ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('stock');
  134. }
  135. public function getStockCountAttr()
  136. {
  137. return ProductPresellSku::where('product_presell_id',$this->product_presell_id)->sum('stock_count');
  138. }
  139. }