GetDataTest.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.sundata.internalevaluation.calc.custom;
  2. import cn.hutool.core.util.RandomUtil;
  3. import com.sundata.internalevaluation.script.ScriptUtil;
  4. import com.sundata.internalevaluation.script.TemplateUtil;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. /**
  8. * Created by IntelliJ IDEA.
  9. *
  10. * @author JoeLazy
  11. * @date 2025-02-14 17:12:49
  12. * @description: 测试Java方法获取参数
  13. */
  14. public class GetDataTest {
  15. private static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  16. private static final String YYYYMMDD = "yyyyMMdd";
  17. private static final String HHMMSS = "HHmmss";
  18. private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  19. private static final String YYYY_MM_DD = "yyyy-MM-dd";
  20. private static final String HH_MM_SS = "HH:mm:ss";
  21. public String getDateTimeNumber() {
  22. return getStr(YYYYMMDDHHMMSS);
  23. }
  24. public String getDateNumber() {
  25. return getStr(YYYYMMDD);
  26. }
  27. public String getTimeNumber() {
  28. return getStr(HHMMSS);
  29. }
  30. public String getDateTime() {
  31. return getStr(YYYY_MM_DD_HH_MM_SS);
  32. }
  33. public String getDate() {
  34. return getStr(YYYY_MM_DD);
  35. }
  36. public String getTime() {
  37. return getStr(HH_MM_SS);
  38. }
  39. public String getStr(String format) {
  40. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
  41. return simpleDateFormat.format(nowDate());
  42. }
  43. public Date nowDate() {
  44. return new Date();
  45. }
  46. public int getInt(int i) {
  47. return i;
  48. }
  49. public String getString(String str) {
  50. return str;
  51. }
  52. // public void aa() {
  53. // // 模板字符${value}的正则表达式
  54. // String regex = "\\$\\{([^}]+)}";
  55. // // 生成正则表达式对象
  56. // Pattern pattern = Pattern.compile(regex);
  57. //
  58. // // 应入参数
  59. // String[] values = {"workDate","account","orgcode","TaskId","TaskSerialNum"};
  60. // // 替换模板字符用
  61. // Map<String,String> inputMap = new HashMap<String,String>();
  62. // // 造假参数
  63. // inputMap.put("workDate","'a'");
  64. // inputMap.put("account","'a'");
  65. // inputMap.put("orgcode","'a'");
  66. // inputMap.put("TaskId","'a'");
  67. // inputMap.put("TaskSerialNum","'a'");
  68. // // 使用apache的StrSubstitutor替换模板字符
  69. // StrSubstitutor sub = new StrSubstitutor(inputMap);
  70. //
  71. //
  72. // // 对请求中的数据逻辑说明的sql进行正则匹配
  73. // Matcher matcher = pattern.matcher(relationModel.getSqlCode());
  74. // // 判断缺失参数用
  75. // ArrayList<String> paramList = new ArrayList<String>(Arrays.asList(values));
  76. // // 游标
  77. // int matcherCount = 0;
  78. // // 检查开始
  79. // while (matcher.find()) {
  80. // if (!paramList.contains(matcher.group(1))) {
  81. // String err = "数据初始化逻辑中,该参数[" + matcher.group(1) + "]不应存在!";
  82. // log.error(err);
  83. // throw new BusinessException(err);
  84. // } else {
  85. // // 排除不缺失参数
  86. // paramList.remove(matcher.group(1));
  87. // }
  88. // matcherCount++;
  89. // }
  90. //
  91. // // 假参数赋值
  92. // String resolvedString = sub.replace(relationModel.getSqlCode());
  93. // }
  94. public static void main(String[] args) {
  95. // String sql = "select * from a where a = ${a} and b = ${b} and c =${a}";
  96. // Map<String, Object> map = new HashMap<>();
  97. //
  98. // map.put("a", "isA");
  99. // map.put("b", "isB");
  100. // Map<String, Object> map = new HashMap<>();
  101. // map.put("isStaff", 0);
  102. // map.put("isEmployrecord", 0);
  103. // map.put("isBankBlack", 1);
  104. // map.put("isIntro", 0);
  105. // map.put("isCredit", false);
  106. // map.put("str", "qiao");
  107. // String script = "${isStaff}==1 || ${isEmployrecord}==1 || ${isBankBlack}==1 || ${isIntro} ==1";
  108. // script = "isCredit==true";
  109. // script = "'${str}'=='qiao'";
  110. // script = TemplateUtil.execute(UUID.randomUUID().toString(), script, map);
  111. // Object o = ScriptUtil.executeScript(UUID.randomUUID().toString(), script, map);
  112. // 创建绑定变量
  113. Map<String, Object> binding = new HashMap<>();
  114. // 创建自定义对象
  115. binding.put("aNumber", 100);
  116. binding.put("name", "CodeLife Leno");
  117. binding.put("location", "china Guangzhou");
  118. String scriptStr = """
  119. "Hello, $name from $location!"
  120. """;
  121. System.out.println(ScriptUtil.executeScript(UUID.randomUUID().toString(), scriptStr, binding));
  122. System.out.println("");
  123. test();
  124. // bb(sql, map);
  125. }
  126. static void test() {
  127. String scriptId = RandomUtil.randomString(10);
  128. String scriptStr = """
  129. "Hello, $name from $location!"
  130. """;
  131. String scriptId2 = RandomUtil.randomString(10);
  132. String scriptStr2 = """
  133. aNumber > 100
  134. """;
  135. String templateId = RandomUtil.randomString(10);
  136. String templateStr = """
  137. Hello, ${user.name}!
  138. You have ${messages.size()} new messages.
  139. <%// 使用 Groovy 脚本计算未读消息数 --%>
  140. <% def unreadMessages = messages.count { it.startsWith("Unread") } %>
  141. Unread messages000: ${unreadMessages}
  142. <%// 条件判断 --%>
  143. <% if (unreadMessages > 0) { %>
  144. You have some unread messages.
  145. <% } else { %>
  146. All messages are read.
  147. <% } %>
  148. Messages:
  149. <%// 遍历消息列表 --%>
  150. ${messages.each { msg -> println(" -- " + msg) }}
  151. """;
  152. // 创建绑定变量
  153. Map<String, Object> binding = new HashMap<>();
  154. // 创建自定义对象
  155. User user = new User("Alice");
  156. binding.put("aNumber", 100);
  157. binding.put("name", "CodeLife Leno");
  158. binding.put("location", "china Guangzhou");
  159. binding.put("user", user);
  160. // 创建消息列表
  161. List<String> messages = List.of("Unread Message 1", "Read Message 2", "Unread Message 3");
  162. binding.put("messages", messages);
  163. System.out.println(ScriptUtil.executeScript(scriptId, scriptStr, binding));
  164. System.out.println("--------------------------------------------");
  165. System.out.println(ScriptUtil.executeScript(scriptId2, scriptStr2, binding));
  166. System.out.println("--------------------------------------------");
  167. templateStr = """
  168. <% if (approveSts == 03) { %>审批状态:审核中,<% } else { %>其他...,<% } %><% if (agrSts == 01) { %>额度状态:生效,<% } else if (agrSts == 02){ %>额度状态:手动冻结,<% } else if (agrSts == 02){%>额度状态:系统冻结,<% } %>
  169. """;
  170. binding.put("approveSts",3);
  171. binding.put("agrSts",1);
  172. System.out.println(TemplateUtil.execute(templateId, templateStr, binding));
  173. }
  174. // 自定义 User 类
  175. static class User {
  176. private String name;
  177. public User(String name) {
  178. this.name = name;
  179. }
  180. public String getName() {
  181. return name;
  182. }
  183. }
  184. }