| | |
| | | pageInfo.setRecords(list); |
| | | return R.ok(pageInfo); |
| | | } |
| | | @Override |
| | | @Override |
| | | public R<PageInfo<EnterpriseChargingAnalysisVO>> enterpriseChargingAnalysis(EnterpriseChargingAnalysisQuery query) { |
| | | PageInfo<EnterpriseChargingAnalysisVO> pageInfo = new PageInfo<>(query.getPageCurr(), query.getPageSize()); |
| | | List<EnterpriseChargingAnalysisVO> list = this.baseMapper.enterpriseChargingAnalysis(pageInfo, query); |
| | | if (list == null || list.isEmpty()) { |
| | | pageInfo.setRecords(Collections.emptyList()); |
| | | return R.ok(pageInfo); |
| | | } |
| | | List<String> companyNames = list.stream() |
| | | .map(EnterpriseChargingAnalysisVO::getCompanyName) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | Map<String, List<Map<String, Object>>> topSitesByCompany = groupByCompany( |
| | | this.baseMapper.enterpriseTopSitesByCompanies(query, companyNames)); |
| | | Map<String, List<Map<String, Object>>> chargingTimesByCompany = groupByCompany( |
| | | this.baseMapper.enterpriseChargingTimesByCompanies(query, companyNames)); |
| | | for (EnterpriseChargingAnalysisVO row : list) { |
| | | EnterpriseChargingAnalysisQuery rowQuery = buildEnterpriseRowQuery(query, row); |
| | | List<Map<String, Object>> topSites = this.baseMapper.enterpriseTopSites(rowQuery); |
| | | String companyKey = companyKey(row.getCompanyName(), row.getSocialCreditCode()); |
| | | List<Map<String, Object>> topSites = topSitesByCompany.getOrDefault(companyKey, Collections.emptyList()); |
| | | String topSiteNames = topSites.stream() |
| | | .sorted(Comparator.comparing((Map<String, Object> item) -> ((Number) item.getOrDefault("counts", 0)).longValue()).reversed()) |
| | | .limit(3) |
| | | .map(item -> Objects.toString(item.get("siteName"), "")) |
| | | .filter(org.springframework.util.StringUtils::hasText) |
| | | .collect(Collectors.joining("、")); |
| | | row.setTopSiteNames(topSiteNames); |
| | | row.setAverageChargingInterval(calculateAverageChargingInterval(this.baseMapper.enterpriseChargingTimes(rowQuery))); |
| | | List<LocalDateTime> chargingTimes = chargingTimesByCompany.getOrDefault(companyKey, Collections.emptyList()).stream() |
| | | .map(item -> toLocalDateTime(item.get("startTime"))) |
| | | .filter(Objects::nonNull) |
| | | .sorted() |
| | | .collect(Collectors.toList()); |
| | | row.setAverageChargingInterval(calculateAverageChargingInterval(chargingTimes)); |
| | | } |
| | | pageInfo.setRecords(list); |
| | | return R.ok(pageInfo); |
| | | } |
| | | |
| | | private Map<String, List<Map<String, Object>>> groupByCompany(List<Map<String, Object>> rows) { |
| | | if (rows == null || rows.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return rows.stream().collect(Collectors.groupingBy(row -> companyKey( |
| | | Objects.toString(row.get("companyName"), null), |
| | | Objects.toString(row.get("socialCreditCode"), "")))); |
| | | } |
| | | |
| | | private String companyKey(String companyName, String socialCreditCode) { |
| | | return Objects.toString(companyName, "") + "\u0000" + Objects.toString(socialCreditCode, ""); |
| | | } |
| | | |
| | | private LocalDateTime toLocalDateTime(Object value) { |
| | | if (value instanceof LocalDateTime) { |
| | | return (LocalDateTime) value; |
| | | } |
| | | if (value instanceof java.sql.Timestamp) { |
| | | return ((java.sql.Timestamp) value).toLocalDateTime(); |
| | | } |
| | | if (value instanceof Date) { |
| | | return LocalDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault()); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public R<PageInfo<EnterpriseChargingDetailVO>> enterpriseChargingDetail(EnterpriseChargingAnalysisQuery query) { |
| | | PageInfo<EnterpriseChargingDetailVO> pageInfo = new PageInfo<>(query.getPageCurr(), query.getPageSize()); |