ProductGroupDao.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\common\dao\store\product;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\store\product\ProductGroup;
  5. use app\common\repositories\store\product\SpuRepository;
  6. class ProductGroupDao extends BaseDao
  7. {
  8. public function getModel(): string
  9. {
  10. return ProductGroup::class;
  11. }
  12. public function search($where)
  13. {
  14. $query = ProductGroup::hasWhere('product',function($query)use($where){
  15. $query->where('status',1);
  16. $query->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  17. $query->whereLike('store_name',"%{$where['keyword']}%");
  18. });
  19. });
  20. $query->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  21. $query->where('ProductGroup.is_show',$where['is_show']);
  22. })
  23. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  24. if($where['product_status'] == -1){
  25. $query->where('ProductGroup.product_status','in',[-1,-2]);
  26. }else{
  27. $query->where('ProductGroup.product_status',$where['product_status']);
  28. }
  29. })
  30. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  31. $query->where('ProductGroup.status',$where['status']);
  32. })
  33. ->when(isset($where['end_time']) && $where['end_time'] !== '',function($query)use($where){
  34. $query->whereTime('ProductGroup.end_time','>',$where['end_time']);
  35. })
  36. ->when(isset($where['active_type']) && $where['active_type'] !== '',function($query)use($where){
  37. $query->where('ProductGroup.action_status',$where['active_type']);
  38. })
  39. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  40. $query->join('Merchant M','M.mer_id = ProductGroup.mer_id')->where('is_trader',$where['is_trader']);
  41. })
  42. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  43. $query->where('ProductGroup.mer_id',$where['mer_id']);
  44. })
  45. ->when(isset($where['product_group_id']) && $where['product_group_id'] !== '',function($query)use($where){
  46. $query->where('ProductGroup.product_group_id',$where['product_group_id']);
  47. })
  48. ->when(isset($where['store_category_id']) && $where['store_category_id'] !== '',function($query)use($where){
  49. $query->join('StoreCategory C','Product.cate_id = C.store_category_id')
  50. ->whereLike('path',"/{$where['store_category_id']}/%");
  51. })
  52. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  53. if($where['us_status'] == 0) {
  54. $query->where('ProductGroup.is_show',0)->where('ProductGroup.status',1)->where('ProductGroup.product_status',1);
  55. }
  56. if($where['us_status'] == 1) {
  57. $query->where('ProductGroup.is_show',1)->where('ProductGroup.status',1)->where('ProductGroup.product_status',1);
  58. }
  59. if($where['us_status'] == -1) {
  60. $query->where(function($query){
  61. $query->where('ProductGroup.status',0)->whereOr('ProductGroup.product_status','<>',1);
  62. });
  63. }
  64. });
  65. $query->join('StoreSpu U','ProductGroup.product_group_id = U.activity_id')->where('U.product_type',4);
  66. $query->when(isset($where['star']) && $where['star'] !== '',function($query)use($where){
  67. $query->where('U.star',$where['star']);
  68. });
  69. $query->when(isset($where['level']) && $where['level'] !== '',function($query)use($where) {
  70. $query->where('U.star',$where['level']);
  71. });
  72. if(isset($where['order'])) {
  73. switch ($where['order']) {
  74. case 'sort':
  75. $order = 'U.sort DESC';
  76. break;
  77. case 'rank':
  78. $order = 'U.rank DESC';
  79. break;
  80. case 'star':
  81. $order = 'U.star DESC,U.rank DESC';
  82. break;
  83. default:
  84. $order = 'U.star DESC,U.rank DESC,U.sort DES';
  85. break;
  86. }
  87. $query->order($order.',ProductGroup.create_time DESC');
  88. }
  89. return $query->where('ProductGroup.is_del',0);
  90. }
  91. public function actionShow()
  92. {
  93. return [
  94. 'is_show' => 1,
  95. 'action_status' => 1,
  96. 'product_status' => 1,
  97. 'status' => 1,
  98. 'end_time' => time()
  99. ];
  100. }
  101. public function category()
  102. {
  103. $query = ProductGroup::alias('G')->join('StoreProduct P','G.product_id = P.product_id')
  104. ->join('StoreCategory C','P.cate_id = C.store_category_id');
  105. $query->where('G.is_show',1)->where('G.action_status',1)->where('G.product_status',1);
  106. $query->group('G.product_id');
  107. return $query->column('path');
  108. }
  109. /**
  110. * TODO
  111. * @author Qinii
  112. * @day 1/27/21
  113. */
  114. public function valActiveStatus()
  115. {
  116. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  117. $id = $query->column($this->getPk());
  118. if($id) {
  119. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  120. $where = [
  121. 'product_type' => 4,
  122. 'activity_ids' => $id
  123. ];
  124. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  125. }
  126. }
  127. /**
  128. * TODO 软删除商户的所有商品
  129. * @param $merId
  130. * @author Qinii
  131. * @day 5/15/21
  132. */
  133. public function clearProduct($merId)
  134. {
  135. $this->getModel()::getDb()->where('mer_id',$merId)->update(['is_del' => 1]);
  136. }
  137. }