优化风控导出bug

This commit is contained in:
yujh 2024-08-29 15:51:06 +08:00
parent 434cff69a6
commit 40602abf58

View File

@ -1,6 +1,7 @@
package com.actionsoft.apps.coe.pal.pal.output.util;
import com.actionsoft.apps.coe.pal.constant.CoEConstant;
import com.actionsoft.apps.coe.pal.constant.YiliWpsConst;
import com.actionsoft.apps.coe.pal.pal.method.cache.PALMethodCache;
import com.actionsoft.apps.coe.pal.pal.method.model.PALMethodAttributeModel;
import com.actionsoft.apps.coe.pal.pal.method.model.PALMethodModel;
@ -17,6 +18,8 @@ import com.actionsoft.apps.coe.pal.pal.repository.model.PALRepositoryPropertyMod
import com.actionsoft.apps.coe.pal.pal.repository.upfile.constant.CoeFileConstant;
import com.actionsoft.apps.coe.pal.pal.repository.upfile.dao.UpFileDao;
import com.actionsoft.apps.coe.pal.pal.repository.upfile.model.UpfileModel;
import com.actionsoft.apps.coe.pal.util.HttpPostUtil;
import com.actionsoft.apps.coe.pal.util.WPS4Util;
import com.actionsoft.apps.resource.plugin.profile.DCPluginProfile;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.org.model.DepartmentModel;
@ -24,6 +27,7 @@ import com.actionsoft.bpms.org.model.RoleModel;
import com.actionsoft.bpms.org.model.UserModel;
import com.actionsoft.bpms.server.DispatcherRequest;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.conf.portal.AWSPortalConf;
import com.actionsoft.bpms.server.fs.DCContext;
import com.actionsoft.bpms.server.fs.dc.DCProfileManager;
import com.actionsoft.bpms.util.DBSql;
@ -52,6 +56,8 @@ import org.apache.commons.lang.StringUtils;
import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.List;
@ -183,7 +189,7 @@ public class OutputWordUtil {
* @param tempName
* @param docName
*/
public static void createDoc2(com.alibaba.fastjson.JSONObject dataMap, String tempPath, String tempName, String docName) {
public static void createDoc2(JSONObject dataMap, String tempPath, String tempName, String docName) {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
@ -592,7 +598,15 @@ public class OutputWordUtil {
//保存结果文档
document.saveToFile(outFile.getPath(), FileFormat.Docx_2013);*/
//此处在合并文档着重处理此处
//先获取第一个文件的下载地址
/*String taskId = UUID.randomUUID().toString();
String thisFileName = taskId+".doc";
String fileURL = outFile.getPath(); // 下载文件的URL
String saveDir = "./test"; // 保存文件的目录
downloadFile(fileURL, saveDir,thisFileName);
//修改成调用wps文件发送
mergeFileForWPS(fileURL, thisFileName,dcContextpdf.getDownloadURL(),fileName,taskId);*/
Document docs1 = new Document();
docs1.loadFromFile(outFile.getPath());
@ -608,7 +622,6 @@ public class OutputWordUtil {
docs2.saveToFile(outFile.getPath(), FileFormat.Docx_2013);
docs2.dispose();
} else if (suffix.equals("pptx")) {
isPPT = true;
}
@ -728,6 +741,7 @@ public class OutputWordUtil {
PALRepositoryPropertyModel riskMatrixInfo = PALRepositoryPropertyCache.getPropertyByPropertyId(repositoryModel.getId(), "riskMatrixInfo");
if(null!=riskMatrixInfo){
JSONObject jsonObject = JSONObject.parseObject(riskMatrixInfo.getPropertyValue());
if(null != jsonObject){
JSONArray tableInfo = jsonObject.getJSONArray("table");
//添加段落设置一级序列
Paragraph paragraph_risk = section.addParagraph();
@ -738,13 +752,15 @@ public class OutputWordUtil {
tr.getCharacterFormat().setFontName("宋体");
tr.getCharacterFormat().setFontSize(12f);
paragraph_risk.applyStyle(BuiltinStyle.Body_Text); //应用标题1样式
//定义表格数据
String[] header = {"关键控制点", "控制描述","对应风险控制","角色/岗位"};
//添加表格的表头
Table table = section.addTable(true);
table.resetCells(tableInfo.size(),header.length);
table.getTableFormat().isAutoResized(true);
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
//table.getTableFormat().isAutoResized(true);
table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
TableRow row = table.getRows().get(0);
row.isHeader(true);
row.setHeight(20);
@ -757,14 +773,28 @@ public class OutputWordUtil {
txtRange.getCharacterFormat().setBold(true);
txtRange.getCharacterFormat().setFontName("宋体");
txtRange.getCharacterFormat().setFontSize(10.5f);
//设置固定列宽
if(i==0){
row.getCells().get(0).setWidth(50);
}else if(i==1){
row.getCells().get(1).setWidth(150);
}else if(i==2){
row.getCells().get(2).setWidth(150);
}else {
row.getCells().get(3).setWidth(50);
}
}
//添加数据到表中
for (int i = 1; i < tableInfo.size(); i++) {
JSONObject cellInfo = tableInfo.getJSONObject(i);
TableRow dataRow = table.getRows().get(i);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.setHeightType(TableRowHeightType.Auto);
dataRow.getRowFormat().setBackColor(Color.white);
int flagNum = 0;
for (String cellKey : cellInfo.keySet()) {
if(cellKey.equals("id")){
@ -782,10 +812,26 @@ public class OutputWordUtil {
dataRow.getRowFormat().setHorizontalAlignment(RowAlignment.Center);
dataRow.getCells().get(flagNum).getCellFormat();
flagNum++;
}
//设置固定列宽
if(i==0){
row.getCells().get(0).setWidth(50);
}else if(i==1){
row.getCells().get(1).setWidth(100);
}else if(i==2){
row.getCells().get(2).setWidth(50);
}else {
row.getCells().get(3).setWidth(50);
}
}
Paragraph paragraph_blank = section.addParagraph();
}
}
//关键控制点风险矩阵 结束
//相关文件 R_relevant_flies
@ -1739,7 +1785,7 @@ public class OutputWordUtil {
JSONObject dataAttributes = (JSONObject) element.get("attributes");
if (dataAttributes != null) {
com.alibaba.fastjson.JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
for (int i = 0; i < dataAttributesJsonArray.size(); i++) {
JSONObject dataAttribute = dataAttributesJsonArray.getJSONObject(i);
if (dataAttribute == null || "null".equals(dataAttribute.toString())) {
@ -1788,7 +1834,7 @@ public class OutputWordUtil {
* @param relationShapeIds
*/
@Deprecated
public static void setTableValue2(String repositoryId, com.alibaba.fastjson.JSONArray table, String relationShapeIds) {
public static void setTableValue2(String repositoryId, JSONArray table, String relationShapeIds) {
// 记录关联属性的流程节点防止多次查询重复文件信息耗费时间
Set<String> relationFileIds = new HashSet<>();
List<Map<String, Object>> relationFileElements2 = new ArrayList<Map<String, Object>>();
@ -1832,17 +1878,17 @@ public class OutputWordUtil {
for (Map<String, Object> element : relationFileElements2) {
String id = (String) element.get("id");
if (relationShapeId.equals(id)) {
com.alibaba.fastjson.JSONObject _tr = new com.alibaba.fastjson.JSONObject();
JSONObject _tr = new JSONObject();
OutputWordUtil.setShapeDefaultValue2((String) element.get("pid"), _tr);
String name = specialCharTransfer(element.get("text").toString());
_tr.put(OutputWordUtil.SHAPE_NAME, name.replace("\n", OutputWordUtil.WRAPSTRING));
_tr.put(OutputWordUtil.SERIAL_NUMBER, index);
com.alibaba.fastjson.JSONObject dataAttributes = (com.alibaba.fastjson.JSONObject) element.get("attributes");
JSONObject dataAttributes = (JSONObject) element.get("attributes");
if (dataAttributes != null) {
com.alibaba.fastjson.JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
for (int i = 0; i < dataAttributesJsonArray.size(); i++) {
com.alibaba.fastjson.JSONObject dataAttribute = dataAttributesJsonArray.getJSONObject(i);
JSONObject dataAttribute = dataAttributesJsonArray.getJSONObject(i);
if (dataAttribute == null || "null".equals(dataAttribute.toString())) {
continue;
}
@ -1855,7 +1901,7 @@ public class OutputWordUtil {
if (!"".equals(privateAttributeContentStr) && !"[]".equals(privateAttributeContentStr)) {
String firstChart = privateAttributeContentStr.substring(0, 1);
if ("{".equals(firstChart)) {
com.alibaba.fastjson.JSONObject privateAttributeContent = dataAttributes.getJSONObject("privateAttributeContent");
JSONObject privateAttributeContent = dataAttributes.getJSONObject("privateAttributeContent");
for (String key : privateAttributeContent.keySet()) {
_tr.put("shape_" + key, specialCharTransfer(privateAttributeContent.getString(key)));
}
@ -1919,11 +1965,11 @@ public class OutputWordUtil {
sbRole.append(",");
}
com.alibaba.fastjson.JSONObject dataAttributes = (com.alibaba.fastjson.JSONObject) element.get("attributes");
JSONObject dataAttributes = (JSONObject) element.get("attributes");
if (dataAttributes != null) {
com.alibaba.fastjson.JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
JSONArray dataAttributesJsonArray = dataAttributes.getJSONArray("attributesJsonArray");
for (int i = 0; i < dataAttributesJsonArray.size(); i++) {
com.alibaba.fastjson.JSONObject dataAttribute = dataAttributesJsonArray.getJSONObject(i);
JSONObject dataAttribute = dataAttributesJsonArray.getJSONObject(i);
if (dataAttribute == null || "null".equals(dataAttribute.toString())) {
continue;
}
@ -2118,7 +2164,7 @@ public class OutputWordUtil {
* @param relationFileElements
*/
@Deprecated
public static void setRelationShapeValueByOrgAndRole(com.alibaba.fastjson.JSONObject _tr, com.alibaba.fastjson.JSONObject dataAttribute,
public static void setRelationShapeValueByOrgAndRole(JSONObject _tr, JSONObject dataAttribute,
Map<String, String> shapeModelMap, List<Map<String, Object>> relationFileElements, String shapeId, Set<String> relationFileIds) {
String key = dataAttribute.getString("key");
if ("organization".equals(dataAttribute.getString("groupPath")) && "relation".equals(dataAttribute.getString("type"))) {
@ -2190,7 +2236,7 @@ public class OutputWordUtil {
* @return
*/
@Deprecated
public static void setShapeValue2(com.alibaba.fastjson.JSONObject _tr, com.alibaba.fastjson.JSONObject dataAttribute, Map<String, String> shapeRelationModelMap) {
public static void setShapeValue2(JSONObject _tr, JSONObject dataAttribute, Map<String, String> shapeRelationModelMap) {
String result = "";
String key = dataAttribute.getString("key");
if ("string".equals(dataAttribute.getString("type")) || "textarea".equals(dataAttribute.getString("type"))) {
@ -2305,8 +2351,8 @@ public class OutputWordUtil {
return dataJson.getJSONObject("privateAttributeContent").getString("number");
}
}
if (json.get("attributes") instanceof com.alibaba.fastjson.JSONObject) {// fastJson
com.alibaba.fastjson.JSONObject dataJson = (com.alibaba.fastjson.JSONObject) json.get("attributes");
if (json.get("attributes") instanceof JSONObject) {// fastJson
JSONObject dataJson = (JSONObject) json.get("attributes");
if (dataJson.containsKey("privateAttributeContent") && dataJson.get("privateAttributeContent").getClass().getName().contains("JSONObject")) {
return dataJson.getJSONObject("privateAttributeContent").getString("number");
}
@ -2461,4 +2507,103 @@ public class OutputWordUtil {
return false;
}
}
/**
* 合并文档wps形式
* @return
*/
public static String mergeFileForWPS(String docUrl1, String docFileName1, String docUrl2, String docFileName2,String taskId) {
try {
String url = String.format("/api/cps/sync/v1/merge");
WPS4Util.initAppInfo(YiliWpsConst.AK, YiliWpsConst.SK);
//组装wps需要的参数
JSONObject result = new JSONObject();
JSONArray array = new JSONArray();
JSONObject merged_file_obj1 = new JSONObject();
JSONObject merged_file_obj2 = new JSONObject();
result.put("task_id", taskId);
merged_file_obj1.put("doc_url",SDK.getAppAPI().getProperty(YiliWpsConst.APPID,"targetPortal")+"/r" + docUrl1.substring(1));
merged_file_obj1.put("doc_filename",docFileName1);
array.add(merged_file_obj1);
merged_file_obj2.put("doc_url",SDK.getAppAPI().getProperty(YiliWpsConst.APPID,"targetPortal")+"/r" + docUrl2.substring(1));
merged_file_obj2.put("doc_filename",docFileName2);
array.add(merged_file_obj2);
result.put("merged_file_list",array);
System.out.println("result.toString() = " + result);
//组装完毕
Map<String,String> headers = WPS4Util.getSignatureHeaders(url, "POST",result.toString(), YiliWpsConst.CONTENT_TYPE);
JSONObject json = HttpPostUtil.sendPostRequest(YiliWpsConst.HOST + "/open" + url, result.toString(), headers, JSONObject.class);
System.out.println("jsonObject = " + json.toString());
if(json.getString("code").equals("200")){
System.out.println(">>>>>>>已合并成功");
String download_id = json.getJSONObject("data").getString("download_id");
String route_key = json.getJSONObject("data").getString("route_key");
//String savePath = AWSPortalConf.getUrl() +"/test/123.doc";
String savePath = docUrl1;
System.out.println("savePath = " + savePath);
operateDocumentResultDownload(YiliWpsConst.HOST,download_id,route_key,savePath);
return savePath;
}
return "";
}catch (Exception e){
e.printStackTrace();
}
return "";
}
/**
* 根据wps的下载id下载替换文件
* @param host
* @param downloadId
* @param routeKey
* @param savePath
* @throws Exception
*/
public static void operateDocumentResultDownload(String host,String downloadId,String routeKey,String savePath) throws Exception {
String url = "/api/cps/v1/download/{download_id}";
url = url.replace("{download_id}",downloadId);
Map<String,String> headers = WPS4Util.getSignatureHeaders(url, "GET",null,YiliWpsConst.CONTENT_TYPE);
headers.put("Route-Key",routeKey);
HttpPostUtil.sendGetDownloadRequest(host + "/open" + url, headers,savePath);
}
public static void downloadFile(String fileURL, String saveDir,String fileName) {
try {
File file = new File(saveDir);
// 确保文件目录存在
if (!file.exists()) {
file.mkdirs();
}
// 创建输出流到文件
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// 检查响应码是否为HTTP_OK
if (responseCode == HttpURLConnection.HTTP_OK) {
// 通过输入流读取数据
InputStream inputStream = httpConn.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}