From 1ffbaa7b82f99443d933b0f7d0add0b35d2db01c Mon Sep 17 00:00:00 2001
From: wangrenhuang <wangren_huang@163.com>
Date: 星期三, 02 三月 2022 14:26:53 +0800
Subject: [PATCH] PTEncryptionUti新增解密方法

---
 src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java |  218 +++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 150 insertions(+), 68 deletions(-)

diff --git a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
index 2c5abcc..d1648b3 100644
--- a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
+++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
@@ -12,10 +12,9 @@
 
 import javax.annotation.PostConstruct;
 import java.lang.reflect.Field;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 public class InitMysqlData {
 
@@ -28,81 +27,164 @@
         //椤圭洰鍚姩鐨勬椂鍊欏~鍏�
         if(!StringUtils.isEmpty(packPath)){
             Set<Class<?>> classes = VariableAesKey.classData(packPath);
-            Map<String,String> aesKeysFild = new HashMap<>();
-            boolean isAes = false;
-            String tableName = null;
-            String fildName = null;
-            String fildValue = null;
 
             SqlSentence sqlSentence = new SqlSentence();
             Map<String,Object> values = new HashMap<>();
 
-            for(Class<?> cl:classes){
-                //琛ㄥ悕绉�
-                boolean hasAnnotation = cl.isAnnotationPresent(Table.class);
-                if(!hasAnnotation){
-                    continue;
-                }
-                Table table = cl.getAnnotation(Table.class);
-                tableName = table.name();
+            //瑙f瀽琛ㄦ暟鎹�
+            List<FieldData> fieldDatas = entityhandle(classes);
 
-                aesKeysFild = new HashMap<>();
-                isAes = false;
-
-                // 鍙栧緱鏈被鐨勫叏閮ㄥ睘鎬�
-                Field[] fields = cl.getDeclaredFields();
-                fields = VariableAesKey.getPatentFields(fields,cl);
-                for (Field field:fields) {
-                    fildName = null;
-                    // 鍒ゆ柇鏂规硶涓槸鍚︽湁鎸囧畾娉ㄨВ绫诲瀷鐨勬敞瑙�
-                    hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class);
-                    if (hasAnnotation) {
-                        // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙�
-                        MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
-
-                        //String aesKeyField = mysqlHexAes.aesKeyField();
-                        String aesKey = mysqlHexAes.aesKey();
-
-                        if(StringUtils.isEmpty(aesKey)){
-                            aesKey = VariableAesKey.AES_KEY;
-                            if(StringUtils.isEmpty(aesKey)){
-                                throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName());
+            int pageSize = 500;
+            //鍒涘缓涓�涓畾闀跨嚎绋嬫睜锛屽彲鎺у埗绾跨▼鏈�澶у苟鍙戞暟锛岃秴鍑虹殑绾跨▼浼氬湪闃熷垪涓瓑寰呫��
+            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
+            try{
+                for(FieldData fieldData:fieldDatas){
+                    //鑾峰彇鏉℃暟
+                    sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values);
+                    int total = commonService.selectCountSql(sqlSentence);
+                    if(total ==0 ){
+                        continue;
+                    }
+                    int pageTotal = total/pageSize+1;
+                    for(int i= 0;i<pageTotal;i++){
+                        int finalI = i;
+                        fixedThreadPool.execute(new Runnable() {
+                            @Override
+                            public void run() {
+                                //鏇存柊鏁版嵁
+                                updateData(fieldData,commonService, finalI,pageSize);
                             }
-                        }
-
-                        hasAnnotation = field.isAnnotationPresent(Column.class);
-                        if(hasAnnotation){
-                            Column column = field.getAnnotation(Column.class);
-                            fildName = column.name();
-                        }
-                        if(StringUtils.isEmpty(fildName)){
-                            fildName = field.getName();
-                        }
-
-                        sqlSentence.sqlSentence("SELECT id,"+fildName+" FROM "+tableName,values);
-                        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
-                        for(Map<String,Object> map:list){
-                            fildValue = (String)map.get(fildName);
-                            System.out.println("fildValue:"+fildValue);
-                            if(StringUtils.isEmpty(fildValue)){
-                                continue;
-                            }
-                            if(fildValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(fildValue)){
-                                continue;
-                            }
-                            values.clear();
-                            values.put("id",map.get("id"));
-                            values.put("filedData",fildValue);
-                            sqlSentence.sqlSentence("UPDATE "+tableName+" SET "+fildName+" = #{m.filedData} WHERE id = #{m.id}",values);
-                            if(commonService.updateSentence(sqlSentence)!=1){
-                                throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�");
-                            }
-                        }
+                        });
                     }
                 }
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                fixedThreadPool.shutdown();
             }
         }
     }
 
+    /**鏇存柊鏁版嵁*/
+    public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
+
+        //鏌ヨ鏁版嵁
+        StringBuilder selectField = new StringBuilder();
+        selectField.append(fieldData.getId());
+        for(String fieldName:fieldData.getEncrypFields()){
+            selectField.append(","+fieldName);
+        }
+        sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values);
+        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
+
+        for (Map<String,Object> map:list){
+            StringBuilder setField = new StringBuilder();
+            for (Map.Entry<String, Object> entry : map.entrySet()) {
+                String mapKey = entry.getKey();
+                String mapValue = (String) entry.getValue();
+                if(StringUtils.isEmpty(mapValue)){
+                    continue;
+                }
+                if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){
+                    continue;
+                }
+                if(fieldData.getId().equals(mapKey)){
+                    continue;
+                }
+                if(setField.length()>0){
+                    setField.append(",");
+                }
+                setField.append(mapKey+" = #{m."+mapKey+"}");
+            }
+
+            values = map;
+            sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values);
+            if(commonService.updateSentence(sqlSentence)!=1){
+                throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�");
+            }
+        }
+
+    }
+
+    /**鑾峰彇鍒拌〃鐨勬暟鎹�*/
+    public static List<FieldData> entityhandle(Set<Class<?>> classes){
+
+        List<FieldData> fildDatas = new ArrayList<>();
+        //瀛樺偍鍗曡〃瀛楁淇℃伅
+        FieldData fildData;
+        //瀛樺偍闇�瑕佸姞瀵嗙殑瀛楁
+        Set<String> encrypFields;
+
+        String fildName;
+
+        for(Class<?> cl:classes){
+            fildData = new FieldData();
+            encrypFields = new HashSet<>();
+
+            //琛ㄥ悕绉�
+            if(!cl.isAnnotationPresent(Table.class)){
+                continue;
+            }
+            Table table = cl.getAnnotation(Table.class);
+            fildData.setTableName(table.name());
+
+            // 鍙栧緱鏈被鐨勫叏閮ㄥ睘鎬�
+            Field[] fields = cl.getDeclaredFields();
+            fields = VariableAesKey.getPatentFields(fields,cl);
+            for (Field field:fields) {
+
+                fildName = null;
+                if(field.isAnnotationPresent(Column.class)){
+                    Column column = field.getAnnotation(Column.class);
+                    fildName = column.name();
+                    if(StringUtils.isEmpty(fildName)){
+                        fildName = field.getName();
+                    }
+                    if(column.isKey()){
+                        fildData.setId(fildName);
+                    }
+                }else{
+                    fildName = field.getName();
+                }
+
+                // 鍒ゆ柇鏂规硶涓槸鍚︽湁鎸囧畾娉ㄨВ绫诲瀷鐨勬敞瑙�
+                if (!field.isAnnotationPresent(MysqlHexAes.class)) {
+                    continue;
+                }
+                // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙�
+                MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
+                //鍒ゆ柇鐗堟湰鍙锋槸涓嶆槸涓�鏍风殑
+                if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){
+                    if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){
+                        continue;
+                    }
+                }else{
+                    if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){
+                        continue;
+                    }
+                }
+
+                String aesKey = mysqlHexAes.aesKey();
+                if(StringUtils.isEmpty(aesKey)){
+                    aesKey = VariableAesKey.AES_KEY;
+                    if(StringUtils.isEmpty(aesKey)){
+                        throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName());
+                    }
+                }
+
+                encrypFields.add(fildName);
+            }
+            //鏄惁鏈夐渶瑕佸姞瀵嗗緱瀛楁
+            if(encrypFields.size()<=0){
+                continue;
+            }
+            fildData.setEncrypFields(encrypFields);
+            fildDatas.add(fildData);
+        }
+        return fildDatas;
+    }
+
 
 }

--
Gitblit v1.8.0