ProductAssist.php 4.5 KB

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