ProductGroup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\store\order\StoreOrderProduct;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\product\SpuRepository;
  17. use crmeb\jobs\ChangeSpuStatusJob;
  18. class ProductGroup extends BaseModel
  19. {
  20. /**
  21. * TODO
  22. * @return string
  23. * @author Qinii
  24. * @day 1/7/21
  25. */
  26. public static function tablePk(): string
  27. {
  28. return 'product_group_id';
  29. }
  30. /**
  31. * TODO
  32. * @return string
  33. * @author Qinii
  34. * @day 1/7/21
  35. */
  36. public static function tableName(): string
  37. {
  38. return 'store_product_group';
  39. }
  40. public function product()
  41. {
  42. return $this->hasOne(Product::class,'product_id','product_id');
  43. }
  44. public function merchant()
  45. {
  46. return $this->hasOne(Merchant::class,'mer_id','mer_id');
  47. }
  48. public function groupBuying()
  49. {
  50. return $this->hasMany(ProductGroupBuying::class,'product_group_id','product_group_id');
  51. }
  52. public function activeSku()
  53. {
  54. return $this->hasMany(ProductGroupSku::class,'product_group_id','product_group_id');
  55. }
  56. public function getActionStatusAttr($value)
  57. {
  58. if($value== -1) return -1;
  59. $start_time = strtotime($this->start_time);
  60. $end_time = strtotime($this->end_time);
  61. if($start_time > time()) return 0;
  62. if($start_time <= time() && $end_time > time()){
  63. $this->action_status = 1;
  64. $this->save();
  65. return 1;
  66. }
  67. if($end_time <= time()){
  68. $this->action_status = -1;
  69. $this->save();
  70. queue(ChangeSpuStatusJob::class, ['id' => $this->product_group_id, 'product_type' => 4]);
  71. //app()->make(SpuRepository::class)->changeStatus($this->product_group_id,4);
  72. return -1;
  73. }
  74. }
  75. public function getStockAttr()
  76. {
  77. return ProductGroupSku::where('product_group_id',$this->product_group_id)->sum('stock');
  78. }
  79. public function getStockCountAttr()
  80. {
  81. return ProductGroupSku::where('product_group_id',$this->product_group_id)->sum('stock_count');
  82. }
  83. public function getUsStatusAttr()
  84. {
  85. return ($this->product_status == 1) ? ($this->status == 1 ? ( $this->is_show ? 1 : 0 ) : -1) : -1;
  86. }
  87. //销量
  88. public function getSalesAttr()
  89. {
  90. $make = app()->make(StoreOrderRepository::class);
  91. $where = [
  92. 'product_id' => $this->product_id,
  93. 'product_type' => 4,
  94. ];
  95. return $make->getTattendCount($where,null)->sum('product_num');
  96. }
  97. //参与人次:所有(含待付款)
  98. public function getCountTakeAttr()
  99. {
  100. return StoreOrderProduct::where('product_id',$this->product_id)->where('product_type',4)->count();
  101. }
  102. //成团人数数量: 成功的团,真实人数
  103. public function getCountUserAttr()
  104. {
  105. return ProductGroupUser::where('product_group_id',$this->product_group_id)
  106. ->where('uid','<>',0)->where('status',10)->count();
  107. }
  108. public function getStarAttr()
  109. {
  110. return Spu::where('product_type',4)->where('activity_id',$this->product_group_id)->value('star');
  111. }
  112. public function searchProductStatusAttr($query,$value)
  113. {
  114. if($value == -1){
  115. $query->where('product_status','in',[-1,-2]);
  116. }else {
  117. $query->where('product_status', $value);
  118. }
  119. }
  120. public function searchMerIdAttr($query,$value)
  121. {
  122. $query->where('mer_id',$value);
  123. }
  124. public function searchStatusAttr($query,$value)
  125. {
  126. $query->where('mer_id',$value);
  127. }
  128. public function searchProductGroupIdAttr($query,$value)
  129. {
  130. $query->where('product_group_id',$value);
  131. }
  132. }