ProductGroup.php 4.4 KB

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