Commit 3fd0a4ff authored by 陈祥烨's avatar 陈祥烨

删除机构优化

parent 6fff422f
......@@ -20,7 +20,7 @@ import java.util.List;
*/
@RestController
@Api(tags = "机构树")
@RequestMapping(value = "/demo/privilege_agency")
@RequestMapping(value = "/privilege_agency")
public class AgencyTreeResource {
@Autowired
......
package com.yeejoin.amos.api.tool.controller;
import com.yeejoin.amos.api.tool.face.model.RelationTreeModel;
import com.yeejoin.amos.api.tool.face.service.AgencyTreeService;
import com.yeejoin.amos.api.tool.face.service.ConnectService;
import com.yeejoin.amos.api.tool.face.service.DataBaseService;
......@@ -11,10 +12,13 @@ import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.foundation.utils.StringUtil;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
@CrossOrigin
@RestController
......@@ -31,16 +35,23 @@ public class RelationResource {
AgencyTreeService agencyTreeService;
@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
@GetMapping(value = "/test")
@ApiOperation(httpMethod = "GET", value = "sql", notes = "sql")
public void joinSQL(@RequestParam(value = "agencyCodeId") Long agencyCodeId,@RequestParam(value = "sequenceNbrs") String sequenceNbrs) throws SQLException, ClassNotFoundException {
@DeleteMapping(value = "/relation/{agencyCodeId}/{sequenceNbrs}")
@ApiOperation(httpMethod = "DELETE", value = "机构删除", notes = "机构删除")
public ResponseModel<String> joinSQL(@PathVariable Long agencyCodeId, @PathVariable String sequenceNbrs) throws ClassNotFoundException, SQLException {
Long[] seqs = dataBaseService.stringToLong(sequenceNbrs);
for (Long seq : seqs) {
Connection connection = connectService.connectNewDatabase(dataBaseService.queryBySeq(seq));
System.out.println(connection);
relationService.deleteDataBaseByAgencyCode(connection, agencyTreeService.queryForAgencyCodeById(agencyCodeId));
}
return ResponseHelper.buildResponse("The data has been deleted !");
}
@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
@GetMapping(value = "/relation/test")
@ApiOperation(httpMethod = "GET", value = "sql",notes = "sql")
public ResponseModel<List<List<RelationTreeModel>>> test() throws SQLException, ClassNotFoundException {
Long[] seqs = dataBaseService.stringToLong("1675757337942056962");
Connection connection = connectService.connectNewDatabase(dataBaseService.queryBySeq(seqs[0]));
return ResponseHelper.buildResponse(relationService.createRelationTree(connection));
}
}
......@@ -3,18 +3,17 @@ package com.yeejoin.amos.api.tool.face.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.typroject.tyboot.core.rdbms.model.BaseModel;
/**
* @author r
*/
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "DataBaseModel", description = "")
public class AgencyTreeModel extends BaseModel {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "机构名称")
private String agencyName;
......
......@@ -22,5 +22,4 @@ public class RelationModel extends BaseModel {
@ApiModelProperty(value = "表2字段")
private String field2;
}
package com.yeejoin.amos.api.tool.face.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@ApiModel(value = "RelationTreeModel", description = "")
public class RelationTreeModel {
@ApiModelProperty(value = "表名称")
private String name;
@ApiModelProperty(value = "表字段")
private String frontField;
@ApiModelProperty(value = "前表关联字段")
private String postField;
@ApiModelProperty(value = "父表名")
private String parentName;
@ApiModelProperty(value = "子节点")
private List<RelationTreeModel> children;
public RelationTreeModel(String name, String frontField,String postField , String parentName, List<RelationTreeModel> children) {
this.name = name;
this.frontField = frontField;
this.postField = postField;
this.parentName = parentName;
this.children = children;
}
}
......@@ -2,7 +2,6 @@ package com.yeejoin.amos.api.tool.face.orm.dao;
import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yeejoin.amos.api.tool.face.model.RelationModel;
import com.yeejoin.amos.api.tool.face.orm.entity.Relation;
import com.yeejoin.amos.api.tool.face.service.RelationService;
import org.apache.ibatis.annotations.Param;
......
package com.yeejoin.amos.api.tool.face.service;
import com.yeejoin.amos.api.tool.face.model.RelationModel;
import com.yeejoin.amos.api.tool.face.model.RelationTreeModel;
import com.yeejoin.amos.api.tool.face.orm.dao.RelationMapper;
import com.yeejoin.amos.api.tool.face.orm.entity.Relation;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -20,19 +21,12 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
@Autowired
ConnectService connectService;
/**
* 读取Relation表
*/
// public List<RelationModel> queryRelation() {
// return this.queryForList(null, false);
// }
/**
* 根据Relation删除数据库
*/
public String deleteDataBaseByAgencyCode(Connection connection,String agencyCode) throws SQLException {
List<HashMap<String,String>> maps = this.queryRelation(connection);
List<String> rs = new ArrayList<>();
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
......@@ -42,8 +36,9 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
e.printStackTrace();
}
StringBuilder sql = new StringBuilder("DELETE ").append(maps.get(0).get("TABLE1"));
for (int i = 0; i < maps.size();i++){
sql.append(", ").append(maps.get(i).get("TABLE2"));
for (HashMap<String, String> map : maps) {
if (map.get("TABLE1") != null)
sql.append(", ").append(map.get("TABLE2"));
}
sql.append(" FROM ").append(maps.get(0).get("TABLE1"));
try {
......@@ -53,37 +48,34 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
else {
StringBuilder extraSql = new StringBuilder("DELETE FROM ");
extraSql.append(map.get("TABLE2")).append(" WHERE AGENCY_CODE = '").append(agencyCode).append("' ;");
// statementSql(extraSql, connection, rs);
System.out.println(extraSql);
System.out.println(statementSql(extraSql, connection));
}
}
sql.append(" WHERE ").append(maps.get(0).get("TABLE1")).append(".AGENCY_CODE = '").append(agencyCode).append("' ;");
// statementSql(sql, connection, rs);
// System.out.println(rs);
System.out.println(sql.toString());
System.out.println(statementSql(sql, connection));
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(sql.toString());
return null;
}
/**
* 执行sql
*/
public void statementSql(StringBuilder sql, Connection connection,List<String> rs) {
public int statementSql(StringBuilder sql, Connection connection) {
int count = 0;
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql.toString());
while (resultSet.next()){
rs.add(resultSet.getString("id"));
}
resultSet.close();
count = statement.executeUpdate(sql.toString());
statement.close();
return count;
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public List<HashMap<String,String>> queryRelation(Connection connection) throws SQLException{
......@@ -102,4 +94,48 @@ public class RelationService extends BaseService<RelationModel, Relation, Relati
statement.close();
return maps;
}
/**
* 递归建立树形结构 (算法)
* @param relationTreeModels
* @param parentName
* @return
*/
public List<RelationTreeModel> createTree(List<RelationTreeModel> relationTreeModels, String parentName){
List<RelationTreeModel> tree = new ArrayList<>();
for (RelationTreeModel relationTreeModel: relationTreeModels){
if(parentName.equals(relationTreeModel.getParentName())){
relationTreeModel.setChildren(createTree(relationTreeModels, relationTreeModel.getName()));
tree.add(relationTreeModel);
}
}
return tree;
}
/**
* 执行sql
*/
public List<RelationTreeModel> createNodes(List<HashMap<String,String>> maps){
List<RelationTreeModel> relationTreeModels = new ArrayList<>();
for(HashMap<String,String> map: maps){
RelationTreeModel model = new RelationTreeModel();
if(map.get("TABLE1") == null)
model = new RelationTreeModel(map.get("TABLE2"), map.get(""), map.get("TABLE2_FIELD"),"",null);
else model = new RelationTreeModel(map.get("TABLE2"), map.get("TABLE1_FIELD"), map.get("TABLE2_FIELD"), map.get("TABLE1"),null);
relationTreeModels.add(model);
}
return relationTreeModels;
}
public List<List<RelationTreeModel>> createRelationTree(Connection connection) throws SQLException {
List<HashMap<String,String>> maps = this.queryRelation(connection);
List<List<RelationTreeModel>> trees = new ArrayList<>();
for (RelationTreeModel root: createNodes(maps)){
if(root.getParentName().equals("")){
trees.add(createTree(createNodes(maps),""));
}
}
System.out.println(trees);
return trees;
}
}
#DB properties:
# jdbc_config
spring.datasource.url=jdbc:mysql://39.98.45.134:3306/amos_project_copy?allowMultiQueries=true
spring.datasource.url=jdbc:mysql://39.98.45.134:3306/amos_studio_delete?allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=Yeejoin@2020
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
......
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