提交 2f5482bc authored 作者: 李炎's avatar 李炎

新增通用导出excel接口

上级 75828b81
......@@ -31,3 +31,4 @@ build/
### VS Code ###
.vscode/
/robot-system/src/test
package com.system.framework.core.excel;
import java.util.List;
public class ExportModel {
private String name;
private List<List<String>> head;
private List<List<Object>> dataList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<List<String>> getHead() {
return head;
}
public void setHead(List<List<String>> head) {
this.head = head;
}
public List<List<Object>> getDataList() {
return dataList;
}
public void setDataList(List<List<Object>> dataList) {
this.dataList = dataList;
}
}
package com.system.framework.core.excel.util;
import com.alibaba.excel.EasyExcel;
import com.system.framework.core.excel.ExportModel;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
public class ExcelUtil {
public static void export(ExportModel exportModel, HttpServletResponse response) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode(exportModel.getName(), "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream()).autoCloseStream(Boolean.FALSE).head(exportModel.getHead()).sheet(exportModel.getName()).doWrite(exportModel.getDataList());
}
}
......@@ -105,6 +105,19 @@ public class KingDeeCommonSyncConstants {
return key;
}
/**
* 封装本机映射金蝶的字段
*/
public static Map<String, String> encapsulationThirdPartyFieldCn(List<Map<String, String>> fieldList) {
Map<String, String> key = new LinkedHashMap<>();
for (Map<String, String> map : fieldList) {
String thirdPartyFieldName = map.get("thirdPartyFieldName");
String thirdPartyField = map.get("thirdPartyField");
key.put(thirdPartyFieldName, thirdPartyField);
}
return key;
}
public static void encapsulationOnWhole(List<List<Object>> list, List<Map<String, String>> fieldList, List<Map<String, Object>> result) {
for (List<Object> objectList : list) {
Map<String, Object> map = new HashMap<>(fieldList.size());
......
package com.system.controller;
import com.alibaba.fastjson.JSON;
import com.system.framework.core.excel.ExportModel;
import com.system.framework.core.excel.util.ExcelUtil;
import com.system.serivce.IKingDeeCommonGetService;
import com.system.transfer.kingdee.KingDeeDataListInVo;
import com.system.transfer.response.RestResponse;
......@@ -12,6 +15,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author Inori
*/
......@@ -30,5 +38,41 @@ public class KingDeeGetController {
return kingDeeCommonGetService.kingDeeDataList(inVo);
}
@ApiOperation("金蝶通用查询")
@PostMapping("/kingdee/data/export")
public void kingDeeDataExport(@RequestBody @Validated KingDeeDataListInVo inVo, HttpServletResponse response) throws IOException {
try {
ExportModel export = kingDeeCommonGetService.export(inVo);
ExcelUtil.export(export, response);
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<>(2);
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
@ApiOperation("金蝶通用查询")
@PostMapping("/kingdee/data/export/all")
public void kingDeeDataExportAll(@RequestBody @Validated KingDeeDataListInVo inVo, HttpServletResponse response) throws IOException {
inVo.setAll(true);
try {
ExportModel export = kingDeeCommonGetService.export(inVo);
ExcelUtil.export(export, response);
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<>(2);
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
}
......@@ -8,10 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author Inori
......@@ -29,6 +26,12 @@ public class KingDeeCommonGetApi {
return encapsulationIms(result, fieldList, config.get("mesEntryName"), Integer.valueOf(config.get("encapsulationFormat")));
}
public List<List<Object>> getKingDeeList(Map<String, String> config, String queueId, List<Map<String, String>> fieldList, Map<String, String> where) {
Map<String, String> key = KingDeeCommonSyncConstants.encapsulationKingDeeField(fieldList);
List<List<Object>> result = kingDeeApi.getKingDeeData(config, queueId, key, where);
return result;
}
/**
* 封装第三方字段
*/
......@@ -37,7 +40,7 @@ public class KingDeeCommonGetApi {
if (!CollectionUtils.isEmpty(list)) {
String errorName = "Errors";
if (!list.get(0).get(0).toString().contains(errorName)) {
Map<String, Object> map = new HashMap<>(fieldList.size());
Map<String, Object> map = new LinkedHashMap<>(fieldList.size());
List<Map<String, Object>> tempList = new ArrayList<>();
if (encapsulationFormat == 1) {
......
package com.system.serivce;
import com.system.framework.core.excel.ExportModel;
import com.system.transfer.kingdee.KingDeeDataListInVo;
import com.system.transfer.response.RestResponse;
......@@ -16,5 +17,12 @@ public interface IKingDeeCommonGetService {
*/
RestResponse kingDeeDataList(KingDeeDataListInVo inVo);
/**
* 金蝶通用导出
*
* @param inVo 条件
* @return 列表
*/
ExportModel export(KingDeeDataListInVo inVo);
}
package com.system.serivce.impl;
import com.system.constants.KingDeeCommonGetConstants;
import com.system.constants.KingDeeCommonSyncConstants;
import com.system.framework.core.excel.ExportModel;
import com.system.framework.core.exception.StarBosException;
import com.system.kingdee.KingDeeCommonGetApi;
import com.system.serivce.IKingDeeCommonGetService;
import com.system.transfer.kingdee.KingDeeDataListInVo;
import com.system.transfer.response.RestResponse;
import com.system.transfer.temp.KingDeeCommonGetListOutVo;
import com.system.utils.JsonUtil;
import com.system.utils.KingDeeUtil;
import com.system.utils.ProductLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -75,6 +80,54 @@ public class KingDeeCommonGetServiceImpl implements IKingDeeCommonGetService {
return RestResponse.success(outVo);
}
@Override
public ExportModel export(KingDeeDataListInVo inVo) {
long start = System.currentTimeMillis();
String queueId = inVo.getQueueId();
Map<String, String> config = kingDeeCommonGetConstants.get(inVo.getDocType());
if (CollectionUtils.isEmpty(config)) {
throw new StarBosException("docType为: " + inVo.getDocType() + " 的表单ERP-MES标识不存在");
}
List<Object> tempList = JsonUtil.toList(config.get("kingDeeFiledDetail"), Object.class);
List<Map<String, String>> fieldList = tempList.stream().map(m -> JsonUtil.toMap(JsonUtil.toString(m), String.class, String.class)).collect(Collectors.toList());
Map<String, String> where = new HashMap<>(5);
if (!CollectionUtils.isEmpty(inVo.getData())) {
for (Map.Entry<String, Object> entry : inVo.getData().entrySet()) {
where.put(entry.getKey(), String.valueOf(entry.getValue()));
}
}
this.setWhereDefaultRule(inVo.getDocType(), where);
List<List<Object>> response = kingDeeCommonGetApi.getKingDeeList(config, queueId, fieldList, where);
long costTime = System.currentTimeMillis() - start;
if (!CollectionUtils.isEmpty(response)) {
String temp = "false";
String errorName = "Errors";
if (response.get(0).get(0).toString().contains(errorName)) {
String message = KingDeeUtil.getMessage(response.get(0).get(0));
throw new StarBosException(String.valueOf(message));
}
}
Map<String, String> key = KingDeeCommonSyncConstants.encapsulationThirdPartyFieldCn(fieldList);
List<List<String>> head = key.keySet().stream().map(m -> new ArrayList() {{
add(m);
}}).collect(Collectors.toList());
List<List<Object>> dataList = response;
if (!inVo.getAll()) {
dataList = response.stream().skip((inVo.getPageNo() - 1) * inVo.getPageSize()).limit(inVo.getPageSize()).collect(Collectors.toList());
}
ExportModel exportModel = new ExportModel();
exportModel.setName(inVo.getDocType());
exportModel.setHead(head);
exportModel.setDataList(dataList);
return exportModel;
}
/**
* 设置默认where自定义查询过滤条件
* 注:此为该项目应急用使用,建议后续扩展
......
......@@ -43,5 +43,5 @@ public class KingDeeDataListInVo {
*/
private Integer pageSize = 10;
private Boolean all = false;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论