Commit e17ba019 authored by Gwofoo's avatar Gwofoo

修改脚本导出-库管理-导出脚本接口

parent 4bc2c299
...@@ -5,8 +5,6 @@ import com.yeejoin.amos.api.tool.face.service.RelationService; ...@@ -5,8 +5,6 @@ import com.yeejoin.amos.api.tool.face.service.RelationService;
import com.yeejoin.amos.api.tool.face.service.StudioResourceService; import com.yeejoin.amos.api.tool.face.service.StudioResourceService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType; import org.typroject.tyboot.core.foundation.enumeration.UserType;
...@@ -16,7 +14,6 @@ import org.typroject.tyboot.core.restful.utils.ResponseModel; ...@@ -16,7 +14,6 @@ import org.typroject.tyboot.core.restful.utils.ResponseModel;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -84,14 +81,14 @@ public class RelationResource { ...@@ -84,14 +81,14 @@ public class RelationResource {
* @return String * @return String
*/ */
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false) @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@GetMapping(value = "/export/{agencyCode}/{ipSeq}/{dbName}") @GetMapping(value = "/export/{agencyCode}/{ipSeq}/{dbSeqs}")
@ApiOperation(httpMethod = "GET", value = "导出", notes = "导出") @ApiOperation(httpMethod = "GET", value = "导出", notes = "导出")
// public ResponseModel<String> generateSQL( // public ResponseModel<String> generateSQL(
// @RequestParam(value = "databaseName",required = false) String databaseName, // @RequestParam(value = "databaseName",required = false) String databaseName,
public void generateSQL( public void generateSQL(
@PathVariable String agencyCode, @PathVariable String agencyCode,
@PathVariable Long ipSeq, @PathVariable Long ipSeq,
@PathVariable String dbName, @PathVariable String dbSeqs,
HttpServletResponse response, HttpServletResponse response,
HttpServletRequest request) throws Exception { HttpServletRequest request) throws Exception {
Map<String, Object> variables = new HashMap<>(); Map<String, Object> variables = new HashMap<>();
...@@ -101,7 +98,7 @@ public class RelationResource { ...@@ -101,7 +98,7 @@ public class RelationResource {
// int end = referer.indexOf("&agencyCode="); // int end = referer.indexOf("&agencyCode=");
// String ipSeq = referer.substring(start,end); // String ipSeq = referer.substring(start,end);
// studioResourceService.generateSQL("Agency","agencyCode", ipSeq, variables, response); // studioResourceService.generateSQL("Agency","agencyCode", ipSeq, variables, response);
studioResourceService.generateSQLByDatabaseName("Agency","agencyCode",dbName, ipSeq, variables, response); studioResourceService.generateSQLByDatabaseName("Agency","agencyCode",dbSeqs, ipSeq, variables, response);
// return ResponseHelper.buildResponse("yes"); // return ResponseHelper.buildResponse("yes");
} }
......
...@@ -177,74 +177,87 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -177,74 +177,87 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
} }
} }
public void generateSQLByDatabaseName(String resourceCode, String dimension, String databaseName, Long ipSeq, Map<String, Object> variables, HttpServletResponse response) { public void generateSQLByDatabaseName(String resourceCode, String dimension, String dbSeqs, Long ipSeq, Map<String, Object> variables, HttpServletResponse response) {
try { try {
DataBaseLinkModel dataBaseLinkModel = dataBaseLinkService.queryBySeq(ipSeq); DataBaseLinkModel dataBaseLinkModel = dataBaseLinkService.queryBySeq(ipSeq);
//根据连接查询到数据库表名 //根据连接查询到数据库表名
Connection connection = dataBaseLinkService.connectNewDatabase(dataBaseLinkModel); Connection connection = dataBaseLinkService.connectNewDatabase(dataBaseLinkModel);
// List<String> list = DataBaseUtils.getList("select table_name from information_schema.tables where table_schema='" + dataBaseLinkModel.getPrefix()+""+databaseName + "';", connection);
List<String> list = DataBaseUtils.getList("select table_name from information_schema.tables where table_schema='" +""+databaseName + "';", connection); // 查询所有数据库
List<StudioResourceModel> resourceList = queryForStudioResourceList(resourceCode); List<String> allDbs = DataBaseUtils.getList("show databases;", connection);
File directory = new File("");// 参数为空 List<String> databaseNames = new ArrayList<>();
String coursePath = directory.getCanonicalPath();
File parentFile = new File(coursePath).getParentFile(); String[] queryDbSeqs = dbSeqs.split(","); // 序号从1开始
String backPath = parentFile.getCanonicalPath() + BACKUP_PATH; for (int i = 0; i < queryDbSeqs.length; i ++ ) {
File sqlDirectory = new File(backPath); int idx = Integer.parseInt(queryDbSeqs[i]);
if (!sqlDirectory.exists()) { databaseNames.add(allDbs.get(idx - 1));
sqlDirectory.mkdir();
} }
// 备份文件路径名称 // 查询到需要导出多个,在databaseNames中,循环,多次下载
for (String databaseName : databaseNames) {
List<String> list = DataBaseUtils.getList("select table_name from information_schema.tables where table_schema='" + "" + databaseName + "';", connection);
List<StudioResourceModel> resourceList = queryForStudioResourceList(resourceCode);
File directory = new File("");// 参数为空
String coursePath = directory.getCanonicalPath();
File parentFile = new File(coursePath).getParentFile();
String backPath = parentFile.getCanonicalPath() + BACKUP_PATH;
File sqlDirectory = new File(backPath);
if (!sqlDirectory.exists()) {
sqlDirectory.mkdir();
}
// 备份文件路径名称
// 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 fileName = databaseName+DateFormatUtils.format(new Date(), "yyyyMMdd") + "." + SUFFIX; String fileName = databaseName + 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();
for (String tableName : list) { for (String tableName : list) {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " + ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = '" + tableName + "' AND COLUMN_NAME = '" + "AGENCY_CODE" + "';"); "WHERE TABLE_NAME = '" + tableName + "' AND COLUMN_NAME = '" + "AGENCY_CODE" + "';");
if (!rs.next()) { if (!rs.next()) {
continue; continue;
}
deleteSql.append("DELETE FROM " + tableName + " WHERE AGENCY_CODE = '" + variables.get(dimension).toString() + "';" + BR);
rs.close();
statement.close();
}
StringBuffer insertSql = new StringBuffer();
for (StudioResourceModel model : resourceList) {
String sql = buildInsertSqlByDatabaseName(model, dimension, databaseName, list, ipSeq, variables);
if (!ObjectUtils.isEmpty(sql))
insertSql.append(sql);
} }
deleteSql.append("DELETE FROM " + tableName + " WHERE AGENCY_CODE = '" + variables.get(dimension).toString() + "';" + BR);
rs.close();
statement.close();
}
StringBuffer insertSql = new StringBuffer();
for (StudioResourceModel model : resourceList) {
String sql = buildInsertSqlByDatabaseName(model, dimension, databaseName, list, ipSeq, variables);
if (!ObjectUtils.isEmpty(sql))
insertSql.append(sql);
}
if (deleteSql.length() > 0) { if (deleteSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建delete语句...", "running"); print(variables.get(dimension).toString(), "开始创建delete语句...", "running");
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR); writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write(deleteSql.toString()); writer.write(deleteSql.toString());
writer.write(BR + BR + DELIMITER + BR); writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "delete语句创建完成...", "running"); print(variables.get(dimension).toString(), "delete语句创建完成...", "running");
} }
if (insertSql.length() > 0) { if (insertSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建insert语句...", "running"); print(variables.get(dimension).toString(), "开始创建insert语句...", "running");
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR); writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR); writer.write(BR + DELIMITER + BR);
writer.write(insertSql.toString()); writer.write(insertSql.toString());
writer.write(BR + BR + DELIMITER + BR); writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "insert语句创建完成...", "running"); print(variables.get(dimension).toString(), "insert语句创建完成...", "running");
} }
writer.flush(); writer.flush();
writer.close(); writer.close();
out.close(); out.close();
download(response, fileName, sqlFilePath); download(response, fileName, sqlFilePath);
}
connection.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (SQLException e) { } catch (SQLException e) {
......
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