新增 字典支持从静态文件加载
This commit is contained in:
@@ -22,4 +22,5 @@ arango.database=db_dev
|
|||||||
tplPath=/tpl
|
tplPath=/tpl
|
||||||
|
|
||||||
# file|db
|
# file|db
|
||||||
dcate=db
|
dataCate=db
|
||||||
|
dataPath=${APP_HOME}/conf/data/
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import net.tccn.base.arango.Doc;
|
|||||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||||
import net.tccn.dbq.jdbc.api.DbKit;
|
import net.tccn.dbq.jdbc.api.DbKit;
|
||||||
import net.tccn.dbq.table.Field;
|
import net.tccn.dbq.table.Field;
|
||||||
import net.tccn.dict.DictKit;
|
import net.tccn.dict.Dict;
|
||||||
import net.tccn.meta.*;
|
import net.tccn.meta.*;
|
||||||
import net.tccn.plat.DbPlat;
|
import net.tccn.plat.DbPlat;
|
||||||
import net.tccn.plat.SysPlat;
|
import net.tccn.plat.SysPlat;
|
||||||
@@ -45,8 +45,11 @@ public final class MetaKit {
|
|||||||
private static List<User> users;
|
private static List<User> users;
|
||||||
@Getter
|
@Getter
|
||||||
private static List<TaskEntity> taskEntities;
|
private static List<TaskEntity> taskEntities;
|
||||||
|
@Getter
|
||||||
|
private static List<Dict> dicts;
|
||||||
|
|
||||||
protected static String dcate;
|
protected static String dcate = "db";
|
||||||
|
protected static String dataPath;
|
||||||
private static final JsonConvert convert = JsonConvert.root();
|
private static final JsonConvert convert = JsonConvert.root();
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
@@ -58,8 +61,7 @@ public final class MetaKit {
|
|||||||
reload(SysPlat.class);
|
reload(SysPlat.class);
|
||||||
reload(User.class);
|
reload(User.class);
|
||||||
reload(TaskEntity.class);
|
reload(TaskEntity.class);
|
||||||
|
reload(Dict.class);
|
||||||
DictKit dictKit = DictKit.use("");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +76,7 @@ public final class MetaKit {
|
|||||||
public static <T extends Doc> void reload(Class<T> clazz, String key) {
|
public static <T extends Doc> void reload(Class<T> clazz, String key) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File file = new File(String.format("tmp/%s.json", clazz.getSimpleName()));
|
File file = new File(String.format("%s%s.json", dataPath, clazz.getSimpleName()));
|
||||||
if ("file".equals(dcate)) {
|
if ("file".equals(dcate)) {
|
||||||
if (MetaTable.class == clazz) {
|
if (MetaTable.class == clazz) {
|
||||||
Type type = new TypeToken<List<MetaTable>>() {
|
Type type = new TypeToken<List<MetaTable>>() {
|
||||||
@@ -108,6 +110,10 @@ public final class MetaKit {
|
|||||||
Type type = new TypeToken<List<TaskEntity>>() {
|
Type type = new TypeToken<List<TaskEntity>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
taskEntities = convert.convertFrom(type, new FileInputStream(file));
|
taskEntities = convert.convertFrom(type, new FileInputStream(file));
|
||||||
|
} else if (Dict.class == clazz) {
|
||||||
|
Type type = new TypeToken<List<Dict>>() {
|
||||||
|
}.getType();
|
||||||
|
dicts = convert.convertFrom(type, new FileInputStream(file));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
|
if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
|
||||||
@@ -120,6 +126,8 @@ public final class MetaKit {
|
|||||||
else if (TaskEntity.class == clazz) {
|
else if (TaskEntity.class == clazz) {
|
||||||
taskEntities = TaskEntity.dao.find();
|
taskEntities = TaskEntity.dao.find();
|
||||||
TaskKit.init();
|
TaskKit.init();
|
||||||
|
} else if (Dict.class == clazz) {
|
||||||
|
dicts = Dict.dao.find();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,13 +137,14 @@ public final class MetaKit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void cacheSave() {
|
public static void cacheSave() {
|
||||||
|
cacheSave(MetaTable.class);
|
||||||
cacheSave(MetaLink.class);
|
cacheSave(MetaLink.class);
|
||||||
cacheSave(MetaService.class);
|
cacheSave(MetaService.class);
|
||||||
cacheSave(DbAccount.class);
|
cacheSave(DbAccount.class);
|
||||||
cacheSave(DbPlat.class);
|
|
||||||
cacheSave(SysPlat.class);
|
cacheSave(SysPlat.class);
|
||||||
cacheSave(User.class);
|
cacheSave(User.class);
|
||||||
cacheSave(TaskEntity.class);
|
cacheSave(TaskEntity.class);
|
||||||
|
cacheSave(Dict.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void cacheSave(Class clazz) {
|
private static void cacheSave(Class clazz) {
|
||||||
@@ -144,13 +153,14 @@ public final class MetaKit {
|
|||||||
else if (MetaLink.class == clazz) list = metaLinks;
|
else if (MetaLink.class == clazz) list = metaLinks;
|
||||||
else if (MetaService.class == clazz) list = metaServices;
|
else if (MetaService.class == clazz) list = metaServices;
|
||||||
else if (DbAccount.class == clazz) list = dbPlats;
|
else if (DbAccount.class == clazz) list = dbPlats;
|
||||||
else if (DbPlat.class == clazz) list = dbPlats;
|
|
||||||
else if (SysPlat.class == clazz) list = sysPlats;
|
else if (SysPlat.class == clazz) list = sysPlats;
|
||||||
else if (User.class == clazz) list = users;
|
else if (User.class == clazz) list = users;
|
||||||
|
else if (TaskEntity.class == clazz) list = taskEntities;
|
||||||
|
else if (Dict.class == clazz) list = dicts;
|
||||||
|
|
||||||
if (list == null || list.size() == 0) return;
|
if (list == null || list.size() == 0) return;
|
||||||
|
|
||||||
File file = new File(String.format("tmp/%s.json", list.get(0).getClass().getSimpleName()));
|
File file = new File(String.format("%s%s.json", dataPath, list.get(0).getClass().getSimpleName()));
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
FileKit.strToFile(MetaKit.convert.convertTo(list), file);
|
FileKit.strToFile(MetaKit.convert.convertTo(list), file);
|
||||||
}
|
}
|
||||||
@@ -167,10 +177,20 @@ public final class MetaKit {
|
|||||||
return MetaKit.getMetaTableByAlias(metaService.getTable());
|
return MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过平台token 得到平台字典数据
|
||||||
|
*
|
||||||
|
* @param platToken
|
||||||
|
*/
|
||||||
|
public static Map<String, List<Dict>> getDictData(String platToken) {
|
||||||
|
String platId = MetaKit.getPlatId(platToken);
|
||||||
|
Map<String, List<Dict>> dicts = MetaKit.dicts.stream().filter(x -> x.getSysPlatId().equals(platId)).collect(Collectors.groupingBy(Dict::getType));
|
||||||
|
return dicts;
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
public void cleanData() {
|
public void cleanData() {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,7 +224,8 @@ public final class MetaKit {
|
|||||||
List<Field> items = t.getItems();
|
List<Field> items = t.getItems();
|
||||||
|
|
||||||
//x 是跨越值
|
//x 是跨越值
|
||||||
a:for (int i = 0, x = 0; i < arr.length; i++) {
|
a:
|
||||||
|
for (int i = 0, x = 0; i < arr.length; i++) {
|
||||||
int inx = i - x;
|
int inx = i - x;
|
||||||
|
|
||||||
if (!items.get(inx).equals(arr[i])) {
|
if (!items.get(inx).equals(arr[i])) {
|
||||||
|
|||||||
@@ -13,10 +13,13 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
*/
|
*/
|
||||||
public class MetaListenter implements ApplicationListener {
|
public class MetaListenter implements ApplicationListener {
|
||||||
|
|
||||||
@Resource(name = "property.dcate")
|
@Resource(name = "property.dataCate")
|
||||||
private String dcate;
|
private String dcate;
|
||||||
@Resource(name = "property.tplPath")
|
@Resource(name = "property.tplPath")
|
||||||
private String tplPath;
|
private String tplPath;
|
||||||
|
@Resource(name = "property.dataPath")
|
||||||
|
private String dataPath;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preStart(Application application) {
|
public void preStart(Application application) {
|
||||||
@@ -25,6 +28,7 @@ public class MetaListenter implements ApplicationListener {
|
|||||||
rf.inject(this);
|
rf.inject(this);
|
||||||
|
|
||||||
MetaKit.dcate = dcate;
|
MetaKit.dcate = dcate;
|
||||||
|
MetaKit.dataPath = dataPath;
|
||||||
MetaKit.init();
|
MetaKit.init();
|
||||||
TplKit.use(true).addTpl(new File(FileKit.rootPath(), tplPath));
|
TplKit.use(true).addTpl(new File(FileKit.rootPath(), tplPath));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
package net.tccn.dict;
|
package net.tccn.dict;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
import net.tccn.base.arango.Doc;
|
import net.tccn.base.arango.Doc;
|
||||||
|
|
||||||
|
import javax.persistence.Table;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: liangxianyou
|
* @author: liangxianyou
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
|
@Table(name = "Dict", catalog = "db_demo")
|
||||||
public class Dict extends Doc<Dict> implements Serializable {
|
public class Dict extends Doc<Dict> implements Serializable {
|
||||||
private String type; // 字典类型
|
public static Dict dao = Doc.dao(Dict.class);
|
||||||
private String value; // 字典值
|
|
||||||
private String label; // 中文名
|
private String type; // 字典类型
|
||||||
private String pValue; // 父级字典值
|
private String value; // 字典值
|
||||||
private String code; //
|
private String label; // 中文名
|
||||||
|
private String pValue; // 父级字典值
|
||||||
|
private String code; //
|
||||||
|
private String sysPlatId; // 系统平台id
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package net.tccn.dict;
|
package net.tccn.dict;
|
||||||
|
|
||||||
import com.arangodb.ArangoDB;
|
|
||||||
import com.arangodb.ArangoDatabase;
|
|
||||||
import net.tccn.base.Kv;
|
|
||||||
import net.tccn.base.MetaKit;
|
import net.tccn.base.MetaKit;
|
||||||
import net.tccn.base.PropKit;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -15,9 +11,12 @@ import java.util.*;
|
|||||||
* @author: liangxianyou
|
* @author: liangxianyou
|
||||||
*/
|
*/
|
||||||
public final class DictKit {
|
public final class DictKit {
|
||||||
|
private static String dcate = "db";
|
||||||
|
private static String dataPath;
|
||||||
|
|
||||||
private static Map<String, DictKit> kits = new HashMap<>();
|
private static Map<String, DictKit> kits = new HashMap<>();
|
||||||
private String platToken;
|
private String platToken;
|
||||||
private Map<String, List<Map>> dicts = new HashMap<>();
|
private Map<String, List<Dict>> dicts;
|
||||||
|
|
||||||
private DictKit() {
|
private DictKit() {
|
||||||
}
|
}
|
||||||
@@ -27,29 +26,27 @@ public final class DictKit {
|
|||||||
if (dictKit == null) {
|
if (dictKit == null) {
|
||||||
dictKit = new DictKit();
|
dictKit = new DictKit();
|
||||||
dictKit.platToken = platToken;
|
dictKit.platToken = platToken;
|
||||||
dictKit.init();
|
dictKit.dicts = MetaKit.getDictData(platToken);
|
||||||
kits.put(platToken, dictKit);
|
kits.put(platToken, dictKit);
|
||||||
}
|
}
|
||||||
return dictKit;
|
return dictKit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化字典,不同模式下,数据来源不同
|
// 初始化字典,不同模式下,数据来源不同
|
||||||
private void init() {
|
|
||||||
// arangodb 加载模式
|
|
||||||
loadByArangodb();
|
|
||||||
|
|
||||||
// 文件数据模式
|
private void stop() {
|
||||||
|
//文件模式下将 字典数据写入到json文件
|
||||||
|
}
|
||||||
|
|
||||||
// excel 加载数据
|
private void loadByFile() {
|
||||||
|
|
||||||
// mysql 加载数据
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过arangodb 加载字典数据
|
* 通过arangodb 加载字典数据
|
||||||
*/
|
*/
|
||||||
private void loadByArangodb() {
|
private void loadByArangodb() {
|
||||||
String host = PropKit.getProperty("arango.host");
|
/*String host = PropKit.getProperty("arango.host");
|
||||||
int port = Kv.toAs(PropKit.getProperty("arango.port", "8529"), int.class);
|
int port = Kv.toAs(PropKit.getProperty("arango.port", "8529"), int.class);
|
||||||
String user = PropKit.getProperty("arango.user");
|
String user = PropKit.getProperty("arango.user");
|
||||||
String passwd = PropKit.getProperty("arango.passwd");
|
String passwd = PropKit.getProperty("arango.passwd");
|
||||||
@@ -57,21 +54,21 @@ public final class DictKit {
|
|||||||
ArangoDB arangoDb = new ArangoDB.Builder().host(host, port).user(user).password(passwd).build();
|
ArangoDB arangoDb = new ArangoDB.Builder().host(host, port).user(user).password(passwd).build();
|
||||||
ArangoDatabase dbDemo = arangoDb.db("db_demo");
|
ArangoDatabase dbDemo = arangoDb.db("db_demo");
|
||||||
|
|
||||||
String platId = MetaKit.getPlatId(platToken);
|
String platId = "28121369";//MetaKit.getPlatId(platToken);
|
||||||
String dictAQL = String.format("for d in Dict filter d.sysPlatId=='%s'return d", platId);
|
String dictAQL = String.format("for d in Dict filter d.sysPlatId=='%s'return d", platId);
|
||||||
List<Map<String, String>> list = (List) dbDemo.query(dictAQL, Map.class).asListRemaining();
|
List<Dict> list = (List) dbDemo.query(dictAQL, Map.class).asListRemaining();
|
||||||
|
|
||||||
String[] fields = {"label", "value", "pValue"};
|
String[] fields = {"label", "value", "pValue"};
|
||||||
list.forEach(x -> {
|
list.forEach(x -> {
|
||||||
String type = x.get("type");
|
String type = x.get("type");
|
||||||
List<Map> items = dicts.getOrDefault(type, new ArrayList<>());
|
List<Dict> items = dicts.getOrDefault(type, new ArrayList<>());
|
||||||
Map dict = new HashMap();
|
Map dict = new HashMap();
|
||||||
for (String field : fields) {
|
for (String field : fields) {
|
||||||
dict.put(field, x.get(field));
|
dict.put(field, x.get(field));
|
||||||
}
|
}
|
||||||
items.add(dict);
|
items.add(dict);
|
||||||
dicts.put(type, items);
|
dicts.put(type, items);
|
||||||
});
|
});*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,33 +118,34 @@ public final class DictKit {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询指定类别的字典
|
* 查询指定类别的字典
|
||||||
|
*
|
||||||
* @param code
|
* @param code
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<Map> getDicts(String code) {
|
public List<Dict> getDicts(String code) {
|
||||||
Objects.requireNonNull(code, "code 不能为空");
|
Objects.requireNonNull(code, "code 不能为空");
|
||||||
return dicts.getOrDefault(code, new ArrayList<>());
|
return dicts.getOrDefault(code, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<Map>> getDicts() {
|
public Map<String, List<Dict>> getDicts() {
|
||||||
return dicts;
|
return dicts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDictLabel(String code, String value) {
|
public String getDictLabel(String code, String value) {
|
||||||
Objects.requireNonNull(code, "code 不能为空");
|
Objects.requireNonNull(code, "code 不能为空");
|
||||||
Objects.requireNonNull(value, "value 不能为空");
|
Objects.requireNonNull(value, "value 不能为空");
|
||||||
List<Map> dicts = getDicts(code);
|
List<Dict> dicts = getDicts(code);
|
||||||
Optional<Map> any = dicts.stream().filter(x -> value.equals(x.get("value"))).findAny();
|
Optional<Dict> any = dicts.stream().filter(x -> value.equals(x.getValue())).findAny();
|
||||||
return any.isPresent() ? any.get().get("label") + "" : "";
|
return any.isPresent() ? any.get().getLabel() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDictValue(String code, String label) {
|
public String getDictValue(String code, String label) {
|
||||||
Objects.requireNonNull(code, "code 不能为空");
|
Objects.requireNonNull(code, "code 不能为空");
|
||||||
Objects.requireNonNull(label, "label 不能为空");
|
Objects.requireNonNull(label, "label 不能为空");
|
||||||
|
|
||||||
List<Map> dicts = getDicts(code);
|
List<Dict> dicts = getDicts(code);
|
||||||
Optional<Map> any = dicts.stream().filter(x -> label.equals(x.get("label"))).findAny();
|
Optional<Dict> any = dicts.stream().filter(x -> label.equals(x.get("label"))).findAny();
|
||||||
return any.isPresent() ? any.get().get("value") + "" : "";
|
return any.isPresent() ? any.get().getValue() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Test
|
//@Test
|
||||||
|
|||||||
@@ -1,28 +1,36 @@
|
|||||||
package net.tccn.service;
|
package net.tccn.service;
|
||||||
|
|
||||||
import net.tccn.base.JBean;
|
import net.tccn.base.JBean;
|
||||||
|
import net.tccn.dict.Dict;
|
||||||
import net.tccn.dict.DictKit;
|
import net.tccn.dict.DictKit;
|
||||||
import org.redkale.net.http.RestMapping;
|
import org.redkale.net.http.RestMapping;
|
||||||
import org.redkale.net.http.RestParam;
|
import org.redkale.net.http.RestParam;
|
||||||
import org.redkale.net.http.RestService;
|
import org.redkale.net.http.RestService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典服务
|
* 字典服务
|
||||||
|
*
|
||||||
* @author: liangxianyou
|
* @author: liangxianyou
|
||||||
*/
|
*/
|
||||||
@RestService(name = "dict", automapping = true, comment = "字典服务")
|
@RestService(name = "dict", automapping = true, comment = "字典服务")
|
||||||
public class DictService extends BaseService {
|
public class DictService extends BaseService {
|
||||||
|
|
||||||
@RestMapping(name = "list")
|
@RestMapping(name = "list")
|
||||||
public JBean list(@RestParam(name = "platToken") String token) {
|
public JBean list(@RestParam(name = "platToken") String token, String type) {
|
||||||
JBean jBean = new JBean();
|
JBean jBean = new JBean();
|
||||||
DictKit dictKit = DictKit.use(token);
|
DictKit dictKit = DictKit.use(token);
|
||||||
|
|
||||||
|
if (!isEmpty(type)) {
|
||||||
|
List<Dict> dicts = dictKit.getDicts(type);
|
||||||
|
jBean.setBody(dicts);
|
||||||
|
} else {
|
||||||
|
Map dicts = dictKit.getDicts();
|
||||||
|
jBean.setBody(dicts);
|
||||||
|
}
|
||||||
|
|
||||||
Map dicts = dictKit.getDicts();
|
|
||||||
jBean.setBody(dicts);
|
|
||||||
return jBean;
|
return jBean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user