ProductPresell.php 4.9 KB

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