优化代码
This commit is contained in:
@@ -177,7 +177,7 @@ public class FormTableExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*统计数量*/
|
/*统计数量*/
|
||||||
public static long count(TableContext ctx,List<String> countField,List<FormWhereCondition> conditions) {
|
public static Long count(TableContext ctx,List<String> countField,List<FormWhereCondition> conditions) {
|
||||||
fillConditionFields(ctx, conditions);
|
fillConditionFields(ctx, conditions);
|
||||||
List<String> countColumn = resolveQueryColumns(ctx,countField);
|
List<String> countColumn = resolveQueryColumns(ctx,countField);
|
||||||
SqlBuildParam param = new SqlBuildParam();
|
SqlBuildParam param = new SqlBuildParam();
|
||||||
@@ -190,7 +190,12 @@ public class FormTableExecutor {
|
|||||||
JDBCAgent agent = new JDBCAgent();
|
JDBCAgent agent = new JDBCAgent();
|
||||||
try {
|
try {
|
||||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
agent.execute((String) sqlMap.get("sql"), (List<Object>) 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) {
|
} catch (Exception e) {
|
||||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||||
@@ -337,8 +342,8 @@ public class FormTableExecutor {
|
|||||||
String countField,
|
String countField,
|
||||||
String tableName,
|
String tableName,
|
||||||
List<FormWhereCondition> conditions) {
|
List<FormWhereCondition> conditions) {
|
||||||
|
countField = countField == null ? "*" : countField;
|
||||||
StringBuilder sql = new StringBuilder("SELECT count(*) as countnum FROM ")
|
StringBuilder sql = new StringBuilder("SELECT count(" + countField + ") as countnum FROM ")
|
||||||
.append(tableName);
|
.append(tableName);
|
||||||
|
|
||||||
List<Object> params = new ArrayList<>();
|
List<Object> params = new ArrayList<>();
|
||||||
@@ -354,31 +359,55 @@ public class FormTableExecutor {
|
|||||||
Integer limit,
|
Integer limit,
|
||||||
Integer offset,
|
Integer offset,
|
||||||
String orderField) {
|
String orderField) {
|
||||||
|
String dbName = (String) AppContext.getCache("DATABASE_NAME");
|
||||||
StringBuilder sql = new StringBuilder("SELECT ");
|
String orderBy = (orderField == null) ? "start_date" : orderField;
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
List<Object> params = new ArrayList<>();
|
List<Object> params = new ArrayList<>();
|
||||||
|
// ===== 1. 构建 SELECT 字段 =====
|
||||||
|
StringBuilder selectFields = new StringBuilder();
|
||||||
if (queryColumns == null || queryColumns.isEmpty()) {
|
if (queryColumns == null || queryColumns.isEmpty()) {
|
||||||
sql.append("*");
|
selectFields.append("*");
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < queryColumns.size(); i++) {
|
for (int i = 0; i < queryColumns.size(); i++) {
|
||||||
sql.append(queryColumns.get(i));
|
selectFields.append(queryColumns.get(i));
|
||||||
if (i < queryColumns.size() - 1) {
|
if (i < queryColumns.size() - 1) {
|
||||||
sql.append(",");
|
selectFields.append(",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sql.append(",ID");
|
selectFields.append(",ID");
|
||||||
}
|
}
|
||||||
|
// ===== 2. Oracle 分页特殊处理 =====
|
||||||
sql.append(" FROM ").append(tableName);
|
if ("oracle".equalsIgnoreCase(dbName) && limit != null && offset != null) {
|
||||||
sql.append(buildWhereClause(conditions, params));
|
sql.append("SELECT * FROM ( ");
|
||||||
if(orderField == null) {
|
sql.append(" SELECT t.*, ROW_NUMBER() OVER (ORDER BY ")
|
||||||
sql.append(" order by start_date desc");
|
.append(orderBy)
|
||||||
}else {
|
.append(" DESC) rn ");
|
||||||
sql.append(" order by "+ orderField +" desc");
|
sql.append(" FROM ")
|
||||||
}
|
.append(tableName)
|
||||||
if(limit != null && offset != null) {
|
.append(" t ");
|
||||||
sql.append(" LIMIT "+ offset + "," + limit);
|
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(";");
|
sql.append(";");
|
||||||
return buildResult(sql, params);
|
return buildResult(sql, params);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.seeyon.utils.http;
|
package com.seeyon.utils.http;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
@@ -46,72 +47,113 @@ public class HttpClient {
|
|||||||
* @param encode 文件内容的编码
|
* @param encode 文件内容的编码
|
||||||
* @return 下载成功返回 true,失败返回 false
|
* @return 下载成功返回 true,失败返回 false
|
||||||
*/
|
*/
|
||||||
public static boolean httpDownloadFile(String url, Map<String, String> headers, String savePath, String encode) {
|
public static String httpDownloadFile(String url, Map<String, String> headers, String savePath, String encode) {
|
||||||
if (encode == null) {
|
if (encode == null) {
|
||||||
encode = "utf-8"; // 默认字符编码
|
encode = "utf-8";
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseableHttpClient httpClient = null;
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
CloseableHttpResponse httpResponse = null;
|
|
||||||
InputStream inputStream = null;
|
|
||||||
OutputStream outputStream = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 创建 HttpClient 实例
|
|
||||||
httpClient = HttpClients.createDefault();
|
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
|
||||||
// 设置请求头
|
// 设置请求头
|
||||||
if (headers != null && headers.size() > 0) {
|
if (headers != null && !headers.isEmpty()) {
|
||||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
httpGet.setHeader(entry.getKey(), entry.getValue());
|
httpGet.setHeader(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行请求
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||||
httpResponse = httpClient.execute(httpGet);
|
|
||||||
HttpEntity entity = httpResponse.getEntity();
|
|
||||||
|
|
||||||
// 检查响应状态码
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
if (statusCode != 200) {
|
||||||
inputStream = entity.getContent();
|
System.out.println("Download failed, HTTP error code: " + statusCode);
|
||||||
|
return null;
|
||||||
// 创建输出流,将文件保存到本地
|
|
||||||
outputStream = new FileOutputStream(savePath);
|
|
||||||
|
|
||||||
// 设置缓冲区
|
|
||||||
byte[] buffer = new byte[4096];
|
|
||||||
int bytesRead;
|
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
||||||
outputStream.write(buffer, 0, bytesRead);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 文件下载成功
|
HttpEntity entity = response.getEntity();
|
||||||
return true;
|
|
||||||
} else {
|
// =========================
|
||||||
System.out.println("Download failed, HTTP error code: " + httpResponse.getStatusLine().getStatusCode());
|
// 1️⃣ 获取 Content-Type
|
||||||
return false;
|
// =========================
|
||||||
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return null;
|
||||||
} 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user