From cdda440ef272d21a33ff73eda3dc2507c027a33d Mon Sep 17 00:00:00 2001
From: wangxuanran <3055088966@qq.com>
Date: Sat, 10 Jun 2023 16:09:18 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E6=B0=B8=E6=95=B0=E6=8D=AE=E5=90=8C?=
=?UTF-8?q?=E6=AD=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../controller/ExportExcelController.java | 53 ++-
.../entity/CusExportExcelConfigDetail.java | 2 +-
.../entity/CusExportExcelConfigMain.java | 1 +
.../mapper/ExportExcelMapper.java | 13 +-
.../service/ExportExcelServiceImpl.java | 156 ++++++-
.../wang/common/entity/CusSuccess.java | 27 ++
.../wang/common/mapper/CommonMapper.java | 3 +
.../wang/common/service/CusDataDecipher.java | 15 +
.../xuanran/wang/common/util/CommonUtil.java | 15 +
.../xuanran/wang/common/util/CusData2OA.java | 113 ++++-
.../wang/common/util/RequestMasterPlate.java | 78 ++++
.../entity/DataAsyncConfigDetail.java | 19 +
.../entity/DataAsyncConfigMain.java | 23 ++
.../entity/EyDataAsyncCusToken.java | 21 +
.../eny/data_async/job/ModelDataAsyncJob.java | 75 ++++
.../data_async/mapper/DataAsyncMapper.java | 58 +++
.../service/DataAsyncServiceImpl.java | 385 ++++++++++++++++++
.../service/convert/CusAsyncConvert.java | 20 +
.../service/convert/CusSubStringConvert.java | 37 ++
.../service/convert/CusTimeConvert.java | 40 ++
.../data_async/util/EyDataAsyncTokenUtil.java | 72 ++++
.../eny/data_async/util/ValueRuleMethod.java | 104 +++++
.../data_async/util/ValueRuleMethodNo.java | 19 +
.../java/xuanran/wang/eny/DataAsyncTest.java | 70 ++++
.../xuanran/wang/eny/ExcelExportTest.java | 95 ++++-
25 files changed, 1481 insertions(+), 33 deletions(-)
create mode 100644 src/main/java/weaver/xuanran/wang/common/entity/CusSuccess.java
create mode 100644 src/main/java/weaver/xuanran/wang/common/service/CusDataDecipher.java
create mode 100644 src/main/java/weaver/xuanran/wang/common/util/RequestMasterPlate.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/entity/DataAsyncConfigDetail.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/entity/DataAsyncConfigMain.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/entity/EyDataAsyncCusToken.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/job/ModelDataAsyncJob.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/mapper/DataAsyncMapper.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/service/DataAsyncServiceImpl.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/service/convert/CusAsyncConvert.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/service/convert/CusSubStringConvert.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/service/convert/CusTimeConvert.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/util/EyDataAsyncTokenUtil.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/util/ValueRuleMethod.java
create mode 100644 src/main/java/weaver/xuanran/wang/eny/data_async/util/ValueRuleMethodNo.java
create mode 100644 src/test/java/xuanran/wang/eny/DataAsyncTest.java
diff --git a/src/main/java/com/api/xuanran/wang/eny/export_excel/controller/ExportExcelController.java b/src/main/java/com/api/xuanran/wang/eny/export_excel/controller/ExportExcelController.java
index cc02515..eca4517 100644
--- a/src/main/java/com/api/xuanran/wang/eny/export_excel/controller/ExportExcelController.java
+++ b/src/main/java/com/api/xuanran/wang/eny/export_excel/controller/ExportExcelController.java
@@ -1,14 +1,23 @@
package com.api.xuanran.wang.eny.export_excel.controller;
+import aiyh.utils.Util;
+import com.api.xuanran.wang.eny.export_excel.entity.CusExportExcelConfigMain;
+import com.api.xuanran.wang.eny.export_excel.service.ExportExcelServiceImpl;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.log4j.Logger;
+import weaver.general.TimeUtil;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import javax.ws.rs.POST;
+import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import java.nio.charset.StandardCharsets;
+import javax.ws.rs.core.StreamingOutput;
+import java.net.URLEncoder;
+import java.util.Objects;
/**
*
安永 建模自定义导出excel
@@ -19,14 +28,42 @@ import java.nio.charset.StandardCharsets;
@Path("/wxr/eny/excel")
public class ExportExcelController {
- @POST
+ private final ExportExcelServiceImpl exportExcelService = new ExportExcelServiceImpl();
+ private final Logger logger = Util.getLogger();
+
+ @GET
@Path("/cus_port")
- @Produces(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportExcel(@Context HttpServletRequest request,
@Context HttpServletResponse response){
- return null;
-// return Response.ok(streamingOutput, MediaType.APPLICATION_OCTET_STREAM).type("application/zip")
-// .header("Content-Disposition", "attachment;filename=" +
-// new String(packageName.toString().getBytes("GBK"), StandardCharsets.ISO_8859_1) + ".zip").build();
+ String onlyMark = request.getParameter("onlyMark");
+ String dataId = request.getParameter("dataId");
+ if(StringUtils.isBlank(onlyMark) || StringUtils.isBlank(dataId)){
+ logger.error("配置唯一标识或dataId为空!");
+ return null;
+ }
+ CusExportExcelConfigMain config = exportExcelService.getConfig(onlyMark);
+ if(Objects.isNull(config)){
+ logger.error("该唯一标识在配置表中没有找到配置数据!");
+ return null;
+ }
+ try {
+ String suffix = config.getTemplate_path().substring(config.getTemplate_path().lastIndexOf("."));
+ StreamingOutput os = exportExcelService.exportExcel(config, dataId, suffix);
+// String mimeType = ".xls".equals(suffix) ? "application/vnd.ms-excel":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+ String fileName = request.getParameter("fileName");
+ if(StringUtils.isBlank(fileName)){
+ fileName = TimeUtil.getCurrentDateString() + config.getFile_name();
+ }
+ fileName += suffix;
+ fileName = URLEncoder.encode(fileName, "UTF-8");
+ logger.info("文件名称 : " + fileName);
+ return Response.ok(os, MediaType.APPLICATION_OCTET_STREAM)
+ .header("Content-Disposition", "attachment; filename*=UTF-8''" + fileName).build();
+ }catch (Exception e){
+ logger.error("生成excel文件异常 : ");
+ logger.error(Util.getErrString(e));
+ return null;
+ }
}
}
diff --git a/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigDetail.java b/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigDetail.java
index 59a203d..b41eeea 100644
--- a/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigDetail.java
+++ b/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigDetail.java
@@ -12,6 +12,6 @@ import lombok.Data;
public class CusExportExcelConfigDetail {
private String excel_row;
private String model_field;
- private String get_value_type;
+ private int get_value_type;
private String value_context;
}
diff --git a/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigMain.java b/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigMain.java
index 84890b9..398aa8c 100644
--- a/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigMain.java
+++ b/src/main/java/com/api/xuanran/wang/eny/export_excel/entity/CusExportExcelConfigMain.java
@@ -15,5 +15,6 @@ public class CusExportExcelConfigMain {
private String model_table;
private String template_path;
private String file_name;
+ private String table_name;
private List configDetailList;
}
diff --git a/src/main/java/com/api/xuanran/wang/eny/export_excel/mapper/ExportExcelMapper.java b/src/main/java/com/api/xuanran/wang/eny/export_excel/mapper/ExportExcelMapper.java
index aa0d63b..3a96430 100644
--- a/src/main/java/com/api/xuanran/wang/eny/export_excel/mapper/ExportExcelMapper.java
+++ b/src/main/java/com/api/xuanran/wang/eny/export_excel/mapper/ExportExcelMapper.java
@@ -3,8 +3,6 @@ package com.api.xuanran.wang.eny.export_excel.mapper;
import aiyh.utils.annotation.recordset.*;
import com.api.xuanran.wang.eny.export_excel.entity.CusExportExcelConfigDetail;
import com.api.xuanran.wang.eny.export_excel.entity.CusExportExcelConfigMain;
-import com.api.xuanran.wang.traffic_bank.email.entity.EmailOutConfigDetail;
-import com.api.xuanran.wang.traffic_bank.email.entity.EmailOutConfigMain;
import java.util.List;
import java.util.Map;
@@ -17,13 +15,17 @@ import java.util.Map;
*/
@SqlMapper
public interface ExportExcelMapper {
- @Select("select * from uf_mod_export_excel where model_table = #{modelTable}")
+ @Select("SELECT a.*,b.tablename as table_name " +
+ "FROM uf_mod_export_excel a " +
+ "left join workflow_mode_table_view b " +
+ "on a.model_table = b.id " +
+ "where only_mark = #{onlyMark}")
@CollectionMappings({
@CollectionMapping(property = "configDetailList",
column = "id",
id = @Id(value = Integer.class, methodId = 1))
})
- CusExportExcelConfigMain selectConfigByModelId(@ParamMapper("modelTable") int modelTable);
+ CusExportExcelConfigMain selectConfigByOnlyMark(@ParamMapper("onlyMark") String onlyMark);
/**
* 查询配置表明细表信息
@@ -39,6 +41,9 @@ public interface ExportExcelMapper {
@CollectionMethod(1)
List selectConfigDetail(@ParamMapper("mainId") int mainId);
+ @Select(custom = true)
+ Map selectCustomerSqlMap(@SqlString String sql, Map map);
+
@Select(custom = true)
String selectCustomerSql(@SqlString String sql, Map map);
}
diff --git a/src/main/java/com/api/xuanran/wang/eny/export_excel/service/ExportExcelServiceImpl.java b/src/main/java/com/api/xuanran/wang/eny/export_excel/service/ExportExcelServiceImpl.java
index a975574..b7f17b6 100644
--- a/src/main/java/com/api/xuanran/wang/eny/export_excel/service/ExportExcelServiceImpl.java
+++ b/src/main/java/com/api/xuanran/wang/eny/export_excel/service/ExportExcelServiceImpl.java
@@ -1,8 +1,27 @@
package com.api.xuanran.wang.eny.export_excel.service;
import aiyh.utils.Util;
+import aiyh.utils.excention.CustomerException;
+import com.api.xuanran.wang.eny.export_excel.entity.CusExportExcelConfigDetail;
import com.api.xuanran.wang.eny.export_excel.entity.CusExportExcelConfigMain;
import com.api.xuanran.wang.eny.export_excel.mapper.ExportExcelMapper;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.log4j.Logger;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import weaver.general.GCONST;
+
+import javax.ws.rs.core.StreamingOutput;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
/**
* 自定义导出excel
@@ -12,16 +31,143 @@ import com.api.xuanran.wang.eny.export_excel.mapper.ExportExcelMapper;
*/
public class ExportExcelServiceImpl {
private final ExportExcelMapper exportExcelMapper = Util.getMapper(ExportExcelMapper.class);
-
+ private final Logger log = Util.getLogger();
/**
* 根据模块id查询配置
+ *
+ * @param onlyMark 唯一标识
+ * @return 配置对象
* @author xuanran.wang
* @dateTime 2023/6/7 11:28
- * @param modelId 模块id
- * @return 配置对象
**/
- public CusExportExcelConfigMain getConfig(int modelId){
- return exportExcelMapper.selectConfigByModelId(modelId);
+ public CusExportExcelConfigMain getConfig(String onlyMark) {
+ return exportExcelMapper.selectConfigByOnlyMark(onlyMark);
+ }
+
+ /**
+ * 导出excel
+ * @author xuanran.wang
+ * @dateTime 2023/6/7 16:26
+ * @param config 配置对象
+ * @param dataId 数据ID
+ * @param suffix 文件后缀
+ * @return 输出流
+ **/
+ public StreamingOutput exportExcel(CusExportExcelConfigMain config,
+ String dataId,
+ String suffix) throws IOException {
+ return outputStream -> {
+ String filePath = config.getTemplate_path();
+ filePath = parseConfigPath(filePath);
+ if(!new File(filePath).exists()){
+ throw new IOException("在服务器中未找到模版文件!");
+ }
+ String sql = "select * from " + config.getTable_name() + " where id in ( " + dataId + " )";
+ Map map = exportExcelMapper.selectCustomerSqlMap(sql, new HashMap<>());
+ if(MapUtils.isEmpty(map)){
+ throw new CustomerException("模块数据查询为空!");
+ }
+ List configDetailList = config.getConfigDetailList();
+ List specialRow = new ArrayList<>();
+ if(CollectionUtils.isNotEmpty(configDetailList)){
+ specialRow = configDetailList.stream().map(CusExportExcelConfigDetail::getExcel_row).collect(Collectors.toList());
+ }
+ FileInputStream fis = new FileInputStream(filePath);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ Workbook workbook;
+ if (".xls".equals(suffix)) {
+ workbook = new HSSFWorkbook(fis);
+ }else {
+ workbook = new XSSFWorkbook(fis);
+ }
+ try {
+ Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
+ for (Row row : sheet) {
+ for (Cell cell : row) {
+ String cellValue = cell.getStringCellValue();
+ String replacedValue = replacePlaceholder(cellValue, map, specialRow, config);
+ cell.setCellValue(replacedValue);
+ }
+ }
+ workbook.write(bos);
+ bos.writeTo(outputStream);
+ outputStream.flush();
+ }catch (Exception e){
+ throw new CustomerException("replacePlaceholder error ", e);
+ }finally {
+ try {
+ outputStream.close();
+ bos.close();
+ workbook.close();
+ fis.close();
+ }catch (Exception e){
+ log.error("关流异常 : " + e.getMessage());
+ }
+ }
+ };
+ }
+
+ /**
+ * 将excel模版中的参数进行替换
+ * @author xuanran.wang
+ * @dateTime 2023/6/7 14:32
+ * @param cellValue 单元格数据
+ * @param modelMap 建模数据集合
+ * @param specialRow 明细1配置的数据
+ * @param config 配置对象
+ * @return 替换后的值
+ **/
+ public String replacePlaceholder(String cellValue, Map modelMap, List specialRow, CusExportExcelConfigMain config){
+ Pattern pattern = Pattern.compile("\\{(.*?)}"); // 匹配以{}包裹的内容
+ Matcher matcher = pattern.matcher(cellValue);
+ StringBuffer replacedValue = new StringBuffer();
+ while (matcher.find()) {
+ String placeholder = matcher.group(1); // 获取{}中的内容
+ Object replacement = Util.null2DefaultStr(modelMap.get(placeholder),"");
+ // sql特殊处理
+ if(CollectionUtils.isNotEmpty(specialRow) && specialRow.contains(placeholder)){
+ List collect = config.getConfigDetailList().stream().filter(item -> item.getExcel_row().equals(placeholder)).collect(Collectors.toList());
+ if(CollectionUtils.isNotEmpty(collect)){
+ CusExportExcelConfigDetail configDetail = collect.get(0);
+ if(configDetail.getGet_value_type() == 2){
+ String cusSql = configDetail.getValue_context();
+ cusSql = Util.sbc2dbcCase(cusSql);
+ replacement = exportExcelMapper.selectCustomerSql(cusSql, modelMap);
+ }
+ }
+ }
+ // 根据需要进行替换的逻辑
+ matcher.appendReplacement(replacedValue, replacement.toString());
+ }
+ matcher.appendTail(replacedValue);
+ return replacedValue.toString();
+ }
+
+ /**
+ * 解析请求xls模板位置
+ * @param configFilePath 模板文件路径
+ * @return 解析后的文件路径
+ */
+ public String parseConfigPath(String configFilePath){
+ StringBuilder filePath = new StringBuilder(GCONST.getSysFilePath());
+ int beginIndex = configFilePath.indexOf(".");
+ int endIndex = configFilePath.lastIndexOf(".");
+ if(beginIndex == endIndex){
+ filePath.append(configFilePath);
+ }else {
+ String[] pathArr = configFilePath.split("\\.");
+ for (int i = 0; i < pathArr.length - 2; i++) {
+ if(i != 0){
+ filePath.append(File.separator);
+ }
+ filePath.append(pathArr[i]);
+ }
+ filePath.append(File.separator)
+ .append(pathArr[pathArr.length - 2])
+ .append(".")
+ .append(pathArr[pathArr.length - 1]);
+ }
+ return filePath.toString();
}
diff --git a/src/main/java/weaver/xuanran/wang/common/entity/CusSuccess.java b/src/main/java/weaver/xuanran/wang/common/entity/CusSuccess.java
new file mode 100644
index 0000000..3bb23ba
--- /dev/null
+++ b/src/main/java/weaver/xuanran/wang/common/entity/CusSuccess.java
@@ -0,0 +1,27 @@
+package weaver.xuanran.wang.common.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import weaver.xuanran.wang.common.service.CusDataDecipher;
+
+
+/**
+ * 自定义请求条件
+ *
+ * @author xuanran.wang
+ * @date 2023/4/6 19:34
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class CusSuccess {
+ private String successField;
+ private Object successValue;
+ private String errorMsg;
+ private String dataKey;
+ private Object response;
+ private CusDataDecipher cusDataDecipher;
+}
diff --git a/src/main/java/weaver/xuanran/wang/common/mapper/CommonMapper.java b/src/main/java/weaver/xuanran/wang/common/mapper/CommonMapper.java
index 1a13798..d51b2b6 100644
--- a/src/main/java/weaver/xuanran/wang/common/mapper/CommonMapper.java
+++ b/src/main/java/weaver/xuanran/wang/common/mapper/CommonMapper.java
@@ -121,4 +121,7 @@ public interface CommonMapper {
@BatchUpdate(custom = true)
boolean updateModelInfoList(@SqlString String sql, @BatchSqlArgs List