ProductAssistDao.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\ProductAssist;
  14. use app\common\repositories\store\product\SpuRepository;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. class ProductAssistDao extends BaseDao
  17. {
  18. protected function getModel(): string
  19. {
  20. return ProductAssist::class;
  21. }
  22. /**
  23. * 商品查询条件
  24. * @param array $where
  25. * @return \think\db\BaseQuery
  26. * @author wuhaotian
  27. * @email 442384644@qq.com
  28. * @date 2024/7/16
  29. */
  30. public function search(array $where)
  31. {
  32. $query = ProductAssist::hasWhere('product',function($query)use($where){
  33. $query->when(isset($where['product_show']) && $where['product_show'] !== '',function($query)use($where){
  34. $query->where('is_del',0)->where('mer_status',1)->where('product_type',3);
  35. })
  36. ->where('status',1);
  37. });
  38. $query->join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', 3);
  39. $query->when(isset($where['product_assist_id']) && $where['product_assist_id'] !== '',function($query)use($where){
  40. $query->where('product_assist_id',$where['product_assist_id']);
  41. })
  42. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  43. $query->whereLike('ProductAssist.store_name|ProductAssist.product_id',"%{$where['keyword']}%");
  44. })
  45. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  46. $query->where('ProductAssist.mer_id',$where['mer_id']);
  47. })
  48. ->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
  49. switch ($where['type']){
  50. case 0: //未开始
  51. $query->whereTime('start_time','>',time());
  52. break;
  53. case 1: //进行中
  54. $query->whereTime('start_time','<=',time())->whereTime('end_time','>',time())
  55. ->where('ProductAssist.product_status',1)->where('ProductAssist.status',1)->where('ProductAssist.is_show',1);
  56. break;
  57. case 2: //已结束
  58. $query->where(function($query){
  59. $query->where('action_status',-1)->whereOr('end_time','<= TIME',time());
  60. });
  61. break;
  62. }
  63. })
  64. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  65. $query->where('ProductAssist.status',$where['status']);
  66. })
  67. ->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  68. $query->where('ProductAssist.is_show',$where['is_show']);
  69. })
  70. ->when(isset($where['is_del']) && $where['is_del'] !== '',function($query)use($where){
  71. $query->where('ProductAssist.is_del',$where['is_del']);
  72. })
  73. ->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
  74. $make = app()->make(MerchantRepository::class);
  75. $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
  76. $query->whereIn('ProductAssist.mer_id',$mer_id);
  77. })
  78. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  79. if($where['product_status'] == -1){
  80. $query->where('ProductAssist.product_status','in',[-1,-2]);
  81. }else{
  82. $query->where('ProductAssist.product_status',$where['product_status']);
  83. }
  84. })
  85. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  86. $make = app()->make(MerchantRepository::class);
  87. $mer_id = $make->search(['is_trader' => $where['is_trader']])->column('mer_id');
  88. $query->whereIn('ProductAssist.mer_id',$mer_id);
  89. })
  90. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  91. if($where['us_status'] == 0) {
  92. $query->where('ProductAssist.is_show',0)->where('ProductAssist.status',1)->where('ProductAssist.product_status',1);
  93. }
  94. if($where['us_status'] == 1) {
  95. $query->where('ProductAssist.is_show',1)->where('ProductAssist.status',1)->where('ProductAssist.product_status',1);
  96. }
  97. if($where['us_status'] == -1) {
  98. $query->where(function($query){
  99. $query->where('ProductAssist.status',0)->whereOr('ProductAssist.product_status','<>',1);
  100. });
  101. }
  102. })
  103. ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
  104. $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
  105. })
  106. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
  107. $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
  108. })
  109. ->when(isset($where['star']),function($query)use($where){
  110. $query->when($where['star'] !== '', function ($query) use ($where) {
  111. $query->where('U.star', $where['star']);
  112. });
  113. $query->order('U.star DESC,U.rank DESC,ProductAssist.create_time DESC');
  114. });
  115. $query->where('ProductAssist.is_del',0);
  116. return $query;
  117. }
  118. /**
  119. * 移动端展示 条件
  120. * @return array
  121. * @author Qinii
  122. * @day 2020-10-19
  123. */
  124. public function assistShow()
  125. {
  126. return [
  127. 'product_show' => 1,
  128. 'product_status' => 1,
  129. 'status' => 1,
  130. 'is_show' => 1,
  131. 'type' => 1,
  132. 'is_del' => 0
  133. ];
  134. }
  135. /**
  136. * 检查并更新过期活动的状态
  137. * 该方法用于定期检查所有活动的结束时间,如果活动已经结束,则将其状态设置为失效(-1)。
  138. * 同时,它也会更新关联的SPU(Special Product Unit)的状态,将这些SPU的活动状态设置为失效。
  139. * 这样做的目的是为了维护数据库中活动和SPU状态的准确性,确保只有当前有效的活动和SPU被展示或参与。
  140. */
  141. public function valActiveStatus()
  142. {
  143. // 根据当前时间,查询所有已经结束的活动,这些活动的状态为1(进行中)
  144. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  145. // 获取这些活动的ID
  146. $id = $query->column($this->getPk());
  147. // 如果查询到了已结束的活动ID
  148. if($id) {
  149. // 更新这些活动的状态为-1(失效)
  150. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  151. // 准备更新SPU状态的条件,特定类型的SPU且关联的活动ID在已结束的活动中
  152. $where = [
  153. 'product_type' => 3,
  154. 'activity_ids' => $id
  155. ];
  156. // 更新所有符合条件的SPU的状态为0(失效)
  157. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  158. }
  159. }
  160. /**
  161. * 软删除商户的所有商品
  162. * @param $merId
  163. * @author Qinii
  164. * @day 5/15/21
  165. */
  166. public function clearProduct($merId)
  167. {
  168. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  169. }
  170. }