Commit 1c9f891b authored by 陈祥烨's avatar 陈祥烨

应用导出优化

parent 59b80ec9
...@@ -46,6 +46,7 @@ DataApiResource { ...@@ -46,6 +46,7 @@ DataApiResource {
/** /**
* 依据参数导出 应用快搭增量脚本 * 依据参数导出 应用快搭增量脚本
*
* @throws SQLException * @throws SQLException
*/ */
@TycloudOperation(ApiLevel = UserType.ANONYMOUS, needAuth = false) @TycloudOperation(ApiLevel = UserType.ANONYMOUS, needAuth = false)
...@@ -54,17 +55,19 @@ DataApiResource { ...@@ -54,17 +55,19 @@ DataApiResource {
public void generateSQL( public void generateSQL(
@RequestParam("resourceCode") String resourceCode, @RequestParam("resourceCode") String resourceCode,
@RequestParam("dimension") String dimension, @RequestParam("dimension") String dimension,
@RequestParam("ipSeq") Long ipSeq,
HttpServletRequest request, HttpServletRequest request,
HttpServletResponse response) throws SQLException { HttpServletResponse response) throws SQLException {
Map<String, Object> variables = new HashMap<>(); Map<String, Object> variables = new HashMap<>();
Enumeration<String> keys = request.getParameterNames(); Enumeration<String> keys = request.getParameterNames();
while(keys.hasMoreElements()){ while (keys.hasMoreElements()) {
String name = (String)keys.nextElement(); String name = (String) keys.nextElement();
String value = request.getParameter(name); String value = request.getParameter(name);
variables.put(name, value); variables.put(name, value);
} }
variables.remove("resourceCode"); variables.remove("resourceCode");
variables.remove("dimension"); variables.remove("dimension");
studioResourceService.generateSQL(resourceCode, dimension, variables, response); variables.remove("ipSeq");
studioResourceService.generateSQL(resourceCode, dimension, ipSeq, variables, response);
} }
} }
...@@ -2,13 +2,14 @@ package com.yeejoin.amos.api.tool.face.service; ...@@ -2,13 +2,14 @@ package com.yeejoin.amos.api.tool.face.service;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.api.tool.enums.SourceEnum; import com.yeejoin.amos.api.tool.enums.SourceEnum;
import com.yeejoin.amos.api.tool.face.model.DataBaseLinkModel;
import com.yeejoin.amos.api.tool.face.orm.entity.StudioResource; import com.yeejoin.amos.api.tool.face.orm.entity.StudioResource;
import com.yeejoin.amos.api.tool.face.model.StudioResourceModel; import com.yeejoin.amos.api.tool.face.model.StudioResourceModel;
import com.yeejoin.amos.api.tool.face.orm.dao.StudioResourceMapper; import com.yeejoin.amos.api.tool.face.orm.dao.StudioResourceMapper;
import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn; import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn;
import com.yeejoin.amos.api.tool.utils.DataBaseUtils;
import com.yeejoin.amos.api.tool.utils.DateUtils; import com.yeejoin.amos.api.tool.utils.DateUtils;
import com.yeejoin.amos.api.tool.utils.SqlHelper; import com.yeejoin.amos.api.tool.utils.SqlHelper;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
...@@ -22,10 +23,11 @@ import org.typroject.tyboot.component.emq.EmqKeeper; ...@@ -22,10 +23,11 @@ import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil; import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService; import org.typroject.tyboot.core.rdbms.service.BaseService;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.*; import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -41,19 +43,19 @@ import java.util.Map; ...@@ -41,19 +43,19 @@ import java.util.Map;
@Component @Component
public class StudioResourceService extends BaseService<StudioResourceModel, StudioResource, StudioResourceMapper> { public class StudioResourceService extends BaseService<StudioResourceModel, StudioResource, StudioResourceMapper> {
/** /**
* 数据库备份路径 * 数据库备份路径
*/ */
public static final String BACKUP_PATH = "/db/"; public static final String BACKUP_PATH = "/db/";
/** /**
* 数据库备份文本前缀 * 数据库备份文本前缀
*/ */
public static String ONESQL_PREFIX = ""; public static String ONESQL_PREFIX = "";
private static String SUFFIX = "sql"; private static String SUFFIX = "sql";
private static String BR = "\r\n"; private static String BR = "\r\n";
private static String SLASH = "/"; private static String SLASH = "/";
private static String BRANCH = ";"; private static String BRANCH = ";";
private static String SPLIT = "`"; private static String SPLIT = "`";
private static String SPACE = " "; private static String SPACE = " ";
private static String INSERT_INTO = " INSERT INTO "; private static String INSERT_INTO = " INSERT INTO ";
...@@ -65,17 +67,20 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -65,17 +67,20 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
private static String COMMA = ","; private static String COMMA = ",";
private static String DISABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 0;\r\n"; private static String DISABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 0;\r\n";
private static String ABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 1;\r\n"; private static String ABLEFOREIGN = "SET FOREIGN_KEY_CHECKS = 1;\r\n";
private static String DELIMITER = "###################################"; private static String DELIMITER = "###################################";
@Autowired @Autowired
JdbcTemplate jdbcTemplate; JdbcTemplate jdbcTemplate;
@Autowired @Autowired
DataSource datasource; DataSource datasource;
@Autowired
private DataBaseLinkService dataBaseLinkService;
@Autowired @Autowired
private SqlHelper sqlHelper; private SqlHelper sqlHelper;
@Autowired @Autowired
EmqKeeper emqKeeper; EmqKeeper emqKeeper;
/** /**
* 列表查询 示例 * 列表查询 示例
...@@ -84,215 +89,181 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -84,215 +89,181 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
return this.queryForList("", false, resourceCode); return this.queryForList("", false, resourceCode);
} }
public void generateSQL(String resourceCode, String dimension,Map<String, Object> variables, HttpServletResponse response) { public void generateSQL(String resourceCode, String dimension,Long ipSeq, Map<String, Object> variables, HttpServletResponse response) {
try { try {
// if (resourceCode.equals("StudioApplication")){ // if (resourceCode.equals("StudioApplication")){
// ApplicationModel model = applicationService.queryBySeq(Long.valueOf(variables.get(dimension).toString())); // ApplicationModel model = applicationService.queryBySeq(Long.valueOf(variables.get(dimension).toString()));
// variables.put("agencyCode", model.getAgencyCode()); // variables.put("agencyCode", model.getAgencyCode());
// } // }
//返回需要查询的数据,dimension ----> seq List<StudioResourceModel> resourceList = queryForStudioResourceList(resourceCode);
List<StudioResourceModel> resourceList = queryForStudioResourceList(resourceCode); File directory = new File("");// 参数为空
File directory = new File("");// 参数为空 String coursePath = directory.getCanonicalPath();
String coursePath = directory.getCanonicalPath(); File parentFile = new File(coursePath).getParentFile();
File parentFile = new File(coursePath).getParentFile(); String backPath = parentFile.getCanonicalPath() + BACKUP_PATH;
String backPath = parentFile.getCanonicalPath() + BACKUP_PATH; File sqlDirectory = new File(backPath);
File sqlDirectory = new File(backPath); if (!sqlDirectory.exists()) {
if (!sqlDirectory.exists()) { sqlDirectory.mkdir();
sqlDirectory.mkdir(); }
}
print(variables.get(dimension).toString(), "开始执行", "start");
print(variables.get(dimension).toString(),"开始执行","start");
// 备份文件路径名称
// 备份文件路径名称 String fileName = (SourceEnum.IDX.getSource().equals(resourceCode.toLowerCase()) ? (resourceCode.toLowerCase() + "_") : "studio_") + DateFormatUtils.format(new Date(), "yyyyMMdd") + "." + SUFFIX;
String fileName = (SourceEnum.IDX.getSource().equals(resourceCode.toLowerCase()) ? (resourceCode.toLowerCase() + "_") : "studio_") + DateFormatUtils.format(new Date(), "yyyyMMdd") + "." + SUFFIX; String sqlFilePath = backPath + SLASH + fileName;
String sqlFilePath = backPath + SLASH + fileName; File file = new File(sqlFilePath);
File file = new File(sqlFilePath);
FileOutputStream out;
FileOutputStream out; OutputStreamWriter writer = null;
OutputStreamWriter writer = null;
out = new FileOutputStream(file);
out = new FileOutputStream(file); writer = new OutputStreamWriter(out, "utf8");
writer = new OutputStreamWriter(out, "utf8");
StringBuffer deleteSql = new StringBuffer();
StringBuffer deleteSql = new StringBuffer(); StringBuffer insertSql = new StringBuffer();
StringBuffer insertSql = new StringBuffer();
for (StudioResourceModel model : resourceList) {
for (StudioResourceModel model : resourceList) { if (!validationParameters(model, variables)) {
//检查是否有appSeq continue;
if (!validationParameters(model, variables)) { }
continue; if (!ObjectUtils.isEmpty(model.getResourceDeleteSql())) {
} String json = format(model.getResourceDeleteSql(), variables);
if (!ObjectUtils.isEmpty(model.getResourceDeleteSql())) { JSONObject dimensionData = JSONObject.parseObject(json);
String json = format(model.getResourceDeleteSql(), variables); String deleteAllSql = dimensionData.getString(dimension);
JSONObject dimensionData = JSONObject.parseObject(json); deleteAllSql.replaceAll(";", ";" + BR);
String deleteAllSql = dimensionData.getString(dimension); deleteSql.append(deleteAllSql).append(BRANCH).append(BR);
deleteAllSql.replaceAll(";", ";" + BR); }
deleteSql.append(deleteAllSql).append(BRANCH).append(BR); if (!ObjectUtils.isEmpty(model.getResourceInsertSql())) {
} String sql = buildInsertSql(model, dimension,ipSeq, variables);
if (!ObjectUtils.isEmpty(model.getResourceInsertSql())) { if (!ObjectUtils.isEmpty(sql)) insertSql.append(sql);
String sql = buildInsertSql(model,dimension, variables); }
if (!ObjectUtils.isEmpty(sql)) insertSql.append(sql); }
}
} if (deleteSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建delete语句...", "running");
if (deleteSql.length() > 0) { writer.write(BR + DELIMITER + BR);
print(variables.get(dimension).toString(),"开始创建delete语句...","running"); writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR); writer.write(deleteSql.toString());
writer.write(BR + DELIMITER + BR); writer.write(BR + BR + DELIMITER + BR);
writer.write(deleteSql.toString()); print(variables.get(dimension).toString(), "delete语句创建完成...", "running");
writer.write(BR + BR + DELIMITER + BR); }
print(variables.get(dimension).toString(),"delete语句创建完成...","running");
} if (insertSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建insert语句...", "running");
if (insertSql.length() > 0) { writer.write(BR + DELIMITER + BR);
print(variables.get(dimension).toString(),"开始创建insert语句...","running"); writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR); writer.write(insertSql.toString());
writer.write(BR + DELIMITER + BR); writer.write(BR + BR + DELIMITER + BR);
writer.write(insertSql.toString()); print(variables.get(dimension).toString(), "insert语句创建完成...", "running");
writer.write(BR + BR + DELIMITER + BR); }
print(variables.get(dimension).toString(),"insert语句创建完成...","running");
} writer.flush();
writer.close();
writer.flush(); out.close();
writer.close();
out.close(); if (deleteSql.length() == 0 && insertSql.length() == 0) {
print(variables.get(dimension).toString(), "该项目下数据路径字段为空,不支持导出...", "start");
if(deleteSql.length() == 0 && insertSql.length() == 0){ return;
print(variables.get(dimension).toString(),"该项目下数据路径字段为空,不支持导出...","start"); }
return; download(response, fileName, sqlFilePath);
} print(variables.get(dimension).toString(), "执行结束", "end");
download(response, fileName, sqlFilePath); } catch (IOException e) {
print(variables.get(dimension).toString(),"执行结束","end"); e.printStackTrace();
} catch (IOException e) { } catch (SQLException e) {
e.printStackTrace(); throw new RuntimeException(e);
} }
} }
private boolean validationParameters(StudioResourceModel model, Map<String, Object> variables) { private boolean validationParameters(StudioResourceModel model, Map<String, Object> variables) {
String text = model.getResourceParams(); String text = model.getResourceParams();
JSONArray fields = JSONObject.parseArray(text); JSONArray fields = JSONObject.parseArray(text);
for (int i = 0; i < fields.size(); i++) { for (int i = 0; i < fields.size(); i++) {
if (!variables.containsKey(fields.getJSONObject(i).getString("name"))) { if (!variables.containsKey(fields.getJSONObject(i).getString("name"))) {
return false; return false;
} }
} }
return true; return true;
} }
private String buildInsertSql(StudioResourceModel resource, String dimension, Map<String, Object> variables) { private String buildInsertSql(StudioResourceModel resource, String dimension, Long ipSeq, Map<String, Object> variables) throws SQLException {
StringBuffer sbsql = new StringBuffer(); StringBuffer sbsql = new StringBuffer();
String json = format(resource.getResourceInsertSql(), variables); String json = format(resource.getResourceInsertSql(), variables);
JSONObject dimensionData = JSONObject.parseObject(json); JSONObject dimensionData = JSONObject.parseObject(json);
JSONObject selectAllSql = dimensionData.getJSONObject(dimension); JSONObject selectAllSql = dimensionData.getJSONObject(dimension);
if (ObjectUtils.isEmpty(selectAllSql.keySet())) { DataBaseLinkModel dataBaseLinkModel = dataBaseLinkService.queryBySeq(ipSeq);
return ""; for (String tableName : selectAllSql.keySet()) {
} List<TableColumn> columns = null;
for (String tableName : selectAllSql.keySet()) { List<Map<String, Object>> dataList = null;
List<TableColumn> columns = null; String selectsql = format(selectAllSql.getString(tableName), variables);
List<Map<String, Object>> dataList = null; if (SourceEnum.IDX.getSource().equals(resource.getResourceCode().toLowerCase())) {
String selectsql = format(selectAllSql.getString(tableName), variables); // String sql = sqlHelper.getNamespaceSql("com.yeejoin.amos.api.studio.face.orm.dao.StudioResourceMapper.queryForListByTableName", tableName);
if (SourceEnum.IDX.getSource().equals(resource.getResourceCode().toLowerCase())) { // Query query = new Query();
String sql = sqlHelper.getNamespaceSql("com.yeejoin.amos.api.studio.face.orm.dao.StudioResourceMapper.queryForListByTableName", tableName); // query.setSql(sql);
Query query = new Query(); // List result = IndicatorsManager.indicatorClient.queryBySql(query).getResult();
query.setSql(sql); // List<TableColumn> finalColumns = new ArrayList<>();
List result = IndicatorsManager.indicatorClient.queryBySql(query).getResult(); // result.stream().forEach(x -> {
List<TableColumn> finalColumns = new ArrayList<>(); // TableColumn column = JsonUtils.jsonToBean(JSON.toJSONString(x), TableColumn.class);
result.stream().forEach(x -> { // finalColumns.add(column);
TableColumn column = JsonUtils.jsonToBean(JSON.toJSONString(x), TableColumn.class); // });
finalColumns.add(column); // columns = finalColumns;
}); //
columns = finalColumns; // query.setSql(selectsql);
// dataList = IndicatorsManager.indicatorClient.queryBySql(query).getResult();
query.setSql(selectsql); } else {
dataList = IndicatorsManager.indicatorClient.queryBySql(query).getResult(); if (dimension.equals("appSeq")) {
} else { dataBaseLinkModel.setDbName("amos_studio");
columns = this.getBaseMapper().queryForListByTableName(tableName); } else if (dimension.equals("projectSeq")) {
dataList = jdbcTemplate.queryForList(selectsql.toString()); dataBaseLinkModel.setDbName("amos_idx");
} }
Connection connection = dataBaseLinkService.connectNewDatabase(dataBaseLinkModel);
for (Map<String, Object> map : dataList) { if (ObjectUtils.isEmpty(selectAllSql.keySet())) {
sbsql.append(INSERT_INTO); return "";
}
sbsql.append(SPACE).append(tableName).append(SPACE); columns = DataBaseUtils.getTableColumn(tableName,connection);
dataList = DataBaseUtils.getMaps(selectsql,connection);
sbsql.append(LEFTBRACE); sbsql.append(DataBaseUtils.getInsertSQL(dataList,columns,tableName));
for (TableColumn column : columns) { }
sbsql.append(SPLIT); }
sbsql.append(column.getColumnName());
sbsql.append(SPLIT);
sbsql.append(COMMA);
}
sbsql.deleteCharAt(sbsql.length() - 1);
sbsql.append(RIGHTBRACE);
sbsql.append(VALUES);
sbsql.append(LEFTBRACE);
for (TableColumn column : columns) {
if (column.getColumnType().contains("bigint") ||
column.getColumnType().contains("int") ||
column.getColumnType().contains("bit(1)")) {
if (!ObjectUtils.isEmpty(map.get(column.getColumnName()))) {
sbsql.append(map.get(column.getColumnName()));
}else {
sbsql.append("0");
}
} else {
if (!ObjectUtils.isEmpty(map.get(column.getColumnName()))) {
sbsql.append("'").append(map.get(column.getColumnName())).append("'");
} else {
sbsql.append("null");
}
}
sbsql.append(COMMA);
}
sbsql.deleteCharAt(sbsql.length() - 1);
sbsql.append(RIGHTBRACE);
sbsql.append(BRANCH).append(BR);
}
}
return sbsql.toString(); return sbsql.toString();
} }
public static String format(String sqlTemplate, Map<String, Object> parameter) { public static String format(String sqlTemplate, Map<String, Object> parameter) {
StringSubstitutor ss = new StringSubstitutor(parameter); StringSubstitutor ss = new StringSubstitutor(parameter);
String newTemplateStr = ss.replace(sqlTemplate); String newTemplateStr = ss.replace(sqlTemplate);
return newTemplateStr; return newTemplateStr;
} }
public void download(HttpServletResponse response, String fileName, String sqlFilePath) throws IOException { public void download(HttpServletResponse response, String fileName, String sqlFilePath) throws IOException {
File f = new File(sqlFilePath); File f = new File(sqlFilePath);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len = 0; int len = 0;
response.reset(); // 非常重要 response.reset(); // 非常重要
response.setContentType("application/x-msdownload"); response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream out = response.getOutputStream(); OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0) while ((len = br.read(buf)) > 0)
out.write(buf, 0, len); out.write(buf, 0, len);
br.close(); br.close();
out.close(); out.close();
} }
private void print(String id,String msg,String step) { private void print(String id, String msg, String step) {
String percent = "0"; String percent = "0";
percent = ValidationUtil.equals(step,"end") ? "100" : ValidationUtil.equals(step,"running") ? "50" : "10"; percent = ValidationUtil.equals(step, "end") ? "100" : ValidationUtil.equals(step, "running") ? "50" : "10";
JSONObject event = new JSONObject(); JSONObject event = new JSONObject();
event.put("percent",percent); event.put("percent", percent);
event.put("status", "running"); event.put("status", "running");
event.put("logInfo", msg); event.put("logInfo", msg);
event.put("time", DateUtils.toDateStr(DateUtils.format())); event.put("time", DateUtils.toDateStr(DateUtils.format()));
try { try {
emqKeeper.getMqttClient().publish("/topicTable/solidify/" + (id.indexOf(',') != -1 ? id.split(",")[0] : id), event.toString().getBytes(), 0, false); emqKeeper.getMqttClient().publish("/topicTable/solidify/" + (id.indexOf(',') != -1 ? id.split(",")[0] : id), event.toString().getBytes(), 0, false);
} catch (MqttException e) { } catch (MqttException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment