This commit is contained in:
2019-05-06 14:03:27 +08:00
parent 6a40917b6f
commit 5f41a320f1
11 changed files with 226 additions and 162 deletions

View File

@@ -8,8 +8,14 @@ const table = {
sheetMySql({dbPlatId, catalog}) {
return table._sheets({cate: "mysql", dbPlatId, catalog})
},
sheetInfo({filePath, sheetName}) {
return red.postX('/_table/sheet_info', {cate: 'excel', filePath, sheetName})
},
saveTable({dbPlatId, catalog, tableArr}) {
return red.postX('/_table/table_save', {dbPlatId, catalog, tableArr: JSON.stringify(tableArr)})
saveTable({dbPlatId, catalog, tableArr}) { //
return red.postX('/_table/table_save', {cate: 'mysql', dbPlatId, catalog, tableArr: JSON.stringify(tableArr)})
},
saveSheet({filePath, sheetNames}) {
return red.postX('/_table/table_save', {cate: 'excel', filePath, sheetNames: JSON.stringify(sheetNames)})
}
}

View File

@@ -56,13 +56,13 @@
</div>
<ul class="nav nav-tabs">
<li @click="cate = 'excel'"><a href="###" data-target="#tab2Content1" data-toggle="tab">从本地上传</a></li>
<li @click="cate = 'mysql'" class="active"><a href="###" data-target="#tab2Content2" data-toggle="tab">从数据源导入</a></li>
<li @click="cate = 'oth'"><a href="###" data-target="#tab2Content3" data-toggle="tab">其他</a></li>
<li @click="cate = 'excel'" :class="{active: cate=='excel'}"><a href="javascript:;">从本地上传</a></li>
<li @click="cate = 'mysql'" :class="{active: cate=='mysql'}"><a href="javascript:;">从数据源导入</a></li>
<li @click="cate = 'oth'" :class="{active: cate=='oth'}"><a href="javascript:;">其他</a></li>
</ul>
<div class="tab-content">
<!-- 本地excel导入 -->
<div class="tab-pane fade" id="tab2Content1">
<div :class="['tab-pane', 'fade','in',{active: cate=='excel'}]">
<div id="tableUploader" class="uploader">
<!--<div class="uploader-message text-center">
<div class="content"></div>
@@ -83,7 +83,7 @@
<td style="width: 38px; padding: 3px"><div class="file-icon"></div></td>
<td style="padding: 0">
<div style="position: relative; padding: 8px;">
<strong class="file-field"></strong>
<strong class="file-name"></strong>
<div class="file-progress-bar"></div>
</div>
</td>
@@ -120,7 +120,7 @@
<li class="checkbox-inline clearfix" v-for="item in sheetArr" >
<input type="checkbox"
v-model="ck"
:value="sheetData[item]['field']"
:value="sheetData[item]['name']"
:disabled="item=='表说明' || item=='表名称' || sheetData[item]['hv']==1"
>
<a :class="[{hv: sheetData[item]['hv']==1 }]" href="javascript:;" @click="sheet=item" data-target="#tab3Content1" data-toggle="tab" v-text="item"></a>
@@ -156,7 +156,7 @@
</thead>
<tbody>
<tr v-for="(item, index) in metaTable.items">
<td v-text="item.field"></td>
<td v-text="item.name"></td>
<td v-text="item.label"></td>
<td v-text="item.type"></td>
<td v-text="item.inType"></td>
@@ -182,7 +182,7 @@
</div>
<!-- 从数据源导入 -->
<div class="tab-pane fade active in" id="tab2Content2">
<div :class="['tab-pane', 'fade','in',{active: cate=='mysql'}]">
<div class="form-group">
<label class="col-md-1 col-sm-1" style="padding-bottom: 10px;padding-top: 5px;">数据源选择</label>
<div class="col-md-2 col-sm-10" style="padding-bottom: 10px">
@@ -250,7 +250,7 @@
</div>
<!-- 其他导入 -->
<div class="tab-pane fade" id="tab2Content3">
<div :class="['tab-pane', 'fade','in',{active: cate=='oth'}]">
<p>其他一些定制的特殊格式的 实体数据形式</p>
</div>
</div>
@@ -258,7 +258,7 @@
<div class="modal-footer" v-show="cate=='excel'">
<button @click="ck=[]" type="button" class="btn btn-default" data-dismiss="modal">取消选择</button>
<button @click="back()" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button @click="saveSheet()" type="button" class="btn btn-primary">确定</button>
<button @click="saveTable()" type="button" class="btn btn-primary">确定</button>
</div>
<div class="modal-footer" v-show="cate=='mysql'">
<button @click="ckTable=[]" type="button" class="btn btn-default" data-dismiss="modal">取消选择</button>
@@ -270,12 +270,12 @@
<script>
let {dbList} = plat
let {catalogList, tableList, tableInfo} = db
let {sheetMySql, saveTable} = table
let {sheetsExcel, sheetMySql, sheetInfo, saveTable, saveSheet} = table
var vm = new Vue({
el: ".importDev",
data: {
cate: 'mysql',
cate: 'excel',
filePath: '',
importStatus: false,
@@ -302,7 +302,7 @@
var arr = [];
var ck = [];
for (k in v) {
let field = v[k]['field'];
let field = v[k]['name'];
if (k!='表说明' && k!='表名称' && v[k]['hv'] != 1) {
ck.push(field);
}
@@ -318,7 +318,6 @@
},
sheet: function (v) { // 查询sheet详情
this.metaTable = this.sheetData[v];
console.log(this.metaTable)
if (this.metaTable['load'] != 1) {
this.loadSheetData();
}
@@ -344,22 +343,29 @@
if(!filePath) {
return false;
}
red.post("/file/data",{filePath: filePath}, function (json) {
console.log(json)
vm.sheetData = json;
sheetsExcel({filePath}).then(res => {
vm.sheetData = res
})
},
loadSheetData: function() {
let sheetName = this.sheet;
red.post("/file/sheet_data",{filePath: this.filePath, sheetName: sheetName}, function (json) {
let sheetName = this.sheet
sheetInfo({filePath: this.filePath, sheetName: this.sheet}).then(res => {
res["hv"] = vm.sheetData[sheetName]["hv"]
res["load"] = 1
this.sheetData[sheetName] = res
if (this.sheet == sheetName) {
this.metaTable = res
}
})
/*red.post("/file/sheet_data",{filePath: this.filePath, sheetName}, function (json) {
json["hv"] = vm.sheetData[sheetName]["hv"];
json["load"] = 1;
vm.sheetData[sheetName] = json;
if (vm.sheet == sheetName) {
vm.metaTable = json;
}
})
})*/
},
getSheetName: function (tableName) {
for(k in this.sheetData) {
@@ -416,12 +422,31 @@
}
},
saveTable() { // 保存数据
if (this.cate == 'mysql') {
let [dbPlatId, catalog, tableArr] = [this.dbPlatId, this.catalog, this.ckTable]
saveTable({dbPlatId, catalog, tableArr}).then(res => {
red.showOk()
this.loadTables()
})
} else if (this.cate == 'excel') {
if (!this.ck || this.ck.length == 0) {
red.showMsg({msg: "请选择导入数据", type: "danger", placement: "top"});
return false;
}
saveSheet({filePath: this.filePath, sheetNames: this.ck}).then(res => {
red.showOk()
// 刷新列表
let sheetData = this.sheetData
for (let i in this.ck) {
if (sheetData[this.ck[i]]) {
sheetData[this.ck[i]]['hv'] = 1
}
}
this.sheetData = sheetData
this.ck = []
})
}
}
},
mounted: function () {

View File

@@ -94,14 +94,12 @@ var red = {
method: 'post',
data: params,
transformRequest: [data => {
console.log(data)
let _data = ''
for (k in data) {
if (data[k] != undefined) {
_data += k + '=' + data[k] + '&'
}
}
console.log(_data)
return _data
}
]

View File

@@ -31,9 +31,8 @@ import java.util.*;
* 使用poi报表导出工具类
* 把poi的一个调用接口抽出来便于导出功能的管理
* @author LiangXianYou lxy208@126.com
* @param
*
* @author LiangXianYou lxy208@126.com
*/
public class ExcelKit {
@@ -96,6 +95,7 @@ public class ExcelKit {
/**
* 使用数据构建 excel 工作薄对象
*
* @param list 数据
* @param hdNames 表头
* @param hds 每条记录中数据的属性名
@@ -395,10 +395,4 @@ public class ExcelKit {
return wk;
}
//dev
public void setWorkbook(File file) {
}
}

View File

@@ -13,7 +13,8 @@ import static java.util.Arrays.asList;
*/
public final class FileKit {
private FileKit() { }
private FileKit() {
}
public static void strToFile(String entityBody, File file) throws IOException {
strToFile(entityBody, file, true);
@@ -38,26 +39,28 @@ public final class FileKit {
/**
* 拷贝文件/文件目录
*
* @param source 源文件目录
* @param target 目标目录
*/
private static void copyFiles(File source, File target) {
copyFiles(source, target, "");
}
private static void copyFiles(File source, File target, String linkPath) {
if (source.isDirectory()) {
final String _linkPath = linkPath + File.separator+ source.getName();
final String linkPath1 = linkPath + File.separator + source.getName();
asList(source.listFiles()).forEach(f -> {
copyFiles(f, target, _linkPath);
copyFiles(f, target, linkPath1);
});
} else if (source.isFile()) {
try {
String _linkPath = "";
String linkPath1 = "";
int index = linkPath.indexOf(File.separator, 1);
if (index > 0) {
_linkPath = linkPath.substring(index);
linkPath1 = linkPath.substring(index);
}
File targetFile = new File(target.toPath() + _linkPath + File.separator + source.getName());
File targetFile = new File(target.toPath() + linkPath1 + File.separator + source.getName());
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
@@ -71,6 +74,7 @@ public final class FileKit {
/**
* 获取 clazz的路径如果是jar里面的文件得到jar存放的目录lib
*
* @param clazz
* @return
*/
@@ -89,12 +93,14 @@ public final class FileKit {
return "";
}
public static String rootPath() {
return rootPath(FileKit.class);
}
/**
* 读取流内的所有内容
*
* @param inputStream
* @return
* @throws IOException

View File

@@ -109,74 +109,72 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
return (T) String.valueOf(v);
}
Object _v = null;
Object v1 = null;
try {
if (v == null || "".equals(v)) {
} else if (v.getClass() == Long.class) {//多种数值类型的处理: Long => x
if (v.getClass() == Long.class) {//多种数值类型的处理: Long => x
switch (clazz.getSimpleName()) {
case "int":
case "Integer": _v = (int)(long) v; break;
case "Integer": v1 = (int)(long) v; break;
case "short":
case "Short": _v = (short)(long) v; break;
case "Short": v1 = (short)(long) v; break;
case "float":
case "Float": _v = (float)(long) v; break;
default: _v = v;
case "Float": v1 = (float)(long) v; break;
default: v1 = v;
}
} else if (v.getClass() == Double.class && clazz != Double.class) {
if (isNumber.test(clazz)) {
switch (clazz.getSimpleName()) {
case "long":
case "Long": _v = (long)(double) v; break;
case "Long": v1 = (long)(double) v; break;
case "int":
case "Integer": _v = (int)(double) v; break;
case "Integer": v1 = (int)(double) v; break;
case "short":
case "Short": _v = (short)(double) v; break;
case "Short": v1 = (short)(double) v; break;
case "float":
case "Float": _v = (float)(double) v; break;
default: _v = v;
case "Float": v1 = (float)(double) v; break;
default: v1 = v;
}
} else if (clazz == String.class){
_v = String.valueOf(v);
v1 = String.valueOf(v);
}
} else if (v.getClass() == String.class) {
switch (clazz.getSimpleName()) {
case "Date":
_v = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) v); break;
v1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) v); break;
//string ==> number
case "short":
case "Short": _v = (short)Double.parseDouble((String) v); break;
case "Short": v1 = (short)Double.parseDouble((String) v); break;
case "float":
case "Float": _v = (float)Double.parseDouble((String) v); break;
case "Float": v1 = (float)Double.parseDouble((String) v); break;
case "int":
case "Integer": _v = (int)Double.parseDouble((String) v); break;
case "Integer": v1 = (int)Double.parseDouble((String) v); break;
case "long":
case "Long": _v = (long)Double.parseDouble((String) v); break;
case "Long": v1 = (long)Double.parseDouble((String) v); break;
case "double":
case "Double": _v = Double.parseDouble((String) v); break;
default: _v = v;
case "Double": v1 = Double.parseDouble((String) v); break;
default: v1 = v;
}
} else if (v.getClass() == Integer.class) {
switch (clazz.getSimpleName()) {
case "long":
case "Long": _v = (long) (int) v; break;
case "Long": v1 = (long) (int) v; break;
case "int":
case "Integer": _v = (int) v; break;
case "Integer": v1 = (int) v; break;
case "short":
case "Short": _v = (short) (int) v; break;
case "Short": v1 = (short) (int) v; break;
case "float":
case "Float": _v = (float) (int) v; break;
default: _v = v;
case "Float": v1 = (float) (int) v; break;
default: v1 = v;
}
}
else {
_v = v;
v1 = v;
}
} catch (ParseException e) {
e.printStackTrace();
}
return (T) _v;
return (T) v1;
}
public static <T> T toBean(Map map, Class<T> clazz) {

View File

@@ -3,6 +3,7 @@ package net.tccn.service;
import net.tccn.base.ExcelKit;
import net.tccn.base.JBean;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.dbq.Field;
import net.tccn.meta.MetaTable;
import net.tccn.plat.SysPlat;
@@ -15,7 +16,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
@@ -67,7 +70,7 @@ public class _FileService extends BaseService {
});
StringBuffer buf = new StringBuffer();
buf.append("for d in meta_cols\n" +
buf.append("for d in MetaTable\n" +
" filter d.name in [");
data.values().forEach(x -> {
buf.append("'").append(x.getName()).append("',");
@@ -113,8 +116,8 @@ public class _FileService extends BaseService {
return jBean;
}
@RestMapping(name = "import_metatable", comment = "导入excel数据到metatable")
public JBean importMetaTable(@RestParam(name = "sheetArr", comment = "sheet名") String[] sheetArr,
@RestMapping(ignore = true, comment = "导入excel数据到metatable")
public JBean saveSheet(@RestParam(name = "sheetArr", comment = "sheet名") String[] sheetArr,
@RestParam(name = "filePath", comment = "文件路径") String filePath,
@RestParam(name = "platToken") String token) {
JBean jBean = new JBean();
@@ -123,26 +126,24 @@ public class _FileService extends BaseService {
String[] fields = {"field", "cate", "must", "remark1", "remark2", "tag", "selects", "column", "filter", "ck", "edit"};
try {
Map<String, List<Map>> map = ExcelKit.readExcelAll(file, fields);
map.forEach((k, v) -> {
if (v.size() > 2) {
MetaTable table = toCols(v);
for (String sn : sheetArr) {
if (table.getName().equals(sn)) { //在选中列表中
MetaTable metaTable = new MetaTable();
Set<String> ks = map.keySet();
// 找到需要导入的sheet名并组装对应的数据
MetaTable[] metaTables = Stream.of(sheetArr).filter(x -> {
if (!ks.contains(x)) return false;
MetaTable bean = new MetaTable();
bean.setSysPlatId(platId(token));
bean.setName(x);
return MetaTable.dao.findFirst(bean) == null;
}).map(x -> {
MetaTable metaTable = toCols(map.get(x));
metaTable.setSysPlatId(platId(token));
metaTable.setName(table.getName());
MetaTable _metaTable = MetaTable.dao.findFirst(metaTable);
if (_metaTable == null) {//库里没有数据保存
table.setSysPlatId(platId(token));
table.save();
}
break;
}
}
}
});
metaTable.setAlias(MetaKit.nextAlias());
return metaTable;
}).toArray(MetaTable[]::new);
MetaKit.save(metaTables);
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -29,10 +29,9 @@ public class _TableService extends BaseService {
@RestMapping(name = "sheets", comment = "导入选择列表数据准备")
public JBean sheets(String cate, //类型
//excel {文件地址}
public JBean sheets(String cate, //类型 excel|mysql
// excel
String filePath,
//mysql {数据库连接账号、数据源id、数据库database数组}
String dbPlatId, String catalog,
@RestParam(name = "platToken") String token) {
@@ -40,7 +39,7 @@ public class _TableService extends BaseService {
JBean jBean = new JBean();
if ("excel".equals(cate)) {
jBean.setBody(fileService.data(filePath, token));
jBean = fileService.data(filePath, token);
} else if ("mysql".equals(cate)) {
List<Table> list = dbService.tableList(dbPlatId, catalog, null);
@@ -65,21 +64,29 @@ public class _TableService extends BaseService {
return jBean;
}
/*public JBean tableInfo(DbAccount dbAccount,
@RestMapping(name = "sheet_info", comment = "sheet详情")
public JBean sheetInfo(String cate, @RestParam(name = "platToken") String token,
// excel
String filePath, String sheetName,
// mysql
String dbPlatId, String catalog, String tableName) {
dbService.tableInfo(dbAccount, dbPlatId, catalog, tableName)
if ("excel".equals(cate)) {
return fileService.sheetData(filePath, sheetName, token);
} else if ("mysql".equals(cate)) {
return new JBean().setBody(dbService.tableInfo(dbPlatId, catalog, tableName));
}
return null;
}*/
}
@RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
public void tableSave(String dbPlatId,
String catalog,
String[] tableArr,
@RestParam(name = "platToken") String token) {
public JBean tableSave(String cate, @RestParam(name = "platToken") String token,
String filePath, String[] sheetNames,
String dbPlatId, String catalog, String[] tableArr) {
if ("excel".equals(cate)) {
return fileService.saveSheet(sheetNames, filePath, token);
} else if ("mysql".equals(cate)) {
List<String> hv = MetaKit.tableExist(tableArr, token);
List<Table> tables = dbService.tableInfoList(dbPlatId, catalog, tableArr);
@@ -96,21 +103,6 @@ public class _TableService extends BaseService {
MetaKit.save(metaTables);
}
/*@Comment("查询元数据中存在的表")
private CompletableFuture<List<String>> 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("',");
return JBean.OK;
}
buf.deleteCharAt(buf.length() - 1);
buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
" return d.name");
List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
return hv;
});
}*/
}

View File

@@ -11,6 +11,7 @@ import org.redkale.net.http.HttpResponse;
import org.redkale.net.http.HttpServlet;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -20,6 +21,8 @@ import java.util.logging.Logger;
*/
public class BaseServlet extends HttpServlet {
@Resource(name = "SERVER_ROOT")
protected File webroot;
@Resource
private UserService userService;

View File

@@ -0,0 +1,41 @@
package net.tccn.servlet;
import net.tccn.base.JBean;
import net.tccn.base.Kv;
import org.redkale.net.http.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author: liangxianyou at 2018/10/11 17:08.
*/
@WebServlet(value = {"/upload/*"}, comment = "测试servlet")
public class FileServlet extends BaseServlet {
@HttpMapping(url = "/upload/", comment = "文件上传,访问地址:/upload/x")
public void uploadExcel(HttpRequest request, HttpResponse response) {
JBean jBean = new JBean();
List list = new ArrayList();
try {
for (MultiPart part : request.multiParts()) {
String filePath = "u/table/" + part.getFilename();
File destFile = new File(webroot, filePath);
destFile.getParentFile().mkdirs();
part.save(destFile);
list.add(
Kv.of("name", part.getFilename())
.set("filePath", filePath)
.set("viewPath", filePath)
);
}
} catch (IOException e) {
e.printStackTrace();
}
response.finish(jBean.set(0, "", list));
}
}