diff --git a/pom.xml b/pom.xml
index eaf8e51..1755489 100644
--- a/pom.xml
+++ b/pom.xml
@@ -112,6 +112,12 @@
jackson-datatype-jsr310
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
diff --git a/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFileClient.java b/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFileClient.java
new file mode 100644
index 0000000..780e2d5
--- /dev/null
+++ b/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFileClient.java
@@ -0,0 +1,63 @@
+package com.finance.system.adapter.feishu;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.finance.system.exception.BusinessException;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.http.*;
+import org.springframework.stereotype.Component;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.util.StringUtils;
+import org.springframework.web.client.RestClientResponseException;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Collections;
+
+/** One-shot adapter for the legacy approval attachment upload contract. */
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class FeishuApprovalFileClient {
+ static final String UPLOAD_URL = "https://www.feishu.cn/approval/openapi/v2/file/upload";
+ private final FeishuAuthService auth;
+ private final RestTemplate http;
+
+ public String upload(String fileName, String contentType, byte[] content) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setBearerAuth(auth.getTenantAccessToken());
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
+ headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
+ MultiValueMap parts = new LinkedMultiValueMap<>();
+ parts.add("name", fileName);
+ parts.add("type", "attachment");
+ HttpHeaders fileHeaders = new HttpHeaders();
+ if (StringUtils.hasText(contentType)) {
+ try { fileHeaders.setContentType(MediaType.parseMediaType(contentType)); }
+ catch (Exception ignored) { fileHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); }
+ } else fileHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+ ByteArrayResource resource = new ByteArrayResource(content) {
+ @Override public String getFilename() { return fileName; }
+ };
+ parts.add("content", new HttpEntity<>(resource, fileHeaders));
+ try {
+ ResponseEntity response = http.exchange(UPLOAD_URL, HttpMethod.POST,
+ new HttpEntity<>(parts, headers), JsonNode.class);
+ JsonNode body = response.getBody();
+ if (body == null || !body.path("code").isIntegralNumber() || body.path("code").asInt() != 0)
+ throw new BusinessException("飞书拒绝上传附件,请检查文件或应用权限", 502);
+ JsonNode code = body.path("data").path("code");
+ if (!code.isTextual() || !StringUtils.hasText(code.asText()) || code.asText().length() > 500)
+ throw new BusinessException("飞书附件上传响应无效,结果可能未知,请勿自动重试", 502);
+ return code.asText();
+ } catch (BusinessException e) { throw e;
+ } catch (RestClientResponseException e) {
+ log.warn("Feishu attachment upload rejected: httpStatus={}", e.getRawStatusCode());
+ throw new BusinessException("飞书附件上传失败,结果可能未知,请勿自动重试", 502);
+ } catch (Exception e) {
+ log.warn("Feishu attachment upload outcome unknown", e);
+ throw new BusinessException("飞书附件上传结果未知,请勿自动重试", 502);
+ }
+ }
+}
diff --git a/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFormBuilder.java b/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFormBuilder.java
index ddbb872..b807bb4 100644
--- a/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFormBuilder.java
+++ b/src/main/java/com/finance/system/adapter/feishu/FeishuApprovalFormBuilder.java
@@ -1,82 +1,354 @@
package com.finance.system.adapter.feishu;
-import com.finance.system.entity.ReimApplication;
-import com.finance.system.entity.ReimExpenseItem;
-import com.finance.system.entity.SysUser;
-import com.finance.system.entity.SysDepartment;
-import com.finance.system.entity.SysExpenseType;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.finance.system.dto.ReimbursementFormField;
+import com.finance.system.entity.*;
+import com.finance.system.exception.BusinessException;
+import com.finance.system.util.Sha256;
+import org.springframework.util.StringUtils;
import java.math.BigDecimal;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
+import java.nio.charset.StandardCharsets;
+import java.time.*;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
-/**
- * 飞书审批表单构建器
- * 将系统报销数据转换为飞书审批实例的表单数据
- */
-public class FeishuApprovalFormBuilder {
+/** Builds approval-v4 form data exclusively from immutable application snapshots. */
+public final class FeishuApprovalFormBuilder {
+ private static final ObjectMapper JSON = new ObjectMapper();
+ private static final int MAX_JSON_BYTES = 1000000;
+ private FeishuApprovalFormBuilder() { }
- /**
- * 构建审批表单数据
- */
- public static Map buildForm(ReimApplication app, SysUser applicant,
- SysDepartment department, SysExpenseType expenseType,
- List items) {
- Map form = new LinkedHashMap<>();
-
- // 基本字段
- form.put("application_no", app.getApplicationNo());
- form.put("title", app.getTitle());
- form.put("applicant_name", applicant != null ? applicant.getRealName() : "");
- form.put("department", department != null ? department.getDeptName() : "");
- form.put("expense_type", expenseType != null ? expenseType.getTypeName() : "");
- form.put("total_amount", app.getTotalAmount() != null ? app.getTotalAmount().toString() : "0");
- form.put("description", app.getDescription() != null ? app.getDescription() : "");
- form.put("source_doc_no", app.getSourceDocNo() != null ? app.getSourceDocNo() : "");
-
- // 费用明细(多行文本展示)
- if (items != null && !items.isEmpty()) {
- String itemDetail = ReimbursementCanonicalizer.orderedItems(items).stream()
- .map(item -> String.format("%s | %s | ¥%.2f",
- item.getExpenseDate() != null ? item.getExpenseDate().toString() : "",
- item.getDescription() != null ? item.getDescription() : "",
- item.getAmount() != null ? item.getAmount() : BigDecimal.ZERO))
- .collect(Collectors.joining("\n"));
- form.put("expense_items", itemDetail);
- }
-
- return form;
+ public static String buildFormJson(ReimApplication app) {
+ if (!StringUtils.hasText(app.getFeishuFormFingerprint()) || !StringUtils.hasText(app.getFeishuFormSchema()))
+ throw new BusinessException("报销申请缺少已固定的飞书表单快照,禁止创建远程审批", 409);
+ if (!app.getFeishuFormFingerprint().equals(Sha256.hex(app.getFeishuFormSchema())))
+ throw new BusinessException("报销申请的飞书表单快照校验失败", 409);
+ if (!StringUtils.hasText(app.getFeishuFormDisplayValues()))
+ throw new BusinessException("报销申请缺少不可变的飞书展示值快照", 409);
+ try {
+ requireSize(app.getFeishuFormSchema(), "飞书表单快照过大");
+ requireSize(app.getFeishuFormValues(), "飞书自定义表单值过大");
+ requireSize(app.getFeishuFormDisplayValues(), "飞书展示值快照过大");
+ List fields = JSON.readValue(app.getFeishuFormSchema(),
+ new TypeReference>() { });
+ Map custom = StringUtils.hasText(app.getFeishuFormValues())
+ ? JSON.readValue(app.getFeishuFormValues(), new TypeReference>() { })
+ : Collections.emptyMap();
+ Map display = JSON.readValue(app.getFeishuFormDisplayValues(),
+ new TypeReference>() { });
+ validateDisplaySnapshot(fields, display);
+ validateTotals(fields, display, app.getTotalAmount());
+ List