1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
| @Service @Slf4j public class ExcelDataProcessor { @Autowired private DataSourceManager dataSourceManager; @Autowired private DataConverterService converterService; @Autowired private DataFilterService filterService;
public ExcelDataContext processData(ExcelExportRequest request) { try { DataSource dataSource = dataSourceManager.getDataSource(request.getDataSource()); if (dataSource == null) { throw new IllegalArgumentException("数据源不存在: " + request.getDataSource()); } QueryCondition condition = buildQueryCondition(request); List<Map<String, Object>> rawData = executeDataQuery(dataSource, condition); List<Map<String, Object>> convertedData = converterService.convertData(rawData, request.getColumns()); List<Map<String, Object>> filteredData = filterService.filterData(convertedData, request.getParameters()); ExcelDataContext context = new ExcelDataContext(); context.setDataSource(dataSource); context.setRawData(rawData); context.setConvertedData(convertedData); context.setFilteredData(filteredData); context.setTotalCount(filteredData.size()); context.setQueryCondition(condition); log.info("数据处理完成: dataSource={}, totalCount={}", request.getDataSource(), context.getTotalCount()); return context; } catch (Exception e) { log.error("数据处理失败: dataSource={}", request.getDataSource(), e); throw new RuntimeException("数据处理失败", e); } }
public List<Map<String, Object>> getPageData(ExcelDataContext context, int page, int pageSize) { try { List<Map<String, Object>> allData = context.getFilteredData(); int startIndex = page * pageSize; int endIndex = Math.min(startIndex + pageSize, allData.size()); if (startIndex >= allData.size()) { return new ArrayList<>(); } return allData.subList(startIndex, endIndex); } catch (Exception e) { log.error("获取分页数据失败: page={}, pageSize={}", page, pageSize, e); return new ArrayList<>(); } }
private QueryCondition buildQueryCondition(ExcelExportRequest request) { QueryCondition condition = new QueryCondition(); if (request.getParameters() != null) { condition.setFilters(request.getParameters()); } if (request.getSortFields() != null) { condition.setSortFields(request.getSortFields()); } if (request.getPageSize() != null) { condition.setPageSize(request.getPageSize()); } return condition; }
private List<Map<String, Object>> executeDataQuery(DataSource dataSource, QueryCondition condition) { try { if (dataSource instanceof DatabaseDataSource) { return executeDatabaseQuery((DatabaseDataSource) dataSource, condition); } else if (dataSource instanceof ApiDataSource) { return executeApiQuery((ApiDataSource) dataSource, condition); } else if (dataSource instanceof CacheDataSource) { return executeCacheQuery((CacheDataSource) dataSource, condition); } else { throw new IllegalArgumentException("不支持的数据源类型: " + dataSource.getClass().getSimpleName()); } } catch (Exception e) { log.error("执行数据查询失败: dataSource={}", dataSource.getName(), e); throw new RuntimeException("执行数据查询失败", e); } }
private List<Map<String, Object>> executeDatabaseQuery(DatabaseDataSource dataSource, QueryCondition condition) { List<Map<String, Object>> results = new ArrayList<>(); try (Connection connection = dataSource.getConnection()) { String sql = buildSqlQuery(dataSource, condition); try (PreparedStatement statement = connection.prepareStatement(sql)) { setQueryParameters(statement, condition); try (ResultSet resultSet = statement.executeQuery()) { ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); while (resultSet.next()) { Map<String, Object> row = new HashMap<>(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnName(i); Object value = resultSet.getObject(i); row.put(columnName, value); } results.add(row); } } } } catch (SQLException e) { log.error("数据库查询失败: dataSource={}", dataSource.getName(), e); throw new RuntimeException("数据库查询失败", e); } return results; }
private List<Map<String, Object>> executeApiQuery(ApiDataSource dataSource, QueryCondition condition) { try { String url = buildApiUrl(dataSource, condition); Map<String, String> headers = buildApiHeaders(dataSource); Map<String, Object> parameters = buildApiParameters(condition); String response = sendHttpRequest(url, headers, parameters); return parseApiResponse(response); } catch (Exception e) { log.error("API查询失败: dataSource={}", dataSource.getName(), e); throw new RuntimeException("API查询失败", e); } }
private List<Map<String, Object>> executeCacheQuery(CacheDataSource dataSource, QueryCondition condition) { try { String cacheKey = buildCacheKey(dataSource, condition); Object cachedData = dataSource.get(cacheKey); if (cachedData instanceof List) { return (List<Map<String, Object>>) cachedData; } else { return new ArrayList<>(); } } catch (Exception e) { log.error("缓存查询失败: dataSource={}", dataSource.getName(), e); throw new RuntimeException("缓存查询失败", e); } }
private String buildSqlQuery(DatabaseDataSource dataSource, QueryCondition condition) { StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM ").append(dataSource.getTableName()); if (condition.getFilters() != null && !condition.getFilters().isEmpty()) { sql.append(" WHERE "); List<String> whereConditions = new ArrayList<>(); for (Map.Entry<String, Object> entry : condition.getFilters().entrySet()) { whereConditions.add(entry.getKey() + " = ?"); } sql.append(String.join(" AND ", whereConditions)); } if (condition.getSortFields() != null && !condition.getSortFields().isEmpty()) { sql.append(" ORDER BY "); List<String> sortConditions = new ArrayList<>(); for (SortField sortField : condition.getSortFields()) { sortConditions.add(sortField.getFieldName() + " " + sortField.getDirection()); } sql.append(String.join(", ", sortConditions)); } if (condition.getPageSize() != null) { sql.append(" LIMIT ").append(condition.getPageSize()); } return sql.toString(); }
private void setQueryParameters(PreparedStatement statement, QueryCondition condition) throws SQLException { if (condition.getFilters() != null) { int index = 1; for (Object value : condition.getFilters().values()) { statement.setObject(index++, value); } } }
private String buildApiUrl(ApiDataSource dataSource, QueryCondition condition) { return dataSource.getBaseUrl() + "/query"; }
private Map<String, String> buildApiHeaders(ApiDataSource dataSource) { Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); headers.put("Authorization", "Bearer " + dataSource.getApiKey()); return headers; }
private Map<String, Object> buildApiParameters(QueryCondition condition) { Map<String, Object> parameters = new HashMap<>(); if (condition.getFilters() != null) { parameters.putAll(condition.getFilters()); } return parameters; }
private String sendHttpRequest(String url, Map<String, String> headers, Map<String, Object> parameters) { return "{}"; }
private List<Map<String, Object>> parseApiResponse(String response) { return new ArrayList<>(); }
private String buildCacheKey(CacheDataSource dataSource, QueryCondition condition) { StringBuilder keyBuilder = new StringBuilder(); keyBuilder.append(dataSource.getName()).append(":"); keyBuilder.append(condition.hashCode()); return keyBuilder.toString(); } }
public class ExcelDataContext { private DataSource dataSource; private List<Map<String, Object>> rawData; private List<Map<String, Object>> convertedData; private List<Map<String, Object>> filteredData; private long totalCount; private QueryCondition queryCondition; }
public class QueryCondition { private Map<String, Object> filters; private List<SortField> sortFields; private Integer pageSize; private Integer pageNumber; }
public class SortField { private String fieldName; private SortDirection direction; public enum SortDirection { ASC, DESC } }
|