ProductGroupDao.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\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. $query->when(isset($where['level']) && $where['level'] !== '',function($query)use($where) {
  79. $query->where('U.star',$where['level']);
  80. });
  81. if(isset($where['order'])) {
  82. switch ($where['order']) {
  83. case 'sort':
  84. $order = 'U.sort DESC';
  85. break;
  86. case 'rank':
  87. $order = 'U.rank DESC';
  88. break;
  89. case 'star':
  90. $order = 'U.star DESC,U.rank DESC';
  91. break;
  92. default:
  93. $order = 'U.star DESC,U.rank DESC,U.sort DES';
  94. break;
  95. }
  96. $query->order($order.',ProductGroup.create_time DESC');
  97. }
  98. return $query->where('ProductGroup.is_del',0);
  99. }
  100. public function actionShow()
  101. {
  102. return [
  103. 'is_show' => 1,
  104. 'action_status' => 1,
  105. 'product_status' => 1,
  106. 'status' => 1,
  107. 'end_time' => time()
  108. ];
  109. }
  110. public function category()
  111. {
  112. $query = ProductGroup::alias('G')->join('StoreProduct P','G.product_id = P.product_id')
  113. ->join('StoreCategory C','P.cate_id = C.store_category_id');
  114. $query->where('G.is_show',1)->where('G.action_status',1)->where('G.product_status',1);
  115. $query->group('G.product_id');
  116. return $query->column('path');
  117. }
  118. /**
  119. * TODO
  120. * @author Qinii
  121. * @day 1/27/21
  122. */
  123. public function valActiveStatus()
  124. {
  125. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  126. $id = $query->column($this->getPk());
  127. if($id) {
  128. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  129. $where = [
  130. 'product_type' => 4,
  131. 'activity_ids' => $id
  132. ];
  133. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  134. }
  135. }
  136. }