Browse Source

增加了修改对比功能

CX 5 months ago
parent
commit
4e3cf81f56

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

@@ -43,10 +43,11 @@ public class StartApplication {
43 43
             //xyGroupTask.start();
44 44
         } else {
45 45
 
46
-            companyTask.start();
47
-            FieldMapper mapper = companyTask.loadCache();
46
+//            companyTask.start();
48 47
 
49
-            System.out.println(mapper);
48
+            userTask.start();
49
+
50
+            System.out.println("AAAAA");
50 51
         }
51 52
 
52 53
 

+ 35 - 0
src/main/java/com/dashitech/migration/core/DataMigrationter.java

@@ -1,5 +1,6 @@
1 1
 package com.dashitech.migration.core;
2 2
 
3
+import com.dashitech.migration.util.JdbcRow;
3 4
 import org.apache.commons.logging.Log;
4 5
 import org.apache.commons.logging.LogFactory;
5 6
 
@@ -15,6 +16,40 @@ public class DataMigrationter {
15 16
     public static Log log = LogFactory.getLog(DataMigrationter.class);
16 17
 
17 18
     /**
19
+     * 根据loadCache的数据,区分是新增,还是修改
20
+     */
21
+    public static RowsWrap getRowsWrap(FieldMapper dataMapper,FieldMapper cacheMapper) {
22
+        RowsWrap rw = new RowsWrap();
23
+        List<JdbcRow> rows = dataMapper.getRows();
24
+        Map<String, Long> targetCache = cacheMapper.getTargetCache();
25
+        if(rows.size() > 0) {
26
+            for (JdbcRow jdbcRow : rows) {
27
+                Map<String, Object> row = jdbcRow.getRow();
28
+
29
+                //拼接联合主键,判断是更新,还是新增
30
+                StringBuffer key = new StringBuffer();
31
+                for (DataMapper fieldMapper : cacheMapper.getFieldList()) {
32
+                    Object value = row.get(fieldMapper.getSourceName());
33
+                    //可能构建联合主键时,需要替换成新数据
34
+                    value = getOprationReplaceValue(dataMapper, fieldMapper, value);
35
+                    key.append(value).append("_");
36
+                }
37
+                key.deleteCharAt(key.length() - 1);
38
+
39
+                //如果不存在是新增,如果存在,是修改
40
+                Object value = targetCache.get(key.toString());
41
+                if(value == null) {
42
+                    rw.addInsertRow(jdbcRow);
43
+                }else {
44
+                    jdbcRow.setWhere("id", Long.valueOf(value.toString()));
45
+                    rw.addUpdateRow(jdbcRow);
46
+                }
47
+            }
48
+        }
49
+        return rw;
50
+    }
51
+
52
+    /**
18 53
      * 开始匹配数据,并缓存到mapper的idCache里面
19 54
      */
20 55
     public static void loadCache(List<Map<String, Object>> targetList, List<Map<String, Object>> sourceList, FieldMapper mapper) {

+ 2 - 0
src/main/java/com/dashitech/migration/core/FieldMapper.java

@@ -69,6 +69,8 @@ public class FieldMapper {
69 69
                     }
70 70
                 }
71 71
 
72
+                //判重复
73
+
72 74
                 JdbcRow jdbcRow = row2JdbcRow(row);
73 75
                 rows.add(jdbcRow);
74 76
             }

+ 41 - 0
src/main/java/com/dashitech/migration/core/RowsWrap.java

@@ -0,0 +1,41 @@
1
+package com.dashitech.migration.core;
2
+
3
+import com.dashitech.migration.util.JdbcRow;
4
+
5
+import java.io.Serializable;
6
+import java.util.ArrayList;
7
+import java.util.HashMap;
8
+import java.util.List;
9
+import java.util.Map;
10
+
11
+/**
12
+ */
13
+public class RowsWrap {
14
+
15
+    private List<JdbcRow> insertRows = new ArrayList<>();
16
+    private List<JdbcRow> updateRows = new ArrayList<>();
17
+
18
+    public void addInsertRow(JdbcRow row) {
19
+        insertRows.add(row);
20
+    }
21
+
22
+    public void addUpdateRow(JdbcRow row) {
23
+        updateRows.add(row);
24
+    }
25
+
26
+    public List<JdbcRow> getInsertRows() {
27
+        return insertRows;
28
+    }
29
+
30
+    public void setInsertRows(List<JdbcRow> insertRows) {
31
+        this.insertRows = insertRows;
32
+    }
33
+
34
+    public List<JdbcRow> getUpdateRows() {
35
+        return updateRows;
36
+    }
37
+
38
+    public void setUpdateRows(List<JdbcRow> updateRows) {
39
+        this.updateRows = updateRows;
40
+    }
41
+}

+ 2 - 0
src/main/java/com/dashitech/migration/task/other/DictionaryTask.java

@@ -58,6 +58,8 @@ public class DictionaryTask extends BaseTask {
58 58
         //公司分类
59 59
         idCache.put(1709L,20309L);
60 60
         idCache.put(1710L,20311L);
61
+        idCache.put(279L,1609L);
62
+        idCache.put(280L,1610L);
61 63
 
62 64
 
63 65
         return mapper;

+ 3 - 1
src/main/java/com/dashitech/migration/task/uc/CompanyTask.java

@@ -4,6 +4,7 @@ import com.dashitech.migration.BaseTask;
4 4
 import com.dashitech.migration.SqlTask;
5 5
 import com.dashitech.migration.core.DataMigrationter;
6 6
 import com.dashitech.migration.core.FieldMapper;
7
+import com.dashitech.migration.core.RowsWrap;
7 8
 import com.dashitech.migration.task.other.DictionaryTask;
8 9
 import com.dashitech.migration.util.JdbcUtil;
9 10
 import org.apache.commons.logging.Log;
@@ -29,13 +30,14 @@ public class CompanyTask extends BaseTask {
29 30
     SqlTask dictionaryTask;
30 31
 
31 32
 
33
+
32 34
     @Override
33 35
     @Transactional
34 36
     public void start() {
35 37
         log.info(" *** 三方公司CompanyTask转换开始 *** ");
36 38
 
37 39
         StringBuffer sb = new StringBuffer();
38
-        sb.append(" select name,contacts,mphone,service_type_ids as 'company_type_ids', ");
40
+        sb.append(" select version,name,contacts,mphone,service_type_ids as 'company_type_ids', ");
39 41
         sb.append(" delete_flag,add_time,branch_id as 'hos_id' ");
40 42
         sb.append(" from itsm_uc_company ");
41 43
 

+ 66 - 0
src/main/java/com/dashitech/migration/task/uc/UserGroupTask.java

@@ -0,0 +1,66 @@
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.DataMigrationter;
6
+import com.dashitech.migration.core.FieldMapper;
7
+import com.dashitech.migration.core.RowsWrap;
8
+import com.dashitech.migration.util.JdbcUtil;
9
+import org.apache.commons.logging.Log;
10
+import org.apache.commons.logging.LogFactory;
11
+import org.springframework.beans.factory.annotation.Autowired;
12
+import org.springframework.stereotype.Component;
13
+import org.springframework.transaction.annotation.Transactional;
14
+
15
+import java.util.List;
16
+import java.util.Map;
17
+
18
+/**
19
+ * Created by CX on 2024/10/10.
20
+ */
21
+@Component
22
+public class UserGroupTask extends BaseTask {
23
+
24
+    @Autowired
25
+    SqlTask groupTask;
26
+
27
+    @Autowired
28
+    SqlTask userTask;
29
+
30
+    public static Log log = LogFactory.getLog(UserGroupTask.class);
31
+
32
+    @Override
33
+    @Transactional
34
+    public void start() {
35
+        log.info(" *** 用户user转换开始 *** ");
36
+
37
+        StringBuffer sb = new StringBuffer();
38
+        sb.append(" select * from itsm_uc_user ");
39
+
40
+        List list = jdbcTemplateHsms.queryForList(sb.toString());
41
+
42
+        FieldMapper mapper = new FieldMapper();
43
+
44
+        FieldMapper groupMapper = groupTask.loadCache();
45
+        mapper.addOperationReplace("branch_id", userTask.loadCache().getIdCache());
46
+
47
+        FieldMapper userMapper = loadCache();
48
+        RowsWrap rw = DataMigrationter.getRowsWrap(mapper,userMapper);
49
+        JdbcUtil jdbcUtil = new JdbcUtil(jdbcTemplateHsms);
50
+        jdbcUtil.batchPageInsert("itsm_uc_user", rw.getInsertRows());
51
+        jdbcUtil.batchPageUpdate("itsm_uc_user", rw.getUpdateRows());
52
+
53
+        //clear释放内存
54
+        mapper.clear();
55
+        groupMapper.clear();
56
+        userMapper.clear();
57
+    }
58
+
59
+    @Override
60
+    public FieldMapper loadCache() {
61
+
62
+
63
+
64
+        return null;
65
+    }
66
+}

+ 25 - 13
src/main/java/com/dashitech/migration/task/uc/UserTask.java

@@ -4,6 +4,8 @@ import com.dashitech.migration.BaseTask;
4 4
 import com.dashitech.migration.SqlTask;
5 5
 import com.dashitech.migration.core.DataMigrationter;
6 6
 import com.dashitech.migration.core.FieldMapper;
7
+import com.dashitech.migration.core.RowsWrap;
8
+import com.dashitech.migration.util.JdbcUtil;
7 9
 import org.apache.commons.logging.Log;
8 10
 import org.apache.commons.logging.LogFactory;
9 11
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,10 +22,7 @@ import java.util.Map;
20 22
 public class UserTask extends BaseTask {
21 23
 
22 24
     @Autowired
23
-    SqlTask groupTask;
24
-
25
-    @Autowired
26
-    HospitalTask hospitalTask;
25
+    SqlTask hospitalTask;
27 26
 
28 27
     public static Log log = LogFactory.getLog(UserTask.class);
29 28
 
@@ -33,24 +32,37 @@ public class UserTask extends BaseTask {
33 32
         log.info(" *** 用户user转换开始 *** ");
34 33
 
35 34
         StringBuffer sb = new StringBuffer();
36
-        sb.append(" select * from itsm_uc_user ");
37
-
38
-        List list = jdbcTemplateHsms.queryForList(sb.toString());
39
-
40
-        FieldMapper groupMapper = groupTask.loadCache();
41
-        Map<Long, Long> hosIdCache = hospitalTask.loadXiangyangCache().getIdCache();
42
-        System.out.println(hosIdCache);
35
+        sb.append(" select version,ACCOUNT,GENDER,NAME,PHONE,PWD,WEIXIN,DEPTID,delete_flag ");
36
+        sb.append(" pinyin,py as 'input_code',online,branch_id as 'hospital_id', ");
37
+        sb.append(" duty_id as 'duty_dept',company as 'company_id',engineer ");
38
+        sb.append(" from itsm_uc_user ");
43 39
 
40
+        List list = jdbcTemplateMdv2.queryForList(sb.toString());
44 41
 
42
+        FieldMapper mapper = new FieldMapper();
43
+        mapper.addOperationAdd("usertype", 20343L); //用户类型-默认三方人员
44
+        mapper.addOperationReplace("hospital_id", hospitalTask.loadCache().getIdCache());
45
+        mapper.addOperationReplace("duty_dept", hospitalTask.loadCache().getIdCache());
46
+        mapper.trans2JdbcRow(list);
47
+
48
+        FieldMapper userMapper = loadCache();
49
+        RowsWrap rw = DataMigrationter.getRowsWrap(mapper,userMapper);
50
+        JdbcUtil jdbcUtil = new JdbcUtil(jdbcTemplateHsms);
51
+        jdbcUtil.batchPageInsert("itsm_uc_user", rw.getInsertRows());
52
+        jdbcUtil.batchPageUpdate("itsm_uc_user", rw.getUpdateRows());
53
+
54
+        //clear释放内存
55
+        mapper.clear();
56
+        userMapper.clear();
45 57
     }
46 58
 
47 59
     @Override
48 60
     public FieldMapper loadCache() {
49 61
         log.info(" *** 加载用户user缓存 *** ");
50 62
 
51
-        String sql = " select id,ACCOUNT,delete_flag from itsm_uc_groups ";
63
+        String sql = " select id,ACCOUNT,delete_flag from itsm_uc_user ";
52 64
         List<Map<String, Object>> list = jdbcTemplateHsms.queryForList(sql);
53
-        sql = " select id,ACCOUNT,delete_flag from itsm_uc_groups ";
65
+        sql = " select id,ACCOUNT,delete_flag from itsm_uc_user ";
54 66
         List<Map<String, Object>> list2 = jdbcTemplateMdv2.queryForList(sql);
55 67
 
56 68
         FieldMapper mapper = new FieldMapper();

+ 2 - 2
src/main/resources/application.properties

@@ -47,12 +47,12 @@ spring.redis.pool.max-wait-millis=10000
47 47
 spring.profiles.active=custom
48 48
 spring.redis.topic=hsms-data-service
49 49
 
50
-server.port=29999
50
+server.port=39999
51 51
 spring.jps.showsql=true
52 52
 server.tomcat.compression=on
53 53
 
54 54
 spring.jackson.parser.allow-numeric-leading-zeros=true
55 55
 
56 56
 #\u5F53\u524D\u73AF\u5883\u540D\u79F0
57
-application.name=xiangyang
57
+application.name=nanjing
58 58