package com.nis.web.service.restful; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.log4j.Logger; import com.nis.domain.restful.ConfigCompile; import com.nis.domain.restful.ConfigGroupRelation; import com.nis.domain.restful.IpRegion; import com.nis.domain.restful.NumRegion; import com.nis.domain.restful.StrRegion; import com.nis.util.Configurations; /** * jdbc测试批量插入 * * @author RenKaiGe-Office * */ public class UpdateCompileByJDBCThread implements Runnable { private static Logger logger = Logger.getLogger(SaveCompileByJDBCThread.class); private static Long start; private CountDownLatch latch; private String tableName; private Connection conn; private List compileList; private List groupList; private List ipRegionList; private List numRegionList; private List strRegionList; private Date opTime; public UpdateCompileByJDBCThread() { super(); } public UpdateCompileByJDBCThread(Connection conn, CountDownLatch latch, Long start, Date opTime) { super(); this.conn = conn; this.latch = latch; this.start = start; this.opTime = opTime; } public UpdateCompileByJDBCThread(String tableName, Connection conn, CountDownLatch latch, Long start, Date opTime) { super(); this.tableName = tableName; this.conn = conn; this.latch = latch; this.start = start; this.opTime = opTime; } public Date getOpTime() { return opTime; } public void setOpTime(Date opTime) { this.opTime = opTime; } public Long getStart() { return start; } public void setStart(Long start) { this.start = start; } public CountDownLatch getLatch() { return latch; } public void setLatch(CountDownLatch latch) { this.latch = latch; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public Connection getConn() { return conn; } public void setConn(Connection conn) { this.conn = conn; } public List getCompileList() { return compileList; } public void setCompileList(List compileList) { this.compileList = compileList; } public List getGroupList() { return groupList; } public void setGroupList(List groupList) { this.groupList = groupList; } public List getIpRegionList() { return ipRegionList; } public void setIpRegionList(List ipRegionList) { this.ipRegionList = ipRegionList; } public List getNumRegionList() { return numRegionList; } public void setNumRegionList(List numRegionList) { this.numRegionList = numRegionList; } public List getStrRegionList() { return strRegionList; } public void setStrRegionList(List strRegionList) { this.strRegionList = strRegionList; } public synchronized static java.sql.Timestamp utileDate2TimeStamp(java.util.Date udate) { java.sql.Timestamp sqlDate = null; long t = udate.getTime(); sqlDate = new java.sql.Timestamp(t); return sqlDate; } public static void updateCompile(List compileList, Connection conn, Date opTime, List msgList) { if (null != compileList && compileList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String tabName = Configurations.getStringProperty("compileTabName", "CONFIG_COMPILE"); String fieldName = Configurations.getStringProperty("updateFields", "IS_VALID"); String condition= Configurations.getStringProperty("compileUpdateCondition", "COMPILE_ID"); sb.append("update ").append(tabName).append(" set "); String fieldNameObj [] = fieldName.split(","); //加上where条件的参数名称 String newParamsStr = fieldName+","+condition; for (int i = 0; i < fieldNameObj.length; i++) { sb.append(fieldNameObj[i]+"=?"); if (i+1 groupList, Connection conn, Date opTime, List msgList) { if (null != groupList && groupList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String tabName = Configurations.getStringProperty("groupTabName", "CONFIG_GROUP"); String fieldName = Configurations.getStringProperty("updateFields", "IS_VALID"); String condition = Configurations.getStringProperty("groupUpdateCondition", "GROUP_ID"); sb.append(" update ").append(tabName); sb.append(" set "); String fieldNameObj [] = fieldName.split(","); //加上where条件的参数名称 String newParamsStr = fieldName+","+condition; for (int i = 0; i < fieldNameObj.length; i++) { sb.append(fieldNameObj[i]+"=?"); if (i+1 ipRegionList, Connection conn, Date opTime, List msgList) { if (null != ipRegionList && ipRegionList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String fieldName = Configurations.getStringProperty("updateFields", "IS_VALID"); String condition = Configurations.getStringProperty("regionId", "REGION_ID"); sb.append("update "); sb.append(name); sb.append(" set "); String fieldNameObj [] = fieldName.split(","); //加上where条件的参数名称 String newParamsStr = fieldName+","+condition; for (int i = 0; i < fieldNameObj.length; i++) { sb.append(fieldNameObj[i]+"=?"); if (i+1 numRegionList, Connection conn, Date opTime, List msgList) { if (null != numRegionList && numRegionList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String fieldName = Configurations.getStringProperty("updateFields", "IS_VALID"); String condition = Configurations.getStringProperty("regionId", "REGION_ID"); sb.append("update "); sb.append(name); sb.append(" set "); String fieldNameObj [] = fieldName.split(","); //加上where条件的参数名称 String newParamsStr = fieldName+","+condition; for (int i = 0; i < fieldNameObj.length; i++) { sb.append(fieldNameObj[i]+"=?"); if (i+1 strRegionList, Connection conn, Date opTime, List msgList) { if (null != strRegionList && strRegionList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String fieldName = Configurations.getStringProperty("updateFields", "IS_VALID"); String condition = Configurations.getStringProperty("regionId", "REGION_ID"); sb.append("update "); sb.append(name); sb.append(" set "); String fieldNameObj [] = fieldName.split(","); //加上where条件的参数名称 String newParamsStr = fieldName+","+condition; for (int i = 0; i < fieldNameObj.length; i++) { sb.append(fieldNameObj[i]+"=?"); if (i+1-1) { // name = name.substring(0, name.indexOf("_"))+name.substring(name.indexOf("_")+1,name.indexOf("_")+2).toUpperCase()+name.substring(name.indexOf("_")+2); // } while (name.indexOf("_")>-1) { name = name.substring(0, name.indexOf("_"))+name.substring(name.indexOf("_")+1,name.indexOf("_")+2).toUpperCase()+name.substring(name.indexOf("_")+2); } } //需要特殊处理字段名 if (name.equals("DoBlacklist")) { name = "DoBlackList"; }else if (name.equals("AffairId")) { name = "AffAirId"; }else if (name.equals("TopicId")) { name = "TopIcId"; } Method method = object.getClass().getMethod("get"+name); obj[i]= method.invoke(object); } for (int x = 0; x < obj.length; x++) { if (obj[x] instanceof Date) { ps.setObject(x + 1, utileDate2TimeStamp((Date) obj[x])); }else { ps.setObject(x + 1, obj[x]); } } } /** * * @Description: 根据compileId更新与之对应的group关系表的生效状态 * @author (zdx) * @date 2017年8月16日 上午9:46:44 * @param compileList * @param conn * @param opTime * @param msgList */ public static void updateGroupByCompileId(List compileList, Connection conn, Date opTime, List msgList) { if (null != compileList && compileList.size() > 0) { try { StringBuffer sb = new StringBuffer(); String tabName = Configurations.getStringProperty("groupTabName", "CONFIG_GROUP"); sb.append("update "+tabName+" set IS_VALID=?,op_time=?,LAST_UPDATE=sysdate where COMPILE_ID=? "); conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement(sb.toString()); for (ConfigCompile compile : compileList) { Object[] obj = new Object[] { compile.getIsValid(), opTime,compile.getCompileId() }; for (int x = 0; x < obj.length; x++) { if (x == 1) { ps.setTimestamp(x + 1, utileDate2TimeStamp(opTime)); } else { ps.setObject(x + 1, obj[x]); } } ps.addBatch(); } ps.executeBatch(); } catch (Exception e) { logger.error(e); msgList.add(e); } } } @Override public void run() { List msgList = ConfigSourcesService.getMsgList(); synchronized (msgList) { try { if (null != compileList && compileList.size() > 0) { updateCompile(compileList, conn, opTime, msgList); } else if (null != groupList && groupList.size() > 0) { updateGroup(groupList, conn, opTime, msgList); } else if (null != ipRegionList && ipRegionList.size() > 0) { updateIPRegion(tableName, ipRegionList, conn, opTime, msgList); } else if (null != strRegionList && strRegionList.size() > 0) { updateStrRegion(tableName, strRegionList, conn, opTime, msgList); } else if (null != numRegionList && numRegionList.size() > 0) { updateNumRegion(tableName, numRegionList, conn, opTime, msgList); } if((null !=compileList && compileList.size() > 0)&&(null != groupList && groupList.size()==0)){ updateGroupByCompileId(compileList, conn, opTime, msgList); } latch.countDown(); // System.out.println("latchCount=======================" + // latch.getCount()); } catch (Exception e) { e.printStackTrace(); } } } }