diff --git a/src/main/java/com/seeyon/utils/form/FormTableExecutor.java b/src/main/java/com/seeyon/utils/form/FormTableExecutor.java index 3f7acc4..b6d07af 100644 --- a/src/main/java/com/seeyon/utils/form/FormTableExecutor.java +++ b/src/main/java/com/seeyon/utils/form/FormTableExecutor.java @@ -177,7 +177,7 @@ public class FormTableExecutor { } /*统计数量*/ - public static long count(TableContext ctx,List countField,List conditions) { + public static Long count(TableContext ctx,List countField,List conditions) { fillConditionFields(ctx, conditions); List countColumn = resolveQueryColumns(ctx,countField); SqlBuildParam param = new SqlBuildParam(); @@ -190,7 +190,12 @@ public class FormTableExecutor { JDBCAgent agent = new JDBCAgent(); try { agent.execute((String) sqlMap.get("sql"), (List) sqlMap.get("params")); - return (Long) agent.resultSetToMap().get("countnum"); + String dbName = (String) AppContext.getCache("DATABASE_NAME"); + if ("mysql".equalsIgnoreCase(dbName)) { + return (Long) agent.resultSetToMap().get("countnum"); + } else { + return Long.parseLong(agent.resultSetToMap().get("countnum") + ""); + } } catch (Exception e) { log.error("执行sql为: " + sqlMap.get("sql")); log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params"))); @@ -337,8 +342,8 @@ public class FormTableExecutor { String countField, String tableName, List conditions) { - - StringBuilder sql = new StringBuilder("SELECT count(*) as countnum FROM ") + countField = countField == null ? "*" : countField; + StringBuilder sql = new StringBuilder("SELECT count(" + countField + ") as countnum FROM ") .append(tableName); List params = new ArrayList<>(); @@ -354,31 +359,55 @@ public class FormTableExecutor { Integer limit, Integer offset, String orderField) { - - StringBuilder sql = new StringBuilder("SELECT "); + String dbName = (String) AppContext.getCache("DATABASE_NAME"); + String orderBy = (orderField == null) ? "start_date" : orderField; + StringBuilder sql = new StringBuilder(); List params = new ArrayList<>(); - + // ===== 1. 构建 SELECT 字段 ===== + StringBuilder selectFields = new StringBuilder(); if (queryColumns == null || queryColumns.isEmpty()) { - sql.append("*"); + selectFields.append("*"); } else { for (int i = 0; i < queryColumns.size(); i++) { - sql.append(queryColumns.get(i)); + selectFields.append(queryColumns.get(i)); if (i < queryColumns.size() - 1) { - sql.append(","); + selectFields.append(","); } } - sql.append(",ID"); + selectFields.append(",ID"); } - - sql.append(" FROM ").append(tableName); - sql.append(buildWhereClause(conditions, params)); - if(orderField == null) { - sql.append(" order by start_date desc"); - }else { - sql.append(" order by "+ orderField +" desc"); - } - if(limit != null && offset != null) { - sql.append(" LIMIT "+ offset + "," + limit); + // ===== 2. Oracle 分页特殊处理 ===== + if ("oracle".equalsIgnoreCase(dbName) && limit != null && offset != null) { + sql.append("SELECT * FROM ( "); + sql.append(" SELECT t.*, ROW_NUMBER() OVER (ORDER BY ") + .append(orderBy) + .append(" DESC) rn "); + sql.append(" FROM ") + .append(tableName) + .append(" t "); + sql.append(buildWhereClause(conditions, params)); + sql.append(" ) WHERE rn > ") + .append(offset) + .append(" AND rn <= ") + .append(offset + limit); + } else { + // ===== 3. MySQL / SQLServer 通用逻辑 ===== + sql.append("SELECT ").append(selectFields); + sql.append(" FROM ").append(tableName); + sql.append(buildWhereClause(conditions, params)); + sql.append(" ORDER BY ").append(orderBy).append(" DESC"); + if (limit != null && offset != null) { + if ("mysql".equalsIgnoreCase(dbName)) { + sql.append(" LIMIT ").append(offset).append(",").append(limit); + } + if ("sqlserver".equalsIgnoreCase(dbName)) { + sql.append(" OFFSET ") + .append(offset) + .append(" ROWS FETCH NEXT ") + .append(limit) + .append(" ROWS ONLY"); + } + } } sql.append(";"); return buildResult(sql, params); diff --git a/src/main/java/com/seeyon/utils/http/HttpClient.java b/src/main/java/com/seeyon/utils/http/HttpClient.java index e0bb645..efb86d1 100644 --- a/src/main/java/com/seeyon/utils/http/HttpClient.java +++ b/src/main/java/com/seeyon/utils/http/HttpClient.java @@ -1,5 +1,6 @@ package com.seeyon.utils.http; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -46,72 +47,113 @@ public class HttpClient { * @param encode 文件内容的编码 * @return 下载成功返回 true,失败返回 false */ - public static boolean httpDownloadFile(String url, Map headers, String savePath, String encode) { + public static String httpDownloadFile(String url, Map headers, String savePath, String encode) { if (encode == null) { - encode = "utf-8"; // 默认字符编码 + encode = "utf-8"; } - CloseableHttpClient httpClient = null; - CloseableHttpResponse httpResponse = null; - InputStream inputStream = null; - OutputStream outputStream = null; + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - try { - // 创建 HttpClient 实例 - httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 设置请求头 - if (headers != null && headers.size() > 0) { + if (headers != null && !headers.isEmpty()) { for (Map.Entry entry : headers.entrySet()) { httpGet.setHeader(entry.getKey(), entry.getValue()); } } - // 执行请求 - httpResponse = httpClient.execute(httpGet); - HttpEntity entity = httpResponse.getEntity(); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { - // 检查响应状态码 - if (httpResponse.getStatusLine().getStatusCode() == 200) { - inputStream = entity.getContent(); - - // 创建输出流,将文件保存到本地 - outputStream = new FileOutputStream(savePath); - - // 设置缓冲区 - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) { + System.out.println("Download failed, HTTP error code: " + statusCode); + return null; } - // 文件下载成功 - return true; - } else { - System.out.println("Download failed, HTTP error code: " + httpResponse.getStatusLine().getStatusCode()); - return false; + HttpEntity entity = response.getEntity(); + + // ========================= + // 1️⃣ 获取 Content-Type + // ========================= + String contentType = null; + if (entity.getContentType() != null) { + contentType = entity.getContentType().getValue(); + } + + // ========================= + // 2️⃣ 获取文件名(优先) + // ========================= + String fileName = null; + Header disposition = response.getFirstHeader("Content-Disposition"); + if (disposition != null) { + String value = disposition.getValue(); + + try { + if (value.contains("filename*=")) { + String encoded = value.substring(value.indexOf("filename*=") + 10); + encoded = encoded.replace("UTF-8''", "").replace("\"", ""); + fileName = java.net.URLDecoder.decode(encoded, "UTF-8"); + } else if (value.contains("filename=")) { + String encoded = value.substring(value.indexOf("filename=") + 9) + .replace("\"", ""); + fileName = java.net.URLDecoder.decode(encoded, "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + // ========================= + // 4️⃣ 根据 Content-Type 补后缀 + // ========================= + if (fileName != null && !fileName.contains(".") && contentType != null) { + if (contentType.contains("png")) { + fileName += ".png"; + } else if (contentType.contains("jpeg")) { + fileName += ".jpg"; + } else if (contentType.contains("pdf")) { + fileName += ".pdf"; + } else if (contentType.contains("mp4")) { + fileName += ".mp4"; + } else if (contentType.contains("zip")) { + fileName += ".zip"; + } else { + fileName += ".dat"; // 默认兜底 + } + } + + // ========================= + // 5️⃣ 生成最终路径 + // ========================= + String finalPath; + if (savePath.endsWith("/") || savePath.endsWith("\\")) { + finalPath = savePath + fileName; + } else { + finalPath = savePath; + } + + + // ========================= + // 6️⃣ 下载文件 + // ========================= + try (InputStream inputStream = entity.getContent(); + OutputStream outputStream = new FileOutputStream(finalPath)) { + + byte[] buffer = new byte[8192]; + int len; + + while ((len = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, len); + } + } + + return fileName; } + } catch (Exception e) { e.printStackTrace(); - return false; - } finally { - try { - if (inputStream != null) { - inputStream.close(); - } - if (outputStream != null) { - outputStream.close(); - } - if (httpResponse != null) { - httpResponse.close(); - } - if (httpClient != null) { - httpClient.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } + return null; } }