ProductAssist.php 4.0 KB

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