|
|
@ -3,37 +3,32 @@ package com.keyware.shandan.bianmu.export; |
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
import cn.hutool.poi.excel.BigExcelWriter; |
|
|
|
import cn.hutool.poi.excel.BigExcelWriter; |
|
|
|
import cn.hutool.poi.excel.ExcelUtil; |
|
|
|
import cn.hutool.poi.excel.ExcelUtil; |
|
|
|
import cn.hutool.poi.excel.ExcelWriter; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.keyware.shandan.bianmu.entity.MetadataBasicVo; |
|
|
|
import com.keyware.shandan.bianmu.entity.MetadataBasicVo; |
|
|
|
import com.keyware.shandan.bianmu.service.MetadataService; |
|
|
|
import com.keyware.shandan.bianmu.service.MetadataService; |
|
|
|
import com.keyware.shandan.common.util.StreamUtil; |
|
|
|
|
|
|
|
import com.keyware.shandan.common.util.StringUtils; |
|
|
|
import com.keyware.shandan.common.util.StringUtils; |
|
|
|
import com.keyware.shandan.frame.config.component.AppContext; |
|
|
|
import com.keyware.shandan.frame.config.component.AppContext; |
|
|
|
import org.apache.ibatis.session.ResultContext; |
|
|
|
|
|
|
|
import org.apache.ibatis.session.ResultHandler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
public class MetaTableExport extends ExportProgress implements ResultHandler<HashMap<String, Object>> { |
|
|
|
public class MetaTableExport extends ExportProgress { |
|
|
|
private final MetadataBasicVo metadata; |
|
|
|
private final MetadataBasicVo metadata; |
|
|
|
private Consumer<Object> queryFunc; |
|
|
|
|
|
|
|
private final String fileName; |
|
|
|
private final String fileName; |
|
|
|
private JSONArray colsArray; |
|
|
|
|
|
|
|
private final MetadataService metadataService; |
|
|
|
private final MetadataService metadataService; |
|
|
|
|
|
|
|
|
|
|
|
// 所有行的集合
|
|
|
|
// 所有行的集合
|
|
|
|
private List<List<String>> rowList = new ArrayList<>(); |
|
|
|
private final List<List<String>> rowList = new ArrayList<>(); |
|
|
|
// 字段列名集合
|
|
|
|
// 字段列名集合
|
|
|
|
private List<String> colNameList = new ArrayList<>(); |
|
|
|
private final List<String> colNameList = new ArrayList<>(); |
|
|
|
private int dataTotal; |
|
|
|
private final List<Map<String, Object>> datas = new ArrayList<>(); |
|
|
|
|
|
|
|
private Consumer<List<Map<String, Object>>> queryHandler; |
|
|
|
|
|
|
|
private boolean isDone = false; |
|
|
|
|
|
|
|
|
|
|
|
public MetaTableExport(String userId, MetadataBasicVo metadata) { |
|
|
|
public MetaTableExport(String userId, MetadataBasicVo metadata) { |
|
|
|
super(userId); |
|
|
|
super(userId); |
|
|
@ -49,7 +44,7 @@ public class MetaTableExport extends ExportProgress implements ResultHandler<Has |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
this.setTitle("正在读取数据表信息"); |
|
|
|
this.setTitle("正在读取数据表信息"); |
|
|
|
colsArray = metadataService.getColumns(metadata.getId()); |
|
|
|
JSONArray colsArray = metadataService.getColumns(metadata.getId()); |
|
|
|
|
|
|
|
|
|
|
|
// 添加第一行,字段列名注释集合,当做表格的第一行
|
|
|
|
// 添加第一行,字段列名注释集合,当做表格的第一行
|
|
|
|
rowList.add(colsArray.stream().map((json) -> { |
|
|
|
rowList.add(colsArray.stream().map((json) -> { |
|
|
@ -60,26 +55,37 @@ public class MetaTableExport extends ExportProgress implements ResultHandler<Has |
|
|
|
return colName + (StringUtils.hasText(comment) ? "[" + comment + "]" : ""); |
|
|
|
return colName + (StringUtils.hasText(comment) ? "[" + comment + "]" : ""); |
|
|
|
}).collect(Collectors.toList())); |
|
|
|
}).collect(Collectors.toList())); |
|
|
|
|
|
|
|
|
|
|
|
this.setTitle("正在读取数据到临时文件"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行数据查询
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
queryFunc.accept(null); |
|
|
|
this.setTitle("正在查询数据"); |
|
|
|
|
|
|
|
this.autoAddStep(); |
|
|
|
|
|
|
|
this.queryHandler.accept(this.datas); |
|
|
|
|
|
|
|
handleResult(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File file = new File(fileName); |
|
|
|
File file = new File(fileName); |
|
|
|
if(file.exists()){ |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
List<List<String>> rows = CollUtil.newArrayList(rowList); |
|
|
|
List<List<String>> rows = CollUtil.newArrayList(rowList); |
|
|
|
//通过工具类创建writer
|
|
|
|
//通过工具类创建writer
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter(file); |
|
|
|
BigExcelWriter writer = ExcelUtil.getBigWriter(file); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setTitle("正在写入缓存"); |
|
|
|
|
|
|
|
this.autoAddStep(); |
|
|
|
//一次性写出内容
|
|
|
|
//一次性写出内容
|
|
|
|
writer.write(rows); |
|
|
|
writer.write(rows); |
|
|
|
//关闭writer,释放内存
|
|
|
|
|
|
|
|
|
|
|
|
this.setTitle("正在写入文件"); |
|
|
|
|
|
|
|
this.autoAddStep(); |
|
|
|
|
|
|
|
//写入文件后关闭writer,释放内存
|
|
|
|
writer.close(); |
|
|
|
writer.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.autoAddStep(); |
|
|
|
this.setTitle("数据文件准备完毕,开始下载"); |
|
|
|
this.setTitle("数据文件准备完毕,开始下载"); |
|
|
|
this.setDone(); |
|
|
|
isDone = true; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
this.error = true; |
|
|
|
this.error = true; |
|
|
|
this.setTitle("准备数据时出现异常"); |
|
|
|
this.setTitle("准备数据时出现异常"); |
|
|
|
this.clean(); |
|
|
|
this.clean(); |
|
|
@ -88,7 +94,7 @@ public class MetaTableExport extends ExportProgress implements ResultHandler<Has |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long getStepTotal() { |
|
|
|
public long getStepTotal() { |
|
|
|
return this.dataTotal; |
|
|
|
return 4; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -96,24 +102,24 @@ public class MetaTableExport extends ExportProgress implements ResultHandler<Has |
|
|
|
return this.fileName; |
|
|
|
return this.fileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void handleResult() { |
|
|
|
public void handleResult(ResultContext<? extends HashMap<String, Object>> context) { |
|
|
|
for (Map<String, Object> data : this.datas) { |
|
|
|
Map<String, Object> data = context.getResultObject(); |
|
|
|
// 遍历字段列名,获取对应数据并拼接成列的集合
|
|
|
|
// 遍历字段列名,获取对应数据并拼接成列的集合
|
|
|
|
List<String> cells = colNameList.stream().map(col -> { |
|
|
|
List<String> cells = colNameList.stream().map(col -> { |
|
|
|
Object value = data.get(col); |
|
|
|
Object value = data.get(col); |
|
|
|
value = value == null ? "" : value; |
|
|
|
value = value == null ? "" : value; |
|
|
|
return String.valueOf(value); |
|
|
|
return String.valueOf(value); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
rowList.add(cells); |
|
|
|
rowList.add(cells); |
|
|
|
} |
|
|
|
this.autoAddStep(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void queryData(Consumer<Object> query) { |
|
|
|
public void setQueryHandler(Consumer<List<Map<String, Object>>> handler) { |
|
|
|
this.queryFunc = query; |
|
|
|
this.queryHandler = handler; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setDataCount(int count) { |
|
|
|
@Override |
|
|
|
this.dataTotal = count; |
|
|
|
public boolean getIsDone() { |
|
|
|
|
|
|
|
return isDone; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|