123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package com.sundata.internalevaluation.calc.custom;
- import cn.hutool.core.util.RandomUtil;
- import com.sundata.internalevaluation.script.ScriptUtil;
- import com.sundata.internalevaluation.script.TemplateUtil;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * Created by IntelliJ IDEA.
- *
- * @author JoeLazy
- * @date 2025-02-14 17:12:49
- * @description: 测试Java方法获取参数
- */
- public class GetDataTest {
- private static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
- private static final String YYYYMMDD = "yyyyMMdd";
- private static final String HHMMSS = "HHmmss";
- private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
- private static final String YYYY_MM_DD = "yyyy-MM-dd";
- private static final String HH_MM_SS = "HH:mm:ss";
- public String getDateTimeNumber() {
- return getStr(YYYYMMDDHHMMSS);
- }
- public String getDateNumber() {
- return getStr(YYYYMMDD);
- }
- public String getTimeNumber() {
- return getStr(HHMMSS);
- }
- public String getDateTime() {
- return getStr(YYYY_MM_DD_HH_MM_SS);
- }
- public String getDate() {
- return getStr(YYYY_MM_DD);
- }
- public String getTime() {
- return getStr(HH_MM_SS);
- }
- public String getStr(String format) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
- return simpleDateFormat.format(nowDate());
- }
- public Date nowDate() {
- return new Date();
- }
- public int getInt(int i) {
- return i;
- }
- public String getString(String str) {
- return str;
- }
- // public void aa() {
- // // 模板字符${value}的正则表达式
- // String regex = "\\$\\{([^}]+)}";
- // // 生成正则表达式对象
- // Pattern pattern = Pattern.compile(regex);
- //
- // // 应入参数
- // String[] values = {"workDate","account","orgcode","TaskId","TaskSerialNum"};
- // // 替换模板字符用
- // Map<String,String> inputMap = new HashMap<String,String>();
- // // 造假参数
- // inputMap.put("workDate","'a'");
- // inputMap.put("account","'a'");
- // inputMap.put("orgcode","'a'");
- // inputMap.put("TaskId","'a'");
- // inputMap.put("TaskSerialNum","'a'");
- // // 使用apache的StrSubstitutor替换模板字符
- // StrSubstitutor sub = new StrSubstitutor(inputMap);
- //
- //
- // // 对请求中的数据逻辑说明的sql进行正则匹配
- // Matcher matcher = pattern.matcher(relationModel.getSqlCode());
- // // 判断缺失参数用
- // ArrayList<String> paramList = new ArrayList<String>(Arrays.asList(values));
- // // 游标
- // int matcherCount = 0;
- // // 检查开始
- // while (matcher.find()) {
- // if (!paramList.contains(matcher.group(1))) {
- // String err = "数据初始化逻辑中,该参数[" + matcher.group(1) + "]不应存在!";
- // log.error(err);
- // throw new BusinessException(err);
- // } else {
- // // 排除不缺失参数
- // paramList.remove(matcher.group(1));
- // }
- // matcherCount++;
- // }
- //
- // // 假参数赋值
- // String resolvedString = sub.replace(relationModel.getSqlCode());
- // }
- public static void main(String[] args) {
- // String sql = "select * from a where a = ${a} and b = ${b} and c =${a}";
- // Map<String, Object> map = new HashMap<>();
- //
- // map.put("a", "isA");
- // map.put("b", "isB");
- // Map<String, Object> map = new HashMap<>();
- // map.put("isStaff", 0);
- // map.put("isEmployrecord", 0);
- // map.put("isBankBlack", 1);
- // map.put("isIntro", 0);
- // map.put("isCredit", false);
- // map.put("str", "qiao");
- // String script = "${isStaff}==1 || ${isEmployrecord}==1 || ${isBankBlack}==1 || ${isIntro} ==1";
- // script = "isCredit==true";
- // script = "'${str}'=='qiao'";
- // script = TemplateUtil.execute(UUID.randomUUID().toString(), script, map);
- // Object o = ScriptUtil.executeScript(UUID.randomUUID().toString(), script, map);
- // 创建绑定变量
- Map<String, Object> binding = new HashMap<>();
- // 创建自定义对象
- binding.put("aNumber", 100);
- binding.put("name", "CodeLife Leno");
- binding.put("location", "china Guangzhou");
- String scriptStr = """
- "Hello, $name from $location!"
- """;
- System.out.println(ScriptUtil.executeScript(UUID.randomUUID().toString(), scriptStr, binding));
- System.out.println("");
- test();
- // bb(sql, map);
- }
- static void test() {
- String scriptId = RandomUtil.randomString(10);
- String scriptStr = """
- "Hello, $name from $location!"
- """;
- String scriptId2 = RandomUtil.randomString(10);
- String scriptStr2 = """
- aNumber > 100
- """;
- String templateId = RandomUtil.randomString(10);
- String templateStr = """
- Hello, ${user.name}!
- You have ${messages.size()} new messages.
- <%// 使用 Groovy 脚本计算未读消息数 --%>
- <% def unreadMessages = messages.count { it.startsWith("Unread") } %>
- Unread messages000: ${unreadMessages}
- <%// 条件判断 --%>
- <% if (unreadMessages > 0) { %>
- You have some unread messages.
- <% } else { %>
- All messages are read.
- <% } %>
- Messages:
- <%// 遍历消息列表 --%>
- ${messages.each { msg -> println(" -- " + msg) }}
- """;
- // 创建绑定变量
- Map<String, Object> binding = new HashMap<>();
- // 创建自定义对象
- User user = new User("Alice");
- binding.put("aNumber", 100);
- binding.put("name", "CodeLife Leno");
- binding.put("location", "china Guangzhou");
- binding.put("user", user);
- // 创建消息列表
- List<String> messages = List.of("Unread Message 1", "Read Message 2", "Unread Message 3");
- binding.put("messages", messages);
- System.out.println(ScriptUtil.executeScript(scriptId, scriptStr, binding));
- System.out.println("--------------------------------------------");
- System.out.println(ScriptUtil.executeScript(scriptId2, scriptStr2, binding));
- System.out.println("--------------------------------------------");
- templateStr = """
- <% if (approveSts == 03) { %>审批状态:审核中,<% } else { %>其他...,<% } %><% if (agrSts == 01) { %>额度状态:生效,<% } else if (agrSts == 02){ %>额度状态:手动冻结,<% } else if (agrSts == 02){%>额度状态:系统冻结,<% } %>
- """;
- binding.put("approveSts",3);
- binding.put("agrSts",1);
- System.out.println(TemplateUtil.execute(templateId, templateStr, binding));
- }
- // 自定义 User 类
- static class User {
- private String name;
- public User(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
- }
|