From 60ebfde91e9c975f59df4d60a6cb5a7861958bd2 Mon Sep 17 00:00:00 2001
From: shinnosuke <tantao@1hoper.com>
Date: 星期三, 30 九月 2026 17:04:37 +0800
Subject: [PATCH] fix(live): 修复管理端「编辑」「查看」直播内容报 500 的问题

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/DataStatisticsController.java |  247 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 210 insertions(+), 37 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/DataStatisticsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/DataStatisticsController.java
index 89a7375..52c3a8e 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/DataStatisticsController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/DataStatisticsController.java
@@ -660,10 +660,6 @@
 
                 }
             }
-            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
-            branchAdminListVO.setId(branch.getId());
-            branchAdminListVO.setName(branch.getBranchName());
-            branchAdminListVOs.add(branchAdminListVO);
         }
         return R.ok(branchAdminListVOs);
     }
@@ -726,11 +722,6 @@
 
                 }
             }
-            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
-            branchAdminListVO.setId(branch.getId());
-            branchAdminListVO.setName(branch.getBranchName());
-            branchAdminListVOs.add(branchAdminListVO);
-
         }
 
         ArrayList<BranchAdminListVO> list = new ArrayList<>();
@@ -760,28 +751,59 @@
         // 找出分公司下的所有员工 -》 所有诊所-》订单(状态 3=待发货 4=已发货 5=已入库)
         SysUser user = tokenService.getLoginUser().getUser();
         Integer roleType = user.getRoleType();
+        Set<String> branchIdsAll = new HashSet<>();
         if (roleType == 2) {
-            TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
-                    .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
-                    .last("LIMIT 1"));
+            TCrmBranch branch =null;
+            if(query.getBranchId()==null || query.getBranchId().isEmpty()){
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }else {
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getId, query.getBranchId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }
             if (branch != null) {
+                branchIdsAll.add(branch.getId());
                 // 先查自己本身的
                 List<TCrmClinic> list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
+                if (branch.getPersonType() != null && branch.getPersonType() == 1) {
+                    List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus, 2));
+                    List<String> branchIdsThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                    if(!crmBranches.isEmpty()){
+                        branchIdsAll.addAll(crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList()));
+                        List<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toList());
+                        List<String> branchIdsFour = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                        if(!branchIdsFour.isEmpty()){
+                            branchIdsAll.addAll(branchIdsFour);
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsFour).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+
+                        }
+
+                    }
+                    if(!branchIdsThree.isEmpty()){
+                        branchIdsAll.addAll(branchIdsThree);
+                    }
+                }
+
                 // 如果是区
                 if (branch.getPersonType() != null && branch.getPersonType() == 2) {
                     // 区查下面所有城市的公司
                     List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2));
                     if (!crmBranches.isEmpty()) {
                         List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                        branchIdsAll.addAll(branchIds);
                         list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
                     }
                 }
 
                 if (branch.getPersonType() != null && branch.getPersonType() == 3) {
-                    List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus,2));
+                    List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getAuditStatus,2));
                     if (!crmBranches.isEmpty()) {
                         // 区公司
                         List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+
+                        branchIdsAll.addAll(branchIds);
                         list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
 
                         Set<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toSet());
@@ -789,6 +811,8 @@
                         List<TCrmBranch> crmBranchesThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2));
                         if (!crmBranchesThree.isEmpty()) {
                             List<String> branchIdsThree = crmBranchesThree.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+
+                            branchIdsAll.addAll(branchIdsThree);
                             list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsThree).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
                         }
                     }
@@ -801,7 +825,8 @@
                     // 获取最近一年的时间
                     LocalDateTime time = LocalDateTime.now().minusYears(1);
                     List<TErpProcurement> procurementList = erpProcurementService.list(new LambdaQueryWrapper<TErpProcurement>().between(TErpProcurement::getCreateTime, DateUtils.localDateTimeToDate(time), DateUtils.localDateTimeToDate(LocalDateTime.now())).ne(TErpProcurement::getStatus, 6).isNotNull(TErpProcurement::getPayTime).in(TErpProcurement::getClinicId, crmClinicIds));
-                    List<TErpProcurementCommission> list1 = erpProcurementCommissionService.list(new LambdaQueryWrapper<TErpProcurementCommission>().eq(TErpProcurementCommission::getBranchSalespersonId, branch.getId()).eq(TErpProcurementCommission::getCommissionType, 1).between(TErpProcurementCommission::getCreateTime, DateUtils.localDateTimeToDate(time), DateUtils.localDateTimeToDate(LocalDateTime.now())));
+
+                    List<TErpProcurementCommission> list1 = erpProcurementCommissionService.list(new LambdaQueryWrapper<TErpProcurementCommission>().in(TErpProcurementCommission::getBranchSalespersonId,branchIdsAll).eq(TErpProcurementCommission::getCommissionType, 1).between(TErpProcurementCommission::getCreateTime, DateUtils.localDateTimeToDate(time), DateUtils.localDateTimeToDate(LocalDateTime.now())));
 
                     // list1 获取本月的
                     List<TErpProcurementCommission> list2 = list1.stream().filter(commission -> commission.getCreateTime().toLocalDate().isAfter(time.toLocalDate())).collect(Collectors.toList());
@@ -845,7 +870,7 @@
 
 
                         // 获取公司业务员
-                        List<String> salesIds = crmSalespersonService.list(new LambdaQueryWrapper<TCrmSalesperson>().eq(TCrmSalesperson::getBranchId, branch.getId())).stream().map(TCrmSalesperson::getId).collect(Collectors.toList());
+                        List<String> salesIds = crmSalespersonService.list(new LambdaQueryWrapper<TCrmSalesperson>().in(TCrmSalesperson::getBranchId,branchIdsAll)).stream().map(TCrmSalesperson::getId).collect(Collectors.toList());
                         if (salesIds.isEmpty()) {
                             salesIds.add("0");
                         }
@@ -885,14 +910,18 @@
                         branchAdminStatisticsOneVo.setStaffMoneyProcurement(collect1.stream().map(TErpProcurement::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
                         branchAdminStatisticsOneVo.setStaffMoney(collect3.stream().map(TErpProcurementCommission::getMoney).filter(Objects::nonNull).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
 
-                        long count = crmSalespersonService.count(new LambdaQueryWrapper<TCrmSalesperson>().eq(TCrmSalesperson::getBranchId, branch.getId()));
-                        branchAdminStatisticsOneVo.setStaffCount((int) count);
+
                     }
 
 
                 }
             }
-
+            if(CollectionUtils.isEmpty(branchIdsAll)){
+                branchAdminStatisticsOneVo.setStaffCount(0);
+            }else{
+                long count = crmSalespersonService.count(new LambdaQueryWrapper<TCrmSalesperson>().in(TCrmSalesperson::getBranchId, branchIdsAll));
+                branchAdminStatisticsOneVo.setStaffCount((int) count);
+            }
         }
         return R.ok(branchAdminStatisticsOneVo);
 
@@ -908,13 +937,41 @@
         BranchAdminStatisticsTwoVo branchAdminStatisticsTwoVo = new BranchAdminStatisticsTwoVo();
         SysUser user = tokenService.getLoginUser().getUser();
         Integer roleType = user.getRoleType();
+        Set<String> branchIdsAll = new HashSet<>();
         if (roleType == 2) {
-            TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
-                    .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
-                    .last("LIMIT 1"));
+            TCrmBranch branch =null;
+            if(query.getBranchId()==null || query.getBranchId().isEmpty()){
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }else {
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getId, query.getBranchId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }
             if (branch != null) {
                 // 先查自己本身的
                 List<TCrmClinic> list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
+
+                if (branch.getPersonType() != null && branch.getPersonType() == 1) {
+                    List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus, 2));
+                    List<String> branchIdsThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                    if(!crmBranches.isEmpty()){
+                        branchIdsAll.addAll(crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList()));
+                        List<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toList());
+                        List<String> branchIdsFour = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                        if(!branchIdsFour.isEmpty()){
+                            branchIdsAll.addAll(branchIdsFour);
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsFour).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+
+                        }
+
+                    }
+                    if(!branchIdsThree.isEmpty()){
+                        branchIdsAll.addAll(branchIdsThree);
+                    }
+                }
+
                 // 如果是区
                 if (branch.getPersonType() != null && branch.getPersonType() == 2) {
                     // 区查下面所有城市的公司
@@ -1023,13 +1080,40 @@
         SysUser user = tokenService.getLoginUser().getUser();
         SimpleDateFormat formatMonth = new SimpleDateFormat("yyyy-MM");
         Integer roleType = user.getRoleType();
+        Set<String> branchIdsAll = new HashSet<>();
         if (roleType == 2) {
-            TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
-                    .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
-                    .last("LIMIT 1"));
+            TCrmBranch branch =null;
+            if(query.getBranchId()==null || query.getBranchId().isEmpty()){
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }else {
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getId, query.getBranchId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }
             if (branch != null) {
                 // 先查自己本身的
                 List<TCrmClinic> list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
+
+                if (branch.getPersonType() != null && branch.getPersonType() == 1) {
+                    List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus, 2));
+                    List<String> branchIdsThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                    if(!crmBranches.isEmpty()){
+                        branchIdsAll.addAll(crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList()));
+                        List<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toList());
+                        List<String> branchIdsFour = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2)).stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                        if(!branchIdsFour.isEmpty()){
+                            branchIdsAll.addAll(branchIdsFour);
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsFour).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+
+                        }
+
+                    }
+                    if(!branchIdsThree.isEmpty()){
+                        branchIdsAll.addAll(branchIdsThree);
+                    }
+                }
                 // 如果是区
                 if (branch.getPersonType() != null && branch.getPersonType() == 2) {
                     // 区查下面所有城市的公司
@@ -1287,16 +1371,65 @@
         SysUser user = tokenService.getLoginUser().getUser();
 
         Integer roleType = user.getRoleType();
+        List<TCrmClinic> list =new ArrayList<>();
         if (roleType == 1) {
-            LambdaQueryWrapper<TCrmClinic> w = new LambdaQueryWrapper<>();
-            if (query.getBranchId() != null && !query.getBranchId().isEmpty()) {
-                w.eq(TCrmClinic::getBranchId, query.getBranchId());
+
+            if(   ((query.getBranchId()==null || query.getBranchId().isEmpty()) && ( query.getSalespersonId()==null || query.getSalespersonId().isEmpty())) || ((query.getBranchId()!=null || !query.getBranchId().isEmpty()) && (query.getSalespersonId()==null || !query.getSalespersonId().isEmpty()))){
+
+                LambdaQueryWrapper<TCrmClinic> w = new LambdaQueryWrapper<>();
+                if (query.getBranchId() != null && !query.getBranchId().isEmpty()) {
+                    w.eq(TCrmClinic::getBranchId, query.getBranchId());
+                }
+                if (query.getSalespersonId() != null && !query.getSalespersonId().isEmpty()) {
+                    w.eq(TCrmClinic::getSalespersonId, query.getSalespersonId());
+                }
+                w.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
+                list = crmClinicService.list(w);
+            }else {
+                TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getId, query.getBranchId()).eq(TCrmBranch::getAuditStatus, 2)
+                        .last("LIMIT 1"));
+                if (branch != null) {
+                    // 先查自己本身的
+                    list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
+                    // 如果是区
+                    if (branch.getPersonType() != null && branch.getPersonType() == 2) {
+                        // 区查下面所有城市的公司
+                        List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus, 2));
+                        if (!crmBranches.isEmpty()) {
+                            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+                        }
+                    }
+
+                    if (branch.getPersonType() != null && branch.getPersonType() == 3) {
+                        List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus, 2));
+                        if (!crmBranches.isEmpty()) {
+                            // 区公司
+                            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+
+                            Set<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toSet());
+                            // 城市公司
+                            List<TCrmBranch> crmBranchesThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus, 2));
+                            if (!crmBranchesThree.isEmpty()) {
+                                List<String> branchIdsThree = crmBranchesThree.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                                list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsThree).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+                            }
+                        }
+                    }
+
+                    if (query.getSalespersonId() != null && !query.getSalespersonId().isEmpty()) {
+                        list = list.stream().filter(e -> e.getSalespersonId().equals(query.getSalespersonId())).collect(Collectors.toList());
+                    }
+
+                }
             }
-            if (query.getSalespersonId() != null && !query.getSalespersonId().isEmpty()) {
-                w.eq(TCrmClinic::getSalespersonId, query.getSalespersonId());
-            }
-            w.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
-            List<TCrmClinic> list = crmClinicService.list(w);
+
+
+
+
+
             branchAdminStatisticsOneVo.setClinicNum(list.size());
 
             if (!list.isEmpty()) {
@@ -1404,13 +1537,53 @@
         SysUser user = tokenService.getLoginUser().getUser();
         SimpleDateFormat formatMonth = new SimpleDateFormat("yyyy-MM");
         Integer roleType = user.getRoleType();
+        List<TCrmClinic> list =new ArrayList<>();
         if (roleType == 1) {
-            LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
-            if (query.getBranchId() != null && !query.getBranchId().isEmpty()) {
-                eq.eq(TCrmClinic::getBranchId, query.getBranchId());
+            if(query.getBranchId()==null || query.getBranchId().isEmpty() ){
+
+                LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
+                if (query.getBranchId() != null && !query.getBranchId().isEmpty()) {
+                    eq.eq(TCrmClinic::getBranchId, query.getBranchId());
+                }
+                eq.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
+                list = crmClinicService.list(eq);
+            }else {
+                TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getId, query.getBranchId()).eq(TCrmBranch::getAuditStatus, 2)
+                        .last("LIMIT 1"));
+                if (branch != null) {
+                    // 先查自己本身的
+                    list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
+                    // 如果是区
+                    if (branch.getPersonType() != null && branch.getPersonType() == 2) {
+                        // 区查下面所有城市的公司
+                        List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus, 2));
+                        if (!crmBranches.isEmpty()) {
+                            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+                        }
+                    }
+
+                    if (branch.getPersonType() != null && branch.getPersonType() == 3) {
+                        List<TCrmBranch> crmBranches = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().eq(TCrmBranch::getCorrelationPersonId, branch.getUserId()).eq(TCrmBranch::getPersonType, 2).eq(TCrmBranch::getAuditStatus, 2));
+                        if (!crmBranches.isEmpty()) {
+                            // 区公司
+                            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                            list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIds).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+
+                            Set<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toSet());
+                            // 城市公司
+                            List<TCrmBranch> crmBranchesThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus, 2));
+                            if (!crmBranchesThree.isEmpty()) {
+                                List<String> branchIdsThree = crmBranchesThree.stream().map(TCrmBranch::getId).collect(Collectors.toList());
+                                list.addAll(crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getBranchId, branchIdsThree).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5))));
+                            }
+                        }
+                    }
+
+
+                }
             }
-            eq.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
-            List<TCrmClinic> list = crmClinicService.list(eq);
 
             List<String> clinicIds = list.stream().map(TCrmClinic::getId).collect(Collectors.toList());
             if (clinicIds.isEmpty()) {

--
Gitblit v1.7.1