diff --git a/src/main/java/com/peanut/common/utils/DataMigrationUtil.java b/src/main/java/com/peanut/common/utils/DataMigrationUtil.java index bb8e69d1..72dc8b97 100644 --- a/src/main/java/com/peanut/common/utils/DataMigrationUtil.java +++ b/src/main/java/com/peanut/common/utils/DataMigrationUtil.java @@ -6,9 +6,18 @@ import org.apache.commons.lang.time.DateUtils; import java.sql.*; import java.util.*; import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; -public class DataMigrationUtil { +public class DataMigrationUtil extends Thread { + Map map; + int i; + + public DataMigrationUtil(Map map,int i){ + this.map = map; + this.i = i; + } public static void main(String[] args){ @@ -16,7 +25,7 @@ public class DataMigrationUtil { // courseCatalogue(); // courseCatalogueChapter(); // courseCatalogueChapterVideo(); - user(); + user();//用的都是copy表里的数据 } @@ -305,9 +314,6 @@ public class DataMigrationUtil { public static void user(){ try { - Connection fzdsconn = DriverManager.getConnection( - "jdbc:mysql://rm-2zev4157t67trxuu3yo.mysql.rds.aliyuncs.com:3306/e_book_test?rewriteBatchedStatements=true", - "nuttyreading", "Wu751019!"); Connection yljkconn = DriverManager.getConnection( "jdbc:mysql://goldorchid.mysql.rds.aliyuncs.com:3309/everhealth?", "yljkmaster", "Wu751019!@"); @@ -331,81 +337,88 @@ public class DataMigrationUtil { } resultSet.close(); statement.close(); - int q = 0; - int w = 0; - int e = 0; - int r = 0; + // 1、创建服务,创建线程池 + ExecutorService service = Executors.newFixedThreadPool(200); + for (int i=1;i<=list.size(); i++){ + DataMigrationUtil d1 = new DataMigrationUtil(list.get(i),i); + service.execute(d1); + } + } catch (SQLException e) { + System.out.println("Error: " + e.getMessage()); + } + } + + @Override + public void run(){ + try { + if (i % 1000 == 0) { + System.out.println("---已插入数据"+i); + } + String userId = ""; + Connection fzdsconn = DriverManager.getConnection( + "jdbc:mysql://rm-2zev4157t67trxuu3yo.mysql.rds.aliyuncs.com:3306/e_book_test?rewriteBatchedStatements=true", + "nuttyreading", "Wu751019!"); PreparedStatement telInsertStatement = fzdsconn.prepareStatement( "INSERT ignore INTO user_copy1 (name,avatar,nickname,tel,vip,peanut_coin,jf,yljk_oid) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); PreparedStatement userVipStatement = fzdsconn.prepareStatement( "INSERT ignore INTO user_vip_copy1 (user_id,type,start_time,end_time,state) VALUES (?, ?, ?, ?, ?)"); - for (int i=1;i<=list.size(); i++){ - String userId = ""; - System.out.println(i+"---"+list.get(i).get("nameCN")); - //已绑定过 - PreparedStatement oidStatements = fzdsconn.prepareStatement("select * from user_copy1 where del_flag = 0 and yljk_oid = '"+list.get(i).get("oid")+"' "); - ResultSet oidResultSet = oidStatements.executeQuery(); - if (oidResultSet.next()){ - String sql = "update user_copy1 set " + - "name = if(isnull(name),'"+list.get(i).get("nameCN")+"',name) , nickname = if(isnull(nickname),'"+list.get(i).get("nickName")+"',nickname) ," + - "tel = if(isnull(tel),'"+list.get(i).get("cellPhone")+"',tel) ," + - "vip= "+list.get(i).get("payStatus")+" ,peanut_coin = peanut_coin + "+list.get(i).get("point")+" , jf = jf + "+list.get(i).get("pointByJF")+" where id = "+oidResultSet.getString("id")+""; - PreparedStatement oidUpdateStatement = fzdsconn.prepareStatement(sql); - userId = oidResultSet.getString("id"); - oidUpdateStatement.executeBatch(); - }else { - //未绑定过,用电话查询 - PreparedStatement telStatements = fzdsconn.prepareStatement("select * from user_copy1 where del_flag = 0 and tel = '"+list.get(i).get("cellPhone")+"' "); - ResultSet telResultSet = telStatements.executeQuery(); - if (telResultSet.next()){ - String sql = "update user_copy1 set yljk_oid = '" + list.get(i).get("oid") +"',"+ - " name = if(isnull(name),'"+list.get(i).get("nameCN")+"',name) , nickname = if(isnull(nickname),'"+list.get(i).get("nickName")+"',nickname) ," + - "vip= "+list.get(i).get("payStatus")+" ,peanut_coin = peanut_coin + "+list.get(i).get("point")+" , jf = jf + "+list.get(i).get("pointByJF")+" where tel = '"+list.get(i).get("cellPhone")+"'"; + PreparedStatement oidStatements = fzdsconn.prepareStatement("select * from user_copy1 where del_flag = 0 and yljk_oid = '"+map.get("oid")+"' "); + ResultSet oidResultSet = oidStatements.executeQuery(); + //已绑定过id + if (oidResultSet.next()){ + String sql = "update user_copy1 set " + + "name = if(isnull(name),'"+map.get("nameCN")+"',name) , nickname = if(isnull(nickname),'"+map.get("nickName")+"',nickname) ," + + "tel = if(isnull(tel),'"+map.get("cellPhone")+"',tel) ," + + "vip= "+map.get("payStatus")+" ,peanut_coin = peanut_coin + "+map.get("point")+" , jf = jf + "+map.get("pointByJF")+" where id = "+oidResultSet.getString("id")+""; + PreparedStatement oidUpdateStatement = fzdsconn.prepareStatement(sql); + userId = oidResultSet.getString("id"); + oidUpdateStatement.execute(); + }else { + //未绑定过id,用电话查询 + PreparedStatement telStatements = fzdsconn.prepareStatement("select * from user_copy1 where del_flag = 0 and tel = '"+map.get("cellPhone")+"' "); + ResultSet telResultSet = telStatements.executeQuery(); + if (telResultSet.next()){ + if (telResultSet.getRow()>1){ + System.out.println(map.get("oid").toString()+"---"+map.get("nameCN").toString()+"有多账号"); + }else { + String sql = "update user_copy1 set yljk_oid = '" + map.get("oid") +"',"+ + " name = if(isnull(name),'"+map.get("nameCN")+"',name) , nickname = if(isnull(nickname),'"+map.get("nickName")+"',nickname) ," + + "vip= "+map.get("payStatus")+" ,peanut_coin = peanut_coin + "+map.get("point")+" , jf = jf + "+map.get("pointByJF")+" where tel = '"+map.get("cellPhone")+"'"; PreparedStatement telUpdateStatement = fzdsconn.prepareStatement(sql); userId = telResultSet.getString("id"); - telUpdateStatement.executeBatch(); - }else { - telInsertStatement.setString(1,list.get(i).get("nameCN")==null?"":list.get(i).get("nameCN").toString()); - telInsertStatement.setString(2,list.get(i).get("icons")==null?"":list.get(i).get("icons").toString()); - telInsertStatement.setString(3,list.get(i).get("nickName")==null?"":list.get(i).get("nickName").toString()); - telInsertStatement.setString(4,list.get(i).get("cellPhone")==null?"":list.get(i).get("cellPhone").toString()); - telInsertStatement.setString(5,list.get(i).get("payStatus")==null?"":list.get(i).get("payStatus").toString()); - telInsertStatement.setString(6,list.get(i).get("point")==null?"":list.get(i).get("point").toString()); - telInsertStatement.setString(7,list.get(i).get("pointByJF")==null?"":list.get(i).get("pointByJF").toString()); - telInsertStatement.setString(8,list.get(i).get("oid")==null?"":list.get(i).get("oid").toString()); - // 添加到批处理中 - telInsertStatement.addBatch(); - e++; - if (e % 1000 == 0) { - telInsertStatement.executeBatch(); - } - } - } - if (list.get(i).get("paydate")!=null&&list.get(i).get("payValidDate")!=null){ - userVipStatement.setString(1,userId); - userVipStatement.setString(2,list.get(i).get("payStatus").toString()); - userVipStatement.setString(3,list.get(i).get("paydate").toString()); - userVipStatement.setString(4,list.get(i).get("payValidDate").toString()); - if (DateUtil.compare(DateUtil.parseDate(list.get(i).get("payValidDate").toString()),new Date())>0){ - userVipStatement.setString(5,"0"); - }else { - userVipStatement.setString(5,"1"); - } - userVipStatement.execute(); - // 添加到批处理中 - userVipStatement.addBatch(); - r++; - if (r % 1000 == 0) { - userVipStatement.executeBatch(); + telUpdateStatement.execute(); } + }else { + //没有关联,直接导入 + telInsertStatement.setString(1,map.get("nameCN")==null?"":map.get("nameCN").toString()); + telInsertStatement.setString(2,map.get("icons")==null?"":map.get("icons").toString()); + telInsertStatement.setString(3,map.get("nickName")==null?"":map.get("nickName").toString()); + telInsertStatement.setString(4,map.get("cellPhone")==null?"":map.get("cellPhone").toString()); + telInsertStatement.setString(5,map.get("payStatus")==null?"":map.get("payStatus").toString()); + telInsertStatement.setString(6,map.get("point")==null?"":map.get("point").toString()); + telInsertStatement.setString(7,map.get("pointByJF")==null?"":map.get("pointByJF").toString()); + telInsertStatement.setString(8,map.get("oid")==null?"":map.get("oid").toString()); + telInsertStatement.execute(); } } - telInsertStatement.executeBatch(); - userVipStatement.executeBatch(); - } catch (SQLException e) { - System.out.println("Error: " + e.getMessage()); + //添加超v + if (map.get("paydate")!=null&&map.get("payValidDate")!=null){ + userVipStatement.setString(1,userId); + userVipStatement.setString(2,map.get("payStatus").toString()); + userVipStatement.setString(3,map.get("paydate").toString()); + userVipStatement.setString(4,map.get("payValidDate").toString()); + if (DateUtil.compare(DateUtil.parseDate(map.get("payValidDate").toString()),new Date())>0){ + userVipStatement.setString(5,"0"); + }else { + userVipStatement.setString(5,"1"); + } + userVipStatement.execute(); + } + }catch (Exception ee){ + } + } @@ -417,5 +430,4 @@ public class DataMigrationUtil { - }