ProductPresellDao.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\ProductPresell;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\repositories\store\product\SpuRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. class ProductPresellDao extends BaseDao
  18. {
  19. protected function getModel(): string
  20. {
  21. return ProductPresell::class;
  22. }
  23. public function search(array $where)
  24. {
  25. $query = ProductPresell::hasWhere('product',function($query)use($where){
  26. $query->when(isset($where['product_show']) && $where['product_show'] !== '',function($query)use($where){
  27. $query->where('is_del',0)->where('mer_status',1);
  28. })
  29. ->when(isset($where['product_type']) && $where['product_type'] !== '',function($query)use($where){
  30. $query->where('product_type',2);
  31. })
  32. ->where('status',1);
  33. });
  34. $query->when(isset($where['product_presell_id']) && $where['product_presell_id'] !== '',function($query)use($where){
  35. $query->where('product_presell_id',$where['product_presell_id']);
  36. })
  37. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  38. $query->where('ProductPresell.mer_id',$where['mer_id']);
  39. })
  40. ->when(isset($where['action_status']) && $where['action_status'] !== '',function($query)use($where){
  41. $query->where('ProductPresell.action_status',$where['action_status']);
  42. })
  43. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  44. $query->whereLike('ProductPresell.store_name|ProductPresell.product_id',"%{$where['keyword']}%");
  45. })
  46. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  47. if($where['product_status'] == -1){
  48. $query->where('ProductPresell.product_status','in',[-1,-2]);
  49. }else{
  50. $query->where('ProductPresell.product_status',$where['product_status']);
  51. }
  52. })
  53. ->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
  54. switch ($where['type']){
  55. case 0: //未开始
  56. if(isset($where['api_type'])){
  57. $query->where('product_status',1);
  58. }
  59. $query->where(function($query){
  60. $query->where('start_time','> TIME',time())->whereOr(function($query){
  61. $query->whereOr('ProductPresell.status','<>',1)->whereOr('ProductPresell.is_show','<>',1);
  62. });
  63. });
  64. break;
  65. case 1: //进行中
  66. $query->whereTime('start_time','<=',time())->whereTime('end_time','>',time())
  67. ->where('product_status',1)->where('ProductPresell.status',1)->where('ProductPresell.is_show',1);
  68. break;
  69. case 2: //已结束
  70. // if(isset($where['api_type'])){
  71. // $query->where('ProductPresell.presell_type',2)
  72. // ->whereTime('end_time','<=',time())
  73. // ->whereTime('final_end_time','>=',time());
  74. // }else{
  75. $query->where(function($query){
  76. $query->where('action_status',-1)->whereOr('end_time','<= TIME',time());
  77. });
  78. // }
  79. break;
  80. case 3: //已关闭
  81. $query->where(function($query){
  82. $query->where(function($query){
  83. $query->where('ProductPresell.presell_type',1)->whereTime('end_time','<',time());
  84. })->whereOr(function($query){
  85. $query->where('ProductPresell.presell_type',2)->whereTime('final_end_time','<',time());
  86. });
  87. });
  88. break;
  89. }
  90. })
  91. ->when(isset($where['presell_type']) && $where['presell_type'] !== '',function($query)use($where){
  92. $query->where('ProductPresell.presell_type',$where['presell_type']);
  93. })
  94. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  95. $query->where('ProductPresell.status',$where['status']);
  96. })
  97. ->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  98. $query->where('ProductPresell.is_show',$where['is_show']);
  99. })
  100. ->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
  101. $make = app()->make(MerchantRepository::class);
  102. $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
  103. $query->whereIn('ProductPresell.mer_id',$mer_id);
  104. })
  105. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  106. $make = app()->make(MerchantRepository::class);
  107. $mer_id = $make->search(['is_trader' => $where['is_trader']])->column('mer_id');
  108. $query->whereIn('ProductPresell.mer_id',$mer_id);
  109. })
  110. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  111. if($where['us_status'] == 0) {
  112. $query->where('ProductPresell.is_show',0)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  113. }
  114. if($where['us_status'] == 1) {
  115. $query->where('ProductPresell.is_show',1)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  116. }
  117. if($where['us_status'] == -1) {
  118. $query->where(function($query){
  119. $query->where('ProductPresell.status',0)->whereOr('ProductPresell.product_status','<>',1);
  120. });
  121. }
  122. });
  123. $query->when(isset($where['star']),function($query)use($where){
  124. $query->Join('StoreSpu U', 'ProductPresell.product_presell_id = U.activity_id')->where('U.product_type', 2);
  125. $query->when($where['star'] !== '', function ($query) use ($where) {
  126. $query->where('U.star', $where['star']);
  127. });
  128. $query->order('U.star DESC,U.rank DESC,ProductPresell.create_time DESC');
  129. });
  130. $query->where('ProductPresell.is_del',0);
  131. return $query;
  132. }
  133. /**
  134. * TODO 移动端展示 条件
  135. * @return array
  136. * @author Qinii
  137. * @day 2020-10-19
  138. */
  139. public function presellShow()
  140. {
  141. return [
  142. 'product_show' => 1,
  143. //'product_status' => 1,
  144. 'status' => 1,
  145. 'is_show' => 1,
  146. 'api_type' => 1
  147. ];
  148. }
  149. /**
  150. * TODO
  151. * @author Qinii
  152. * @day 1/27/21
  153. */
  154. public function valActiveStatus()
  155. {
  156. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  157. $id = $query->column($this->getPk());
  158. if($id){
  159. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  160. $where = [
  161. 'product_type' => 2,
  162. 'activity_ids' => $id
  163. ];
  164. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  165. }
  166. }
  167. }