main
parent
2b7f4daceb
commit
a21cd8b6d0
@ -0,0 +1,227 @@
|
|||||||
|
package com.nm.gsgl.common.utils;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class OracleSqlUtil {
|
||||||
|
// 判重插入
|
||||||
|
public static Map<String, Object> getPCCRSql(String tableName, String[] primary, Map<String, String> map) {
|
||||||
|
List<String> col = new ArrayList<>();
|
||||||
|
Map<String, Object> reMap = new HashMap<>();
|
||||||
|
try {
|
||||||
|
String sql = "";
|
||||||
|
StringBuilder sql1 = new StringBuilder();
|
||||||
|
StringBuilder sql2 = new StringBuilder();
|
||||||
|
Iterator<Map.Entry<String, String>> iteratorMap = map.entrySet().iterator();
|
||||||
|
List<String> stringList = new ArrayList<>();
|
||||||
|
List<String> clobList = new ArrayList<>();
|
||||||
|
boolean haveClob = false;
|
||||||
|
while (iteratorMap.hasNext()) {
|
||||||
|
Map.Entry<String, String> entryMap = iteratorMap.next();
|
||||||
|
if (entryMap.getValue() !=null) {
|
||||||
|
|
||||||
|
if (entryMap.getValue().toLowerCase().contains("time") || entryMap.getValue().toLowerCase().contains("date")) {
|
||||||
|
stringList.add(entryMap.getKey().toUpperCase());
|
||||||
|
}
|
||||||
|
if (entryMap.getValue().toLowerCase().contains("clob")) {
|
||||||
|
clobList.add(entryMap.getKey().toUpperCase());
|
||||||
|
haveClob = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (haveClob) {
|
||||||
|
sql1.append("declare ");
|
||||||
|
for (String clobKey : clobList) {
|
||||||
|
sql1.append("V_" + clobKey + " CLOB :=?;");
|
||||||
|
}
|
||||||
|
sql1.append(" BEGIN ");
|
||||||
|
}
|
||||||
|
sql1.append("insert into ").append(tableName).append(" (");
|
||||||
|
sql2.append("select ");
|
||||||
|
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
|
||||||
|
int m = 0;
|
||||||
|
for (String s : stringList) {
|
||||||
|
if (stringStringEntry.getKey().equalsIgnoreCase(s)) {
|
||||||
|
m = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (String c : clobList) {
|
||||||
|
if (stringStringEntry.getKey().equalsIgnoreCase(c)) {
|
||||||
|
m = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String key = stringStringEntry.getKey();
|
||||||
|
col.add(key.toUpperCase());
|
||||||
|
if (m == 1) {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("TO_TIMESTAMP(?,'yyyy-mm-dd hh24:mi:ss.ff'),");
|
||||||
|
} else if (m == 2) {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("V_" + key + ",");
|
||||||
|
} else {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("?,");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sql1.replace(sql1.lastIndexOf(","), sql1.lastIndexOf(",") + 1, ") ");
|
||||||
|
sql2.replace(sql2.lastIndexOf(","), sql2.lastIndexOf(",") + 1, "");
|
||||||
|
sql2.append(" FROM DUAL ");
|
||||||
|
sql1.append(sql2);
|
||||||
|
sql1.append("WHERE NOT EXISTS (SELECT 1 FROM ").append(tableName).append(" WHERE ");
|
||||||
|
for (int i = 0; i < primary.length; i++) {
|
||||||
|
int m = 0;
|
||||||
|
for (String s : stringList) {
|
||||||
|
if (primary[i].equalsIgnoreCase(s)) {
|
||||||
|
m = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m == 1) {
|
||||||
|
if (i == primary.length - 1) {
|
||||||
|
sql1.append(primary[i]).append("= TO_TIMESTAMP(?,'yyyy-MM-dd hh24:mi:ss.ff') ) ");
|
||||||
|
} else {
|
||||||
|
sql1.append(primary[i]).append("= TO_TIMESTAMP(?,'yyyy-MM-dd hh24:mi:ss.ff') and ");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (i == primary.length - 1) {
|
||||||
|
sql1.append(primary[i]).append("= ? )");
|
||||||
|
} else {
|
||||||
|
sql1.append(primary[i]).append("= ? AND ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (haveClob) {
|
||||||
|
sql1.append("; END; ");
|
||||||
|
}
|
||||||
|
sql = sql1.toString();
|
||||||
|
reMap.put("sql", sql);
|
||||||
|
reMap.put("col", col);
|
||||||
|
reMap.put("haveClob", haveClob);
|
||||||
|
reMap.put("clobList", clobList);
|
||||||
|
return reMap;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// throw new RuntimeException("拼接Oracle判重插入语句出错");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDeleteSql(String tableName, String[] primary, Map<String, String> map) {
|
||||||
|
try {
|
||||||
|
StringBuilder deleteSql = new StringBuilder();
|
||||||
|
deleteSql.append("DELETE FROM ").append(tableName).append(" WHERE ");
|
||||||
|
Iterator<Map.Entry<String, String>> iteratorMap = map.entrySet().iterator();
|
||||||
|
List<String> stringList = new ArrayList<>();
|
||||||
|
while (iteratorMap.hasNext()) {
|
||||||
|
Map.Entry<String, String> entryMap = iteratorMap.next();
|
||||||
|
if (entryMap.getValue().toLowerCase().contains("date") || entryMap.getValue().toLowerCase().contains("time")) {
|
||||||
|
stringList.add(entryMap.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < primary.length; i++) {
|
||||||
|
int m = 0;
|
||||||
|
for (String s : stringList) {
|
||||||
|
if (primary[i].equalsIgnoreCase(s)) {
|
||||||
|
m = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m == 1) {
|
||||||
|
if (i == primary.length - 1) {
|
||||||
|
deleteSql.append(primary[i]).append("= TO_TIMESTAMP(?,'yyyy-MM-dd hh24:mi:ss.ff') ");
|
||||||
|
} else {
|
||||||
|
deleteSql.append(primary[i]).append("= TO_TIMESTAMP(?,'yyyy-MM-dd hh24:mi:ss.ff') and ");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (i == primary.length - 1) {
|
||||||
|
deleteSql.append(primary[i]).append("= ? ");
|
||||||
|
} else {
|
||||||
|
deleteSql.append(primary[i]).append("= ? AND ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deleteSql.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// throw new RuntimeException("拼接Oracle删除语句出错");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//oracle 直接插入sql语句
|
||||||
|
public static Map<String, Object> getInsertSql(String tableName, Map<String, String> map) {
|
||||||
|
List<String> col = new ArrayList<>();
|
||||||
|
Map<String, Object> reMap = new HashMap<>();
|
||||||
|
try {
|
||||||
|
StringBuilder sql1 = new StringBuilder();
|
||||||
|
StringBuilder sql2 = new StringBuilder();
|
||||||
|
Iterator<Map.Entry<String, String>> iteratorMap = map.entrySet().iterator();
|
||||||
|
List<String> stringList = new ArrayList<>();
|
||||||
|
List<String> clobList = new ArrayList<>();
|
||||||
|
boolean haveClob = false;
|
||||||
|
while (iteratorMap.hasNext()) {
|
||||||
|
Map.Entry<String, String> entryMap = iteratorMap.next();
|
||||||
|
if (entryMap.getValue().toLowerCase().contains("date") || entryMap.getValue().toLowerCase().contains("time")) {
|
||||||
|
stringList.add(entryMap.getKey().toUpperCase());
|
||||||
|
}
|
||||||
|
if (entryMap.getValue().toLowerCase().contains("clob")) {
|
||||||
|
clobList.add(entryMap.getKey().toUpperCase());
|
||||||
|
haveClob = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (haveClob) {
|
||||||
|
sql1.append("declare ");
|
||||||
|
for (String clobKey : clobList) {
|
||||||
|
sql1.append("V_" + clobKey + " CLOB :=?;");
|
||||||
|
}
|
||||||
|
sql1.append(" BEGIN ");
|
||||||
|
}
|
||||||
|
sql1.append(" Insert into ").append(tableName).append(" (");
|
||||||
|
sql2.append("values(");
|
||||||
|
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
|
||||||
|
int m = 0;
|
||||||
|
Map.Entry<String, String> entry = stringStringEntry;
|
||||||
|
for (String s : stringList) {
|
||||||
|
if (entry.getKey().equalsIgnoreCase(s)) {
|
||||||
|
m = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (String c : clobList) {
|
||||||
|
if (stringStringEntry.getKey().equalsIgnoreCase(c)) {
|
||||||
|
m = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String key = entry.getKey();
|
||||||
|
col.add(key.toUpperCase());
|
||||||
|
if (m == 1) {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("to_timestamp(?,'yyyy-MM-dd hh24:mi:ss.ff'),");
|
||||||
|
} else if (m == 2) {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("V_" + key + ",");
|
||||||
|
} else {
|
||||||
|
sql1.append(key).append(",");
|
||||||
|
sql2.append("?,");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sql1.replace(sql1.lastIndexOf(","), sql1.lastIndexOf(",") + 1, ") ");
|
||||||
|
sql2.replace(sql2.lastIndexOf(","), sql2.lastIndexOf(",") + 1, ")");
|
||||||
|
String sql = sql1.append(sql2).toString();
|
||||||
|
if (haveClob) {
|
||||||
|
sql += "; END; ";
|
||||||
|
}
|
||||||
|
reMap.put("sql", sql);
|
||||||
|
reMap.put("col", col);
|
||||||
|
reMap.put("haveClob", haveClob);
|
||||||
|
reMap.put("clobList", clobList);
|
||||||
|
return reMap;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// throw new RuntimeException("拼接Oracle插入语句出错");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package com.nm.gsgl.test;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.TypeReference;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.TableNameParser;
|
||||||
|
import com.nm.gsgl.common.Constant;
|
||||||
|
import com.nm.gsgl.common.utils.OracleSqlUtil;
|
||||||
|
import com.nm.gsgl.entity.business.db.GantryPassId;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: shuguang
|
||||||
|
* @date: 2024年07月12日 10:54
|
||||||
|
* @description:
|
||||||
|
*/
|
||||||
|
public class OracleSqlUtilTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String tableName ="CT_GANTRYPASSID_JAVA";
|
||||||
|
String[] primary={"TOLLPROVINCEID","ID"};
|
||||||
|
|
||||||
|
String json = "[{\"appointId\":null,\"cardId\":\"37012304232367460332\",\"enTollLaneId\":\"G00651500300101010330\",\"exFee\":11258,\"exTime\":\"2024-07-05T16:04:44\",\"exTollLaneId\":\"G00651500200302020610\",\"exTollStationName\":\"内蒙高路公司关碾房主线\",\"exTransactionId\":\"G006515002003020206102024070516616484\",\"exitFeeType\":1,\"exitTransType\":1,\"fee\":11258,\"id\":\"013701230423236746033220240705152048-G006515002011220010-20240705152428\",\"issuerId\":\"370101\",\"mediaNo\":\"3701014322758032\",\"mediaType\":1,\"passId\":\"013701230423236746033220240705152048\",\"processTime\":\"2024-07-05T18:24:06\",\"serProvinceId\":\"150201\",\"splitFee\":11258,\"splitRule\":1,\"splitTime\":\"2024-07-06\",\"tollIntervalId\":\"G006515003000110|G006515003002220|G006515002011220|G006515002011020|G006515002010820|G006515002010620|G006515002010420|G006515002010320|G006515002012220\",\"tollProvinceId\":\"150201\",\"transNum\":1,\"vehicleId\":\"蒙L43616_1\",\"vehicleSign\":\"0xff\",\"vehicleType\":16},{\"appointId\":null,\"cardId\":\"22012249230001446137\",\"enTollLaneId\":\"G55111501801601010010\",\"exFee\":4180,\"exTime\":\"2024-07-05T14:25:29\",\"exTollLaneId\":\"G12042200100202010160\",\"exTollStationName\":\"吉林白城西收费站\",\"exTransactionId\":\"G120422001002020101602024070514252945\",\"exitFeeType\":1,\"exitTransType\":1,\"fee\":2090,\"id\":\"012201224923000144613720240705132855-S005115002000210010-20240705133347\",\"issuerId\":\"220101\",\"mediaNo\":\"2201230003510940\",\"mediaType\":1,\"passId\":\"012201224923000144613720240705132855\",\"processTime\":\"2024-07-05T18:24:07\",\"serProvinceId\":\"150201\",\"splitFee\":2090,\"splitRule\":1,\"splitTime\":\"2024-07-06\",\"tollIntervalId\":\"G551115018003610|G551115018001810|S005115002000210|S005115002000410|S005115002000610|S005115002002010|G001215001000820|G001215001000220\",\"tollProvinceId\":\"220201\",\"transNum\":1,\"vehicleId\":\"吉JZK605_0\",\"vehicleSign\":\"0xff\",\"vehicleType\":1}]";
|
||||||
|
List<GantryPassId> mapList = JSON.parseObject(json, new TypeReference<List<GantryPassId>>() {
|
||||||
|
});
|
||||||
|
List<GantryPassId> mapLists = new ArrayList<>(mapList);
|
||||||
|
System.out.println(mapLists);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue