ProductGroupBuyingDao.php 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\ProductGroupBuying;
  14. class ProductGroupBuyingDao extends BaseDao
  15. {
  16. public function getModel(): string
  17. {
  18. return ProductGroupBuying::class;
  19. }
  20. /**
  21. * 根据条件搜索团购信息
  22. *
  23. * 本函数用于构建并返回一个根据指定条件搜索产品团购信息的查询语句。
  24. * 它支持多种搜索条件,包括商家ID、日期、状态、用户名、关键词和是否为交易者等。
  25. * 搜索功能通过动态拼接查询条件来实现,以适应不同的搜索需求。
  26. *
  27. * @param array $where 搜索条件数组,包含各种可能的搜索参数。
  28. * @return \yii\db\ActiveQuery 返回构建的查询语句对象。
  29. */
  30. public function search($where)
  31. {
  32. // 初始化查询,从ProductGroupBuying表开始,使用别名B
  33. $query = ProductGroupBuying::getDb()->alias('B')
  34. // 加入StoreProductGroup表,通过product_group_id关联
  35. ->join('StoreProductGroup G','B.product_group_id = G.product_group_id');
  36. // 动态添加条件:根据商家ID搜索
  37. $query
  38. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function($query)use($where){
  39. // 如果指定了商家ID,则添加到查询条件中
  40. $query->where('B.mer_id',$where['mer_id']);
  41. })
  42. // 动态添加条件:根据日期搜索
  43. ->when(isset($where['date']) && $where['date'] , function($query)use($where){
  44. // 如果指定了日期,则调用getModelTime函数处理,并添加到查询条件中
  45. getModelTime($query,$where['date'],'B.create_time');
  46. })
  47. // 动态添加条件:根据状态搜索
  48. ->when(isset($where['status']) && $where['status'] !== '', function($query)use($where){
  49. // 如果指定了状态,则添加到查询条件中
  50. $query->where('B.status',$where['status']);
  51. })
  52. // 动态添加条件:根据用户名搜索
  53. ->when(isset($where['user_name']) && $where['user_name'] !== '', function($query)use($where){
  54. // 加入StoreProductGroupUser表,通过group_buying_id关联
  55. // 并搜索初始化者(is_initiator=1),用户名(uid|nickname)包含搜索关键字
  56. $query->join('StoreProductGroupUser U','U.group_buying_id = B.group_buying_id')
  57. ->where('is_initiator',1)
  58. ->whereLike('uid|nickname',"%{$where['user_name']}%");
  59. })
  60. // 动态添加条件:根据关键词搜索
  61. ->when(isset($where['keyword']) && $where['keyword'] !== '' , function($query)use($where){
  62. // 加入StoreProduct表,通过product_id关联
  63. // 搜索团购ID、产品ID或商店名称包含搜索关键字
  64. $query->join('StoreProduct P','G.product_id = P.product_id')
  65. ->whereLike('B.group_buying_id|P.product_id|store_name',"%{$where['keyword']}%");
  66. })
  67. // 动态添加条件:根据是否为交易者搜索
  68. ->when(isset($where['is_trader']) && $where['is_trader'] !== '', function($query)use($where){
  69. // 加入Merchant表,通过mer_id关联
  70. // 并根据是否为交易者(is_trader)进行筛选
  71. $query->join('Merchant M','M.mer_id = B.mer_id')->where('is_trader',$where['is_trader']);
  72. })
  73. ;
  74. // 返回构建好的查询语句
  75. return $query;
  76. }
  77. }