刘帅
2026-09-21 3f45e8fdfa31d7b764d60a7c0cb5dd11944ed418
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
@@ -82,8 +82,29 @@
    }
    /**
     * 运营商统计的门店范围:平台(0)统计平台门店,其他值统计对应运营商门店。
     * 将门店统计目标限制在当前账号可见范围内。门店账号固定统计自身;运营商账号
     * 只能统计归属自己的门店,避免其它门店的数据混入其统计结果。
     */
    private Integer resolveStatisticsStoreId(Integer storeId) {
        Integer objectType = UserExt.getUser().getObjectType();
        if (Integer.valueOf(3).equals(objectType)) {
            return UserExt.getUser().getObjectId();
        }
        if (storeId == null) {
            return null;
        }
        TStore store = storeService.getById(storeId);
        if (store == null || Integer.valueOf(3).equals(store.getState())) {
            return null;
        }
        if (Integer.valueOf(2).equals(objectType)
                && !UserExt.getUser().getObjectId().equals(store.getOperatorId())) {
            return null;
        }
        return storeId;
    }
    /** 平台(0)或运营商统计均以门店的 operatorId 作为唯一归属依据。 */
    private List<TStore> listStatisticsStores(Integer operatorId, boolean enabledOnly) {
        LambdaQueryWrapper<TStore> wrapper = new LambdaQueryWrapper<>();
        if (enabledOnly) {
@@ -91,12 +112,19 @@
        } else {
            wrapper.ne(TStore::getState, 3);
        }
        if (Integer.valueOf(0).equals(operatorId)) {
            wrapper.eq(TStore::getType, 1);
        if (operatorId == null) {
            // 平台统计:所有门店总和,不按门店所属运营商过滤。
        } else {
            wrapper.eq(TStore::getType, 2).eq(TStore::getOperatorId, operatorId);
            wrapper.eq(TStore::getOperatorId, operatorId);
        }
        return storeService.list(wrapper);
    }
    /** 空门店范围必须显式传递无匹配值,不能让下游 SQL 因空集合而省略 IN 条件。 */
    private List<Integer> listStatisticsStoreIds(Integer operatorId, boolean enabledOnly) {
        List<Integer> storeIds = listStatisticsStores(operatorId, enabledOnly).stream()
                .map(TStore::getId).collect(Collectors.toList());
        return storeIds.isEmpty() ? Collections.singletonList(-1) : storeIds;
    }
    /** 获取统计范围门店发生课程、订场或赛事消费的用户。 */
@@ -116,6 +144,30 @@
                .stream().map(SiteBooking::getAppUserId).filter(Objects::nonNull)
                .collect(Collectors.toList()));
        List<Integer> competitionUserIds = competitionClient.queryAppUserId(storeIds);
        if (competitionUserIds != null) {
            userIds.addAll(competitionUserIds);
        }
        List<Integer> validUserIds = userIds.stream().filter(Objects::nonNull).distinct()
                .collect(Collectors.toList());
        return validUserIds.isEmpty() ? Collections.singletonList(-1) : validUserIds;
    }
    private List<Integer> listStatisticsAppUserIdsForStore(Integer storeId) {
        if (storeId == null) {
            return Collections.singletonList(-1);
        }
        List<Integer> userIds = new ArrayList<>();
        List<Integer> courseUserIds = coursePackagePaymentClient.queryPaymentCoursePackage(
                Collections.singletonList(storeId));
        if (courseUserIds != null) {
            userIds.addAll(courseUserIds);
        }
        userIds.addAll(siteBookingService.list(new QueryWrapper<SiteBooking>()
                        .eq("storeId", storeId).eq("state", 1))
                .stream().map(SiteBooking::getAppUserId).filter(Objects::nonNull)
                .collect(Collectors.toList()));
        List<Integer> competitionUserIds = competitionClient.queryAppUserId(
                Collections.singletonList(storeId));
        if (competitionUserIds != null) {
            userIds.addAll(competitionUserIds);
        }
@@ -240,6 +292,23 @@
            storeId = resolveStoreId(storeId);
        }
        // 下游多处查询会在门店集合为空时忽略门店过滤。运营商没有任何门店时,
        // 直接返回完整的零值结构,避免被误统计为全平台数据。
        if (operatorId != null && storeId == null
                && storeService.count(new LambdaQueryWrapper<TStore>()
                        .eq(TStore::getOperatorId, operatorId)
                        .ne(TStore::getState, 3)) == 0) {
            Map<String, Object> emptyIncomeData = buildIncomeStatistics(Collections.emptyList());
            emptyIncomeData.put("incomeTypeNet", buildIncomeTypeNet(Collections.emptyList(), null));
            Map<String, Object> emptyTicketIncome = new HashMap<>();
            emptyTicketIncome.put("day", BigDecimal.ZERO);
            emptyTicketIncome.put("month", BigDecimal.ZERO);
            emptyTicketIncome.put("quarter", BigDecimal.ZERO);
            emptyTicketIncome.put("year", BigDecimal.ZERO);
            emptyIncomeData.put("ticketIncome", emptyTicketIncome);
            return emptyIncomeData;
        }
        // 参数代表当前统计目标,而非当前登录人的角色;平台账号查看运营商/门店页时,
        // 会员、充值也必须跟随被查看的运营商/门店归属。
        Integer statisticsScopeType = Integer.valueOf(3).equals(roleType) || storeId != null ? 3
@@ -247,10 +316,12 @@
        Integer statisticsScopeObjectId = statisticsScopeType == 3 ? storeId
                : (statisticsScopeType == 2 ? operatorId : null);
        List<Integer> cityManagerId= new ArrayList<>();
        // 积分商品收入接口按门店 ID 过滤;空集合会被其 SQL 视为“不过滤”,
        // 因此运营商统计必须传入该运营商名下的全部门店,不能传空集合。
        List<Integer> cityManagerId = new ArrayList<>();
        if (Integer.valueOf(2).equals(statisticsScopeType) && statisticsScopeObjectId != null) {
            cityManagerId = storeService.list(new QueryWrapper<TStore>()
                            .eq("cityManagerId", statisticsScopeObjectId).ne("state", 3))
                            .eq("operatorId", statisticsScopeObjectId).ne("state", 3))
                    .stream().map(TStore::getId).collect(Collectors.toList());
        } else if (Integer.valueOf(3).equals(statisticsScopeType) && statisticsScopeObjectId != null) {
            cityManagerId.add(statisticsScopeObjectId);
@@ -518,20 +589,24 @@
        // 门票饼图需要日/月/季/年卡拆分;此前该数据只在 type1 内部返回,
        // 现随统一营收接口返回,供平台页直接使用。
        monthlyData.put("ticketIncome", getTicketIncome(time, operatorId, storeId));
        // 平台收入类型饼图还需要课程、订场分类明细。此前页面只能读取 type1
        // 的模板数据,筛选后 getIncomeData 缺少该字段,导致课程图显示为空。
        if (operatorId == null && storeId == null) {
            Map<String, Object> platformTypeData = (Map<String, Object>) type1(time);
            monthlyData.put("courseData", platformTypeData.get("courseData"));
            monthlyData.put("siteData", platformTypeData.get("siteData"));
        }
        return monthlyData;
    }
    private Map<String, Object> getTicketIncome(String time, Integer operatorId, Integer storeId) {
        List<Integer> userIds;
        if (storeId != null) {
            userIds = appUserClient.getUserStore(storeId);
            userIds = listStatisticsAppUserIdsForStore(storeId);
        } else if (operatorId != null) {
            userIds = appUserClient.getUserYYs(operatorId);
            userIds = listStatisticsAppUserIds(operatorId);
        } else {
            List<Integer> platformUserIds = sysUserService.list(new LambdaQueryWrapper<User>()
                            .eq(User::getObjectType, 1))
                    .stream().map(User::getId).collect(Collectors.toList());
            userIds = appUserClient.getUserPt(platformUserIds);
            userIds = listStatisticsAppUserIds(null);
        }
        if (userIds == null) {
            userIds = new ArrayList<>();
@@ -902,10 +977,7 @@
    @ResponseBody
    public Object backPt(Integer type) {
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> userPt = appUserClient.getUserPt(Arrays.asList(1));
        if(userPt.size()==0){
            userPt.add(-1);
        }
        List<Integer> userPt = listStatisticsAppUserIds(null);
        ArrayList<Object> integers = new ArrayList<>();
        int year = DateUtil.year(new Date());
        List<TBackRecord> thisYear = backRecordService.list(new LambdaQueryWrapper<TBackRecord>().like(TBackRecord::getTime, year).in(TBackRecord::getUserId,userPt));
@@ -1108,7 +1180,8 @@
    @RequestMapping(value = "/backStore")
    @ResponseBody
    public Object backStore(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        id = resolveStatisticsStoreId(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1227,15 +1300,16 @@
    @RequestMapping(value = "/userAndVipPt")
    @ResponseBody
    public Object userAndVipPt(Integer type) {
        type=1;
        HashMap<String, Object> map = appUserClient.userAndVipPt(type);
        // 平台用户统计按用户归属全量统计,不能以“已有消费记录”筛选,
        // 否则会遗漏尚未在门店消费的新注册用户。
        HashMap<String, Object> map = appUserClient.userAndVipPt(1);
        return map;
    }
    @RequestMapping(value = "/stuPt")
    @ResponseBody
    public Object stuPt() {
        HashMap<String, Object> map = appUserClient.stuPt();
        HashMap<String, Object> map = appUserClient.stuYys(listStatisticsAppUserIds(null));
        return map;
    }
    @RequestMapping(value = "/stuYys")
@@ -1269,7 +1343,8 @@
    @RequestMapping(value = "/stuStores")
    @ResponseBody
    public Object stuStores(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        id = resolveStatisticsStoreId(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1289,7 +1364,7 @@
    @RequestMapping(value = "/coursePt")
    @ResponseBody
    public Object coursePt() {
        List<Integer> userPt = appUserClient.getUserPt(Arrays.asList(1));
        List<Integer> userPt = listStatisticsAppUserIds(null);
        HashMap<String, Object> map = coursePackagePaymentClient.coursePt(userPt);
        return map;
    }
@@ -1306,7 +1381,8 @@
    @RequestMapping(value = "/courseStore")
    @ResponseBody
    public Object courseStore(Integer id) {
        List<Integer> userPt = appUserClient.getUserPt(Arrays.asList(id));
        id = resolveStatisticsStoreId(id);
        List<Integer> userPt = listStatisticsAppUserIdsForStore(id);
        HashMap<String, Object> map = coursePackagePaymentClient.coursePt(userPt);
        return map;
    }
@@ -1315,8 +1391,7 @@
    @ResponseBody
    public Object userAndVipYys(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        List<Integer> collect1 = listStatisticsStoreIds(id, true);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1334,7 +1409,8 @@
    @RequestMapping(value = "/userAndVipStore")
    @ResponseBody
    public Object userAndVipStore(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        id = resolveStatisticsStoreId(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1352,10 +1428,10 @@
    @RequestMapping(value = "/actPt")
    @ResponseBody
    public Object actPt(Integer type) {
        List<Integer> userPt = appUserClient.getUserPt(Arrays.asList(1));
        List<Integer> userPt = listStatisticsAppUserIds(null);
        HashMap<String, Object> map = competitionClient.actPt(userPt);
        List<TStore> list = storeService.list(new LambdaQueryWrapper<TStore>().eq(TStore::getOperatorId, 0).ne(TStore::getState, 3));
        List<TStore> list = listStatisticsStores(null, false);
        long l1=0;
        for (TStore tStore : list) {
            l1 += getStoreOperatingHours(tStore, new Date());
@@ -1368,8 +1444,7 @@
    public Object actYys(Integer id) {
        id = resolveOperatorId(id);
        CompetionVO competionVO = new CompetionVO();
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        List<Integer> collect1 = listStatisticsStoreIds(id, true);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1448,8 +1523,7 @@
//        List<User> list = sysUserService.list(new LambdaQueryWrapper<User>().eq(User::getObjectType, 2));
//        List<Integer> ids = list.stream().map(User::getId).collect(Collectors.toList());
//        List<Integer> userIds = appUserClient.getUserYYs(id);
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        List<Integer> collect1 = listStatisticsStoreIds(id, true);
        QueryDataFee queryDataFee = new QueryDataFee(time, collect1);
        // 课程收入
        HashMap<String, Object> map3 = courseClient.queryFee(queryDataFee);
@@ -1469,7 +1543,7 @@
        }else{
            List<TSite> list = siteService.list(new QueryWrapper<TSite>().in("storeId", collect1).eq("state", 1));
            ids = list.stream().map(TSite::getId).collect(Collectors.toList());
            if(collect1.size()==0){
            if(ids.isEmpty()){
                ids.add(-1);
            }
        }
@@ -1485,9 +1559,29 @@
        objects.add(1);
        objects.add(2);
        vipPaymentLambdaQueryWrapper.in(SiteBooking::getPayType,objects);
        List<SiteBooking> list1 = siteBookingService.list(vipPaymentLambdaQueryWrapper);
        List<SiteBooking> list1 = new ArrayList<>(siteBookingService.list(vipPaymentLambdaQueryWrapper));
        double sum = list1.stream().mapToDouble(SiteBooking::getPayMoney).sum();
        map.put("fee5",sum);
        // 智慧球场支付记录的场地可能只保存在 t_game.siteId。ballClient 已补齐该值,
        // 将其转换为订场明细后可与普通订场一起按场地类型统计。
        GameDataQuery gameDataQuery = new GameDataQuery();
        gameDataQuery.setObjectId(id);
        if (ToolUtil.isNotEmpty(time)) {
            gameDataQuery.setStartTime(time.split(" - ")[0] + " 00:00:00");
            gameDataQuery.setEndTime(time.split(" - ")[1] + " 23:59:59");
        }
        double gameIncome = 0D;
        for (TGameRecord gameRecord : ballClient.getData(gameDataQuery)) {
            if (gameRecord.getSiteId() == null || gameRecord.getMoney() == null || !ids.contains(gameRecord.getSiteId())) {
                continue;
            }
            SiteBooking gameBooking = new SiteBooking();
            gameBooking.setSiteId(gameRecord.getSiteId());
            gameBooking.setPayMoney(gameRecord.getMoney().doubleValue());
            list1.add(gameBooking);
            gameIncome += gameRecord.getMoney().doubleValue();
        }
        map.put("fee5", sum + gameIncome);
        List<Integer> collect = list1.stream().map(SiteBooking::getSiteId).collect(Collectors.toList());
@@ -1549,8 +1643,9 @@
    @ResponseBody
    @RequestMapping("/selectStore")
    public Object selectStore(Integer id,String time) {
        id = resolveStatisticsStoreId(id);
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> collect1 = Arrays.asList(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1699,10 +1794,8 @@
    @ResponseBody
    public Object type1(String time) {
        HashMap<String, Object> map = new HashMap<>();
        // 找出平台的用户
        List<User> list = sysUserService.list(new LambdaQueryWrapper<User>().eq(User::getObjectType, 1));
        List<Integer> ids = list.stream().map(User::getId).collect(Collectors.toList());
        List<Integer> userIds = appUserClient.getUserPt(ids);
        // 平台统计是所有门店总和,用户范围同样来自所有门店的消费用户。
        List<Integer> userIds = listStatisticsAppUserIds(null);
        QueryDataFee queryDataFee = new QueryDataFee(time, userIds);
        // 课程和赛事接口的 ids 参数表示门店 ID;不能把平台用户 ID 传入。
        List<Integer> platformStoreIds = storeService.list(new LambdaQueryWrapper<TStore>()
@@ -1749,8 +1842,20 @@
        }
        // 平台智慧球场
        List<TGameRecord> data1 = ballClient.getData(gameDataQuery);
        // 将游戏支付转换为场地收入明细参与后续 siteId -> siteTypeId 分类。
        // fee5 仍由普通订场金额与游戏金额分别相加,避免重复计入总额。
        for (TGameRecord gameRecord : data1) {
            if (gameRecord.getSiteId() == null || gameRecord.getMoney() == null) {
                continue;
            }
            SiteBooking gameBooking = new SiteBooking();
            gameBooking.setSiteId(gameRecord.getSiteId());
            gameBooking.setPayMoney(gameRecord.getMoney().doubleValue());
            list1.add(gameBooking);
        }
        double sum1 = data1.stream()
                .map(TGameRecord::getMoney) // 获取每个 TGameRecord 对象的 money 字段
                .filter(Objects::nonNull)
                .mapToDouble(BigDecimal::doubleValue) // 将 BigDecimal 转换为 double
                .sum();
        map.put("fee5",sum+sum1);
@@ -1807,9 +1912,7 @@
    @RequestMapping("/stuCourseData")
    @ResponseBody
    public Object stuCourseData() {
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        List<Integer> userIds = appUserClient.getUserPt(objects);
        List<Integer> userIds = listStatisticsAppUserIds(null);
        List<Map<String,Object>> list = coursePackagePaymentClient.queryCourseData(userIds);
        ArrayList<Object> objects2 = new ArrayList<>();
        ArrayList<Object> objects3 = new ArrayList<>();
@@ -1827,8 +1930,7 @@
    @RequestMapping("/stuUserData")
    @ResponseBody
    public Object stuUserData() {
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        // 平台地域/年龄统计按平台注册用户全量口径,不能仅取已有消费记录的用户。
        HashMap<String, Object> map = appUserClient.queryUserAge1();
        return map;
@@ -1837,8 +1939,7 @@
    @ResponseBody
    public Object stuUserDataYys(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        List<Integer> collect1 = listStatisticsStoreIds(id, true);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1855,8 +1956,8 @@
    @RequestMapping("/stuUserDataStore")
    @ResponseBody
    public Object stuUserDataStore(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        id = resolveStatisticsStoreId(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1876,8 +1977,7 @@
    @ResponseBody
    public Object selectYysJx(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        List<Integer> collect1 = listStatisticsStoreIds(id, true);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1909,7 +2009,8 @@
    @RequestMapping("/selectStoreJx")
    @ResponseBody
    public Object selectStoreJx(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        id = resolveStatisticsStoreId(id);
        List<Integer> collect1 = Collections.singletonList(id == null ? -1 : id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地