feat(REQ-5965) - 调整原来的方法

This commit is contained in:
wangli 2025-12-22 10:15:33 +08:00
parent f9ab24fcea
commit 05e7ad4563

View File

@ -30,8 +30,8 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -64,178 +64,6 @@ public class WpsUtil {
@Resource @Resource
private OrganizationalNodeUserQueryApi organizationalNodeUserQueryApi; private OrganizationalNodeUserQueryApi organizationalNodeUserQueryApi;
public static void main(String[] args) {
String jsonStr = "[{\"desc\":\"审批编号\",\"key\":\"processInstanceId\",\"type\":\"text\",\"value\":\"202512200123200000000\"},{\"desc\":\"发起时间\",\"key\":\"startTime\",\"type\":\"text\",\"value\":\"2025.12.20 01:23:56\"},{\"desc\":\"审批结束时间\",\"key\":\"endTime\",\"type\":\"text\"},{\"desc\":\"业务名称\",\"key\":\"processDefinitionKey\",\"type\":\"text\",\"value\":\"安全教育签字(anquanjiaoyu)\"},{\"desc\":\"业务变量主讲人单位\",\"key\":\"speakerUnit\",\"type\":\"text\",\"value\":\"四川星腾贸易有限公司\"},{\"desc\":\"业务变量主讲人部门\",\"key\":\"speakerNode\",\"type\":\"text\",\"value\":\"四川星腾贸易有限公司\"},{\"desc\":\"业务变量项目名称\",\"key\":\"workspaceName\",\"type\":\"text\",\"value\":\"test-0925项目服务部\"},{\"desc\":\"业务变量签署发起时间\",\"key\":\"signingInitiationTime\",\"type\":\"text\",\"value\":\"20251219\"},{\"desc\":\"业务变量主讲人姓名\",\"key\":\"speakerName\",\"type\":\"text\",\"value\":\"罗福\"},{\"desc\":\"发起者\",\"key\":\"initiator\",\"type\":\"obj\",\"value\":\"{\\\"assignerName\\\":\\\"罗福\\\",\\\"avatar\\\":\\\"https://axzo-app.oss-cn-chengdu.aliyuncs.com/face/8c29d036-5db3-431d-b537-9d6d5c8019da.jpg\\\",\\\"nodeId\\\":\\\"7399\\\",\\\"ouId\\\":\\\"6158\\\",\\\"personId\\\":\\\"9000399522\\\",\\\"tenantId\\\":\\\"399\\\"}\"},{\"desc\":\"主讲人审批\",\"key\":\"node_330665593249_axv0\",\"type\":\"signatureAndAdvice\",\"value\":[{\"advice\":\"123\",\"approverName\":\"罗福\",\"operationTime\":1766165056926,\"result\":\"已同意\"}]},{\"desc\":\"教育日期\",\"key\":\"educationDate\",\"type\":\"text\",\"value\":\"2025-12-02\"},{\"desc\":\"教育时长\",\"key\":\"educationDuration\",\"type\":\"text\",\"value\":\"11小时\"},{\"desc\":\"图片\",\"key\":\"picture\",\"type\":\"img\",\"value\":\"[{\\\"fileName\\\":\\\"1738899982734-97oknewbe77.png\\\",\\\"fileUrl\\\":\\\"https://axzo-public.oss-cn-chengdu.aliyuncs.com/cms/cms_test/4c7875246b9049188a055fb1c80effa4.png\\\",\\\"fileKey\\\":\\\"4c7875246b9049188a055fb1c80effa4\\\"}]\"},{\"desc\":\"签字工人\",\"key\":\"worker\",\"type\":\"text\",\"value\":\"母春田\"},{\"desc\":\"主讲人审批姓名\",\"key\":\"node_330665593249_axv0_approverName\",\"type\":\"text\",\"value\":\"罗福\"},{\"desc\":\"主讲人审批审批结果\",\"key\":\"node_330665593249_axv0_activityResult\",\"type\":\"text\",\"value\":\"已同意\"},{\"desc\":\"主讲人审批电子签名\",\"key\":\"node_330665593249_axv0_signature\",\"type\":\"img\",\"value\":[{}]},{\"desc\":\"主讲人审批审批意见\",\"key\":\"node_330665593249_axv0_advice\",\"type\":\"text\",\"value\":\"123\"},{\"desc\":\"主讲人审批日期\",\"key\":\"node_330665593249_axv0_activityOperationTime\",\"type\":\"text\",\"value\":\"2025.12.20\"},{\"desc\":\"发起人姓名\",\"key\":\"initiatorName\",\"type\":\"text\",\"value\":\"罗福\"},{\"desc\":\"发起人岗位\",\"key\":\"initiatorPosition\",\"type\":\"text\",\"value\":\"测试岗位003\"},{\"desc\":\"发起人联系方式\",\"key\":\"initiatorPhone\",\"type\":\"text\",\"value\":\"18080995942\"}]";
String jsonStr2 = "[\n" +
" {\n" +
" \"desc\": \"审批编号\",\n" +
" \"key\": \"processInstanceId\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"202512200123200000000\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"发起时间\",\n" +
" \"key\": \"startTime\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"2025.12.20 01:23:56\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"审批结束时间\",\n" +
" \"key\": \"endTime\",\n" +
" \"type\": \"text\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务名称\",\n" +
" \"key\": \"processDefinitionKey\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"安全教育签字(anquanjiaoyu)\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务变量主讲人单位\",\n" +
" \"key\": \"speakerUnit\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"四川星腾贸易有限公司\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务变量主讲人部门\",\n" +
" \"key\": \"speakerNode\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"四川星腾贸易有限公司\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务变量项目名称\",\n" +
" \"key\": \"workspaceName\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"test-0925项目服务部\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务变量签署发起时间\",\n" +
" \"key\": \"signingInitiationTime\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"20251219\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"业务变量主讲人姓名\",\n" +
" \"key\": \"speakerName\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"罗福\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"发起者\",\n" +
" \"key\": \"initiator\",\n" +
" \"type\": \"obj\",\n" +
" \"value\": \"{\\\"assignerName\\\":\\\"罗福\\\",\\\"avatar\\\":\\\"https://axzo-app.oss-cn-chengdu.aliyuncs.com/face/8c29d036-5db3-431d-b537-9d6d5c8019da.jpg\\\",\\\"nodeId\\\":\\\"7399\\\",\\\"ouId\\\":\\\"6158\\\",\\\"personId\\\":\\\"9000399522\\\",\\\"tenantId\\\":\\\"399\\\"}\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批\",\n" +
" \"key\": \"node_330665593249_axv0\",\n" +
" \"type\": \"signatureAndAdvice\",\n" +
" \"value\": [\n" +
" {\n" +
" \"advice\": \"123\",\n" +
" \"approverName\": \"罗福\",\n" +
" \"operationTime\": 1766165056926,\n" +
" \"result\": \"已同意\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"desc\": \"教育日期\",\n" +
" \"key\": \"educationDate\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"2025-12-02\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"教育时长\",\n" +
" \"key\": \"educationDuration\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"11小时\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"图片\",\n" +
" \"key\": \"picture\",\n" +
" \"type\": \"img\",\n" +
" \"value\": \"[{\\\"fileName\\\":\\\"1738899982734-97oknewbe77.png\\\",\\\"fileUrl\\\":\\\"https://axzo-public.oss-cn-chengdu.aliyuncs.com/cms/cms_test/4c7875246b9049188a055fb1c80effa4.png\\\",\\\"fileKey\\\":\\\"4c7875246b9049188a055fb1c80effa4\\\"}]\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"签字工人\",\n" +
" \"key\": \"worker\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"母春田\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批姓名\",\n" +
" \"key\": \"node_330665593249_axv0_approverName\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"罗福\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批审批结果\",\n" +
" \"key\": \"node_330665593249_axv0_activityResult\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"已同意\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批电子签名\",\n" +
" \"key\": \"node_330665593249_axv0_signature\",\n" +
" \"type\": \"img\",\n" +
" \"value\": []\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批审批意见\",\n" +
" \"key\": \"node_330665593249_axv0_advice\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"123\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"主讲人审批日期\",\n" +
" \"key\": \"node_330665593249_axv0_activityOperationTime\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"2025.12.20\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"发起人姓名\",\n" +
" \"key\": \"initiatorName\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"罗福\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"发起人岗位\",\n" +
" \"key\": \"initiatorPosition\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"测试岗位003\"\n" +
" },\n" +
" {\n" +
" \"desc\": \"发起人联系方式\",\n" +
" \"key\": \"initiatorPhone\",\n" +
" \"type\": \"text\",\n" +
" \"value\": \"18080995942\"\n" +
" }\n" +
"]";
List<VariableObjectDTO> wpsVariables = JSON.parseArray(jsonStr2, VariableObjectDTO.class);
List<FileReplaceContent> fileReplaceContents = BeanMapper.copyList(wpsVariables.stream()
.filter(i -> Objects.nonNull(i.getValue()) && checkNotEmptyColl(i))
.filter(i -> !(Objects.equals(i.getType().name(), "img") && !StringUtils.hasText(i.getValue().toString())))
.filter(i -> Objects.equals(i.getType().name(), "img") || Objects.equals(i.getType().name(), "text"))
.collect(Collectors.toList()), FileReplaceContent.class, (s, t) -> {
t.setKey(s.getDesc());
if (Objects.equals(s.getType().name(), "img")) {
if (isJson(s.getValue().toString())) {
t.setContent(JSON.parseArray(s.getValue().toString(), UploadFieldDTO.class).get(0).getFileUrl());
} else {
t.setContent(JSON.parseArray(JSON.toJSONString(s.getValue()), UploadFieldDTO.class).get(0).getFileUrl());
}
} else {
t.setContent(Objects.nonNull(s.getValue()) ? s.getValue().toString() : "");
}
t.setType(s.getType().name());
t.setPrefix("[");
t.setSuffix("]");
});
System.out.println("fileReplaceContents = " + fileReplaceContents);
}
/** /**
* 调用 wps 文件变量替换接口 * 调用 wps 文件变量替换接口
* *
@ -297,6 +125,52 @@ public class WpsUtil {
} }
} }
/**
* 调用 wps 文件变量替换接口
*
* @param fileCode 基于该模板进行变量替换
* @param fileKey 如果有值则变量替换完成后直接将文档覆盖该 fileKey 对应的文件
* @return 返回替换变量后的文件oss fileKey
*/
public String wpsFileVariableReplace(List<VariableObjectDTO> wpsVariables,
String fileCode, String fileKey, String fileName) {
List<FileReplaceContent> fileReplaceContents = BeanMapper.copyList(wpsVariables.stream()
.filter(i -> Objects.nonNull(i.getValue()) && checkNotEmptyColl(i))
.filter(i -> !(Objects.equals(i.getType().name(), "img") && !StringUtils.hasText(i.getValue().toString())))
.filter(i -> Objects.equals(i.getType().name(), "img") || Objects.equals(i.getType().name(), "text"))
.collect(Collectors.toList()), FileReplaceContent.class, (s, t) -> {
t.setKey(s.getDesc());
if (Objects.equals(s.getType().name(), "img")) {
List<UploadFieldDTO> uploadFieldDTOS;
if (isJson(s.getValue().toString())) {
uploadFieldDTOS = JSON.parseArray(s.getValue().toString(), UploadFieldDTO.class);
} else {
uploadFieldDTOS = JSON.parseArray(JSON.toJSONString(s.getValue()), UploadFieldDTO.class);
}
t.setContent(CollectionUtils.isEmpty(uploadFieldDTOS) ? "" : uploadFieldDTOS.get(0).getFileUrl());
} else {
t.setContent(Objects.nonNull(s.getValue()) ? s.getValue().toString() : "");
}
t.setType(s.getType().name());
t.setPrefix("[");
t.setSuffix("]");
});
if (StringUtils.hasText(fileCode)) {
FileTemplateReplaceRequest request = new FileTemplateReplaceRequest();
request.setFileCode(fileCode);
request.setFileKey(fileKey);
request.setReplaceContentList(fileReplaceContents);
return RpcExternalUtil.rpcProcessor(() -> fileTemplateApi.replaceWordText(request), "替换 WPS 文档变量", request);
} else {
SimpleFileTemplateReplaceRequest request = new SimpleFileTemplateReplaceRequest();
request.setFileName(fileName);
request.setFileKey(fileKey);
request.setReplaceContentList(fileReplaceContents);
return RpcExternalUtil.rpcProcessor(() -> fileTemplateApi.replaceWordTextForFileKey(request), "替换 WPS 文档变量(simple)", request);
}
}
private static boolean checkNotEmptyColl(VariableObjectDTO i) { private static boolean checkNotEmptyColl(VariableObjectDTO i) {
if (!(i.getValue() instanceof Collection)) { if (!(i.getValue() instanceof Collection)) {
return true; return true;