ProductAssistDao.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\common\dao\store\product;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\store\product\ProductAssist;
  5. use app\common\repositories\store\product\SpuRepository;
  6. use app\common\repositories\system\merchant\MerchantRepository;
  7. class ProductAssistDao extends BaseDao
  8. {
  9. protected function getModel(): string
  10. {
  11. return ProductAssist::class;
  12. }
  13. public function search(array $where)
  14. {
  15. $query = ProductAssist::hasWhere('product',function($query)use($where){
  16. $query->when(isset($where['product_show']) && $where['product_show'] !== '',function($query)use($where){
  17. $query->where('is_del',0)->where('mer_status',1)->where('product_type',3);
  18. })
  19. ->where('status',1);
  20. });
  21. $query->when(isset($where['product_assist_id']) && $where['product_assist_id'] !== '',function($query)use($where){
  22. $query->where('product_assist_id',$where['product_assist_id']);
  23. })
  24. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  25. $query->whereLike('ProductAssist.store_name|ProductAssist.product_id',"%{$where['keyword']}%");
  26. })
  27. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  28. $query->where('ProductAssist.mer_id',$where['mer_id']);
  29. })
  30. ->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
  31. switch ($where['type']){
  32. case 0: //未开始
  33. $query->whereTime('start_time','>',time());
  34. break;
  35. case 1: //进行中
  36. $query->whereTime('start_time','<=',time())->whereTime('end_time','>',time())
  37. ->where('ProductAssist.product_status',1)->where('ProductAssist.status',1)->where('ProductAssist.is_show',1);
  38. break;
  39. case 2: //已结束
  40. $query->where(function($query){
  41. $query->where('action_status',-1)->whereOr('end_time','<= TIME',time());
  42. });
  43. break;
  44. }
  45. })
  46. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  47. $query->where('ProductAssist.status',$where['status']);
  48. })
  49. ->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  50. $query->where('ProductAssist.is_show',$where['is_show']);
  51. })
  52. ->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
  53. $make = app()->make(MerchantRepository::class);
  54. $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
  55. $query->whereIn('ProductAssist.mer_id',$mer_id);
  56. })
  57. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  58. if($where['product_status'] == -1){
  59. $query->where('ProductAssist.product_status','in',[-1,-2]);
  60. }else{
  61. $query->where('ProductAssist.product_status',$where['product_status']);
  62. }
  63. })
  64. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  65. $make = app()->make(MerchantRepository::class);
  66. $mer_id = $make->search(['is_trader' => $where['is_trader']])->column('mer_id');
  67. $query->whereIn('ProductAssist.mer_id',$mer_id);
  68. })
  69. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  70. if($where['us_status'] == 0) {
  71. $query->where('ProductAssist.is_show',0)->where('ProductAssist.status',1)->where('ProductAssist.product_status',1);
  72. }
  73. if($where['us_status'] == 1) {
  74. $query->where('ProductAssist.is_show',1)->where('ProductAssist.status',1)->where('ProductAssist.product_status',1);
  75. }
  76. if($where['us_status'] == -1) {
  77. $query->where(function($query){
  78. $query->where('ProductAssist.status',0)->whereOr('ProductAssist.product_status','<>',1);
  79. });
  80. }
  81. })
  82. ->when(isset($where['star']),function($query)use($where){
  83. $query->Join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', 3);
  84. $query->when($where['star'] !== '', function ($query) use ($where) {
  85. $query->where('U.star', $where['star']);
  86. });
  87. $query->order('U.star DESC,U.rank DESC,ProductAssist.create_time DESC');
  88. });
  89. $query->where('ProductAssist.is_del',0);
  90. return $query;
  91. }
  92. /**
  93. * TODO 移动端展示 条件
  94. * @return array
  95. * @author Qinii
  96. * @day 2020-10-19
  97. */
  98. public function assistShow()
  99. {
  100. return [
  101. 'product_show' => 1,
  102. 'product_status' => 1,
  103. 'status' => 1,
  104. 'is_show' => 1,
  105. 'type' => 1
  106. ];
  107. }
  108. /**
  109. * TODO
  110. * @author Qinii
  111. * @day 1/27/21
  112. */
  113. public function valActiveStatus()
  114. {
  115. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  116. $id = $query->column($this->getPk());
  117. if($id) {
  118. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  119. $where = [
  120. 'product_type' => 3,
  121. 'activity_ids' => $id
  122. ];
  123. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  124. }
  125. }
  126. /**
  127. * TODO 软删除商户的所有商品
  128. * @param $merId
  129. * @author Qinii
  130. * @day 5/15/21
  131. */
  132. public function clearProduct($merId)
  133. {
  134. $this->getModel()::getDb()->where('mer_id',$merId)->update(['is_del' => 1]);
  135. }
  136. }