Commit 35533eef authored by 陈祥烨's avatar 陈祥烨

更新脚本导出

parent 8144ce93
...@@ -17,7 +17,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -17,7 +17,7 @@ import javax.servlet.http.HttpServletResponse;
@CrossOrigin @CrossOrigin
@RestController @RestController
@Api(tags = "机构管理Api") @Api(tags = "机构管理Api")
@RequestMapping(value = "/Agency") @RequestMapping(value = "/agency")
public class RelationResource { public class RelationResource {
@Autowired @Autowired
private RelationService relationService; private RelationService relationService;
...@@ -47,7 +47,7 @@ public class RelationResource { ...@@ -47,7 +47,7 @@ public class RelationResource {
* @return String * @return String
*/ */
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false) @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@GetMapping(value = "/output/{agencyCode}/{sequenceNbrs}") @GetMapping(value = "/export/{agencyCode}/{sequenceNbrs}")
@ApiOperation(httpMethod = "GET", value = "导出", notes = "导出") @ApiOperation(httpMethod = "GET", value = "导出", notes = "导出")
public ResponseModel<String> generateSQL( public ResponseModel<String> generateSQL(
......
...@@ -55,11 +55,11 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase ...@@ -55,11 +55,11 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase
* 编辑 * 编辑
*/ */
public DataBaseLinkModel update(DataBaseLinkModel dataBaseLinkModel) throws Exception { public DataBaseLinkModel update(DataBaseLinkModel dataBaseLinkModel) throws Exception {
DataBaseLinkModel model = this.encryptPwd(dataBaseLinkModel); DataBaseLinkModel model = this.decryptPwd(dataBaseLinkModel);
if (this.testConnection(model)) { if (this.testConnection(model)) {
return this.updateWithModel(model); return this.updateWithModel(encryptPwd(model));
} else { } else {
return new DataBaseLinkModel(); return null;
} }
} }
...@@ -97,12 +97,12 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase ...@@ -97,12 +97,12 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase
public List<AgencyModel> queryForAgencyList() throws SQLException, IOException { public List<AgencyModel> queryForAgencyList() throws SQLException, IOException {
Connection con = dataSource.getConnection(); Connection con = dataSource.getConnection();
URL url = new URL URL url = new URL
(con.getMetaData().getURL().replace("jdbc:mysql","http")); (con.getMetaData().getURL().replace("jdbc:mysql", "http"));
con.close(); con.close();
log.info(url); log.info(url);
DataBaseLink dataBaseLink = getOne(new QueryWrapper<DataBaseLink>().eq("IP",url.getHost())); DataBaseLink dataBaseLink = getOne(new QueryWrapper<DataBaseLink>().eq("IP", url.getHost()));
DataBaseLinkModel dataBaseLinkModel = new DataBaseLinkModel(); DataBaseLinkModel dataBaseLinkModel = new DataBaseLinkModel();
Bean.toModel(dataBaseLink,dataBaseLinkModel); Bean.toModel(dataBaseLink, dataBaseLinkModel);
Connection connection = this.connectNewDatabase(dataBaseLinkModel); Connection connection = this.connectNewDatabase(dataBaseLinkModel);
List<Map<String, Object>> maps = DataBaseUtils.getMaps List<Map<String, Object>> maps = DataBaseUtils.getMaps
("SELECT AGENCY_NAME, AGENCY_CODE, AGENCY_DESC FROM privilege_agency;", connection); ("SELECT AGENCY_NAME, AGENCY_CODE, AGENCY_DESC FROM privilege_agency;", connection);
...@@ -180,6 +180,10 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase ...@@ -180,6 +180,10 @@ public class DataBaseLinkService extends BaseService<DataBaseLinkModel, DataBase
return model; return model;
} }
public DataBaseLinkModel decryptPwd(DataBaseLinkModel model) throws Exception {
model.setPassWord(AESUtils.decrypt(model.getPassWord()));
return model;
}
} }
package com.yeejoin.amos.api.tool.face.service; package com.yeejoin.amos.api.tool.face.service;
import com.yeejoin.amos.api.tool.enums.SourceEnum;
import com.yeejoin.amos.api.tool.face.model.DataBaseLinkModel; import com.yeejoin.amos.api.tool.face.model.DataBaseLinkModel;
import com.yeejoin.amos.api.tool.face.model.DataBaseNameListModel; import com.yeejoin.amos.api.tool.face.model.DataBaseNameListModel;
import com.yeejoin.amos.api.tool.face.model.RelationModel; import com.yeejoin.amos.api.tool.face.model.RelationModel;
...@@ -9,6 +10,7 @@ import com.yeejoin.amos.api.tool.face.orm.entity.Relation; ...@@ -9,6 +10,7 @@ import com.yeejoin.amos.api.tool.face.orm.entity.Relation;
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.DataBaseUtils;
import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -21,6 +23,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -21,6 +23,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -63,8 +66,7 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati ...@@ -63,8 +66,7 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
DataBaseLinkModel model = dataBaseLinkService.queryBySeq(nameListModel.getParentId()); DataBaseLinkModel model = dataBaseLinkService.queryBySeq(nameListModel.getParentId());
model.setDbName(nameListModel.getDbName()); model.setDbName(nameListModel.getDbName());
Connection connection = dataBaseLinkService.connectNewDatabase(model); Connection connection = dataBaseLinkService.connectNewDatabase(model);
this.insertData( this.insertData(connection, agencyCode, model.getDbName(),seq, response);
connection, agencyCode, model.getDbName(), response);
} }
} }
...@@ -189,7 +191,7 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati ...@@ -189,7 +191,7 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
/** /**
* 插入数据 * 插入数据
*/ */
public void insertData(Connection connection, String agencyCode, String dbName,HttpServletResponse response) throws SQLException { public void insertData(Connection connection, String agencyCode, String dbName,Long seq,HttpServletResponse response) throws SQLException {
try { try {
File directory = new File("");// 参数为空 File directory = new File("");// 参数为空
String coursePath = directory.getCanonicalPath(); String coursePath = directory.getCanonicalPath();
...@@ -199,16 +201,21 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati ...@@ -199,16 +201,21 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
if (!sqlDirectory.exists()) { if (!sqlDirectory.exists()) {
sqlDirectory.mkdir(); sqlDirectory.mkdir();
} }
String fileName = agencyCode + "." + SUFFIX;
print(seq.toString(),"开始执行", "start");
String fileName = "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 insertSql = new StringBuffer(); StringBuffer insertSql = new StringBuffer();
if (!connection.isClosed())
System.out.println("Succeeded connecting to the Database!");
//通过连接获取relation表树形结构 //通过连接获取relation表树形结构
List<RelationTreeModel> trees = this.createRelationTree(dbName); List<RelationTreeModel> trees = this.createRelationTree(dbName);
for (RelationTreeModel tree : trees) { for (RelationTreeModel tree : trees) {
...@@ -218,12 +225,14 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati ...@@ -218,12 +225,14 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
for (RelationTreeModel model : array) { for (RelationTreeModel model : array) {
List<RelationTreeModel> tableSet = tableRelation(model, array); List<RelationTreeModel> tableSet = tableRelation(model, array);
List<TableColumn> columns = DataBaseUtils.getTableColumn(model.getName(), connection); List<TableColumn> columns = DataBaseUtils.getTableColumn(model.getName(), connection);
String sql = this.getSelectSQL(tableSet, model.getName(), columns, agencyCode).toString(); String selectSQL = this.getSelectSQL(tableSet, model.getName(), columns, agencyCode).toString();
List<Map<String, Object>> dataList = DataBaseUtils.getMaps(sql, connection); print(seq.toString(),"开始查询"+model.getName()+"表","running");
System.out.println(sql); List<Map<String, Object>> dataList = DataBaseUtils.getMaps(selectSQL, connection);
print(seq.toString(),"开始创建"+model.getName()+"表insert语句","running");
System.out.println(selectSQL);
StringBuffer insertSQL = DataBaseUtils.getInsertSQL(dataList, columns, model.getName()); StringBuffer insertSQL = DataBaseUtils.getInsertSQL(dataList, columns, model.getName());
System.out.println(insertSQL); System.out.println(insertSQL);
if (!ObjectUtils.isEmpty(sql)) insertSql.append(insertSQL); if (!ObjectUtils.isEmpty(insertSQL)) insertSql.append(insertSQL);
} }
} }
if (insertSql.length() > 0) { if (insertSql.length() > 0) {
...@@ -232,15 +241,22 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati ...@@ -232,15 +241,22 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
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(seq.toString(), "insert语句创建完成...", "running");
} }
writer.flush(); writer.flush();
writer.close(); writer.close();
out.close(); out.close();
connection.close();
if (insertSql.length() == 0) {
print(seq.toString(), "该项目下数据路径字段为空,不支持导出...", "start");
return;
}
download(response, fileName, sqlFilePath); download(response, fileName, sqlFilePath);
print(seq.toString(),"执行结束", "end");
} catch (SQLException | IOException e) { } catch (SQLException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
connection.close();
} }
/** /**
......
...@@ -33,6 +33,8 @@ import java.util.Date; ...@@ -33,6 +33,8 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static com.yeejoin.amos.api.tool.utils.DataBaseUtils.print;
/** /**
* <p> * <p>
* 服务类 * 服务类
...@@ -80,8 +82,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -80,8 +82,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
@Autowired @Autowired
private SqlHelper sqlHelper; private SqlHelper sqlHelper;
@Autowired
EmqKeeper emqKeeper;
/** /**
* 列表查询 示例 * 列表查询 示例
...@@ -158,7 +159,6 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -158,7 +159,6 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
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();
...@@ -233,21 +233,6 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud ...@@ -233,21 +233,6 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
out.close(); out.close();
} }
private void print(String id, String msg, String step) {
String percent = "0";
percent = ValidationUtil.equals(step, "end") ? "100" : ValidationUtil.equals(step, "running") ? "50" : "10";
JSONObject event = new JSONObject();
event.put("percent", percent);
event.put("status", "running");
event.put("logInfo", msg);
event.put("time", DateUtils.toDateStr(DateUtils.format()));
try {
emqKeeper.getMqttClient().publish("/topicTable/solidify/" + (id.indexOf(',') != -1 ? id.split(",")[0] : id), event.toString().getBytes(), 0, false);
} catch (MqttException e) {
e.printStackTrace();
}
}
/** /**
* 获取项目列表 * 获取项目列表
......
package com.yeejoin.amos.api.tool.utils; package com.yeejoin.amos.api.tool.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn; import com.yeejoin.amos.api.tool.face.orm.entity.TableColumn;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource; import javax.sql.DataSource;
...@@ -17,6 +21,8 @@ import java.util.*; ...@@ -17,6 +21,8 @@ import java.util.*;
public class DataBaseUtils { public class DataBaseUtils {
static EmqKeeper emqKeeper;
/** /**
* 数据库备份路径 * 数据库备份路径
*/ */
...@@ -151,6 +157,22 @@ public class DataBaseUtils { ...@@ -151,6 +157,22 @@ public class DataBaseUtils {
return sql; return sql;
} }
public static void print(String id, String msg, String step) {
String percent = "0";
percent = ValidationUtil.equals(step, "end") ? "100" : ValidationUtil.equals(step, "running") ? "50" : "10";
JSONObject event = new JSONObject();
event.put("percent", percent);
event.put("status", "running");
event.put("logInfo", msg);
event.put("time", DateUtils.toDateStr(DateUtils.format()));
try {
emqKeeper.getMqttClient().publish("/topicTable/solidify/" + (id.indexOf(',') != -1 ? id.split(",")[0] : id), event.toString().getBytes(), 0, false);
} catch (MqttException e) {
e.printStackTrace();
}
}
public static <V> Page<V> listToPage(List<V> list, int current, int size) { public static <V> Page<V> listToPage(List<V> list, int current, int size) {
//当前页 //当前页
Page<V> page = new Page<>(current, size); Page<V> page = new Page<>(current, size);
......
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