ProductPresellDao.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\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->Join('StoreSpu U', 'ProductPresell.product_presell_id = U.activity_id')->where('U.product_type', 2);
  35. $query->when(isset($where['product_presell_id']) && $where['product_presell_id'] !== '',function($query)use($where){
  36. $query->where('product_presell_id',$where['product_presell_id']);
  37. })
  38. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  39. $query->where('ProductPresell.mer_id',$where['mer_id']);
  40. })
  41. ->when(isset($where['action_status']) && $where['action_status'] !== '',function($query)use($where){
  42. $query->where('ProductPresell.action_status',$where['action_status']);
  43. })
  44. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  45. $query->whereLike('ProductPresell.store_name|ProductPresell.product_id|ProductPresell.product_presell_id',"%{$where['keyword']}%");
  46. })
  47. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  48. if($where['product_status'] == -1){
  49. $query->where('ProductPresell.product_status','in',[-1,-2]);
  50. }else{
  51. $query->where('ProductPresell.product_status',$where['product_status']);
  52. }
  53. })
  54. ->when(isset($where['presell_type']) && $where['presell_type'] !== '',function($query)use($where){
  55. $query->where('ProductPresell.presell_type',$where['presell_type']);
  56. })
  57. ->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
  58. switch ($where['type']){
  59. case 0: //未开始
  60. if(isset($where['api_type'])){
  61. $query->where('product_status',1);
  62. }
  63. $query->where('action_status',1);
  64. $query->where(function($query){
  65. $query->whereTime('start_time','>',time())->whereOr(function ($query){
  66. $query->whereTime('start_time','<',time())->whereTime('end_time','>',time())->where(function($query){
  67. $query->where('ProductPresell.product_status','<>',1)->whereOr('ProductPresell.is_show','<>',1)->whereOr('ProductPresell.status','<>',1);
  68. });
  69. });
  70. });
  71. break;
  72. case 1: //进行中
  73. $query->where('action_status',1)
  74. ->whereTime('start_time','<=',time())
  75. ->whereTime('end_time','>',time())
  76. ->where('product_status',1)
  77. ->where('ProductPresell.status',1)
  78. ->where('ProductPresell.is_show',1);
  79. break;
  80. case 2: //已结束
  81. $query->where(function($query){
  82. $query->where('action_status',-1)->whereOr('end_time','<= TIME',time());
  83. });
  84. break;
  85. case 3: //已关闭
  86. $query->where(function($query){
  87. $query->where(function($query){
  88. $query->where('ProductPresell.presell_type',1)->whereTime('end_time','<',time());
  89. })->whereOr(function($query){
  90. $query->where('ProductPresell.presell_type',2)->whereTime('final_end_time','<',time());
  91. });
  92. });
  93. break;
  94. default:
  95. $query->where(function($query){
  96. $query->where(function($query){
  97. $query->where('ProductPresell.presell_type',1)->whereTime('end_time','>',time());
  98. })->whereOr(function($query){
  99. $query->where('ProductPresell.presell_type',2)->whereTime('final_end_time','>',time());
  100. });
  101. });
  102. break;
  103. }
  104. })
  105. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  106. $query->where('ProductPresell.status',$where['status']);
  107. })
  108. ->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  109. $query->where('ProductPresell.is_show',$where['is_show']);
  110. })
  111. ->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
  112. $make = app()->make(MerchantRepository::class);
  113. $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
  114. $query->whereIn('ProductPresell.mer_id',$mer_id);
  115. })
  116. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  117. $make = app()->make(MerchantRepository::class);
  118. $mer_id = $make->search(['is_trader' => $where['is_trader']])->column('mer_id');
  119. $query->whereIn('ProductPresell.mer_id',$mer_id);
  120. })
  121. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  122. if($where['us_status'] == 0) {
  123. $query->where('ProductPresell.is_show',0)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  124. }
  125. if($where['us_status'] == 1) {
  126. $query->where('ProductPresell.is_show',1)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  127. }
  128. if($where['us_status'] == -1) {
  129. $query->where(function($query){
  130. $query->where('ProductPresell.status',0)->whereOr('ProductPresell.product_status','<>',1);
  131. });
  132. }
  133. });
  134. $query->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
  135. $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
  136. })
  137. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
  138. $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
  139. })
  140. ->when(isset($where['star']),function($query)use($where){
  141. $query->when($where['star'] !== '', function ($query) use ($where) {
  142. $query->where('U.star', $where['star']);
  143. });
  144. $query->order('U.star DESC,U.rank DESC,ProductPresell.create_time DESC');
  145. });
  146. $query->where('ProductPresell.is_del',0);
  147. return $query;
  148. }
  149. /**
  150. * TODO 移动端展示 条件
  151. * @return array
  152. * @author Qinii
  153. * @day 2020-10-19
  154. */
  155. public function actionShow()
  156. {
  157. return [
  158. 'product_show' => 1,
  159. //'product_status' => 1,
  160. 'status' => 1,
  161. 'is_show' => 1,
  162. 'api_type' => 1
  163. ];
  164. }
  165. /**
  166. * TODO
  167. * @author Qinii
  168. * @day 1/27/21
  169. */
  170. public function valActiveStatus()
  171. {
  172. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  173. $id = $query->column($this->getPk());
  174. if($id){
  175. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  176. $where = [
  177. 'product_type' => 2,
  178. 'activity_ids' => $id
  179. ];
  180. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  181. }
  182. }
  183. /**
  184. * TODO 软删除商户的所有商品
  185. * @param $merId
  186. * @author Qinii
  187. * @day 5/15/21
  188. */
  189. public function clearProduct($merId)
  190. {
  191. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  192. }
  193. }