diff --git a/src_app_fresh/apx/com/amarsoft/als/base/awe/util/AweDoCommUtil.java b/src_app_fresh/apx/com/amarsoft/als/base/awe/util/AweDoCommUtil.java index c6cdc98bb..3c35a4007 100644 --- a/src_app_fresh/apx/com/amarsoft/als/base/awe/util/AweDoCommUtil.java +++ b/src_app_fresh/apx/com/amarsoft/als/base/awe/util/AweDoCommUtil.java @@ -8,26 +8,101 @@ import com.amarsoft.are.util.SpecialTools; public class AweDoCommUtil { public static void main(String[] args) throws Exception { - System.out.println(SpecialTools.real2Amarsoft("123 ")); - String str = "((EXISTS( SELECT * FROM jbo.com .tenwa.lease.comm.LB_GUARANTEE_UNIT lgu " - + "WHERE lgu.CONTRACT_ID=O.CONTRACT_ID AND lgu.ASSUROR=:customerid) " - + "OR EXISTS( SELECT * FROM jbo.com .tenwa.lease.comm.LB_UNION_LESSEE lul " - + "WHERE lul.CONTRACT_ID=O.CONTRACT_ID AND lul.CUSTOMER_ID=:customerid)))"; - + String str = "id=:id1 and (ida>=:id2 or idb<=:id3) and idc >: id4 (idd <: id5 and ide <>:id6)"; + System.out.println(str); Map map = new HashMap(); - map.put("ASSUROR", "123"); - map.put("ASSUROR".toLowerCase(), "123"); - map.put("CUSTOMER_ID", "123"); - map.put("CUSTOMER_ID".toLowerCase(), "123"); - System.out.println(whereHandle(str, map)); + map.put("id", "1"); + map.put("ida", "2"); + map.put("idb", "3"); + map.put("idc", "4"); + map.put("idd", "5"); + map.put("ide", "6"); + +// map.put("ASSUROR".toLowerCase(), "123"); +// map.put("CUSTOMER_ID", "123"); +// map.put("CUSTOMER_ID".toLowerCase(), "123"); +// System.out.println(whereHandle(str, map)); + whereHandle(str, map); } public static String whereHandle(String sJboWhere, Map paramMap) { - return whereHandle1(sJboWhere, paramMap); + return whereHandle2(sJboWhere, paramMap); + } + + private static String whereHandle2(String sJboWhere, + Map paramMap) { + while (sJboWhere.contains(" ")) { + sJboWhere = sJboWhere.replace(" ", " "); + } + String where = sJboWhere; + String operation = "="; + if (where.contains(":")) { + String [] array = where.split(":"); + String oldStr = "", newStr = ""; + for (int i = 1; i < array.length; i++) { + + String key = array[i - 1].trim(); + String value = array[i].trim(); + + if (value.endsWith(")")) { + value = value.substring(0, value.length() - 1).trim(); + } + String sKey = "", sKeyStr = ""; + if (key.endsWith("==") || key.endsWith("!=") || key.endsWith("<>") || key.endsWith(">=") || key.endsWith("<=") + || key.equals("!<") || key.equals("!>")) { + sKey = key.substring(0, key.length() - 2).trim(); + operation = key.substring(key.length() - 2, key.length()).trim(); + } else if (key.endsWith("=") || key.endsWith(">") || key.endsWith("<")) { + sKey = key.substring(0, key.length() - 1).trim(); + operation = key.substring(key.length() - 1, key.length()).trim(); + } + + String [] keyArray = sKey.split(" "); + + if (keyArray.length > 0) { + sKeyStr = keyArray[keyArray.length - 1].trim(); + } else { + sKeyStr = sKey.trim(); + } + + if (sKeyStr.startsWith("(")) { + sKeyStr = sKeyStr.substring(1, sKeyStr.length()); + } + + if (sKeyStr.contains(".")) { + String [] keyArray2 = sKeyStr.split("\\."); + + sKeyStr = keyArray2[keyArray2.length - 1].trim(); + + } + if (value.contains(" ")) { + value = value.substring(0, value.indexOf(" ")); + } + value = value.trim(); + if (value.endsWith(")")) { + value = value.substring(0, value.indexOf(")")); + } + oldStr = array[i - 1] + ":" + value; +// System.out.println("old: " + oldStr); + + newStr = sKeyStr + operation + "'" + paramMap.get(sKeyStr) + "'"; + System.out.println(sKeyStr + operation + ":" + value + " 替换成 " + newStr); + oldStr = sKeyStr + operation + ":" + value; + sJboWhere = sJboWhere.replace(oldStr, newStr); + oldStr = sKeyStr + " " + operation + ": " + value; + sJboWhere = sJboWhere.replace(oldStr, newStr); + oldStr = sKeyStr + operation + ": " + value; + sJboWhere = sJboWhere.replace(oldStr, newStr); + oldStr = sKeyStr + " " + operation + ":" + value; + sJboWhere = sJboWhere.replace(oldStr, newStr); + } + } + System.out.println(sJboWhere); + return sJboWhere; } private static String whereHandle1(String sJboWhere, @@ -36,24 +111,23 @@ public class AweDoCommUtil { where = where.replace(" AND ", " and "); where = where.replace(" OR ", " or "); String[] array = where.split("and"); + String[] array1 = null; + String sKey = ""; + String newStr = ""; for (String str : array) { if (str.contains(":")) { + array1 = str.split(":"); String[] orArray = str.split("or"); if (orArray.length >= 2) { - // [ (O.INPUTORGID=:inputOrgId , O.INPUTORGID like :inputOrgId1)] for (String orStr : orArray) { - // O.contract_id = :contract_id - String[] array1 = orStr.split(":"); - // O.contract_id = > 0 - // contract_id> 1 - if (array1.length != 2) - continue; +// if (array1.length != 2) +// continue; if (!array1[0].contains(".")) { - String sKey = array1[0].toLowerCase(); + sKey = array1[0].toLowerCase(); sKey = sKey.trim(); sKey = sKey.substring(0, sKey.length() - 1); sKey = sKey.trim(); - String newStr = array1[0] + "'" + newStr = array1[0] + "'" + paramMap.get(sKey) + "' "; sJboWhere = sJboWhere.replace(orStr, newStr); continue; @@ -61,47 +135,42 @@ public class AweDoCommUtil { String[] array2 = array1[0].split("\\."); if (array2.length < 2) continue; - String sKey = array2[1].toLowerCase(); - // inputorgid like + sKey = array2[array2.length - 1].toLowerCase().trim(); + if (sKey.contains("like") || sKey.contains("LIKE")) { - String newStr = array1[0] + "'" + paramMap.get(sKey.trim().replaceAll("like", "").trim()) + "' "; - String sStr = array1[1]; - while (sStr.endsWith(")")) { - newStr += ")"; - sStr = sStr.substring(0, sStr.length() - 1); - } - sJboWhere = sJboWhere.replace(orStr, newStr); + newStr = array1[0] + "'" + paramMap.get(sKey.trim().replaceAll("like", "").trim()) + "' ".trim(); + } else if (sKey.contains(" in ") || sKey.contains(" IN ")) { + newStr = array1[0] + "'" + paramMap.get(sKey.trim().replaceAll(" in ", "").trim()) + "' ".trim(); } else { - sKey = sKey.trim(); - sKey = sKey.substring(0, sKey.length() - 1); - sKey = sKey.trim(); - String newStr = array1[0] + "'" - + paramMap.get(sKey) + "' "; - String sStr = array1[1]; - while (sStr.endsWith(")")) { - newStr += ")"; - sStr = sStr.substring(0, sStr.length() - 1); - } - sJboWhere = sJboWhere.replace(orStr, newStr); + sKey = sKey.substring(0, sKey.length() - 1).trim(); + newStr = array1[0] + "'" + + paramMap.get(sKey) + "' ".trim(); + } + String sStr = orStr.trim(); + while (sStr.endsWith(")")) { +// newStr += ")"; + sStr = sStr.substring(0, sStr.length()-1); + } + sJboWhere = sJboWhere.replace(sStr, newStr); sJboWhere += " "; } - sJboWhere = sJboWhere.substring(0, sJboWhere.length() - 1); - } else { - // O.contract_id = :contract_id - String[] array1 = str.split(":"); - // O.contract_id = > 0 - // contract_id> 1 + } else { // 没有 or if (array1.length != 2) continue; - if (!array1[0].contains(".")) { - String sKey = array1[0].toLowerCase(); - sKey = sKey.trim(); - sKey = sKey.substring(0, sKey.length() - 1); - sKey = sKey.trim(); - String newStr = array1[0] + "'" - + paramMap.get(sKey) + "' "; - String sStr = array1[1]; + if (!array1[0].contains(".")) { // 没有 O. xx. + sKey = array1[0].toLowerCase().trim(); + newStr = ""; + if (sKey.contains(" like ") || sKey.contains(" LIKE ")) { // flowUnid like : flowUnid + newStr = array1[0] + "'" + paramMap.get(sKey.replaceAll(" like ", "").trim()) + "'".trim(); + } else if (sKey.contains(" in ") || sKey.contains(" IN ")) { // flowUnid in : flowUnid + newStr = array1[0] + "'" + paramMap.get(sKey.replaceAll(" in ", "").trim()) + "'".trim(); + } else { // flowUnid = || > || < : flowUnid + sKey = sKey.substring(0, sKey.length() - 1).trim(); + newStr = array1[0] + "'" + paramMap.get(sKey) + "' ".trim(); + } + + String sStr = array1[1].trim(); while (sStr.endsWith(")")) { newStr += ")"; sStr = sStr.substring(0, sStr.length() - 1); @@ -112,19 +181,25 @@ public class AweDoCommUtil { String[] array2 = array1[0].split("\\."); if (array2.length < 2) continue; - String sKey = array2[1].toLowerCase(); - sKey = sKey.trim(); - sKey = sKey.substring(0, sKey.length() - 1); - sKey = sKey.trim(); - String newStr = array1[0] + "'" - + paramMap.get(sKey) + "' "; - String sStr = array1[1]; - while (sStr.endsWith(")")) { - newStr += ")"; - sStr = sStr.substring(0, sStr.length() - 1); + sKey = array2[array2.length - 1].toLowerCase().trim(); + newStr = ""; + if (sKey.contains(" like ") || sKey.contains(" LIKE ")) { // flowUnid like : flowUnid + newStr = array1[0] + "'" + paramMap.get(sKey.replaceAll(" like ", "").trim()) + "'".trim(); + } else if (sKey.contains(" in ") || sKey.contains(" IN ")) { // flowUnid in : flowUnid + newStr = array1[0] + "'" + paramMap.get(sKey.replaceAll(" in ", "").trim()) + "'".trim(); + } else { // flowUnid = || > || < : flowUnid + sKey = sKey.substring(0, sKey.length() - 1).trim(); + newStr = array1[0] + "'" + paramMap.get(sKey) + "' ".trim(); } - sJboWhere = sJboWhere.replace(str, newStr); } + newStr = newStr.trim(); + String sStr = array1[1].trim(); + while (sStr.endsWith(")")) { + newStr += ")"; + sStr = sStr.substring(0, sStr.length() - 1); + } + newStr = " " + newStr + " "; + sJboWhere = sJboWhere.replace(str, newStr); } } return sJboWhere;