Forráskód Böngészése

fix 数据迁移 导出脚本文件 sql格式处理

lvxianyun 2 éve
szülő
commit
0de0048332

+ 1 - 0
adm-business/adm-middleware/src/main/java/com/persagy/proxy/migration/constant/MigrationConstant.java

@@ -11,6 +11,7 @@ public class MigrationConstant {
     public final static String ERROR = "目标地址错误不能正常访问!";
 
     public final static int BATCH_SUBMIT_DATA_COUNT = 5000;
+    public final static long BATCH_SQL_DATA_COUNT = 5000L;
 
     /** 数据拼接 用于list->map 时key和value拼接 **/
     public final static String SPLITER = "_";

+ 1 - 0
adm-business/adm-middleware/src/main/java/com/persagy/proxy/migration/service/Impl/DefineInfoMigration.java

@@ -144,6 +144,7 @@ public class DefineInfoMigration extends MigrationAbstractServiceImpl<ObjectInfo
             return "";
         }
         StringBuffer sqls = new StringBuffer(" \n -- 数据迁移  dt_define_info ------ \n");
+        sqls.append(" delete from dt_define_info where project_id='"+context.getProjectId()+"' or group_code='"+context.getGroupCode()+"'; \n ");
         sqls.append(MigrationConstant.DEF_FUNCID_SQL_PRE);
         admDefineList.forEach(infoDefine -> {
             sqls.append("(");

+ 1 - 0
adm-business/adm-middleware/src/main/java/com/persagy/proxy/migration/service/Impl/DefineTypeMigration.java

@@ -147,6 +147,7 @@ public class DefineTypeMigration extends MigrationAbstractServiceImpl<ObjectType
             return "";
         }
         StringBuffer sqls = new StringBuffer(" \n -- 数据迁移  dt_define_type ------ \n");
+        sqls.append(" delete from dt_define_type where project_id='"+context.getProjectId()+"' or group_code='"+context.getGroupCode()+"'; \n ");
         sqls.append(MigrationConstant.DEF_CLASS_SQL_PRE);
         admDefineList.forEach(typeDefine ->{
             sqls.append("(");

+ 5 - 4
adm-business/adm-middleware/src/main/java/com/persagy/proxy/migration/service/Impl/ObjectDigitalMigration.java

@@ -99,12 +99,13 @@ public class ObjectDigitalMigration extends MigrationAbstractServiceImpl<ObjectD
     @Override
     public String migrateForSql(InstanceUrlParam context) {
         //获取已经采集的数据
-        String sqls_temp = "\n -- 数据迁移  dt_object ------ \n  ";
+        String sqls_temp = "\n -- 数据迁移  dt_object ------ \n"
+                            + "delete from dt_object where project_id='"+context.getProjectId()+"' and group_code='"+context.getGroupCode()+"';";
         List<String> objTypes = getObjTypes(context);
         //List<String> objTypes = Arrays.asList("equipment");
         for(String objType : objTypes){
             QueryCriteria queryCriteria = getQueryCriteria(objType);
-            queryCriteria.setSize(1000L);
+            queryCriteria.setSize(MigrationConstant.BATCH_SQL_DATA_COUNT);
             CommonResult<List<ObjectNode>> result = getAdmDataPage(context,queryCriteria);
             if(result == null){
                 continue;
@@ -115,8 +116,8 @@ public class ObjectDigitalMigration extends MigrationAbstractServiceImpl<ObjectD
             }
             sqls_temp += processSqlStr(appendValues(admData).toString());
 
-            if(result.getCount() > 1000){
-                long page = result.getCount() / 1000 + 1;
+            if(result.getCount() > MigrationConstant.BATCH_SQL_DATA_COUNT){
+                long page = result.getCount() / MigrationConstant.BATCH_SQL_DATA_COUNT + 1;
                 for(long i = 2; i<=page; i++){
                     queryCriteria.setPage(i);
                     result = getAdmDataPage(context,queryCriteria);

+ 17 - 16
adm-business/adm-middleware/src/main/java/com/persagy/proxy/migration/service/Impl/ObjectRelationMigration.java

@@ -125,13 +125,14 @@ public class ObjectRelationMigration extends MigrationAbstractServiceImpl<Object
     @Override
     public String migrateForSql(InstanceUrlParam context) {
         StringBuffer sqls = new StringBuffer(" \n -- 数据迁移  dt_relation ------ \n");
+        sqls.append(" delete from dt_relation where project_id='"+context.getProjectId()+"' and group_code='"+context.getGroupCode()+"'; \n ");
         //获取已经采集的数据
         String sqls_temp = "";
         ExecutorService service = getExcecutor();
-        List<Future<StringBuffer>> futureList = new ArrayList<>();
+        List<Future<String>> futureList = new ArrayList<>();
         for (MiGrationRelCode miGrationRelCode: MiGrationRelCode.values()){
             QueryCriteria queryCriteria = getQueryCriteria(miGrationRelCode.getCode());
-            queryCriteria.setSize(10000L);
+            queryCriteria.setSize(MigrationConstant.BATCH_SQL_DATA_COUNT);
             CommonResult<List<ObjectRelation>> admDatas = getAdmDataPage(context,queryCriteria);
             if (admDatas.getResult().equals(DmpResult.SUCCESS) && admDatas.getCount() > queryCriteria.getSize()) {
                 long pages = admDatas.getCount() / queryCriteria.getSize() + 1;
@@ -146,11 +147,11 @@ public class ObjectRelationMigration extends MigrationAbstractServiceImpl<Object
             }
         }
         service.shutdown();
-        for (Future<StringBuffer> future : futureList) {
+        for (Future<String> future : futureList) {
             try {
-                StringBuffer stringBuffer = future.get();
-                if(StrUtil.isNotEmpty(stringBuffer)){
-                    sqls_temp += stringBuffer.toString();
+                String string = future.get();
+                if(StrUtil.isNotEmpty(string)){
+                    sqls_temp += string;
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
@@ -164,22 +165,18 @@ public class ObjectRelationMigration extends MigrationAbstractServiceImpl<Object
         if(sqls_temp.length() == 0){
             return "";
         }
-        sqls.append(MigrationConstant.DT_RELATION_SQL_PRE);
         sqls.append(sqls_temp);
-        String sql = sqls.toString();
-        sql = sql.substring(0, sql.lastIndexOf(","));
-        sql +=";";
-        return sql;
+        return sqls.toString();
     }
 
-    private void appendValuesBefore(ExecutorService service, List<ObjectRelation> admData, List<Future<StringBuffer>> futureList){
+    private void appendValuesBefore(ExecutorService service, List<ObjectRelation> admData, List<Future<String>> futureList){
         if(CollUtil.isEmpty(admData)){
             return;
         }
         StringBuffer sqls_temp = new StringBuffer();
-        Future<StringBuffer> future = service.submit(new Callable<StringBuffer>(){
+        Future<String> future = service.submit(new Callable<String>(){
             @Override
-            public StringBuffer call() throws Exception {
+            public String call() throws Exception {
                 return appendValues(admData, sqls_temp);
             }
         });
@@ -192,7 +189,7 @@ public class ObjectRelationMigration extends MigrationAbstractServiceImpl<Object
      * @param sqls
      * @return
      */
-    private StringBuffer appendValues(List<ObjectRelation> admData, StringBuffer sqls){
+    private String appendValues(List<ObjectRelation> admData, StringBuffer sqls){
         admData.forEach(relation -> {
             sqls.append("(");
             sqls.append("'").append(relation.getId()).append("',");
@@ -241,7 +238,11 @@ public class ObjectRelationMigration extends MigrationAbstractServiceImpl<Object
             sqls.append(relation.getValid());
             sqls.append(")\n ,");
         });
-        return sqls;
+        String sql = "";
+        if(sqls.length() > 0){
+            sql = MigrationConstant.DT_RELATION_SQL_PRE + StrUtil.sub(sqls.toString(), 0 , -1) + "; \n ";
+        }
+        return sql;
     }
     private ExecutorService getExcecutor(){
         ExecutorService service = new ThreadPoolExecutor(10, 15, 0L, TimeUnit.MILLISECONDS,