刘帅
2026-09-17 dce5a34e89123f487cd22b9a4e5254ffc8a5df75
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
@@ -82,6 +82,49 @@
    }
    /**
     * 运营商统计的门店范围:平台(0)统计平台门店,其他值统计对应运营商门店。
     */
    private List<TStore> listStatisticsStores(Integer operatorId, boolean enabledOnly) {
        LambdaQueryWrapper<TStore> wrapper = new LambdaQueryWrapper<>();
        if (enabledOnly) {
            wrapper.eq(TStore::getState, 1);
        } else {
            wrapper.ne(TStore::getState, 3);
        }
        if (Integer.valueOf(0).equals(operatorId)) {
            wrapper.eq(TStore::getType, 1);
        } else {
            wrapper.eq(TStore::getType, 2).eq(TStore::getOperatorId, operatorId);
        }
        return storeService.list(wrapper);
    }
    /** 获取统计范围门店发生课程、订场或赛事消费的用户。 */
    private List<Integer> listStatisticsAppUserIds(Integer operatorId) {
        List<Integer> storeIds = listStatisticsStores(operatorId, true).stream()
                .map(TStore::getId).collect(Collectors.toList());
        if (storeIds.isEmpty()) {
            return Collections.singletonList(-1);
        }
        List<Integer> userIds = new ArrayList<>();
        List<Integer> courseUserIds = coursePackagePaymentClient.queryPaymentCoursePackage(storeIds);
        if (courseUserIds != null) {
            userIds.addAll(courseUserIds);
        }
        userIds.addAll(siteBookingService.list(new QueryWrapper<SiteBooking>()
                        .in("storeId", storeIds).eq("state", 1))
                .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;
    }
    /**
     * 计算门店每日营业整小时数。相同的起止时间表示全天营业,结束时间早于
     * 开始时间时按跨午夜营业处理。
     */
@@ -181,7 +224,7 @@
        if(objectId>0){
            operatorId=objectId;
            if(storeId==null){
                cityManagerId= storeService.list(new QueryWrapper<TStore>().eq("cityManagerId", objectId)).stream().map(TStore::getId).collect(Collectors.toList());
                cityManagerId= storeService.list(new QueryWrapper<TStore>().eq("cityManagerId", objectId).ne("state", 3)).stream().map(TStore::getId).collect(Collectors.toList());
            }else {
                cityManagerId.add(storeId);
            }
@@ -207,7 +250,7 @@
        // 根据运营商ID 查询运营商下的所有门店
        if (storeId==null){
            if (operatorId!=null){
                List<TStore> stores = storeService.list(new QueryWrapper<TStore>().eq("operatorId", operatorId));
                List<TStore> stores = listStatisticsStores(operatorId, false);
                // 拿到该运营商下的门店id集合
                storeIds = stores.stream().map(TStore::getId).collect(Collectors.toList());
                query.setStoresIds(storeIds);
@@ -476,23 +519,28 @@
        int currentYear = today.getYear();
        List<IncomeStatisticsVO> yearly = new ArrayList<>();
        List<Object> years = new ArrayList<>();
        List<String> yearLabels = new ArrayList<>();
        for (int year = currentYear - 9; year <= currentYear; year++) {
            final int statisticYear = year;
            yearly.add(sumIncomeStatistics(incomeRecords, record -> getRecordDate(record)
                    .getYear() == statisticYear));
            years.add(year);
            yearLabels.add(String.valueOf(year));
        }
        List<IncomeStatisticsVO> monthly = new ArrayList<>();
        List<String> monthLabels = new ArrayList<>();
        for (int month = 1; month <= 12; month++) {
            final int statisticMonth = month;
            monthly.add(sumIncomeStatistics(incomeRecords, record -> {
                LocalDate date = getRecordDate(record);
                return date.getYear() == currentYear && date.getMonthValue() == statisticMonth;
            }));
            monthLabels.add(String.format("%d-%02d", currentYear, month));
        }
        List<IncomeStatisticsVO> weekly = new ArrayList<>();
        List<String> weekLabels = new ArrayList<>();
        for (int weekIndex = 3; weekIndex >= 0; weekIndex--) {
            LocalDate startDate = today.minusDays(weekIndex * 7L + 6L);
            LocalDate endDate = today.minusDays(weekIndex * 7L);
@@ -500,13 +548,17 @@
                LocalDate date = getRecordDate(record);
                return !date.isBefore(startDate) && !date.isAfter(endDate);
            }));
            weekLabels.add(startDate.format(DateTimeFormatter.ISO_LOCAL_DATE)
                    + " 至 " + endDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<IncomeStatisticsVO> daily = new ArrayList<>();
        List<String> dayLabels = new ArrayList<>();
        for (int dayIndex = 6; dayIndex >= 0; dayIndex--) {
            LocalDate statisticDate = today.minusDays(dayIndex);
            daily.add(sumIncomeStatistics(incomeRecords, record -> getRecordDate(record)
                    .equals(statisticDate)));
            dayLabels.add(statisticDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        IncomeStatisticsVO total = sumIncomeStatistics(incomeRecords, record -> true);
@@ -527,6 +579,10 @@
        result.put("weekIncomeStatistics", weekly);
        result.put("dayIncomeStatistics", daily);
        result.put("year", years);
        result.put("yearLabels", yearLabels);
        result.put("monthLabels", monthLabels);
        result.put("weekLabels", weekLabels);
        result.put("dayLabels", dayLabels);
        // 兼容既有图表消费字段,值调整为总营收金额。
        result.put("yearIncome", yearly.stream().map(IncomeStatisticsVO::getTotalIncomeAmount)
                .collect(Collectors.toList()));
@@ -817,10 +873,7 @@
    public Object backYys(Integer id) {
        id = resolveOperatorId(id);
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> userPt = appUserClient.getUserYYs(id);
        if(userPt.size()==0){
            userPt.add(-1);
        }
        List<Integer> userPt = listStatisticsAppUserIds(id);
        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));
@@ -930,6 +983,10 @@
        //赛事
        List<Integer> list2 = competitionClient.queryAppUserId(collect1);
        list.addAll(list2);
        // 没有消费用户时,避免 MyBatis-Plus 生成 userId IN () 的非法 SQL。
        if (list.isEmpty()) {
            list.add(-1);
        }
        //门票
        HashMap<String, Object> map = new HashMap<>();
@@ -1050,8 +1107,12 @@
    @ResponseBody
    public Object stuYys(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        // 运营商没有门店时,后续查询不能生成 storeId IN ()。
        if (collect1.isEmpty()) {
            collect1.add(-1);
        }
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
@@ -1061,6 +1122,10 @@
        //赛事
        List<Integer> list2 = competitionClient.queryAppUserId(collect1);
        list.addAll(list2);
        // 下游会员统计同样使用 IN 查询,空用户列表按无匹配记录处理。
        if (list.isEmpty()) {
            list.add(-1);
        }
        //门票
        HashMap<String, Object> map = appUserClient.stuYys(list);
@@ -1098,7 +1163,7 @@
    @ResponseBody
    public Object courseYys(Integer id) {
        id = resolveOperatorId(id);
        List<Integer> userPt = appUserClient.getUserYYs(id);
        List<Integer> userPt = listStatisticsAppUserIds(id);
        HashMap<String, Object> map = coursePackagePaymentClient.coursePt(userPt);
        return map;
    }
@@ -1115,7 +1180,7 @@
    @ResponseBody
    public Object userAndVipYys(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
@@ -1155,7 +1220,7 @@
        List<Integer> userPt = appUserClient.getUserPt(Arrays.asList(1));
        HashMap<String, Object> map = competitionClient.actPt(userPt);
        List<TStore> list = storeService.list(new LambdaQueryWrapper<TStore>().eq(TStore::getOperatorId, 0));
        List<TStore> list = storeService.list(new LambdaQueryWrapper<TStore>().eq(TStore::getOperatorId, 0).ne(TStore::getState, 3));
        long l1=0;
        for (TStore tStore : list) {
            l1 += getStoreOperatingHours(tStore, new Date());
@@ -1168,7 +1233,7 @@
    public Object actYys(Integer id) {
        id = resolveOperatorId(id);
        CompetionVO competionVO = new CompetionVO();
        List<TStore> list4 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
@@ -1187,7 +1252,7 @@
        competionVO.setStoreIds(collect1);
        HashMap<String, Object> map = competitionClient.actYys(competionVO);
        List<TStore> lists = storeService.list(new LambdaQueryWrapper<TStore>().eq(TStore::getOperatorId, id));
        List<TStore> lists = listStatisticsStores(id, true);
        long l1=0;
        for (TStore tStore : lists) {
            l1 += getStoreOperatingHours(tStore, new Date());
@@ -1198,26 +1263,22 @@
    @RequestMapping(value = "/actStore")
    @ResponseBody
    public Object actStore(Integer id) {
        List<Integer> collect1 = Arrays.asList(id);
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
        //场地
        List<SiteBooking> list1 = siteBookingService.list(new QueryWrapper<SiteBooking>().in("storeId", collect1).eq("state", 1));
        List<Integer> collect = list1.stream().map(SiteBooking::getAppUserId).collect(Collectors.toList());
        list.addAll(collect);
        //赛事
        List<Integer> list2 = competitionClient.queryAppUserId(collect1);
        list.addAll(list2);
        //门票
        HashMap<String, Object> map = competitionClient.actPt(list);
        List<TStore> lists = storeService.list(new LambdaQueryWrapper<TStore>().eq(TStore::getOperatorId, 0));
        long l1=0;
        for (TStore tStore : lists) {
            l1 += getStoreOperatingHours(tStore, new Date());
        if (Integer.valueOf(3).equals(UserExt.getUser().getObjectType())) {
            id = UserExt.getUser().getObjectId();
        } else {
            id = resolveStoreId(id);
        }
        map.put("allHour",l1);
        TStore store = id == null ? null : storeService.getById(id);
        CompetionVO competionVO = new CompetionVO();
        if (store == null || !Integer.valueOf(1).equals(store.getState())) {
            competionVO.setStoreIds(Collections.singletonList(-1));
        } else {
            List<Integer> storeIds = Collections.singletonList(store.getId());
            competionVO.setStoreIds(storeIds);
        }
        HashMap<String, Object> map = competitionClient.actStore(competionVO);
        map.put("allHour", store == null || !Integer.valueOf(1).equals(store.getState())
                ? 0 : getStoreOperatingHours(store, new Date()));
        return map;
    }
@@ -1252,7 +1313,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 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        QueryDataFee queryDataFee = new QueryDataFee(time, collect1);
        // 课程收入
@@ -1298,7 +1359,7 @@
        if(collect.size()==0){
            collect.add(-1);
        }
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collect));
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collect).ne(TSite::getState, 3));
        for (SiteBooking siteBooking : list1) {
            for (TSite tSite : list3) {
                if(siteBooking.getSiteId().equals(tSite.getId())){
@@ -1308,7 +1369,7 @@
        }
        List<TSiteType> list2 = siteTypeService.list();
        List<TSiteType> list2 = siteTypeService.list(new LambdaQueryWrapper<TSiteType>().ne(TSiteType::getState, 3));
        List<Map<String,Object>> mapList = new ArrayList<>();
@@ -1364,6 +1425,10 @@
        //赛事
        List<Integer> list2 = competitionClient.queryAppUserId(collect1);
        list.addAll(list2);
        // 没有消费用户时,避免 MyBatis-Plus 生成 appUserId IN () 的非法 SQL。
        if (list.isEmpty()) {
            list.add(-1);
        }
        //门票
        QueryDataFee queryDataFee = new QueryDataFee(time, list);
@@ -1406,9 +1471,9 @@
        List<Integer> collects = list1.stream().map(SiteBooking::getSiteId).collect(Collectors.toList());
        if(collects.size()==0){
            collect.add(-1);
            collects.add(-1);
        }
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collects));
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collects).ne(TSite::getState, 3));
        for (SiteBooking siteBooking : list1) {
            for (TSite tSite : list3) {
                if(siteBooking.getSiteId().equals(tSite.getId())){
@@ -1418,7 +1483,7 @@
        }
        List<TSiteType> lists2 = siteTypeService.list();
        List<TSiteType> lists2 = siteTypeService.list(new LambdaQueryWrapper<TSiteType>().ne(TSiteType::getState, 3));
        List<Map<String,Object>> mapList = new ArrayList<>();
@@ -1549,7 +1614,7 @@
        if(collect.size()==0){
            collect.add(-1);
        }
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collect));
        List<TSite> list3 = siteService.list(new LambdaQueryWrapper<TSite>().in(TSite::getId, collect).ne(TSite::getState, 3));
        for (SiteBooking siteBooking : list1) {
            for (TSite tSite : list3) {
                if(siteBooking.getSiteId().equals(tSite.getId())){
@@ -1559,7 +1624,7 @@
        }
        List<TSiteType> list2 = siteTypeService.list();
        List<TSiteType> list2 = siteTypeService.list(new LambdaQueryWrapper<TSiteType>().ne(TSiteType::getState, 3));
        List<Map<String,Object>> mapList = new ArrayList<>();
@@ -1626,7 +1691,7 @@
    @ResponseBody
    public Object stuUserDataYys(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);
@@ -1665,7 +1730,7 @@
    @ResponseBody
    public Object selectYysJx(Integer id) {
        id = resolveOperatorId(id);
        List<TStore> list4 = storeService.list(new QueryWrapper<TStore>().eq("operatorId", id).eq("state", 1));
        List<TStore> list4 = listStatisticsStores(id, true);
        List<Integer> collect1 = list4.stream().map(TStore::getId).collect(Collectors.toList());
        //课程
        List<Integer> list = coursePackagePaymentClient.queryPaymentCoursePackage(collect1);