浏览代码

完善工具

CX 9 月之前
父节点
当前提交
514d6409df

+ 2 - 2
src/main/java/com/dashitech/migration/SqlTask.java

@@ -8,9 +8,9 @@ import com.dashitech.migration.core.FieldMapper;
8
 public interface SqlTask {
8
 public interface SqlTask {
9
 
9
 
10
     /**
10
     /**
11
-     * 不需要try/catch,有错就抛出来处理
11
+     * 不需要try/catch,有错就抛出来处理。一个start只能单独执行,如果嵌套到别的方法内,事务可能不会正常执行
12
      */
12
      */
13
-    FieldMapper start(FieldMapper fieldMapper);
13
+    void start();
14
 
14
 
15
     /**
15
     /**
16
      * 如果之前执行有报错,需要重新加载特定字段缓存数据
16
      * 如果之前执行有报错,需要重新加载特定字段缓存数据

+ 11 - 4
src/main/java/com/dashitech/migration/StartApplication.java

@@ -1,6 +1,5 @@
1
 package com.dashitech.migration;
1
 package com.dashitech.migration;
2
 
2
 
3
-import com.dashitech.migration.core.FieldMapper;
4
 import org.apache.commons.logging.Log;
3
 import org.apache.commons.logging.Log;
5
 import org.apache.commons.logging.LogFactory;
4
 import org.apache.commons.logging.LogFactory;
6
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,12 +17,20 @@ public class StartApplication {
18
     @Autowired
17
     @Autowired
19
     SqlTask groupTask;
18
     SqlTask groupTask;
20
 
19
 
20
+    @Autowired
21
+    SqlTask userTask;
22
+
21
     public void start() {
23
     public void start() {
24
+        log.info("########### 开始迁移任务!###########");
25
+
22
         //工作组
26
         //工作组
23
-        FieldMapper groupMapper = groupTask.start(null);
24
-//        FieldMapper groupMapper = groupTask.loadCache();
25
-        System.out.println(groupMapper);
27
+        groupTask.start();
28
+
29
+//        userTask.start();
30
+
31
+//        groupTask.loadCache();
26
 
32
 
33
+        log.info("########### 迁移任务完成!###########");
27
     }
34
     }
28
 
35
 
29
 
36
 

+ 6 - 0
src/main/java/com/dashitech/migration/core/DataMapper.java

@@ -9,16 +9,22 @@ import java.util.Map;
9
  */
9
  */
10
 public class DataMapper implements Serializable{
10
 public class DataMapper implements Serializable{
11
 
11
 
12
+    //目标
12
     private String targetName;
13
     private String targetName;
13
     private Object targetValue;
14
     private Object targetValue;
14
     private Long targetId;
15
     private Long targetId;
16
+
17
+    //源
15
     private String sourceName;
18
     private String sourceName;
16
     private Object sourceValue;
19
     private Object sourceValue;
17
     private Long sourceId;
20
     private Long sourceId;
21
+
22
+    //操作类型
18
     private boolean remove = false;
23
     private boolean remove = false;
19
     private boolean update = false;
24
     private boolean update = false;
20
     private boolean add = false;
25
     private boolean add = false;
21
     private boolean replace = false;
26
     private boolean replace = false;
27
+
22
     /**
28
     /**
23
      * 匹配完成后的id对照,source:target
29
      * 匹配完成后的id对照,source:target
24
      */
30
      */

+ 26 - 1
src/main/java/com/dashitech/migration/core/DataMigrationter.java

@@ -14,6 +14,9 @@ import java.util.Map;
14
 public class DataMigrationter {
14
 public class DataMigrationter {
15
     public static Log log = LogFactory.getLog(DataMigrationter.class);
15
     public static Log log = LogFactory.getLog(DataMigrationter.class);
16
 
16
 
17
+    /**
18
+     * 开始匹配数据,并缓存到mapper的idCache里面
19
+     */
17
     public static void loadCache(List<Map<String, Object>> targetList, List<Map<String, Object>> sourceList, FieldMapper mapper) {
20
     public static void loadCache(List<Map<String, Object>> targetList, List<Map<String, Object>> sourceList, FieldMapper mapper) {
18
         mapperData(targetList, sourceList, mapper);
21
         mapperData(targetList, sourceList, mapper);
19
         trans2IdCache(mapper);
22
         trans2IdCache(mapper);
@@ -51,7 +54,7 @@ public class DataMigrationter {
51
             Long targetId = entry.getValue();
54
             Long targetId = entry.getValue();
52
             if (sourceId != null && targetId != null) {
55
             if (sourceId != null && targetId != null) {
53
                 cache.put(sourceId, targetId);
56
                 cache.put(sourceId, targetId);
54
-            }else if(sourceId != null){
57
+            } else if (sourceId != null) {
55
                 cache.put(sourceId, null);
58
                 cache.put(sourceId, null);
56
             }
59
             }
57
         }
60
         }
@@ -59,17 +62,25 @@ public class DataMigrationter {
59
     }
62
     }
60
 
63
 
61
 
64
 
65
+    /**
66
+     * 设置源数据,联合主键
67
+     */
62
     public static void setSourceKey(Map<String, Object> map, FieldMapper mapper) {
68
     public static void setSourceKey(Map<String, Object> map, FieldMapper mapper) {
63
         StringBuffer source = new StringBuffer();
69
         StringBuffer source = new StringBuffer();
64
         Object id = map.get("id");
70
         Object id = map.get("id");
65
         for (DataMapper dataMapper : mapper.getFieldList()) {
71
         for (DataMapper dataMapper : mapper.getFieldList()) {
66
             Object value = map.get(dataMapper.getSourceName());
72
             Object value = map.get(dataMapper.getSourceName());
73
+            //可能构建联合主键时,需要替换成新数据
74
+            value = getOprationReplaceValue(mapper, dataMapper, value);
67
             source.append(value).append("_");
75
             source.append(value).append("_");
68
         }
76
         }
69
         source.deleteCharAt(source.length() - 1);
77
         source.deleteCharAt(source.length() - 1);
70
         mapper.getSourceCache().put(source.toString(), id == null ? null : Long.valueOf(id.toString()));
78
         mapper.getSourceCache().put(source.toString(), id == null ? null : Long.valueOf(id.toString()));
71
     }
79
     }
72
 
80
 
81
+    /**
82
+     * 设置目标数据,联合主键
83
+     */
73
     public static void setTargetKey(Map<String, Object> map, FieldMapper mapper) {
84
     public static void setTargetKey(Map<String, Object> map, FieldMapper mapper) {
74
         StringBuffer target = new StringBuffer();
85
         StringBuffer target = new StringBuffer();
75
         Object id = map.get("id");
86
         Object id = map.get("id");
@@ -81,4 +92,18 @@ public class DataMigrationter {
81
         mapper.getTargetCache().put(target.toString(), id == null ? null : Long.valueOf(id.toString()));
92
         mapper.getTargetCache().put(target.toString(), id == null ? null : Long.valueOf(id.toString()));
82
     }
93
     }
83
 
94
 
95
+    /**
96
+     * 构建联合主键时,替换成新数据
97
+     */
98
+    private static Object getOprationReplaceValue(FieldMapper mapper, DataMapper dataMapper, Object value) {
99
+        for (DataMapper opMapper : mapper.getRowsOperationList()) {
100
+            //如果是要替换的,使用id缓存替换。
101
+            if (opMapper.isReplace() && opMapper.getSourceName().equals(dataMapper.getSourceName()) && value != null) {
102
+                Long valueId = opMapper.getIdCache().get(value);
103
+                return valueId;
104
+            }
105
+        }
106
+        return value;
107
+    }
108
+
84
 }
109
 }

+ 8 - 7
src/main/java/com/dashitech/migration/core/FieldMapper.java

@@ -55,16 +55,16 @@ public class FieldMapper {
55
                     }
55
                     }
56
                     //如果是新增,直接put
56
                     //如果是新增,直接put
57
                     if (dataMapper.isAdd()) {
57
                     if (dataMapper.isAdd()) {
58
-                        row.put(dataMapper.getSourceName(),dataMapper.getTargetValue());
58
+                        row.put(dataMapper.getSourceName(), dataMapper.getTargetValue());
59
                     }
59
                     }
60
                     //如果是要替换的,使用id缓存替换
60
                     //如果是要替换的,使用id缓存替换
61
                     if (dataMapper.isReplace() && row.containsKey(dataMapper.getSourceName())) {
61
                     if (dataMapper.isReplace() && row.containsKey(dataMapper.getSourceName())) {
62
                         Object value = row.get(dataMapper.getSourceName());
62
                         Object value = row.get(dataMapper.getSourceName());
63
-                        if(value != null) {
63
+                        if (value != null) {
64
                             Long id = dataMapper.getIdCache().get(value);
64
                             Long id = dataMapper.getIdCache().get(value);
65
-                            row.put(dataMapper.getSourceName(),id);
66
-                        }else {
67
-                            row.put(dataMapper.getSourceName(),null);
65
+                            row.put(dataMapper.getSourceName(), id);
66
+                        } else { //没有拿到数据,就置空
67
+                            row.put(dataMapper.getSourceName(), null);
68
                         }
68
                         }
69
                     }
69
                     }
70
                 }
70
                 }
@@ -122,14 +122,14 @@ public class FieldMapper {
122
     }
122
     }
123
 
123
 
124
     /**
124
     /**
125
-     * 待替换的字段。注意:name是旧source名称,不是新target名称,每次加入都在队列最前面
125
+     * 待替换的字段,每次加入都在队列最前面。注意:如果是loadCache过滤,使用target名称,如果是start转换,使用source名称
126
      */
126
      */
127
     public FieldMapper addOperationReplace(String name, Map<Long, Long> idCache) {
127
     public FieldMapper addOperationReplace(String name, Map<Long, Long> idCache) {
128
         DataMapper mapper = new DataMapper(name, name);
128
         DataMapper mapper = new DataMapper(name, name);
129
         mapper.setReplace(true);
129
         mapper.setReplace(true);
130
         mapper.setIdCache(idCache);
130
         mapper.setIdCache(idCache);
131
         //TODO 替换,必须放在第一个,避免被修改字段名称后,导致使用新名称的错误
131
         //TODO 替换,必须放在第一个,避免被修改字段名称后,导致使用新名称的错误
132
-        rowsOperationList.add(0,mapper);
132
+        rowsOperationList.add(0, mapper);
133
         return this;
133
         return this;
134
     }
134
     }
135
 
135
 
@@ -142,6 +142,7 @@ public class FieldMapper {
142
         this.targetCache.clear();
142
         this.targetCache.clear();
143
         this.idCache.clear();
143
         this.idCache.clear();
144
         this.rows.clear();
144
         this.rows.clear();
145
+        this.rowsOperationList.clear();
145
     }
146
     }
146
 
147
 
147
     public List<DataMapper> getFieldList() {
148
     public List<DataMapper> getFieldList() {

+ 13 - 11
src/main/java/com/dashitech/migration/task/uc/GroupTask.java

@@ -1,15 +1,16 @@
1
 package com.dashitech.migration.task.uc;
1
 package com.dashitech.migration.task.uc;
2
 
2
 
3
 import com.dashitech.migration.BaseTask;
3
 import com.dashitech.migration.BaseTask;
4
+import com.dashitech.migration.SqlTask;
4
 import com.dashitech.migration.core.DataMigrationter;
5
 import com.dashitech.migration.core.DataMigrationter;
5
 import com.dashitech.migration.core.FieldMapper;
6
 import com.dashitech.migration.core.FieldMapper;
6
 import com.dashitech.migration.util.JdbcUtil;
7
 import com.dashitech.migration.util.JdbcUtil;
7
 import org.apache.commons.logging.Log;
8
 import org.apache.commons.logging.Log;
8
 import org.apache.commons.logging.LogFactory;
9
 import org.apache.commons.logging.LogFactory;
10
+import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.stereotype.Component;
11
 import org.springframework.stereotype.Component;
10
 import org.springframework.transaction.annotation.Transactional;
12
 import org.springframework.transaction.annotation.Transactional;
11
 
13
 
12
-import java.util.HashMap;
13
 import java.util.List;
14
 import java.util.List;
14
 import java.util.Map;
15
 import java.util.Map;
15
 
16
 
@@ -21,9 +22,12 @@ public class GroupTask extends BaseTask {
21
 
22
 
22
     public static Log log = LogFactory.getLog(GroupTask.class);
23
     public static Log log = LogFactory.getLog(GroupTask.class);
23
 
24
 
25
+    @Autowired
26
+    SqlTask hospitalTask;
27
+
24
     @Override
28
     @Override
25
     @Transactional
29
     @Transactional
26
-    public FieldMapper start(FieldMapper fieldMapper) {
30
+    public void start() {
27
         log.info(" *** 工作组group转换开始 *** ");
31
         log.info(" *** 工作组group转换开始 *** ");
28
 
32
 
29
         StringBuffer sb = new StringBuffer();
33
         StringBuffer sb = new StringBuffer();
@@ -36,34 +40,32 @@ public class GroupTask extends BaseTask {
36
         mapper.addOperationRemove("id").addOperationRemove("duty_id");
40
         mapper.addOperationRemove("id").addOperationRemove("duty_id");
37
         mapper.addOperationUpdate("_hospital_id", "branch_id");
41
         mapper.addOperationUpdate("_hospital_id", "branch_id");
38
         mapper.addOperationAdd("type", 3);
42
         mapper.addOperationAdd("type", 3);
39
-        Map<Long, Long> hosIdCache = new HashMap<>();
40
-        hosIdCache.put(1L, 4L);
43
+        Map<Long, Long> hosIdCache = hospitalTask.loadCache().getIdCache();
41
         mapper.addOperationReplace("branch_id", hosIdCache);
44
         mapper.addOperationReplace("branch_id", hosIdCache);
42
         mapper.trans2JdbcRow(list);
45
         mapper.trans2JdbcRow(list);
43
 
46
 
44
         JdbcUtil jdbcUtil = new JdbcUtil(jdbcTemplateHsms);
47
         JdbcUtil jdbcUtil = new JdbcUtil(jdbcTemplateHsms);
45
         jdbcUtil.batchPageInsert("itsm_uc_groups", mapper.getRows());
48
         jdbcUtil.batchPageInsert("itsm_uc_groups", mapper.getRows());
46
 
49
 
47
-        //不需要缓存id的话,clear释放内存
50
+        //clear释放内存
48
         mapper.clear();
51
         mapper.clear();
49
-
50
-        mapper = loadCache();
51
-
52
-        return mapper;
53
     }
52
     }
54
 
53
 
55
     @Override
54
     @Override
56
     public FieldMapper loadCache() {
55
     public FieldMapper loadCache() {
57
         log.info(" *** 加载工作组group缓存 *** ");
56
         log.info(" *** 加载工作组group缓存 *** ");
58
 
57
 
59
-        String sql = " select id,groupname,delete_flag from itsm_uc_groups ";
58
+        String sql = " select id,groupname,delete_flag,_hospital_id from itsm_uc_groups ";
60
         List<Map<String, Object>> list = jdbcTemplateHsms.queryForList(sql);
59
         List<Map<String, Object>> list = jdbcTemplateHsms.queryForList(sql);
61
-        sql = " select id,groupname,delete_flag from itsm_uc_groups ";
60
+        sql = " select id,groupname,delete_flag,branch_id from itsm_uc_groups ";
62
         List<Map<String, Object>> list2 = jdbcTemplateMdv2.queryForList(sql);
61
         List<Map<String, Object>> list2 = jdbcTemplateMdv2.queryForList(sql);
63
 
62
 
64
         FieldMapper mapper = new FieldMapper();
63
         FieldMapper mapper = new FieldMapper();
65
         mapper.addFieldMapper("groupname", "groupname");
64
         mapper.addFieldMapper("groupname", "groupname");
66
         mapper.addFieldMapper("delete_flag", "delete_flag");
65
         mapper.addFieldMapper("delete_flag", "delete_flag");
66
+        mapper.addFieldMapper("_hospital_id", "branch_id");
67
+        Map<Long, Long> hosIdCache = hospitalTask.loadCache().getIdCache();
68
+        mapper.addOperationReplace("branch_id", hosIdCache);
67
 
69
 
68
         DataMigrationter.loadCache(list, list2, mapper);
70
         DataMigrationter.loadCache(list, list2, mapper);
69
 
71
 

+ 46 - 0
src/main/java/com/dashitech/migration/task/uc/HospitalTask.java

@@ -0,0 +1,46 @@
1
+package com.dashitech.migration.task.uc;
2
+
3
+import com.dashitech.migration.BaseTask;
4
+import com.dashitech.migration.SqlTask;
5
+import com.dashitech.migration.core.FieldMapper;
6
+import org.apache.commons.logging.Log;
7
+import org.apache.commons.logging.LogFactory;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.stereotype.Component;
10
+import org.springframework.transaction.annotation.Transactional;
11
+
12
+import java.util.HashMap;
13
+import java.util.List;
14
+import java.util.Map;
15
+
16
+/**
17
+ * Created by CX on 2024/10/10.
18
+ */
19
+@Component
20
+public class HospitalTask extends BaseTask {
21
+
22
+    public static Log log = LogFactory.getLog(HospitalTask.class);
23
+
24
+    @Override
25
+    @Transactional
26
+    public void start() {
27
+
28
+    }
29
+
30
+    @Override
31
+    public FieldMapper loadCache() {
32
+        log.info(" *** 加载院区Hospital缓存 *** ");
33
+
34
+        FieldMapper mapper = new FieldMapper();
35
+        Map<Long, Long> hosIdCache = new HashMap<>();
36
+        mapper.setIdCache(hosIdCache);
37
+
38
+        hosIdCache.put(1L, 4L); //总务科
39
+        hosIdCache.put(2L, 4L); //总务科
40
+        hosIdCache.put(3L, 5L); //信息科
41
+        hosIdCache.put(4L, 6L); //设备科
42
+
43
+
44
+        return mapper;
45
+    }
46
+}

+ 9 - 4
src/main/java/com/dashitech/migration/task/uc/UserTask.java

@@ -1,15 +1,15 @@
1
 package com.dashitech.migration.task.uc;
1
 package com.dashitech.migration.task.uc;
2
 
2
 
3
 import com.dashitech.migration.BaseTask;
3
 import com.dashitech.migration.BaseTask;
4
-import com.dashitech.migration.core.DataMigrationter;
4
+import com.dashitech.migration.SqlTask;
5
 import com.dashitech.migration.core.FieldMapper;
5
 import com.dashitech.migration.core.FieldMapper;
6
 import org.apache.commons.logging.Log;
6
 import org.apache.commons.logging.Log;
7
 import org.apache.commons.logging.LogFactory;
7
 import org.apache.commons.logging.LogFactory;
8
+import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.stereotype.Component;
9
 import org.springframework.stereotype.Component;
9
 import org.springframework.transaction.annotation.Transactional;
10
 import org.springframework.transaction.annotation.Transactional;
10
 
11
 
11
 import java.util.List;
12
 import java.util.List;
12
-import java.util.Map;
13
 
13
 
14
 /**
14
 /**
15
  * Created by CX on 2024/10/10.
15
  * Created by CX on 2024/10/10.
@@ -17,11 +17,14 @@ import java.util.Map;
17
 @Component
17
 @Component
18
 public class UserTask extends BaseTask {
18
 public class UserTask extends BaseTask {
19
 
19
 
20
+    @Autowired
21
+    SqlTask groupTask;
22
+
20
     public static Log log = LogFactory.getLog(UserTask.class);
23
     public static Log log = LogFactory.getLog(UserTask.class);
21
 
24
 
22
     @Override
25
     @Override
23
     @Transactional
26
     @Transactional
24
-    public FieldMapper start(FieldMapper fieldMapper) {
27
+    public void start() {
25
         log.info(" *** 用户user转换开始 *** ");
28
         log.info(" *** 用户user转换开始 *** ");
26
 
29
 
27
         StringBuffer sb = new StringBuffer();
30
         StringBuffer sb = new StringBuffer();
@@ -29,8 +32,10 @@ public class UserTask extends BaseTask {
29
 
32
 
30
         List list = jdbcTemplateHsms.queryForList(sb.toString());
33
         List list = jdbcTemplateHsms.queryForList(sb.toString());
31
 
34
 
35
+        FieldMapper groupMapper = groupTask.loadCache();
36
+        System.out.println(groupMapper.getIdCache());
37
+
32
 
38
 
33
-        return fieldMapper;
34
     }
39
     }
35
 
40
 
36
     @Override
41
     @Override

+ 1 - 1
src/main/resources/start.bat

@@ -1,5 +1,5 @@
1
 echo "start ing"
1
 echo "start ing"
2
 
2
 
3
-java -Xmx512m -XX:MaxPermSize=256m -jar C:/Users/Administrator/Desktop/hsmsV1-DataService-1.0.0.jar --server.port=29999 &
3
+java -Xmx512m -XX:MaxPermSize=256m -jar C:/Users/Administrator/Desktop/hsmsV1-DataMigration-1.0.0.jar --server.port=29999 &
4
 
4
 
5
 echo "start end"
5
 echo "start end"