Commit 03db73d1e307df1f270c88ae237d590349ab6d69

Authored by zhangmeiyang
1 parent 617c9355

refactor(mapping): 重构校验逻辑并优化字段反射访问

- 移除未使用的 Optional 导入
- 添加 putDefaultValid 抽象方法用于自定义校验规则
- 将 getVerifyKeys 和 getReturnKeys 方法重命名为 loadDbValid 和 loadDbValidReturn
- 新增 reflectFieldValue 工具方法统一处理字段访问异常
- 优化验证字符串生成逻辑,支持默认校验规则注入
- 调整 Transformer 类结构,增强扩展性和可维护性
tax-central/src/main/java/com/diligrp/tax/central/utils/MappingUtils.java
... ... @@ -8,8 +8,8 @@ import com.diligrp.tax.central.converter.anno.SubConverter;
8 8 import com.diligrp.tax.central.domain.BaseDocument;
9 9 import com.diligrp.tax.central.domain.BaseMapping;
10 10 import com.diligrp.tax.central.exception.TaxAgentServiceException;
11   -import com.diligrp.tax.central.model.PipelineDocFieldTypeRemoteParamDO;
12 11 import com.diligrp.tax.central.model.PipelineDocFieldTypeDO;
  12 +import com.diligrp.tax.central.model.PipelineDocFieldTypeRemoteParamDO;
13 13 import com.diligrp.tax.central.type.TaxSystemType;
14 14 import com.fasterxml.jackson.core.type.TypeReference;
15 15 import lombok.extern.slf4j.Slf4j;
... ... @@ -79,6 +79,15 @@ public class MappingUtils {
79 79 return fields;
80 80 }
81 81  
  82 + public static Object reflectFieldValue(Field field, Object o) {
  83 + field.setAccessible(true);
  84 + try {
  85 + return field.get(o);
  86 + } catch (IllegalAccessException e) {
  87 + throw new TaxAgentServiceException(TaxSystemType.UNKNOWN_ERROR, e.getMessage());
  88 + }
  89 + }
  90 +
82 91 public static void reflectField(Object object, Field field, Map<String, PipelineDocFieldTypeDO> kvMap) {
83 92 if (kvMap.containsKey(field.getName())) {
84 93 field.setAccessible(true);
... ...
tax-map/src/main/java/com/diligrp/tax/mapping/demarcate/Transformer.java
... ... @@ -2,7 +2,6 @@ package com.diligrp.tax.mapping.demarcate;
2 2  
3 3 import com.diligrp.tax.central.domain.BaseDocument;
4 4 import com.diligrp.tax.central.domain.BaseMapping;
5   -import com.diligrp.tax.central.domain.mapping.kingdee.CustomerMapping;
6 5 import com.diligrp.tax.central.domain.mapping.kingdee.VerifyMarkInterface;
7 6 import com.diligrp.tax.central.exception.TaxAgentServiceException;
8 7 import com.diligrp.tax.central.model.PipelineDO;
... ... @@ -16,6 +15,7 @@ import jakarta.annotation.Resource;
16 15 import lombok.extern.slf4j.Slf4j;
17 16  
18 17 import java.lang.reflect.Field;
  18 +import java.text.MessageFormat;
19 19 import java.util.*;
20 20  
21 21 /**
... ... @@ -39,7 +39,7 @@ public abstract class Transformer&lt;T extends BaseMapping&gt; {
39 39 /**
40 40 * 变换
41 41 *
42   - * @param document 公文
  42 + * @param document 公文
43 43 * @param pipelineDO
44 44 * @return {@link T }
45 45 */
... ... @@ -47,12 +47,21 @@ public abstract class Transformer&lt;T extends BaseMapping&gt; {
47 47  
48 48  
49 49 /**
  50 + * 设置默认校验
  51 + *
  52 + * @param verifyKeys 验证密钥
  53 + * @param t t
  54 + */
  55 + protected abstract void putDefaultValid(List<String> verifyKeys, T t);
  56 +
  57 +
  58 + /**
50 59 * 获取验证密钥
51 60 *
52 61 * @param pipelineId 管道 ID
53 62 * @return {@link List }<{@link String }>
54 63 */
55   - protected List<String> getVerifyKeys(Long pipelineId) {
  64 + private List<String> loadDbValid(Long pipelineId) {
56 65 List<PipelineDocValidDO> taxPipelineVerifies = taxPipelineVerifyService.verifyInput(pipelineId, markDocument().value);
57 66 return Optional.ofNullable(taxPipelineVerifies).orElse(List.of()).stream().map(PipelineDocValidDO::getConfigVerifyField).toList();
58 67 }
... ... @@ -63,10 +72,10 @@ public abstract class Transformer&lt;T extends BaseMapping&gt; {
63 72 * @param pipelineId 管道 ID
64 73 * @return {@link String }
65 74 */
66   - protected String getReturnKeys(Long pipelineId) {
  75 + private String loadDbValidReturn(Long pipelineId) {
67 76 PipelineValidReturnDO pipelineValidReturnDO = taxPipelineVerifyService.verifyOutput(pipelineId, markDocument().value);
68 77 if (Objects.isNull(pipelineValidReturnDO)) {
69   - return "";
  78 + throw new TaxAgentServiceException(TaxSystemType.ABNORMAL_PARAMETERS, "配置了单据验证但是未配置验证返回值");
70 79 }
71 80 return pipelineValidReturnDO.getConfigReturnField();
72 81 }
... ... @@ -79,50 +88,45 @@ public abstract class Transformer&lt;T extends BaseMapping&gt; {
79 88 * @param mapping
80 89 * @return {@link String }
81 90 */
82   - protected String getVerifyString(List<String> verifyKeys, BaseMapping mapping) {
  91 + protected List<String> loadDbValidation(List<String> verifyKeys, BaseMapping mapping) {
83 92 var verifyKeysSet = new HashSet<>(verifyKeys);
84 93 List<String> concatVerify = new ArrayList<>();
85 94 List<Field> fields = MappingUtils.listFields(mapping.getClass());
86   - fields.forEach(field -> {
87   - if (verifyKeysSet.contains(field.getName())) {
88   - try {
89   - if (VerifyMarkInterface.class.isAssignableFrom(field.getType())) {
90   - // 获取字段值
91   - field.setAccessible(true);
92   - Object fieldValue = field.get(mapping);
93   - // 检查字段值是否为null
94   - if (Objects.nonNull(fieldValue)) {
95   - VerifyMarkInterface verifyObject = (VerifyMarkInterface) fieldValue;
96   - String verifyMark = verifyObject.verify();
97   - String value = verifyObject.value();
98   - if (Objects.nonNull(value)) {
99   - var verify = verifyMark + "=" + value;
100   - concatVerify.add(verify);
101   - }
102   - }
103   - }
104   - if (field.getType().equals(String.class)) {
105   - field.setAccessible(true);
106   - Object fieldValue = field.get(mapping);
107   - if (Objects.nonNull(fieldValue)) {
108   - String verify = field.getName() + "=" + fieldValue;
109   - concatVerify.add(verify);
110   - }
111   - }
112   - } catch (IllegalAccessException e) {
113   - log.error("获取字段值异常", e);
114   - throw new TaxAgentServiceException(TaxSystemType.ABNORMAL_PARAMETERS);
115   - }
  95 + fields.stream().filter(field -> verifyKeysSet.contains(field.getName())).forEach(e -> {
  96 + Object fieldValue = MappingUtils.reflectFieldValue(e, mapping);
  97 + var fieldType = e.getType();
  98 + if (VerifyMarkInterface.class.isAssignableFrom(fieldType)) {
  99 + Optional.ofNullable(fieldValue).ifPresent(f -> {
  100 + VerifyMarkInterface verifyObject = (VerifyMarkInterface) f;
  101 + Optional.ofNullable(verifyObject.value()).ifPresent(v -> {
  102 + String valid = MessageFormat.format("{0} = {1}", verifyObject.verify(), v);
  103 + concatVerify.add(valid);
  104 + });
  105 + });
  106 + }
  107 + if (fieldType.equals(String.class)) {
  108 + Optional.ofNullable(fieldValue).ifPresent(v -> {
  109 + String valid = MessageFormat.format("{0} = {1}", e.getName(), v);
  110 + concatVerify.add(valid);
  111 + });
116 112 }
117 113 });
118   - log.info("verifyInfo is {},", String.join(" and ", concatVerify));
119   - return String.join(" and ", concatVerify);
  114 + return concatVerify;
120 115 }
121 116  
122   - protected void setValidKeys(PipelineDO pipelineDO, BaseMapping mapping) {
123   - List<String> verifyKeys = getVerifyKeys(pipelineDO.getId());
124   - String returnKeys = getReturnKeys(pipelineDO.getId());
125   - Optional.ofNullable(verifyKeys).ifPresent(e -> mapping.setVerifyInformation(getVerifyString(verifyKeys, mapping)));
126   - Optional.ofNullable(returnKeys).ifPresent(mapping::setReturnKeys);
  117 + /**
  118 + * 设置有效密钥
  119 + *
  120 + * @param pipelineDO 管道DO
  121 + * @param t t
  122 + */
  123 + protected void setValidKeys(PipelineDO pipelineDO, T t) {
  124 + List<String> verifyKeys = loadDbValid(pipelineDO.getId());
  125 + Optional.ofNullable(verifyKeys).ifPresent(e -> {
  126 + t.setReturnKeys(loadDbValidReturn(pipelineDO.getId()));
  127 + List<String> validations = loadDbValidation(verifyKeys, t);
  128 + putDefaultValid(validations, t);
  129 + t.setVerifyInformation(String.join(" AND ", validations));
  130 + });
127 131 }
128 132 }
... ...
tax-map/src/main/java/com/diligrp/tax/mapping/demarcate/kingdee/CustomerTransformer.java
... ... @@ -11,7 +11,6 @@ import lombok.extern.slf4j.Slf4j;
11 11 import org.springframework.stereotype.Component;
12 12  
13 13 import java.util.List;
14   -import java.util.Optional;
15 14  
16 15 /**
17 16 * @Author: zhangmeiyang
... ... @@ -28,6 +27,11 @@ public class CustomerTransformer extends Transformer&lt;CustomerMapping&gt; {
28 27 }
29 28  
30 29 @Override
  30 + protected void putDefaultValid(List<String> verifyKeys, CustomerMapping customerMapping) {
  31 + // 空方法 不需要校验
  32 + }
  33 +
  34 + @Override
31 35 public CustomerMapping transform(BaseDocument document, PipelineDO pipelineDO) {
32 36 StandardCustomer customer = (StandardCustomer) document;
33 37 CustomerMapping mapping = MappingUtils.convertValue(customer, CustomerMapping.class);
... ...
tax-map/src/main/java/com/diligrp/tax/mapping/demarcate/kingdee/ReceiptTransformer.java
... ... @@ -3,12 +3,15 @@ package com.diligrp.tax.mapping.demarcate.kingdee;
3 3 import com.diligrp.tax.central.domain.BaseDocument;
4 4 import com.diligrp.tax.central.domain.document.kingdee.bill.ReceiptBill;
5 5 import com.diligrp.tax.central.domain.mapping.kingdee.ReceiptMapping;
  6 +import com.diligrp.tax.central.domain.mapping.kingdee.base.FRECEIVEBILLENTRY;
6 7 import com.diligrp.tax.central.model.PipelineDO;
7 8 import com.diligrp.tax.central.type.DocumentType;
8 9 import com.diligrp.tax.central.utils.MappingUtils;
9 10 import com.diligrp.tax.mapping.demarcate.Transformer;
10 11 import org.springframework.stereotype.Component;
11 12  
  13 +import java.math.BigDecimal;
  14 +import java.text.MessageFormat;
12 15 import java.util.List;
13 16 import java.util.Optional;
14 17  
... ... @@ -27,10 +30,15 @@ public class ReceiptTransformer extends Transformer&lt;ReceiptMapping&gt; {
27 30 }
28 31  
29 32 @Override
  33 + protected void putDefaultValid(List<String> verifyKeys, ReceiptMapping receiptMapping) {
  34 + // 空方法 不需要校验金额
  35 + }
  36 +
  37 + @Override
30 38 public ReceiptMapping transform(BaseDocument document, PipelineDO pipelineDO) {
31 39 ReceiptBill bill = (ReceiptBill) document;
32   - ReceiptMapping mapping = MappingUtils.convertValue(bill,ReceiptMapping.class);
33   - setValidKeys(pipelineDO,mapping);
  40 + ReceiptMapping mapping = MappingUtils.convertValue(bill, ReceiptMapping.class);
  41 + setValidKeys(pipelineDO, mapping);
34 42 return mapping;
35 43 }
36 44 }
... ...
tax-map/src/main/java/com/diligrp/tax/mapping/demarcate/kingdee/ReceivableTransformer.java
... ... @@ -9,6 +9,11 @@ import com.diligrp.tax.central.utils.MappingUtils;
9 9 import com.diligrp.tax.mapping.demarcate.Transformer;
10 10 import org.springframework.stereotype.Component;
11 11  
  12 +import java.math.BigDecimal;
  13 +import java.text.MessageFormat;
  14 +import java.util.List;
  15 +import java.util.Optional;
  16 +
12 17  
13 18 /**
14 19 * @Author: zhangmeiyang
... ... @@ -24,6 +29,17 @@ public class ReceivableTransformer extends Transformer&lt;ReceivableMapping&gt; {
24 29 }
25 30  
26 31 @Override
  32 + protected void putDefaultValid(List<String> verifyKeys, ReceivableMapping mapping) {
  33 + var sum = mapping.getFEntityDetail().stream().map(e -> {
  34 + BigDecimal absoluteValue = Optional.ofNullable(e.getFPrice()).map(BigDecimal::new).orElse(BigDecimal.ZERO);
  35 + BigDecimal factor = Optional.ofNullable(e.getFPriceQty()).map(BigDecimal::new).orElse(BigDecimal.ZERO);
  36 + return absoluteValue.multiply(factor);
  37 + }).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
  38 + String format = MessageFormat.format("FALLAMOUNTFOR = {0}", sum);
  39 + verifyKeys.add(format);
  40 + }
  41 +
  42 + @Override
27 43 public ReceivableMapping transform(BaseDocument document, PipelineDO pipelineDO) {
28 44 ReceivableBill bill = (ReceivableBill) document;
29 45 ReceivableMapping mapping = MappingUtils.convertValue(bill, ReceivableMapping.class);
... ...
tax-map/src/main/java/com/diligrp/tax/mapping/demarcate/kingdee/RefundTransformer.java
... ... @@ -27,13 +27,15 @@ public class RefundTransformer extends Transformer&lt;RefundMapping&gt; {
27 27 }
28 28  
29 29 @Override
  30 + protected void putDefaultValid(List<String> verifyKeys, RefundMapping refundMapping) {
  31 + // 空方法 不需要默认校验
  32 + }
  33 +
  34 + @Override
30 35 public RefundMapping transform(BaseDocument document, PipelineDO pipelineDO) {
31 36 RefundBill bill = (RefundBill) document;
32 37 RefundMapping mapping = MappingUtils.convertValue(bill,RefundMapping.class);
33   - List<String> verifyKeys = getVerifyKeys(pipelineDO.getId());
34   - String returnKeys = getReturnKeys(pipelineDO.getId());
35   - Optional.ofNullable(verifyKeys).ifPresent(e -> mapping.setVerifyInformation(getVerifyString(verifyKeys, mapping)));
36   - Optional.ofNullable(returnKeys).ifPresent(mapping::setReturnKeys);
  38 + setValidKeys(pipelineDO,mapping);
37 39 return mapping;
38 40 }
39 41 }
... ...