|
@@ -0,0 +1,76 @@
|
|
|
+package com.chelvc.framework.common.util;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.fasterxml.jackson.databind.util.JSONPObject;
|
|
|
+import com.google.common.cache.Cache;
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
|
+
|
|
|
+import javax.management.Query;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author valley
|
|
|
+ * @date 2024/03/19 01:13
|
|
|
+ **/
|
|
|
+public enum DictUtil {
|
|
|
+ INSTANCE;
|
|
|
+
|
|
|
+ private static final String url = "jdbc:mysql://47.108.128.78:6180/user?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true";
|
|
|
+ private static final String user = "user";
|
|
|
+ private static final String pwd = "Chelvc@user!2023#";
|
|
|
+ private static final String dictQuerySql = "SELECT CODE, NAME FROM DICT_ITEM WHERE ENTRY_ID = (SELECT ID FROM DICT_ENTRY WHERE CODE = {})";
|
|
|
+ private Cache<String, Optional<JSONObject>> dictCache;
|
|
|
+
|
|
|
+ DictUtil() {
|
|
|
+ this.dictCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDictItem(String entry, String itemCode) {
|
|
|
+ try {
|
|
|
+ JSONObject dictItems = getDictItemCache(entry);
|
|
|
+ if (JSONUtil.isNull(dictItems)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String itemValue = dictItems.getStr(itemCode);
|
|
|
+ if (StrUtil.isEmpty(itemValue)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return itemValue;
|
|
|
+ } catch (ExecutionException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject getDictItemCache(String entryCode) throws ExecutionException {
|
|
|
+ Optional<JSONObject> dictItemInfo = DictUtil.INSTANCE.dictCache.get(entryCode, () -> {
|
|
|
+ if (StrUtil.isEmpty(entryCode)) {
|
|
|
+ return Optional.empty();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
+ Connection dbConnect = DriverManager.getConnection(url, user, pwd);
|
|
|
+ Statement statement = dbConnect.createStatement();
|
|
|
+ String sql = StrUtil.format(dictQuerySql, entryCode);
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ JSONObject dictItemJson = new JSONObject();
|
|
|
+ while (resultSet.next()) {
|
|
|
+ dictItemJson.set(resultSet.getString(1), resultSet.getString(2));
|
|
|
+ }
|
|
|
+ Optional<JSONObject> res = Optional.of(dictItemJson);
|
|
|
+ DictUtil.INSTANCE.dictCache.put(entryCode, res);
|
|
|
+ return res;
|
|
|
+ } catch (SQLException sql) {
|
|
|
+ sql.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|