ProductGroup.php 3.8 KB

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