|
|
@@ -1,5 +1,6 @@
|
|
|
package com.malk.minglei.dto;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
|
import javax.validation.constraints.NotBlank;
|
|
|
@@ -30,6 +31,64 @@ public class ReplyErpRequest {
|
|
|
private String ob1;
|
|
|
private String ob2;
|
|
|
|
|
|
+ private ReplyErpRequest() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Supports both the legacy flat body and the T100 standard envelope. */
|
|
|
+ public static ReplyErpRequest from(JsonNode root) {
|
|
|
+ if (root == null || !root.isObject()) {
|
|
|
+ throw new IllegalArgumentException("request body must be a JSON object");
|
|
|
+ }
|
|
|
+ JsonNode parameter = root.path("payload").path("std_data").path("parameter");
|
|
|
+ if (!parameter.isObject()) {
|
|
|
+ parameter = root;
|
|
|
+ }
|
|
|
+
|
|
|
+ ReplyErpRequest request = new ReplyErpRequest();
|
|
|
+ request.prog = text(parameter, "prog");
|
|
|
+ request.docno = text(parameter, "docno");
|
|
|
+ request.stus = text(parameter, "stus");
|
|
|
+ request.ref = text(parameter, "ref");
|
|
|
+ request.dept = text(parameter, "dept");
|
|
|
+ request.ob1 = text(parameter, "ob1");
|
|
|
+ request.ob2 = text(parameter, "ob2");
|
|
|
+
|
|
|
+ JsonNode userid = root.get("userid");
|
|
|
+ if (userid == null || userid.isNull()) {
|
|
|
+ userid = parameter.get("userid");
|
|
|
+ }
|
|
|
+ request.userid = userid == null || userid.isNull()
|
|
|
+ ? null
|
|
|
+ : userid.isArray() ? firstText(userid) : userid.asText(null);
|
|
|
+ request.validate();
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String text(JsonNode node, String field) {
|
|
|
+ JsonNode value = node.get(field);
|
|
|
+ return value == null || value.isNull() ? null : value.asText(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String firstText(JsonNode values) {
|
|
|
+ for (JsonNode value : values) {
|
|
|
+ if (value != null && !value.isNull() && !value.asText().trim().isEmpty()) {
|
|
|
+ return value.asText();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validate() {
|
|
|
+ if (isBlank(prog) || isBlank(docno) || isBlank(userid) || isBlank(stus)
|
|
|
+ || !("Y".equals(stus) || "N".equals(stus))) {
|
|
|
+ throw new IllegalArgumentException("invalid reply ERP request");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isBlank(String value) {
|
|
|
+ return value == null || value.trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
public String getProg() {
|
|
|
return prog;
|
|
|
}
|