1、重构代码将数据处理全部交给 repository 来操作

2、升级支持免 Db启动提供服务
This commit is contained in:
2019-04-26 20:04:52 +08:00
parent 9a38d69eb0
commit ff240dd3ea
17 changed files with 350 additions and 212 deletions

View File

@@ -3,6 +3,7 @@
<application port="5001"> <application port="5001">
<resources> <resources>
<listener value="net.tccn.base.MetaListenter"/>
<properties load="config.txt"></properties> <properties load="config.txt"></properties>
</resources> </resources>

View File

@@ -20,3 +20,6 @@ arango.database=db_dev
#--------------- arango -------------- #--------------- arango --------------
tplPath=/tpl tplPath=/tpl
# file|db
dcate=db

View File

@@ -63,7 +63,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button @click="row={}" type="button" class="btn btn-default" data-dismiss="modal">重置</button> <button @click="row={}" type="button" class="btn btn-default" data-dismiss="modal">重置</button>
<button @click="loginCheck(row)" type="button" class="btn btn-primary">确定</button> <button @click="login(row)" type="button" class="btn btn-primary">确定</button>
</div> </div>
</div> </div>
</div> </div>
@@ -142,41 +142,6 @@
}) })
}) })
}, },
/*loginCheck: function ({username, pwd}) {
/!*login({username, pwd}, function (json) {
if (json.code == 0) {
platList().then(res => {
let rows = res.rows
red.showMsg({msg:"请选择业务平台"})
vm.sysPlats = rows;
if (!vm.sysPlat) {
vm.sysPlat = rows[0];
}
red.setData("sysPlats", rows);
setTimeout(function () {
vm.choose = true;
}, 500);
})
} else {
red.showMsg({msg: json.message})
}
});*!/
},*/
/*loadPlats: function () {
var para = {
doc:"sys_plat",
shows: JSON.stringify(["_id", "_key", "field", "token","remark", "status"])
};
red.getJSON("/meta/plat_list", {}, function (json) {
vm.sysPlats = json;
if (!vm.sysPlat) {
vm.sysPlat = json[0];
}
red.setData("sysPlats", json);
});
},*/
goIndex: function () { goIndex: function () {
if (!this.sysPlat) { if (!this.sysPlat) {
red.showMsg({msg:"请选择选择业务平台", type:"error"}); red.showMsg({msg:"请选择选择业务平台", type:"error"});

View File

@@ -12,13 +12,22 @@ import net.tccn.meta.MetaService;
import net.tccn.meta.MetaTable; import net.tccn.meta.MetaTable;
import net.tccn.plat.DbPlat; import net.tccn.plat.DbPlat;
import net.tccn.plat.SysPlat; 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.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.*;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Arrays.asList;
/** /**
* *
* Created by liangxianyou at 2019/1/7 13:31. * Created by liangxianyou at 2019/1/7 13:31.
@@ -31,19 +40,23 @@ public final class MetaKit {
private static List<MetaLink> metaLinks; private static List<MetaLink> metaLinks;
@Getter @Getter
private static List<MetaService> metaServices; private static List<MetaService> metaServices;
@Getter @Getter
private static List<DbAccount> dbPlats; private static List<DbAccount> dbPlats;
@Getter @Getter
private static List<SysPlat> sysPlats; private static List<SysPlat> sysPlats;
@Getter
private static List<User> users;
static { protected static String dcate;
metaTables = MetaTable.dao.find(); private static final JsonConvert convert = JsonConvert.root();
metaLinks = MetaLink.dao.find(); // -----------------------------------
metaServices = MetaService.dao.find(); protected static void init() {
reload(MetaTable.class);
dbPlats = DbAccount.dao.find(); reload(MetaLink.class);
sysPlats = SysPlat.dao.find(); reload(MetaService.class);
reload(DbAccount.class);
reload(SysPlat.class);
reload(User.class);
} }
public static <T extends Doc> void reload(Class<T> clazz) { 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) { public static <T extends Doc> void reload(Class<T> clazz, String 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(); if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
else if (MetaLink.class == clazz) metaLinks = MetaLink.dao.find(); else if (MetaLink.class == clazz) metaLinks = MetaLink.dao.find();
else if (MetaService.class == clazz) metaServices = MetaService.dao.find(); else if (MetaService.class == clazz) metaServices = MetaService.dao.find();
else if (DbAccount.class == clazz) dbPlats = DbAccount.dao.find(); else if (DbAccount.class == clazz) dbPlats = DbAccount.dao.find();
else if (DbPlat.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 (SysPlat.class == clazz) sysPlats = SysPlat.dao.find();
// todo: 按照key 查询更新内存 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(); 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) { 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); return service.orElse(null);
} }
@@ -258,7 +309,7 @@ public final class MetaKit {
.set("details", _details); .set("details", _details);
} }
@Comment("业务导出表头配置") @Comment("获取导出excel表头配置k-v")
public static Kv cfgExport(String name, String token) { public static Kv cfgExport(String name, String token) {
MetaService metaService = getMetaService(name, token); MetaService metaService = getMetaService(name, token);
List<Map<String,String>> exports = metaService.getExports(); List<Map<String,String>> exports = metaService.getExports();
@@ -399,11 +450,8 @@ public final class MetaKit {
public static String lastAlias; public static String lastAlias;
public static String nextAlias() { public static String nextAlias() {
if (lastAlias == null) { if (lastAlias == null) {
lastAlias = MetaTable.dao.findFirst( String aql = TplKit.use(true).getTpl("metaTable.lastAlias");
"for d in MetaTable\n" + lastAlias = MetaTable.dao.findFirst(aql, String.class);
"sort length(d.alias) desc, d.alias desc\n" +
"limit 1\n" +
"return d.alias", String.class);
} }
return lastAlias = next(lastAlias, ""); return lastAlias = next(lastAlias, "");
} }
@@ -422,4 +470,51 @@ public final class MetaKit {
} }
return null; 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);
}
} }

View File

@@ -0,0 +1,35 @@
package net.tccn.base;
import org.redkale.boot.Application;
import org.redkale.boot.ApplicationListener;
import org.redkale.util.ResourceFactory;
import javax.annotation.Resource;
import java.io.File;
/**
* @author: liangxianyou
*/
public class MetaListenter implements ApplicationListener {
@Resource(name = "property.dcate")
private String dcate;
@Resource(name = "property.tplPath")
private String tplPath;
@Override
public void preStart(Application application) {
ResourceFactory rf = application.getResourceFactory();
rf.inject(this);
MetaKit.dcate = dcate;
MetaKit.init();
TplKit.use(true).addTpl(new File(FileKit.rootPath(), tplPath));
}
@Override
public void preShutdown(Application application) {
MetaKit.cacheSave();
}
}

View File

@@ -159,9 +159,13 @@ public abstract class Doc<T extends Doc> {
sourceName = source.name(); sourceName = source.name();
} }
try {
arangoSource = ArangoSource.use(sourceName); arangoSource = ArangoSource.use(sourceName);
this.db = arangoSource.db(table.catalog()); this.db = arangoSource.db(table.catalog());
this.collection = arangoSource.collection(this); this.collection = arangoSource.collection(this);
} catch (Exception e) {
}
} }
protected final static <T extends Doc> T dao(Class<T> type) { protected final static <T extends Doc> T dao(Class<T> type) {

View File

@@ -1,6 +1,7 @@
package net.tccn.dbq.jdbc.api; package net.tccn.dbq.jdbc.api;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
/** /**
* Created by liangxianyou at 2019/3/12 14:11. * Created by liangxianyou at 2019/3/12 14:11.
@@ -58,4 +59,18 @@ public class DbKit implements DbSource{
public void exetute(String sql) { public void exetute(String sql) {
dbSource.exetute(sql); dbSource.exetute(sql);
} }
// -----------------------------------------
public <T> CompletableFuture<T> findfirstAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> findfirst(sql, type));
}
public <T> CompletableFuture<List<T>> findListAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> findList(sql, type));
}
public <T> CompletableFuture<T> queryColumnAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> queryColumn(sql, type));
}
public CompletableFuture<Void> exetuteAsync(String sql) {
return CompletableFuture.runAsync(() -> exetute(sql));
}
} }

View File

@@ -1,7 +1,6 @@
package net.tccn.service; package net.tccn.service;
import com.arangodb.Predicate; import com.arangodb.Predicate;
import net.tccn.base.FileKit;
import net.tccn.base.MetaKit; import net.tccn.base.MetaKit;
import net.tccn.base.TplKit; import net.tccn.base.TplKit;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
@@ -56,9 +55,6 @@ public class BaseService implements Service {
@Resource(name = "APP_HOME") @Resource(name = "APP_HOME")
protected File APP_HOME; protected File APP_HOME;
@Resource(name = "property.tplPath")
private String tplPath;
public static Properties prop = new Properties(); public static Properties prop = new Properties();
protected static TplKit tplKit = TplKit.use(true); protected static TplKit tplKit = TplKit.use(true);
@@ -78,7 +74,7 @@ public class BaseService implements Service {
try { try {
if (!tplInit) { if (!tplInit) {
tplInit = true; tplInit = true;
tplKit.addTpl(new File(FileKit.rootPath(), tplPath)); //tplKit.addTpl(new File(FileKit.rootPath(), tplPath));
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -97,14 +97,10 @@ public class MetadataService extends BaseService { //arango
break; break;
} }
if (service.getKey() != null) { if (service.getKey() == null) {
service.update();
} else {
service.setSysPlatId(platId(token)); service.setSysPlatId(platId(token));
service.save();
} }
MetaKit.reload(MetaService.class); MetaKit.save(service);
} while (false); } while (false);
return jBean; return jBean;
} }
@@ -125,13 +121,14 @@ public class MetadataService extends BaseService { //arango
//修改item的排序 //修改item的排序
@RestMapping(name = "itemsort", comment = "字段排序") @RestMapping(name = "itemsort", comment = "字段排序")
public String[] itemSortSave(String alias , String[] items, @RestParam(name = "platToken") String token) { public JBean itemSortSave(String alias , String[] items, @RestParam(name = "platToken") String token) {
MetaTable metaTable = MetaKit.getMetaTableByAlias(alias); MetaTable metaTable = MetaKit.getMetaTableByAlias(alias);
MetaKit.sortItem.apply(metaTable, items); MetaKit.sortItem.apply(metaTable, items);
metaTable.update();
return items; MetaKit.save(metaTable);
return JBean.OK;
} }
@RestMapping(name = "itemupdate", comment = "字段修改") @RestMapping(name = "itemupdate", comment = "字段修改")
@@ -139,8 +136,7 @@ public class MetadataService extends BaseService { //arango
MetaTable metaTable = MetaKit.getMetaTableByAlias(alias); MetaTable metaTable = MetaKit.getMetaTableByAlias(alias);
MetaKit.itemUpdate.apply(metaTable, items); MetaKit.itemUpdate.apply(metaTable, items);
metaTable.update(); MetaKit.save(metaTable);
MetaKit.reload(metaTable);
return JBean.OK; return JBean.OK;
} }
@@ -149,10 +145,8 @@ public class MetadataService extends BaseService { //arango
if (shows == null || shows.size() == 0) return null; if (shows == null || shows.size() == 0) return null;
MetaService metaService = MetaKit.getMetaService(name, token); MetaService metaService = MetaKit.getMetaService(name, token);
metaService.setShows(shows); metaService.setShows(shows);
metaService.update(); MetaKit.save(metaService);
MetaKit.reload(metaService);
return JBean.OK; return JBean.OK;
} }
@@ -162,20 +156,20 @@ public class MetadataService extends BaseService { //arango
MetaService metaService = MetaKit.getMetaService(name, token); MetaService metaService = MetaKit.getMetaService(name, token);
metaService.setExports(exports); metaService.setExports(exports);
metaService.update();
MetaKit.reload(metaService); MetaKit.save(metaService);
return JBean.OK; return JBean.OK;
} }
@RestMapping(name = "importsort", comment = "导入字段保存") @RestMapping(name = "importsort", comment = "导入字段保存")
public List<String> importSort(String serviceKey , List<String> items, @RestParam(name = "platToken") String token) { public JBean importSort(String serviceKey , List<String> items, @RestParam(name = "platToken") String token) {
if (isEmpty.test(items)) return null; if (isEmpty.test(items)) return null;
MetaTable metaTable = MetaKit.getMetaTable(serviceKey, token); MetaTable metaTable = MetaKit.getMetaTable(serviceKey, token);
//fixme: metaTable.setImports(items); //fixme: metaTable.setImports(items);
metaTable.update();
return items; MetaKit.save(metaTable);
return JBean.OK;
} }
@RestMapping(name = "dbplatupdate", comment = "数据平台修改") @RestMapping(name = "dbplatupdate", comment = "数据平台修改")
@@ -187,8 +181,8 @@ public class MetadataService extends BaseService { //arango
_metaTable.setDbPlatId(metaTable.getDbPlatId()); _metaTable.setDbPlatId(metaTable.getDbPlatId());
_metaTable.setCatalog(metaTable.getCatalog()); _metaTable.setCatalog(metaTable.getCatalog());
_metaTable.update(); MetaKit.save(_metaTable);
return JBean.by(0, ""); return JBean.OK;
} }
@RestMapping(name = "filter_update", comment = "查询配置修改") @RestMapping(name = "filter_update", comment = "查询配置修改")
@@ -196,18 +190,17 @@ public class MetadataService extends BaseService { //arango
MetaService metaService = MetaKit.getMetaService(name, token); MetaService metaService = MetaKit.getMetaService(name, token);
metaService.setFilters(filters); metaService.setFilters(filters);
metaService.update(); MetaKit.save(metaService);
return JBean.by(0, ""); return JBean.OK;
} }
@RestMapping(name = "table_link_list", comment = "实体表包含link信息的列表,(metalink 管理页面使用)") @RestMapping(name = "table_link_list", comment = "实体表包含link信息的列表,(metalink 管理页面使用)")
public JBean tableLinkList(@RestParam(name = "platToken") String token) { public JBean tableLinkList(@RestParam(name = "platToken") String token) {
JBean jBean = new JBean(); JBean jBean = new JBean();
List<Kv> list = List<Kv> list = MetaKit.getMetaTables().stream()
MetaKit.getMetaTables().stream().filter(x -> .filter(x -> (isEmpty.test(token) || x.getSysPlatId().equals(platId(token))))
(isEmpty.test(token) || x.getSysPlatId().equals(platId(token))) .map(x -> {
).map(x -> {
Kv kv = Kv.of("name", x.getName()) Kv kv = Kv.of("name", x.getName())
.set("comment", x.getComment()) .set("comment", x.getComment())
.set("alias", x.getAlias()) .set("alias", x.getAlias())
@@ -265,12 +258,10 @@ public class MetadataService extends BaseService { //arango
public JBean linkSave(MetaLink link, @RestParam(name = "platToken") String token) { public JBean linkSave(MetaLink link, @RestParam(name = "platToken") String token) {
if (link.getKey() != null) { if (link.getKey() != null) {
link.find(String.format("UPDATE '%s' WITH { link:null } IN MetaLink", link.getKey()), Map.class); //避免删除属性无效 link.find(String.format("UPDATE '%s' WITH { link:null } IN MetaLink", link.getKey()), Map.class); //避免删除属性无效
link.update(); // fixme: 将逻辑迁移到 MetaKit中:
} else {
link.save();
} }
MetaKit.reload(MetaLink.class);
MetaKit.save(link);
return JBean.OK; return JBean.OK;
} }
@@ -278,19 +269,17 @@ public class MetadataService extends BaseService { //arango
@RestMapping(name = "plat_list", comment = "平台列表") @RestMapping(name = "plat_list", comment = "平台列表")
public JBean platList() { public JBean platList() {
JBean jBean = new JBean(); JBean jBean = new JBean();
List<SysPlat> plats = SysPlat.dao.find(); List<SysPlat> plats = MetaKit.getSysPlats();
jBean.setBody(plats); return jBean.setBody(plats);
return jBean;
} }
// ------------------------------------ 对外服务 -------------------------------------- // ------------------------------------ 对外服务 --------------------------------------
@RestMapping(name = "cfg", auth = false, comment = " 功能配置") @RestMapping(name = "cfg", auth = false, comment = " 功能配置")
public JBean cfg(String name, @RestParam(name = "platToken") String token) { public JBean cfg(String name, @RestParam(name = "platToken") String token) {
JBean jBean = JBean.by(0, ""); JBean jBean = new JBean();
Map cfg = MetaKit.cfg(name, token);
jBean.set(0, "", MetaKit.cfg(name, token)); return jBean.setBody(cfg);
return jBean;
} }
} }

View File

@@ -29,13 +29,8 @@ public class PlatService extends BaseService {
@Comment("平台信息保存") @Comment("平台信息保存")
public JBean save(SysPlat plat) { public JBean save(SysPlat plat) {
if (plat.getKey() == null) { MetaKit.save(plat);
plat.save(); return JBean.OK;
} else {
plat.update();
}
return new JBean();
} }
@RestMapping(name = "info", comment = "平台详情") @RestMapping(name = "info", comment = "平台详情")
@@ -49,8 +44,6 @@ public class PlatService extends BaseService {
public JBean dbList(DbPlat plat, Flipper flipper) { public JBean dbList(DbPlat plat, Flipper flipper) {
JBean jBean = new JBean(); JBean jBean = new JBean();
//PageBean<DbPlat> page = DbPlat.dao.findPage(plat, flipper);
List<DbAccount> list = MetaKit.getDbPlats(); List<DbAccount> list = MetaKit.getDbPlats();
PageBean page = PageBean.by(list, list.size()); PageBean page = PageBean.by(list, list.size());
@@ -59,16 +52,9 @@ public class PlatService extends BaseService {
@RestMapping(name = "db_save", comment = "数据源信息保存") @RestMapping(name = "db_save", comment = "数据源信息保存")
public JBean dbSave(DbPlat plat) { public JBean dbSave(DbPlat plat) {
//DbAccount dbPlat = MetaKit.getDbPlat(plat.getKey()); MetaKit.save(plat);
if (plat.getKey() == null) { return JBean.OK;
plat.save();
} else {
plat.update();
}
MetaKit.reload(DbPlat.class);
return new JBean();
} }

View File

@@ -1,6 +1,7 @@
package net.tccn.service; package net.tccn.service;
import net.tccn.base.JBean; import net.tccn.base.JBean;
import net.tccn.base.MetaKit;
import net.tccn.user.User; import net.tccn.user.User;
import org.redkale.net.http.RestMapping; import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestService; import org.redkale.net.http.RestService;
@@ -19,7 +20,7 @@ public class UserService extends BaseService {
User bean = new User(); User bean = new User();
bean.setUsername(username); bean.setUsername(username);
User user = User.dao.findFirst(bean); User user = MetaKit.findFirst(bean);
if (user == null) { if (user == null) {
return JBean.by(-1, "登陆失败:账号无效"); return JBean.by(-1, "登陆失败:账号无效");
} }
@@ -30,7 +31,7 @@ public class UserService extends BaseService {
user.setSessionid(sessionid); user.setSessionid(sessionid);
user.setLoginTime(System.currentTimeMillis()); user.setLoginTime(System.currentTimeMillis());
user.update(); MetaKit.save(user);
} }
return jBean; return jBean;
@@ -38,19 +39,19 @@ public class UserService extends BaseService {
@RestMapping(name = "current") @RestMapping(name = "current")
public User current(@RestSessionid String sessionid) { public User current(@RestSessionid String sessionid) {
return getT("user_" + sessionid, User.class, () -> User.dao.findFirst(new User(sessionid))); return getT("user_" + sessionid, User.class, () -> MetaKit.findFirst(new User(sessionid)));
} }
@RestMapping(name = "logout", comment = "退出登陆") @RestMapping(name = "logout", comment = "退出登陆")
public JBean logout(@RestSessionid String sessionid) { public JBean logout(@RestSessionid String sessionid) {
User user = User.dao.findFirst(new User(sessionid)); User user = MetaKit.findFirst(new User(sessionid));
if (user != null) { if (user != null) {
user.setSessionid(""); user.setSessionid("");
user.update(); MetaKit.save(user);
} }
cacheSource.removeAsync("user_" + sessionid); cacheSource.removeAsync("user_" + sessionid);
return new JBean(); return JBean.OK;
} }
} }

View File

@@ -76,8 +76,8 @@ public class _DbService extends BaseService {
String sql = tplKit.getTpl("db.table_list", Kv.of("table", tableName)); String sql = tplKit.getTpl("db.table_list", Kv.of("table", tableName));
String columnSql = String.format("SHOW FULL COLUMNS FROM %s.`%s`", catalog, tableName); String columnSql = String.format("SHOW FULL COLUMNS FROM %s.`%s`", catalog, tableName);
CompletableFuture<Table> tableFuture = CompletableFuture.supplyAsync(() -> dbKit.findfirst(sql, Table.class)); CompletableFuture<Table> tableFuture = dbKit.findfirstAsync(sql, Table.class);
CompletableFuture<List<Column>> columnFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(columnSql, Column.class)); CompletableFuture<List<Column>> columnFuture = dbKit.findListAsync(columnSql, Column.class);
try { try {
Table table = tableFuture.get(); Table table = tableFuture.get();

View File

@@ -75,7 +75,6 @@ public class _FileService extends BaseService {
buf.deleteCharAt(buf.length() - 1); buf.deleteCharAt(buf.length() - 1);
buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" + buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
" return d.name"); " return d.name");
List<String> hv = MetaTable.dao.find(buf.toString(), String.class); List<String> hv = MetaTable.dao.find(buf.toString(), String.class);
Kv res = Kv.of(); Kv res = Kv.of();

View File

@@ -8,13 +8,10 @@ import net.tccn.meta.MetaTable;
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 org.redkale.util.Comment;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/** /**
* 元数据实体管理 * 元数据实体管理
@@ -26,9 +23,9 @@ import java.util.concurrent.ExecutionException;
public class _TableService extends BaseService { public class _TableService extends BaseService {
@Resource @Resource
_FileService fileService; private _FileService fileService;
@Resource @Resource
_DbService dbService; private _DbService dbService;
@RestMapping(name = "sheets", comment = "导入选择列表数据准备") @RestMapping(name = "sheets", comment = "导入选择列表数据准备")
@@ -49,15 +46,12 @@ public class _TableService extends BaseService {
List<Table> list = dbService.tableList(dbPlatId, catalog, null); List<Table> list = dbService.tableList(dbPlatId, catalog, null);
String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new); String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new);
CompletableFuture<List<String>> hvFuture = tableExist(tableArr, token); List<String> hv = MetaKit.tableExist(tableArr, token);
try {
List<String> _hv = hvFuture.get();
List<MetaTable> sheets = new ArrayList<>(); List<MetaTable> sheets = new ArrayList<>();
list.forEach(x -> { list.forEach(x -> {
MetaTable bean = MetaTable.toAs(x); MetaTable bean = MetaTable.toAs(x);
bean.setHv(_hv.contains(x.getName()) ? 1 : 0); bean.setHv(hv.contains(x.getName()) ? 1 : 0);
sheets.add(bean); sheets.add(bean);
}); });
@@ -66,9 +60,6 @@ public class _TableService extends BaseService {
sheets.forEach(x -> data.set(x.getName(), x)); sheets.forEach(x -> data.set(x.getName(), x));
jBean.setBody(data); jBean.setBody(data);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} }
return jBean; return jBean;
@@ -89,31 +80,24 @@ public class _TableService extends BaseService {
String[] tableArr, String[] tableArr,
@RestParam(name = "platToken") String token) { @RestParam(name = "platToken") String token) {
CompletableFuture<List<String>> hvfuture = tableExist(tableArr, token); List<String> hv = MetaKit.tableExist(tableArr, token);
List<Table> tables = dbService.tableInfoList(dbPlatId, catalog, tableArr); List<Table> tables = dbService.tableInfoList(dbPlatId, catalog, tableArr);
try { MetaTable[] metaTables = tables.stream()
List<String> hvs = hvfuture.get(); .filter(t -> !hv.contains(t.getName())) // 去除同名
tables.forEach(t -> { .map(t -> {
if (!hvs.contains(t.getName())) {
MetaTable metaTable = MetaTable.toAs(t); MetaTable metaTable = MetaTable.toAs(t);
metaTable.setCatalog(catalog); metaTable.setCatalog(catalog);
metaTable.setDbPlatId(dbPlatId); metaTable.setDbPlatId(dbPlatId);
metaTable.setAlias(MetaKit.nextAlias());//todo: 表别名 metaTable.setAlias(MetaKit.nextAlias());// 表别名
metaTable.setSysPlatId(platId(token)); metaTable.setSysPlatId(platId(token));
return metaTable;
}).toArray(MetaTable[]::new);
//保存数据到元数据表 MetaKit.save(metaTables);
metaTable.save();
}
});
MetaKit.reload(MetaTable.class);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} }
} /*@Comment("查询元数据中存在的表")
@Comment("查询元数据中存在的表")
private CompletableFuture<List<String>> tableExist(String[] tableArr, String token) { private CompletableFuture<List<String>> tableExist(String[] tableArr, String token) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
@@ -128,5 +112,5 @@ public class _TableService extends BaseService {
List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
return hv; return hv;
}); });
} }*/
} }

View File

@@ -3,8 +3,6 @@ package net.tccn.user;
import lombok.Data; import lombok.Data;
import net.tccn.base.JBean; import net.tccn.base.JBean;
import net.tccn.base.arango.Doc; import net.tccn.base.arango.Doc;
import org.redkale.convert.ConvertColumn;
import org.redkale.convert.ConvertType;
import org.redkale.util.Utility; import org.redkale.util.Utility;
import javax.persistence.Table; import javax.persistence.Table;
@@ -18,7 +16,7 @@ public class User extends Doc<User> {
public static User dao = dao(User.class); public static User dao = dao(User.class);
private String username; private String username;
@ConvertColumn(ignore = true,type = ConvertType.JSON) //@ConvertColumn(ignore = true,type = ConvertType.JSON)
private String pwd; private String pwd;
private Long createTime; private Long createTime;
private Long loginTime; private Long loginTime;
@@ -43,6 +41,9 @@ public class User extends Doc<User> {
public JBean checkLogin(String pwd) { public JBean checkLogin(String pwd) {
JBean jBean = JBean.by(0, ""); JBean jBean = JBean.by(0, "");
if (this.pwd == null || this.pwd.isEmpty()) {
jBean.set(-1, "密码错误");
}
if (!this.pwd.equalsIgnoreCase(md5IfNeed(pwd))) { if (!this.pwd.equalsIgnoreCase(md5IfNeed(pwd))) {
jBean.set(-1, "密码错误"); jBean.set(-1, "密码错误");
} else if (status != 1) { } else if (status != 1) {

View File

@@ -4,3 +4,10 @@
#sql("link.update") #sql("link.update")
UPDATE "#(key)" WITH { link:null } IN Characters UPDATE "#(key)" WITH { link:null } IN Characters
#end #end
#sql("metaTable.lastAlias")
for d in MetaTable
sort length(d.alias) desc, d.alias desc
limit 1
return d.alias
#end

View File

@@ -13,8 +13,13 @@ import net.tccn.qtask.TaskKit;
import net.tccn.user.User; import net.tccn.user.User;
import org.junit.Test; import org.junit.Test;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
import org.redkale.source.CacheMemorySource;
import org.redkale.util.TypeToken;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -243,18 +248,70 @@ public class RunTest<T> {
List<MetaService> metaServices = MetaService.dao.find(); List<MetaService> metaServices = MetaService.dao.find();
MetaKit metaKit = new MetaKit();
JsonConvert convert = JsonConvert.root(); try {
System.out.println(convert.convertTo(metaServices)); File file = new File("tmp/metaKit.json");
file.getParentFile().mkdirs();
MetaService metaService = new MetaService(); FileKit.strToFile(convert.convertTo(metaKit), file);
/*try {
FileKit.strToFile(gson.toJson(metaServices), file);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }
}
@Test
public void readJson() {
File file = new File("tmp/MetaService.json");
try {
Type type = new TypeToken<List<MetaService>>() {
}.getType();
List<MetaService> list = convert.convertFrom(type, new FileInputStream(file));
System.out.println(list);
} catch (IOException e) {
e.printStackTrace();
}
Class clazz = MetaService.class;
//File file = new File(String.format("tmp/%s.json", clazz.getSimpleName()));
/*
写入数据到 文件
MetaKit.cacheSave(MetaTable.class);
MetaKit.cacheSave(MetaLink.class);
MetaKit.cacheSave(MetaService.class);
MetaKit.cacheSave(DbAccount.class);
MetaKit.cacheSave(SysPlat.class);*/
}
//@Test
public void cacheMemorySourceTest() {
CacheMemorySource source = new CacheMemorySource();
//MetaKit.dcate = "db";
//MetaKit.init();
List<MetaTable> list = MetaKit.getMetaTables();
list.forEach(x -> {
source.set(x.getKey(), x.getClass(), x);
});
MetaTable metaTable = (MetaTable) source.get(list.get(0).getKey(), MetaTable.class);
metaTable.setAlias("xxxxx");
metaTable = (MetaTable) source.get(list.get(0).getKey(), MetaTable.class);
System.out.println(metaTable);
} }
} }