Commit c0189004 by huangfusuper

参数替换工具开发

parent 379551a0
package com.byit.job.utils; package com.byit.job.utils;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.byit.job.dto.ScriptParamAndPlaceholderDto;
import lombok.extern.java.Log; import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -10,6 +13,7 @@ import java.io.IOException; ...@@ -10,6 +13,7 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -28,6 +32,10 @@ public class PlaceholderUtils { ...@@ -28,6 +32,10 @@ public class PlaceholderUtils {
* 占位符后缀 * 占位符后缀
*/ */
private static final String PLACEHOLDER_SUFFIX = "}"; private static final String PLACEHOLDER_SUFFIX = "}";
/**
* 补批时间参数占位符
*/
private static final String DATE_KEY = "biz_date";
/** /**
* 占位符数据替换 * 占位符数据替换
...@@ -72,15 +80,49 @@ public class PlaceholderUtils { ...@@ -72,15 +80,49 @@ public class PlaceholderUtils {
return sbt.toString().getBytes(StandardCharsets.UTF_8); return sbt.toString().getBytes(StandardCharsets.UTF_8);
} }
public static void main(String[] args) { public static String paramPlaceholder(String runParam, String thisDate){
String text = "123${name},1232321${sex}123123"; ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto = JSON.parseObject(runParam, ScriptParamAndPlaceholderDto.class);
Map<String,String> map = new HashMap<String,String>(); Map<String, String> placeholder = scriptParamAndPlaceholderDto.getPlaceholder();
map.put("name","皇甫科星"); Map<String, String> param = scriptParamAndPlaceholderDto.getParam();
map.put("sex","'男'");
byte[] bytes = resolvePlaceholders(text.getBytes(StandardCharsets.UTF_8), map); /**
* 替换时间参数
*/
if (CollectionUtil.isNotEmpty(placeholder) && placeholder.containsKey(DATE_KEY)) {
placeholder.put(DATE_KEY,thisDate);
}
/**
* 替换时间占位符
*/
if(CollectionUtil.isNotEmpty(param) && param.containsKey(DATE_KEY)){
param.put(DATE_KEY,thisDate);
}
System.out.println(new String(bytes, StandardCharsets.UTF_8)); return JSON.toJSONString(scriptParamAndPlaceholderDto);
} }
public static void main(String[] args) {
ScriptParamAndPlaceholderDto sc = new ScriptParamAndPlaceholderDto();
Map<String,String> m1 = new HashMap<>(2);
Map<String,String> m2 = new HashMap<>(2);
m1.put(DATE_KEY,"2018年12月12日");
m1.put("name","狗子");
m2.put(DATE_KEY,"2010年10月10日");
m2.put("age","1000");
sc.setPlaceholder(m2);
sc.setParam(m1);
String toJSONString = JSON.toJSONString(sc);
String s = paramPlaceholder(toJSONString, "2020/3/12 15:13");
System.out.println(s);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment