|
@@ -1,7 +1,11 @@
|
|
|
package com.malk.minglei.controller;
|
|
package com.malk.minglei.controller;
|
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.malk.minglei.dto.MingleiPortRequest;
|
|
import com.malk.minglei.dto.MingleiPortRequest;
|
|
|
|
|
+import com.malk.minglei.dto.SupplierPriceApprovalProcessRequest;
|
|
|
import com.malk.minglei.service.port.MingleiPortService;
|
|
import com.malk.minglei.service.port.MingleiPortService;
|
|
|
|
|
+import com.malk.minglei.service.port.SupplierPriceApprovalProcessService;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -15,10 +19,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.client.RestClientException;
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
|
|
|
|
-import javax.validation.Valid;
|
|
|
|
|
|
|
+import javax.validation.ConstraintViolation;
|
|
|
|
|
+import javax.validation.Validation;
|
|
|
|
|
+import javax.validation.Validator;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 提供调用明磊外部单据接口的本地触发入口。
|
|
* 提供调用明磊外部单据接口的本地触发入口。
|
|
@@ -30,27 +37,60 @@ public class MingleiPortController {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MingleiPortController.class);
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MingleiPortController.class);
|
|
|
|
|
|
|
|
private final MingleiPortService mingleiPortService;
|
|
private final MingleiPortService mingleiPortService;
|
|
|
|
|
+ private final SupplierPriceApprovalProcessService supplierPriceApprovalProcessService;
|
|
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
|
|
+ private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 创建明磊外部单据接口控制器。
|
|
* 创建明磊外部单据接口控制器。
|
|
|
*
|
|
*
|
|
|
* @param mingleiPortService 明磊外部单据接口服务
|
|
* @param mingleiPortService 明磊外部单据接口服务
|
|
|
|
|
+ * @param supplierPriceApprovalProcessService 宜搭实例转 T100 单据服务
|
|
|
|
|
+ * @param objectMapper JSON 解析器
|
|
|
*/
|
|
*/
|
|
|
- public MingleiPortController(MingleiPortService mingleiPortService) {
|
|
|
|
|
|
|
+ public MingleiPortController(MingleiPortService mingleiPortService,
|
|
|
|
|
+ SupplierPriceApprovalProcessService supplierPriceApprovalProcessService,
|
|
|
|
|
+ ObjectMapper objectMapper) {
|
|
|
this.mingleiPortService = mingleiPortService;
|
|
this.mingleiPortService = mingleiPortService;
|
|
|
|
|
+ this.supplierPriceApprovalProcessService = supplierPriceApprovalProcessService;
|
|
|
|
|
+ this.objectMapper = objectMapper;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 接收本系统的供应商价格审批单请求,并调用外部 T100 接口。
|
|
|
|
|
|
|
+ * 接收两类请求。带 formId 时按宜搭实例读取表单并推送 T100;否则按已组装好的 head、detail 转发。
|
|
|
*
|
|
*
|
|
|
- * @param request 仅包含 head 和 detail 的供应商价格审批单请求体
|
|
|
|
|
|
|
+ * @param body 宜搭实例标识,或 T100 单据 head、detail
|
|
|
* @return 外部 T100 接口响应内容
|
|
* @return 外部 T100 接口响应内容
|
|
|
*/
|
|
*/
|
|
|
@PostMapping("/supplier-price-approvals")
|
|
@PostMapping("/supplier-price-approvals")
|
|
|
- public ResponseEntity<Map<String, Object>> submitSupplierPriceApproval(@Valid @RequestBody MingleiPortRequest request) {
|
|
|
|
|
|
|
+ public ResponseEntity<Map<String, Object>> submitSupplierPriceApproval(@RequestBody JsonNode body) {
|
|
|
|
|
+ if (body != null && body.has("formId")) {
|
|
|
|
|
+ return ResponseEntity.ok(submitYidaInstance(body));
|
|
|
|
|
+ }
|
|
|
|
|
+ MingleiPortRequest request = objectMapper.convertValue(body, MingleiPortRequest.class);
|
|
|
|
|
+ Set<ConstraintViolation<MingleiPortRequest>> violations = validator.validate(request);
|
|
|
|
|
+ if (!violations.isEmpty()) {
|
|
|
|
|
+ return ResponseEntity.badRequest().body(Collections.<String, Object>singletonMap("message", "Request validation failed"));
|
|
|
|
|
+ }
|
|
|
return ResponseEntity.ok(mingleiPortService.submitSupplierPriceApproval(request));
|
|
return ResponseEntity.ok(mingleiPortService.submitSupplierPriceApproval(request));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private Map<String, Object> submitYidaInstance(JsonNode body) {
|
|
|
|
|
+ SupplierPriceApprovalProcessRequest request = objectMapper.convertValue(body, SupplierPriceApprovalProcessRequest.class);
|
|
|
|
|
+ if (isBlank(request.getFormId()) || isBlank(request.getFormInstanceId())) {
|
|
|
|
|
+ throw new IllegalArgumentException("formId and formInstanceId must not be blank");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (request.requiresProcessInstanceId() && isBlank(request.getProcessInstanceId())) {
|
|
|
|
|
+ throw new IllegalArgumentException("processInstanceId must not be blank");
|
|
|
|
|
+ }
|
|
|
|
|
+ return supplierPriceApprovalProcessService.submitByInstanceIds(
|
|
|
|
|
+ request.getFormId(), request.getFormInstanceId(), request.getProcessInstanceId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean isBlank(String value) {
|
|
|
|
|
+ return value == null || value.trim().isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 返回本地请求体校验或 JSON 解析失败的信息,不发起外部调用。
|
|
* 返回本地请求体校验或 JSON 解析失败的信息,不发起外部调用。
|
|
|
*
|
|
*
|
|
@@ -62,6 +102,17 @@ public class MingleiPortController {
|
|
|
return ResponseEntity.badRequest().body(Collections.singletonMap("message", "Request validation failed"));
|
|
return ResponseEntity.badRequest().body(Collections.singletonMap("message", "Request validation failed"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 返回宜搭实例参数不完整或表单不受支持的错误信息。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param exception 参数异常
|
|
|
|
|
+ * @return 参数错误响应
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(IllegalArgumentException.class)
|
|
|
|
|
+ public ResponseEntity<Map<String, String>> handleInvalidArgument(IllegalArgumentException exception) {
|
|
|
|
|
+ return ResponseEntity.badRequest().body(Collections.singletonMap("message", exception.getMessage()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 将外部服务的连接、超时或响应异常转换为本地网关错误。
|
|
* 将外部服务的连接、超时或响应异常转换为本地网关错误。
|
|
|
*
|
|
*
|