MerchantReconciliationDao.php 4.4 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\order;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\order\MerchantReconciliation as model;
  14. use app\common\repositories\system\admin\AdminRepository;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. class MerchantReconciliationDao extends BaseDao
  17. {
  18. public function getModel(): string
  19. {
  20. return model::class;
  21. }
  22. /**
  23. * 根据条件搜索数据。
  24. *
  25. * 该方法用于根据传入的条件数组来构建数据库查询条件,灵活地支持多种搜索逻辑,
  26. * 包括对特定字段的条件查询以及关键词的全文搜索。搜索条件的构建使用了Laravel的查询构建器,
  27. * 通过链式调用where和when方法来动态添加查询条件。
  28. *
  29. * @param array $where 搜索条件数组,包含各种可能的查询条件。
  30. * @return \Illuminate\Database\Eloquent\Builder|static 返回构建好的查询构建器实例,可用于进一步的操作或直接执行查询。
  31. */
  32. public function search(array $where)
  33. {
  34. // 获取模型对应的数据库实例
  35. $query = ($this->getModel()::getDB())
  36. // 当'mer_id'字段存在且不为空时,添加对'mer_id'字段的查询条件
  37. ->when(isset($where['mer_id']) && $where['mer_id'] != '' ,function($query)use($where){
  38. $query->where('mer_id',$where['mer_id']);
  39. })
  40. // 当'status'字段存在且不为空时,添加对'status'字段的查询条件
  41. ->when(isset($where['status']) && $where['status'] != '' ,function($query)use($where){
  42. $query->where('status',$where['status']);
  43. })
  44. // 当'is_accounts'字段存在且不为空时,添加对'is_accounts'字段的查询条件
  45. ->when(isset($where['is_accounts']) && $where['is_accounts'] != '' ,function($query)use($where){
  46. $query->where('is_accounts',$where['is_accounts']);
  47. })
  48. // 当'date'字段存在且不为空时,调用getModelTime函数添加对日期的查询条件
  49. ->when(isset($where['date']) && $where['date'] != '' ,function($query)use($where){
  50. getModelTime($query,$where['date']);
  51. })
  52. // 当'reconciliation_id'字段存在且不为空时,添加对'reconciliation_id'字段的查询条件
  53. ->when(isset($where['reconciliation_id']) && $where['reconciliation_id'] != '' ,function($query)use($where){
  54. $query->where('reconciliation_id',$where['reconciliation_id']);
  55. })
  56. // 当'keyword'字段存在且不为空时,执行关键词搜索逻辑
  57. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  58. // 实例化管理员仓库,用于获取管理员ID列表
  59. $make = app()->make(AdminRepository::class);
  60. $admin_id = $make->getSearch(['real_name' => $where['keyword']],null,false)->column('admin_id');
  61. // 根据是否存在'mer_id'字段,构建不同的查询条件
  62. $query->where(function($query) use($admin_id,$where){
  63. if(isset($where['mer_id'])){
  64. $query->where('admin_id','in',$admin_id);
  65. }else {
  66. // 实例化商家仓库,用于获取商家ID列表
  67. $mer_make = app()->make(MerchantRepository::class);
  68. $mer_id = $mer_make->getSearch(['keyword' => $where['keyword']])->column('mer_id');
  69. // 添加对管理员ID或商家ID的查询条件
  70. $query->where('admin_id','in',$admin_id)->whereOr('mer_id','in',$mer_id);
  71. }
  72. });
  73. });
  74. // 设置查询结果的排序方式
  75. return $query->order('create_time DESC,status DESC');
  76. }
  77. }