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 |  590 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 525 insertions(+), 65 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 f27daed..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
@@ -97,7 +97,13 @@
         }
 
         TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
-                .eq(TCrmSupplier::getUserId, userId));
+                .eq(TCrmSupplier::getUserId, userId)
+                .last("LIMIT 1"));
+        // ***
+        if (Objects.isNull(crmSupplier)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmSupplier = crmSupplierService.getById(sysUser.getObjectId());
+        }
 
         Map<String, Object> map = new HashMap<>();
 
@@ -138,11 +144,25 @@
         String supplierAndClinicId = null;
         if (roleType == 4) {
             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
-                    .eq(TCrmSupplier::getUserId, userId));
+                    .eq(TCrmSupplier::getUserId, userId)
+                    .last("LIMIT 1"));
+            // ***
+            if (Objects.isNull(crmSupplier)) {
+                SysUser sysUser = sysUserService.selectUserById(userId);
+                crmSupplier = crmSupplierService.getById(sysUser.getObjectId());
+            }
             supplierAndClinicId = crmSupplier.getId();
         } else {
             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
-                    .eq(TCrmClinic::getUserId, userId));
+                    .eq(TCrmClinic::getUserId, userId)
+                    .last("LIMIT 1"));
+            if (Objects.isNull(crmClinic)) {
+                SysUser sysUser = sysUserService.selectUserById(userId);
+                crmClinic = crmClinicService.getById(sysUser.getObjectId());
+            }
+            if (Objects.isNull(crmClinic)) {
+                return R.fail("未找到该账号诊所信息");
+            }
             supplierAndClinicId = crmClinic.getId();
         }
 
@@ -156,7 +176,7 @@
         SimpleDateFormat format = new SimpleDateFormat("MM.dd");
         for (int i = 0; i < 30; i++) {
             LocalDateTime timeNow = localDateTime.plusDays(i);
-            List<SupplierThirtyProcurementStatisticsVO> vos = supplierThirtyProcurementStatistics.stream().filter(vo -> Objects.nonNull(vo.getPayTime()) && timeNow.toLocalDate().isEqual(vo.getPayTime())).collect(Collectors.toList());
+            List<SupplierThirtyProcurementStatisticsVO> vos = supplierThirtyProcurementStatistics.stream().filter(vo -> Objects.nonNull(vo.getPayTime()) && timeNow.toLocalDate().equals(vo.getPayTime())).collect(Collectors.toList());
             if (CollectionUtils.isEmpty(vos)) {
                 SupplierThirtyProcurementStatisticsVO vo = new SupplierThirtyProcurementStatisticsVO();
                 String time = format.format(DateUtils.localDateTimeToDate(timeNow));
@@ -164,7 +184,7 @@
                 vo.setOrderCount(0);
                 supplierThirtyProcurementStatistics.add(vo);
             }
-            List<SupplierThirtyMoneyProcurementStatisticsVO> moneyVOS = supplierThirtyMoneyProcurementStatistics.stream().filter(vo -> Objects.nonNull(vo.getPayTime()) && timeNow.toLocalDate().isEqual(vo.getPayTime())).collect(Collectors.toList());
+            List<SupplierThirtyMoneyProcurementStatisticsVO> moneyVOS = supplierThirtyMoneyProcurementStatistics.stream().filter(vo -> Objects.nonNull(vo.getPayTime()) && timeNow.toLocalDate().equals(vo.getPayTime())).collect(Collectors.toList());
             if (CollectionUtils.isEmpty(moneyVOS)) {
                 SupplierThirtyMoneyProcurementStatisticsVO vo = new SupplierThirtyMoneyProcurementStatisticsVO();
                 String time = format.format(DateUtils.localDateTimeToDate(timeNow));
@@ -192,7 +212,15 @@
         Long userId = tokenService.getLoginUser().getUserId();
 
         TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
-                .eq(TCrmClinic::getUserId, userId));
+                .eq(TCrmClinic::getUserId, userId)
+                .last("LIMIT 1"));
+        if (Objects.isNull(crmClinic)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmClinic = crmClinicService.getById(sysUser.getObjectId());
+        }
+        if (Objects.isNull(crmClinic)) {
+            return R.fail("未找到该账号诊所信息");
+        }
 
         List<TSysAppUser> sysAppUsers = sysAppUserService.list(Wrappers.lambdaQuery(TSysAppUser.class)
                 .eq(TSysAppUser::getClinicId, crmClinic.getId()));
@@ -212,8 +240,15 @@
         Long userId = tokenService.getLoginUser().getUserId();
 
         TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
-                .eq(TCrmClinic::getUserId, userId));
-
+                .eq(TCrmClinic::getUserId, userId)
+                .last("LIMIT 1"));
+        if (Objects.isNull(crmClinic)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmClinic = crmClinicService.getById(sysUser.getObjectId());
+        }
+        if (Objects.isNull(crmClinic)) {
+            return R.fail("未找到该账号诊所信息");
+        }
         List<TCrmClinicPoints> crmClinicPoints = crmClinicPointsService.clinicPointsStatistics(crmClinic.getId(), LocalDate.now() + " 23:59:59");
         Map<String, Integer> map = new HashMap<>();
         if (CollectionUtils.isEmpty(crmClinicPoints)) {
@@ -237,7 +272,13 @@
         Long userId = tokenService.getLoginUser().getUserId();
 
         TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
-                .eq(TCrmSupplier::getUserId, userId));
+                .eq(TCrmSupplier::getUserId, userId)
+                .last("LIMIT 1"));
+        // ***
+        if (Objects.isNull(crmSupplier)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmSupplier = crmSupplierService.getById(sysUser.getObjectId());
+        }
         // 查询药品总类和总数
         Integer typeTotalCount = erpProcurementService.supplierSalesStatisticsTypeCount(crmSupplier.getId());
         Integer totalCount = erpProcurementService.supplierSalesStatisticsCount(crmSupplier.getId());
@@ -281,7 +322,13 @@
                 break;
         }
         TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
-                .eq(TCrmSupplier::getUserId, userId));
+                .eq(TCrmSupplier::getUserId, userId)
+                .last("LIMIT 1"));
+        // ***
+        if (Objects.isNull(crmSupplier)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmSupplier = crmSupplierService.getById(sysUser.getObjectId());
+        }
         SupplierSalesStatisticsVO supplierSalesStatisticsVO = new SupplierSalesStatisticsVO();
         // 销售金额
         BigDecimal totalMoney = erpProcurementService.supplierSalesStatisticsMoney(crmSupplier.getId(), startTime, endTime);
@@ -294,7 +341,7 @@
             // 其余的添加到其他
             List<SupplierSalesStatisticsGoodsTypeVO> otherList = supplierSalesStatisticsGoodsTypeVOS.subList(9, supplierSalesStatisticsGoodsTypeVOS.size());
             SupplierSalesStatisticsGoodsTypeVO other = new SupplierSalesStatisticsGoodsTypeVO();
-            other.setTypeName("其他");
+            other.setGoodsName("其他");
             other.setGoodsCount(otherList.stream().mapToInt(SupplierSalesStatisticsGoodsTypeVO::getGoodsCount).sum());
             supplierSalesStatisticsGoodsTypeVOS.add(other);
             supplierSalesStatisticsGoodsTypeVOS = supplierSalesStatisticsGoodsTypeVOS.subList(0, 9);
@@ -332,7 +379,8 @@
                 LocalDateTime days = LocalDateTime.now().minusDays(6);
                 for (int i = 0; i < 7; i++) {
                     LocalDateTime timeNow = days.plusDays(i);
-                    List<SupplierSalesStatisticsGoodsCountVO> vos = supplierSalesStatisticsGoodsCountVOS.stream().filter(vo -> timeNow.toLocalDate().isEqual(vo.getPayTime())).collect(Collectors.toList());
+                    LocalDate localDate = timeNow.toLocalDate();
+                    List<SupplierSalesStatisticsGoodsCountVO> vos = supplierSalesStatisticsGoodsCountVOS.stream().filter(vo -> localDate.equals(vo.getPayTime())).collect(Collectors.toList());
                     if (CollectionUtils.isEmpty(vos)) {
                         SupplierSalesStatisticsGoodsCountVO vo = new SupplierSalesStatisticsGoodsCountVO();
                         String time = format.format(DateUtils.localDateTimeToDate(timeNow));
@@ -389,10 +437,18 @@
         Long userId = tokenService.getLoginUser().getUserId();
 
         TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
-                .eq(TCrmClinic::getUserId, userId));
+                .eq(TCrmClinic::getUserId, userId)
+                .last("LIMIT 1"));
+        if (Objects.isNull(crmClinic)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmClinic = crmClinicService.getById(sysUser.getObjectId());
+        }
+        if (Objects.isNull(crmClinic)) {
+            return R.fail("未找到该账号诊所信息");
+        }
         // 查询药品总类和总数
-        Integer typeTotalCount = sysOrderService.clinicSalesStatisticsTypeCount(Arrays.asList(crmClinic.getId()));
-        Integer totalCount = sysOrderService.clinicSalesStatisticsCount(Arrays.asList(crmClinic.getId()));
+        Integer typeTotalCount = sysOrderService.clinicSalesStatisticsTypeCount(Collections.singletonList(crmClinic.getId()));
+        Integer totalCount = sysOrderService.clinicSalesStatisticsCount(Collections.singletonList(crmClinic.getId()));
         Map<String, Integer> map = new HashMap<>();
         map.put("typeTotalCount", typeTotalCount);
         map.put("totalCount", totalCount);
@@ -433,7 +489,15 @@
                 break;
         }
         TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
-                .eq(TCrmClinic::getUserId, userId));
+                .eq(TCrmClinic::getUserId, userId)
+                .last("LIMIT 1"));
+        if (Objects.isNull(crmClinic)) {
+            SysUser sysUser = sysUserService.selectUserById(userId);
+            crmClinic = crmClinicService.getById(sysUser.getObjectId());
+        }
+        if (Objects.isNull(crmClinic)) {
+            return R.fail("未找到该账号诊所信息");
+        }
         ClinicSalesStatisticsVO clinicSalesStatisticsVO = new ClinicSalesStatisticsVO();
         // 销售金额
         BigDecimal totalMoney = sysOrderService.clinicSalesStatisticsMoney(crmClinic.getId(), startTime, endTime);
@@ -446,7 +510,7 @@
             // 其余的添加到其他
             List<ClinicSalesStatisticsGoodsTypeVO> otherList = clinicSalesStatisticsGoodsTypeVOS.subList(9, clinicSalesStatisticsGoodsTypeVOS.size());
             ClinicSalesStatisticsGoodsTypeVO other = new ClinicSalesStatisticsGoodsTypeVO();
-            other.setTypeName("其他");
+            other.setGoodsName("其他");
             other.setGoodsCount(otherList.stream().mapToInt(ClinicSalesStatisticsGoodsTypeVO::getGoodsCount).sum());
             clinicSalesStatisticsGoodsTypeVOS.add(other);
             clinicSalesStatisticsGoodsTypeVOS = clinicSalesStatisticsGoodsTypeVOS.subList(0, 9);
@@ -484,7 +548,7 @@
                 LocalDateTime days = LocalDateTime.now().minusDays(6);
                 for (int i = 0; i < 7; i++) {
                     LocalDateTime timeNow = days.plusDays(i);
-                    List<ClinicSalesStatisticsGoodsCountVO> vos = clinicSalesStatisticsGoodsCountVOS.stream().filter(vo -> timeNow.toLocalDate().isEqual(vo.getPayTime())).collect(Collectors.toList());
+                    List<ClinicSalesStatisticsGoodsCountVO> vos = clinicSalesStatisticsGoodsCountVOS.stream().filter(vo -> timeNow.toLocalDate().equals(vo.getPayTime())).collect(Collectors.toList());
                     if (CollectionUtils.isEmpty(vos)) {
                         ClinicSalesStatisticsGoodsCountVO vo = new ClinicSalesStatisticsGoodsCountVO();
                         String time = format.format(DateUtils.localDateTimeToDate(timeNow));
@@ -543,6 +607,140 @@
     }
 
 
+    @ApiOperation(value = "分公司管理员-首页统计(获取所有分公司信息)")
+    @GetMapping(value = "/branchAdminList")
+    public R<List<BranchAdminListVO>> branchAdminList() {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        ArrayList<BranchAdminListVO> branchAdminListVOs = new ArrayList<>();
+        if (roleType == 2) {
+            TCrmBranch branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                    .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
+                    .last("LIMIT 1"));
+            if (branch != null) {
+                // 如果是区
+                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()) {
+                        for (TCrmBranch crmBranch : crmBranches) {
+                            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                            branchAdminListVO.setId(crmBranch.getId());
+                            branchAdminListVO.setName(crmBranch.getBranchName());
+                            branchAdminListVOs.add(branchAdminListVO);
+                        }
+                    }
+                }
+
+                // 省
+                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));
+                    if (!crmBranches.isEmpty()) {
+                        Set<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toSet());
+
+                        for (TCrmBranch crmBranch : crmBranches) {
+                            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                            branchAdminListVO.setId(crmBranch.getId());
+                            branchAdminListVO.setName(crmBranch.getBranchName());
+                            branchAdminListVOs.add(branchAdminListVO);
+                        }
+                        // 城市公司
+                        List<TCrmBranch> crmBranchesThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2));
+                        if (!crmBranchesThree.isEmpty()) {
+                            for (TCrmBranch crmBranch : crmBranchesThree) {
+                                BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                                branchAdminListVO.setId(crmBranch.getId());
+                                branchAdminListVO.setName(crmBranch.getBranchName());
+                                branchAdminListVOs.add(branchAdminListVO);
+                            }
+                        }
+                    }
+
+                }
+            }
+        }
+        return R.ok(branchAdminListVOs);
+    }
+    @ApiOperation(value = "分公司管理员-首页统计(获取所有业务员信息)")
+    @GetMapping(value = "/branchAdminSaleList")
+    public R<List<BranchAdminListVO>> branchAdminSaleList(String branchId) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        ArrayList<BranchAdminListVO> branchAdminListVOs = new ArrayList<>();
+        if (roleType == 2) {
+            TCrmBranch branch =null;
+            if(branchId==null || branchId.isEmpty()){
+                branch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
+                        .eq(TCrmBranch::getUserId, user.getUserId()).eq(TCrmBranch::getAuditStatus,2)
+                        .last("LIMIT 1"));
+            }else {
+                branch = crmBranchService.getById(branchId);
+            }
+
+            if (branch != null) {
+                // 如果是区
+                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()) {
+                        for (TCrmBranch crmBranch : crmBranches) {
+                            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                            branchAdminListVO.setId(crmBranch.getId());
+                            branchAdminListVO.setName(crmBranch.getBranchName());
+                            branchAdminListVOs.add(branchAdminListVO);
+                        }
+                    }
+                }
+
+                // 省
+                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));
+                    if (!crmBranches.isEmpty()) {
+                        Set<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toSet());
+
+                        for (TCrmBranch crmBranch : crmBranches) {
+                            BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                            branchAdminListVO.setId(crmBranch.getId());
+                            branchAdminListVO.setName(crmBranch.getBranchName());
+                            branchAdminListVOs.add(branchAdminListVO);
+                        }
+                        // 城市公司
+                        List<TCrmBranch> crmBranchesThree = crmBranchService.list(new LambdaQueryWrapper<TCrmBranch>().in(TCrmBranch::getCorrelationPersonId, userIds).eq(TCrmBranch::getPersonType, 3).eq(TCrmBranch::getAuditStatus,2));
+                        if (!crmBranchesThree.isEmpty()) {
+                            for (TCrmBranch crmBranch : crmBranchesThree) {
+                                BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                                branchAdminListVO.setId(crmBranch.getId());
+                                branchAdminListVO.setName(crmBranch.getBranchName());
+                                branchAdminListVOs.add(branchAdminListVO);
+                            }
+                        }
+                    }
+
+                }
+            }
+        }
+
+        ArrayList<BranchAdminListVO> list = new ArrayList<>();
+        if(!branchAdminListVOs.isEmpty()){
+            Set<String> branchIds = branchAdminListVOs.stream().map(BranchAdminListVO::getId).collect(Collectors.toSet());
+            List<TCrmSalesperson> tCrmSalespersonList = crmSalespersonService.list(new LambdaQueryWrapper<TCrmSalesperson>().in(TCrmSalesperson::getBranchId, branchIds));
+            if(!tCrmSalespersonList.isEmpty()){
+                for (TCrmSalesperson tCrmSalesperson : tCrmSalespersonList) {
+                    BranchAdminListVO branchAdminListVO = new BranchAdminListVO();
+                    branchAdminListVO.setId(tCrmSalesperson.getId());
+                    branchAdminListVO.setName(tCrmSalesperson.getSalespersonName());
+                    list.add(branchAdminListVO);
+                }
+            }
+        }
+        return R.ok(list);
+    }
+
+
     /**
      * 分公司管理员-首页统计(第一部分)
      */
@@ -552,20 +750,83 @@
         BranchAdminStatisticsOneVo branchAdminStatisticsOneVo = new BranchAdminStatisticsOneVo();
         // 找出分公司下的所有员工 -》 所有诊所-》订单(状态 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()));
+            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::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());
+                        // 城市公司
+                        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))));
+                        }
+                    }
+                }
+
+
                 if (!list.isEmpty()) {
                     List<String> crmClinicIds = list.stream().map(TCrmClinic::getId).collect(Collectors.toList());
 
                     // 获取最近一年的时间
                     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());
@@ -609,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");
                         }
@@ -649,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);
 
@@ -672,20 +937,72 @@
         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())
-                    .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) {
-                LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
-                if (query.getSalespersonId() != null && !query.getSalespersonId().isEmpty()) {
-                    eq.eq(TCrmClinic::getSalespersonId, query.getSalespersonId());
-                } else {
-                    eq.eq(TCrmClinic::getBranchId, branch.getId());
-                }
-                eq.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
+                // 先查自己本身的
+                List<TCrmClinic> list = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).eq(TCrmClinic::getBranchId, branch.getId()).in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5)));
 
-                List<TCrmClinic> list = crmClinicService.list(eq);
+                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());
+                        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());
+                }
+
                 branchAdminStatisticsTwoVo.setClinicNum(list.size());
 
                 // 金额
@@ -763,15 +1080,67 @@
         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())
-                    .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) {
-                LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
-                eq.eq(TCrmClinic::getBranchId, branch.getId());
-                eq.eq(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
-                List<TCrmClinic> list = crmClinicService.list(eq);
+                // 先查自己本身的
+                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());
+                        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))));
+                        }
+                    }
+                }
+
 
                 List<String> clinicIds = list.stream().map(TCrmClinic::getId).collect(Collectors.toList());
                 if (clinicIds.isEmpty()) {
@@ -815,7 +1184,7 @@
                 List<ClinicSalesStatisticsGoodsTypeVO> thirtyMoneyProcurementStatisticsVOS = collect2.entrySet().stream().map(entry -> {
                     ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                     clinicSalesStatisticsGoodsTypeVO.setGoodsCount(entry.getValue().stream().mapToInt(TErpProcurementGoods::getPurchaseCount).sum());
-                    clinicSalesStatisticsGoodsTypeVO.setTypeName(entry.getValue().get(0).getGoodsName());
+                    clinicSalesStatisticsGoodsTypeVO.setGoodsName(entry.getValue().get(0).getGoodsName());
                     return clinicSalesStatisticsGoodsTypeVO;
                 }).sorted(Comparator.comparing(ClinicSalesStatisticsGoodsTypeVO::getGoodsCount).reversed()).limit(9).collect(Collectors.toList());
                 // 总数
@@ -824,7 +1193,7 @@
                 if (sum - sum1 > 0) {
                     ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                     clinicSalesStatisticsGoodsTypeVO.setGoodsCount(sum - sum1);
-                    clinicSalesStatisticsGoodsTypeVO.setTypeName("其他");
+                    clinicSalesStatisticsGoodsTypeVO.setGoodsName("其他");
                     thirtyMoneyProcurementStatisticsVOS.add(clinicSalesStatisticsGoodsTypeVO);
                 }
                 vo.setThirtyMoneyProcurementStatisticsVOS(thirtyMoneyProcurementStatisticsVOS);
@@ -1002,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()) {
@@ -1119,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()) {
@@ -1169,7 +1627,7 @@
             List<ClinicSalesStatisticsGoodsTypeVO> thirtyMoneyProcurementStatisticsVOS = collect2.entrySet().stream().map(entry -> {
                 ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                 clinicSalesStatisticsGoodsTypeVO.setGoodsCount(entry.getValue().stream().mapToInt(TErpProcurementGoods::getPurchaseCount).sum());
-                clinicSalesStatisticsGoodsTypeVO.setTypeName(entry.getValue().get(0).getGoodsName());
+                clinicSalesStatisticsGoodsTypeVO.setGoodsName(entry.getValue().get(0).getGoodsName());
                 return clinicSalesStatisticsGoodsTypeVO;
             }).sorted(Comparator.comparing(ClinicSalesStatisticsGoodsTypeVO::getGoodsCount).reversed()).limit(9).collect(Collectors.toList());
             // 总数
@@ -1178,7 +1636,7 @@
             if (sum - sum1 > 0) {
                 ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                 clinicSalesStatisticsGoodsTypeVO.setGoodsCount(sum - sum1);
-                clinicSalesStatisticsGoodsTypeVO.setTypeName("其他");
+                clinicSalesStatisticsGoodsTypeVO.setGoodsName("其他");
                 thirtyMoneyProcurementStatisticsVOS.add(clinicSalesStatisticsGoodsTypeVO);
             }
             vo.setThirtyMoneyProcurementStatisticsVOS(thirtyMoneyProcurementStatisticsVOS);
@@ -1258,8 +1716,9 @@
         SysUser user = tokenService.getLoginUser().getUser();
         Integer roleType = user.getRoleType();
         if (roleType == 3) {
+            TCrmSalesperson one = crmSalespersonService.getOne(new LambdaQueryWrapper<TCrmSalesperson>().eq(TCrmSalesperson::getUserId, user.getUserId()));
             LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
-            eq.eq(TCrmClinic::getSalespersonId, user.getUserId());
+            eq.eq(TCrmClinic::getSalespersonId, one.getId());
             eq.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
 
             List<TCrmClinic> list = crmClinicService.list(eq);
@@ -1339,8 +1798,9 @@
         SimpleDateFormat formatMonth = new SimpleDateFormat("yyyy-MM");
         Integer roleType = user.getRoleType();
         if (roleType == 3) {
+            TCrmSalesperson one = crmSalespersonService.getOne(new LambdaQueryWrapper<TCrmSalesperson>().eq(TCrmSalesperson::getUserId, user.getUserId()));
             LambdaQueryWrapper<TCrmClinic> eq = Wrappers.lambdaQuery(TCrmClinic.class);
-            eq.eq(TCrmClinic::getSalespersonId, user.getUserId());
+            eq.eq(TCrmClinic::getSalespersonId, one.getId());
             eq.in(TCrmClinic::getStatus, Arrays.asList(3, 4, 5));
             List<TCrmClinic> list = crmClinicService.list(eq);
 
@@ -1386,7 +1846,7 @@
             List<ClinicSalesStatisticsGoodsTypeVO> thirtyMoneyProcurementStatisticsVOS = collect2.entrySet().stream().map(entry -> {
                 ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                 clinicSalesStatisticsGoodsTypeVO.setGoodsCount(entry.getValue().stream().mapToInt(TErpProcurementGoods::getPurchaseCount).sum());
-                clinicSalesStatisticsGoodsTypeVO.setTypeName(entry.getValue().get(0).getGoodsName());
+                clinicSalesStatisticsGoodsTypeVO.setGoodsName(entry.getValue().get(0).getGoodsName());
                 return clinicSalesStatisticsGoodsTypeVO;
             }).sorted(Comparator.comparing(ClinicSalesStatisticsGoodsTypeVO::getGoodsCount).reversed()).limit(9).collect(Collectors.toList());
             // 总数
@@ -1395,7 +1855,7 @@
             if (sum - sum1 > 0) {
                 ClinicSalesStatisticsGoodsTypeVO clinicSalesStatisticsGoodsTypeVO = new ClinicSalesStatisticsGoodsTypeVO();
                 clinicSalesStatisticsGoodsTypeVO.setGoodsCount(sum - sum1);
-                clinicSalesStatisticsGoodsTypeVO.setTypeName("其他");
+                clinicSalesStatisticsGoodsTypeVO.setGoodsName("其他");
                 thirtyMoneyProcurementStatisticsVOS.add(clinicSalesStatisticsGoodsTypeVO);
             }
             vo.setThirtyMoneyProcurementStatisticsVOS(thirtyMoneyProcurementStatisticsVOS);

--
Gitblit v1.7.1