浏览代码

完善工具

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

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

@@ -8,9 +8,9 @@ import com.dashitech.migration.core.FieldMapper;
8 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 1
 package com.dashitech.migration;
2 2
 
3
-import com.dashitech.migration.core.FieldMapper;
4 3
 import org.apache.commons.logging.Log;
5 4
 import org.apache.commons.logging.LogFactory;
6 5
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,12 +17,20 @@ public class StartApplication {
18 17
     @Autowired
19 18
     SqlTask groupTask;
20 19
 
20
+    @Autowired
21
+    SqlTask userTask;
22
+
21 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 10
 public class DataMapper implements Serializable{
11 11
 
12
+    //目标
12 13
     private String targetName;
13 14
     private Object targetValue;
14 15
     private Long targetId;
16
+
17
+    //源
15 18
     private String sourceName;
16 19
     private Object sourceValue;
17 20
     private Long sourceId;
21
+
22
+    //操作类型
18 23
     private boolean remove = false;
19 24
     private boolean update = false;
20 25
     private boolean add = false;
21 26
     private boolean replace = false;
27
+
22 28
     /**
23 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 14
 public class DataMigrationter {
15 15
     public static Log log = LogFactory.getLog(DataMigrationter.class);
16 16
 
17
+    /**
18
+     * 开始匹配数据,并缓存到mapper的idCache里面
19
+     */
17 20
     public static void loadCache(List<Map<String, Object>> targetList, List<Map<String, Object>> sourceList, FieldMapper mapper) {
18 21
         mapperData(targetList, sourceList, mapper);
19 22
         trans2IdCache(mapper);
@@ -51,7 +54,7 @@ public class DataMigrationter {
51 54
             Long targetId = entry.getValue();
52 55
             if (sourceId != null && targetId != null) {
53 56
                 cache.put(sourceId, targetId);
54
-            }else if(sourceId != null){
57
+            } else if (sourceId != null) {
55 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 68
     public static void setSourceKey(Map<String, Object> map, FieldMapper mapper) {
63 69
         StringBuffer source = new StringBuffer();
64 70
         Object id = map.get("id");
65 71
         for (DataMapper dataMapper : mapper.getFieldList()) {
66 72
             Object value = map.get(dataMapper.getSourceName());
73
+            //可能构建联合主键时,需要替换成新数据
74
+            value = getOprationReplaceValue(mapper, dataMapper, value);
67 75
             source.append(value).append("_");
68 76
         }
69 77
         source.deleteCharAt(source.length() - 1);
70 78
         mapper.getSourceCache().put(source.toString(), id == null ? null : Long.valueOf(id.toString()));
71 79
     }
72 80
 
81
+    /**
82
+     * 设置目标数据,联合主键
83
+     */
73 84
     public static void setTargetKey(Map<String, Object> map, FieldMapper mapper) {
74 85
         StringBuffer target = new StringBuffer();
75 86
         Object id = map.get("id");
@@ -81,4 +92,18 @@ public class DataMigrationter {
81 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 56
                     //如果是新增,直接put
57 57
                     if (dataMapper.isAdd()) {
58
-                        row.put(dataMapper.getSourceName(),dataMapper.getTargetValue());
58
+                        row.put(dataMapper.getSourceName(), dataMapper.getTargetValue());
59 59
                     }
60 60
                     //如果是要替换的,使用id缓存替换
61 61
                     if (dataMapper.isReplace() && row.containsKey(dataMapper.getSourceName())) {
62 62
                         Object value = row.get(dataMapper.getSourceName());
63
-                        if(value != null) {
63
+                        if (value != null) {
64 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 127
     public FieldMapper addOperationReplace(String name, Map<Long, Long> idCache) {
128 128
         DataMapper mapper = new DataMapper(name, name);
129 129
         mapper.setReplace(true);
130 130
         mapper.setIdCache(idCache);
131 131
         //TODO 替换,必须放在第一个,避免被修改字段名称后,导致使用新名称的错误
132
-        rowsOperationList.add(0,mapper);
132
+        rowsOperationList.add(0, mapper);
133 133
         return this;
134 134
     }
135 135
 
@@ -142,6 +142,7 @@ public class FieldMapper {
142 142
         this.targetCache.clear();
143 143
         this.idCache.clear();
144 144
         this.rows.clear();
145
+        this.rowsOperationList.clear();
145 146
     }
146 147
 
147 148
     public List<DataMapper> getFieldList() {

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

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

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

@@ -1,5 +1,5 @@
1 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 5
 echo "start end"