ProductGroupDao.php 7.1 KB

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