Commit e17ba019 authored by Gwofoo's avatar Gwofoo

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

parent 4bc2c299
......@@ -5,8 +5,6 @@ import com.yeejoin.amos.api.tool.face.service.RelationService;
import com.yeejoin.amos.api.tool.face.service.StudioResourceService;
import io.swagger.annotations.Api;
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.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
......@@ -16,7 +14,6 @@ import org.typroject.tyboot.core.restful.utils.ResponseModel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
......@@ -84,14 +81,14 @@ public class RelationResource {
* @return String
*/
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@GetMapping(value = "/export/{agencyCode}/{ipSeq}/{dbName}")
@GetMapping(value = "/export/{agencyCode}/{ipSeq}/{dbSeqs}")
@ApiOperation(httpMethod = "GET", value = "导出", notes = "导出")
// public ResponseModel<String> generateSQL(
// @RequestParam(value = "databaseName",required = false) String databaseName,
public void generateSQL(
@PathVariable String agencyCode,
@PathVariable Long ipSeq,
@PathVariable String dbName,
@PathVariable String dbSeqs,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
Map<String, Object> variables = new HashMap<>();
......@@ -101,7 +98,7 @@ public class RelationResource {
// int end = referer.indexOf("&agencyCode=");
// String ipSeq = referer.substring(start,end);
// 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");
}
......
......@@ -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 {
DataBaseLinkModel dataBaseLinkModel = dataBaseLinkService.queryBySeq(ipSeq);
//根据连接查询到数据库表名
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);
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();
// 查询所有数据库
List<String> allDbs = DataBaseUtils.getList("show databases;", connection);
List<String> databaseNames = new ArrayList<>();
String[] queryDbSeqs = dbSeqs.split(","); // 序号从1开始
for (int i = 0; i < queryDbSeqs.length; i ++ ) {
int idx = Integer.parseInt(queryDbSeqs[i]);
databaseNames.add(allDbs.get(idx - 1));
}
// 备份文件路径名称
// 查询到需要导出多个,在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 = databaseName+DateFormatUtils.format(new Date(), "yyyyMMdd") + "." + SUFFIX;
String sqlFilePath = backPath + SLASH + fileName;
File file = new File(sqlFilePath);
FileOutputStream out;
OutputStreamWriter writer = null;
out = new FileOutputStream(file);
writer = new OutputStreamWriter(out, "utf8");
StringBuffer deleteSql = new StringBuffer();
for (String tableName : list) {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = '" + tableName + "' AND COLUMN_NAME = '" + "AGENCY_CODE" + "';");
if (!rs.next()) {
continue;
String fileName = databaseName + DateFormatUtils.format(new Date(), "yyyyMMdd") + "." + SUFFIX;
String sqlFilePath = backPath + SLASH + fileName;
File file = new File(sqlFilePath);
FileOutputStream out;
OutputStreamWriter writer = null;
out = new FileOutputStream(file);
writer = new OutputStreamWriter(out, "utf8");
StringBuffer deleteSql = new StringBuffer();
for (String tableName : list) {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = '" + tableName + "' AND COLUMN_NAME = '" + "AGENCY_CODE" + "';");
if (!rs.next()) {
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) {
print(variables.get(dimension).toString(), "开始创建delete语句...", "running");
writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR);
writer.write(deleteSql.toString());
writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "delete语句创建完成...", "running");
}
if (deleteSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建delete语句...", "running");
writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 删除历史资源数据 " + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR);
writer.write(deleteSql.toString());
writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "delete语句创建完成...", "running");
}
if (insertSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建insert语句...", "running");
writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR);
writer.write(insertSql.toString());
writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "insert语句创建完成...", "running");
}
if (insertSql.length() > 0) {
print(variables.get(dimension).toString(), "开始创建insert语句...", "running");
writer.write(BR + DELIMITER + BR);
writer.write("/**" + BR + "* 资源数据" + BR + "**/" + BR);
writer.write(BR + DELIMITER + BR);
writer.write(insertSql.toString());
writer.write(BR + BR + DELIMITER + BR);
print(variables.get(dimension).toString(), "insert语句创建完成...", "running");
}
writer.flush();
writer.close();
out.close();
download(response, fileName, sqlFilePath);
writer.flush();
writer.close();
out.close();
download(response, fileName, sqlFilePath);
}
connection.close();
} catch (IOException e) {
e.printStackTrace();
} 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