package net.tccn.meta;
import net.tccn.base.JBean;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.dbq.jdbc.api.DbAccount;
import net.tccn.dbq.table.Table;
import net.tccn.service.BaseService;
import net.tccn.service._FileService;
import org.redkale.net.http.RestMapping;
import org.redkale.net.http.RestParam;
import org.redkale.net.http.RestService;
import org.redkale.util.Comment;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* 元数据实体管理
* 1、实体导入,
* 2、实体信息维护
* 3、数据库表管理
*/
@RestService(automapping = true, comment = "元数据实体管理")
public class _TableService extends BaseService {
@Resource
_FileService fileService;
@Resource
_DbService dbService;
@RestMapping(name = "sheets", comment = "导入选择列表数据准备")
public JBean sheets(String cate, //类型
//excel {文件地址}
String filePath,
//mysql {数据库连接账号、数据源id、数据库database数组}
String dbPlatId, String[] catalogs,
@RestParam(name = "platToken") String token) {
JBean jBean = new JBean();
if ("excel".equals(cate)) {
jBean.setBody(fileService.data(filePath, token));
} else if ("mysql".equals(cate)){
List
list = dbService.tableList(dbPlatId, catalogs);
String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new);
CompletableFuture> hvFuture = tableExist(tableArr, token);
try {
List _hv = hvFuture.get();
List sheets = new ArrayList<>();
list.forEach(x -> {
MetaTable bean = MetaTable.toAs(x);
bean.setHv(_hv.contains(x.getName()) ? 1 : 0);
sheets.add(bean);
});
//对数据分组后返回
Kv data = Kv.of();
sheets.forEach(x -> data.set(x.getName(), x));
jBean.setBody(data);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
return jBean;
}
/*public JBean tableInfo(DbAccount dbAccount,
String dbPlatId, String catalog, String tableName) {
dbService.tableInfo(dbAccount, dbPlatId, catalog, tableName)
return null;
}*/
@RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
public void tableSave(String dbPlatId,
String catalog,
String[] tableArr,
@RestParam(name = "platToken") String token) {
CompletableFuture> hvfuture = tableExist(tableArr, token);
List tables = dbService.tableList(dbPlatId, catalog, tableArr);
try {
List hvs = hvfuture.get();
tables.forEach(t -> {
if (!hvs.contains(t.getName())) {
MetaTable metaTable = MetaTable.toAs(t);
metaTable.setCatalog(catalog);
metaTable.setDbPlatId(dbPlatId);
metaTable.setAlias(MetaKit.nextAlias());//todo: 表别名
metaTable.setSysPlatId(platId(token));
//保存数据到元数据表
metaTable.save();
}
});
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
@Comment("查询元数据中存在的表")
private CompletableFuture> tableExist(String[] tableArr, String token) {
return CompletableFuture.supplyAsync(() -> {
StringBuffer buf = new StringBuffer();
buf.append("for d in MetaTable\n" +
" filter d.name in [");
for (String x : tableArr) {
buf.append("'").append(x).append("',");
}
buf.deleteCharAt(buf.length() - 1);
buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
" return d.name");
List hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
return hv;
});
}
}