1、重构代码将数据处理全部交给 repository 来操作
2、升级支持免 Db启动提供服务
This commit is contained in:
@@ -12,13 +12,22 @@ import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import net.tccn.plat.DbPlat;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import net.tccn.user.User;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Comment;
|
||||
import org.redkale.util.TypeToken;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
*
|
||||
* Created by liangxianyou at 2019/1/7 13:31.
|
||||
@@ -31,19 +40,23 @@ public final class MetaKit {
|
||||
private static List<MetaLink> metaLinks;
|
||||
@Getter
|
||||
private static List<MetaService> metaServices;
|
||||
|
||||
@Getter
|
||||
private static List<DbAccount> dbPlats;
|
||||
@Getter
|
||||
private static List<SysPlat> sysPlats;
|
||||
@Getter
|
||||
private static List<User> users;
|
||||
|
||||
static {
|
||||
metaTables = MetaTable.dao.find();
|
||||
metaLinks = MetaLink.dao.find();
|
||||
metaServices = MetaService.dao.find();
|
||||
|
||||
dbPlats = DbAccount.dao.find();
|
||||
sysPlats = SysPlat.dao.find();
|
||||
protected static String dcate;
|
||||
private static final JsonConvert convert = JsonConvert.root();
|
||||
// -----------------------------------
|
||||
protected static void init() {
|
||||
reload(MetaTable.class);
|
||||
reload(MetaLink.class);
|
||||
reload(MetaService.class);
|
||||
reload(DbAccount.class);
|
||||
reload(SysPlat.class);
|
||||
reload(User.class);
|
||||
}
|
||||
|
||||
public static <T extends Doc> void reload(Class<T> clazz) {
|
||||
@@ -54,13 +67,80 @@ public final class MetaKit {
|
||||
}
|
||||
public static <T extends Doc> void reload(Class<T> clazz, String key) {
|
||||
|
||||
if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
|
||||
else if (MetaLink.class == clazz) metaLinks = MetaLink.dao.find();
|
||||
else if (MetaService.class == clazz) metaServices = MetaService.dao.find();
|
||||
else if (DbAccount.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (DbPlat.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (SysPlat.class == clazz) sysPlats = SysPlat.dao.find();
|
||||
// todo: 按照key 查询更新内存
|
||||
try {
|
||||
File file = new File(String.format("tmp/%s.json", clazz.getSimpleName()));
|
||||
if ("file".equals(dcate)) {
|
||||
if (MetaTable.class == clazz) {
|
||||
Type type = new TypeToken<List<MetaTable>>() {}.getType();
|
||||
metaTables = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (MetaLink.class == clazz) {
|
||||
Type type = new TypeToken<List<MetaLink>>() {}.getType();
|
||||
metaLinks = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (MetaService.class == clazz) {
|
||||
Type type = new TypeToken<List<MetaService>>() {}.getType();
|
||||
metaServices = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (DbAccount.class == clazz) {
|
||||
Type type = new TypeToken<List<DbAccount>>() {}.getType();
|
||||
dbPlats = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (DbPlat.class == clazz) {
|
||||
Type type = new TypeToken<List<DbAccount>>() {}.getType();
|
||||
dbPlats = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (SysPlat.class == clazz) {
|
||||
Type type = new TypeToken<List<SysPlat>>() {}.getType();
|
||||
sysPlats = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
else if (User.class == clazz) {
|
||||
Type type = new TypeToken<List<User>>() {}.getType();
|
||||
users = convert.convertFrom(type, new FileInputStream(file));
|
||||
}
|
||||
} else {
|
||||
if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
|
||||
else if (MetaLink.class == clazz) metaLinks = MetaLink.dao.find();
|
||||
else if (MetaService.class == clazz) metaServices = MetaService.dao.find();
|
||||
else if (DbAccount.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (DbPlat.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (SysPlat.class == clazz) sysPlats = SysPlat.dao.find();
|
||||
else if (User.class == clazz) users = User.dao.find();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void cacheSave() {
|
||||
cacheSave(MetaLink.class);
|
||||
cacheSave(MetaService.class);
|
||||
cacheSave(DbAccount.class);
|
||||
cacheSave(DbPlat.class);
|
||||
cacheSave(SysPlat.class);
|
||||
cacheSave(User.class);
|
||||
}
|
||||
|
||||
private static void cacheSave(Class clazz) {
|
||||
List list = null;
|
||||
if (MetaTable.class == clazz) list = metaTables;
|
||||
else if (MetaLink.class == clazz) list = metaLinks;
|
||||
else if (MetaService.class == clazz) list = metaServices;
|
||||
else if (DbAccount.class == clazz) list = dbPlats;
|
||||
else if (DbPlat.class == clazz) list = dbPlats;
|
||||
else if (SysPlat.class == clazz) list = sysPlats;
|
||||
else if (User.class == clazz) list = users;
|
||||
|
||||
if (list == null || list.size() == 0) return;
|
||||
|
||||
File file = new File(String.format("tmp/%s.json", list.get(0).getClass().getSimpleName()));
|
||||
file.getParentFile().mkdirs();
|
||||
try {
|
||||
FileKit.strToFile(MetaKit.convert.convertTo(list), file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------
|
||||
@@ -83,39 +163,10 @@ public final class MetaKit {
|
||||
return any.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 MetaLink[] 得到 MetaTable{}
|
||||
* @param links
|
||||
* @return {alias:table}
|
||||
*/
|
||||
private static Map<String, MetaTable> metaTables(List<MetaLink> links) {
|
||||
Map map = new HashMap();
|
||||
List<String> tables = links.stream().map(MetaLink::getTables).flatMap(Arrays::stream).distinct().collect(Collectors.toList());//tables
|
||||
metaTables.stream().filter(x -> tables.contains(x.getAlias())).forEach(x -> {
|
||||
map.put(x.getAlias(), x);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 link._key 得到 MetaLink[]
|
||||
* @param links
|
||||
* @return
|
||||
*/
|
||||
private static List<MetaLink> metaLinks(List<String> links) {
|
||||
if (links == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return metaLinks.stream().filter(x -> links.contains(x.getKey())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建完整的 MetaService
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static MetaService getMetaService(String name, String token) {
|
||||
Optional<MetaService> service = metaServices.stream().filter(x -> x.getName().equals(name)).findAny();
|
||||
Optional<MetaService> service = metaServices.stream()
|
||||
.filter(x -> x.getName().equals(name) && x.getSysPlatId().equals(getPlatId(token)))
|
||||
.findAny();
|
||||
return service.orElse(null);
|
||||
}
|
||||
|
||||
@@ -258,7 +309,7 @@ public final class MetaKit {
|
||||
.set("details", _details);
|
||||
}
|
||||
|
||||
@Comment("业务导出表头配置")
|
||||
@Comment("获取导出excel表头配置k-v")
|
||||
public static Kv cfgExport(String name, String token) {
|
||||
MetaService metaService = getMetaService(name, token);
|
||||
List<Map<String,String>> exports = metaService.getExports();
|
||||
@@ -399,11 +450,8 @@ public final class MetaKit {
|
||||
public static String lastAlias;
|
||||
public static String nextAlias() {
|
||||
if (lastAlias == null) {
|
||||
lastAlias = MetaTable.dao.findFirst(
|
||||
"for d in MetaTable\n" +
|
||||
"sort length(d.alias) desc, d.alias desc\n" +
|
||||
"limit 1\n" +
|
||||
"return d.alias", String.class);
|
||||
String aql = TplKit.use(true).getTpl("metaTable.lastAlias");
|
||||
lastAlias = MetaTable.dao.findFirst(aql, String.class);
|
||||
}
|
||||
return lastAlias = next(lastAlias, "");
|
||||
}
|
||||
@@ -422,4 +470,51 @@ public final class MetaKit {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> tableExist(String[] tableArr, String token) {
|
||||
List<String> _tableArr = asList(tableArr);
|
||||
List<String> hv = metaTables.stream()
|
||||
.filter(x -> _tableArr.contains(x.getName()) && x.getSysPlatId().equals(getPlatId(token)))
|
||||
.map(MetaTable::getName)
|
||||
.collect(Collectors.toList());
|
||||
return hv;
|
||||
}
|
||||
|
||||
// ---------------------- repository -------------------
|
||||
public static <T extends Doc> void save(T ... ts) {
|
||||
Set<Class<T>> clazzs = new HashSet<>();
|
||||
for (T t : ts) {
|
||||
if (t.getKey() == null) {
|
||||
t.save();
|
||||
} else {
|
||||
t.update();
|
||||
}
|
||||
|
||||
clazzs.add((Class<T>) t.getClass());
|
||||
}
|
||||
clazzs.forEach(x -> reload(x));
|
||||
}
|
||||
|
||||
public static <T extends Doc> T findFirst(T t) {
|
||||
Objects.nonNull(t);
|
||||
|
||||
List<T> list = asList();
|
||||
Map doc = t.toDoc();
|
||||
if (t instanceof User) {
|
||||
list = (List<T>) users;
|
||||
}
|
||||
|
||||
Optional any = list.stream().filter(x -> {
|
||||
Map map = x.toDoc();
|
||||
Set<String> keySet = doc.keySet();
|
||||
for (String k : keySet) {
|
||||
if (doc.get(k) != null && (doc.get(k) instanceof String && doc.get(k).equals(map.get(k)))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}).findAny();
|
||||
|
||||
return (T) any.orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user