ProductAssist.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\product\ProductAssistSetRepository;
  15. use app\common\repositories\store\product\ProductAssistUserRepository;
  16. use app\common\repositories\store\product\SpuRepository;
  17. use crmeb\jobs\ChangeSpuStatusJob;
  18. class ProductAssist extends BaseModel
  19. {
  20. /**
  21. * TODO
  22. * @return string
  23. * @author Qinii
  24. * @day 2020-10-12
  25. */
  26. public static function tablePk(): string
  27. {
  28. return 'product_assist_id';
  29. }
  30. /**
  31. * TODO
  32. * @return string
  33. * @author Qinii
  34. * @day 2020-10-12
  35. */
  36. public static function tableName(): string
  37. {
  38. return 'store_product_assist';
  39. }
  40. public function product()
  41. {
  42. return $this->hasOne(Product::class,'product_id','product_id');
  43. }
  44. public function assistSku()
  45. {
  46. return$this->hasMany(ProductAssistSku::class,'product_assist_id','product_assist_id');
  47. }
  48. public function merchant()
  49. {
  50. return $this->hasOne(Merchant::class,'mer_id','mer_id');
  51. }
  52. /**
  53. * TODO 状态
  54. * @return int
  55. * @author Qinii
  56. * @day 2020-10-14
  57. */
  58. public function getAssistStatusAttr()
  59. {
  60. $start_time = strtotime($this->start_time);
  61. $end_time = strtotime($this->end_time);
  62. $time = time();
  63. //已结束
  64. if($this->action_status == -1) return 2;
  65. //未开始
  66. if($start_time > $time) return 0;
  67. //进行中
  68. if($start_time <= $time && $end_time > $time) {
  69. if($this->product_status != 1 || $this->status != 1 || $this->is_show != 1) return 0;
  70. return 1;
  71. }
  72. //已结束
  73. if($end_time <= $time) {
  74. $this->action_status = -1;
  75. $this->save();
  76. //app()->make(SpuRepository::class)->changeStatus($this->product_assist_id,3);
  77. queue(ChangeSpuStatusJob::class, ['id' => $this->product_assist_id, 'product_type' => 3]);
  78. return 2;
  79. }
  80. }
  81. public function getStarAttr()
  82. {
  83. return Spu::where('product_type',3)->where('activity_id',$this->product_assist_id)->value('star');
  84. }
  85. public function getUsStatusAttr()
  86. {
  87. return ($this->product_status == 1) ? ($this->status == 1 ? ( $this->is_show ? 1 : 0 ) : -1) : -1;
  88. }
  89. public function getUserCountAttr()
  90. {
  91. return ProductAssistUser::where('product_assist_id',$this->product_assist_id)->count() + ProductAssistSet::where('product_assist_id',$this->product_assist_id)->count();
  92. }
  93. /**
  94. * TODO 助力成功人数
  95. * @return mixed
  96. * @author Qinii
  97. * @day 2020-10-30
  98. */
  99. public function getSuccessAttr()
  100. {
  101. $make = app()->make(ProductAssistSetRepository::class);
  102. $where = [
  103. 'product_assist_id' => $this->product_assist_id,
  104. 'status' => [10, 20]
  105. ];
  106. return $make->getSearch($where)->count();
  107. }
  108. /**
  109. * TODO 支付成功人数
  110. * @return mixed
  111. * @author Qinii
  112. * @day 2020-10-30
  113. */
  114. public function getPayAttr()
  115. {
  116. $make = app()->make(ProductAssistSetRepository::class);
  117. $where = [
  118. 'product_assist_id' => $this->product_assist_id,
  119. 'status' => 20
  120. ];
  121. return $make->getSearch($where)->count();
  122. }
  123. /**
  124. * TODO 助力人数
  125. * @return mixed
  126. * @author Qinii
  127. * @day 2020-10-30
  128. */
  129. public function getAllAttr()
  130. {
  131. $make = app()->make(ProductAssistUserRepository::class);
  132. $where = [
  133. 'product_assist_id' => $this->product_assist_id,
  134. ];
  135. return $make->getSearch($where)->count();
  136. }
  137. public function getStockCountAttr()
  138. {
  139. return ProductAssistSku::where('product_assist_id',$this->product_assist_id)->sum('stock_count');
  140. }
  141. public function getStockAttr()
  142. {
  143. return ProductAssistSku::where('product_assist_id',$this->product_assist_id)->sum('stock');
  144. }
  145. }