Procházet zdrojové kódy

feat: 优化代码

huangzqa před 1 rokem
rodič
revize
42610a434e

+ 13 - 1
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/business/IBuSyncBusiness.java

@@ -1,7 +1,7 @@
 package com.dragonsoft.dcuc.approve.business;
 
 import com.dragonsoft.dcuc.approve.model.ApproveRecordInfo;
-import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncReqVO;
+import com.dragonsoft.dcuc.approve.model.vo.ApprovalInfoMsgItemDTO;
 import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
 
 /**
@@ -16,6 +16,8 @@ public interface IBuSyncBusiness {
 
     /**
      * 同步到BU
+     *
+     * @return 状态
      */
     ResponseStatus syncTuBu();
 
@@ -26,4 +28,14 @@ public interface IBuSyncBusiness {
      * @return 状态
      */
     ResponseStatus syncToBu(ApproveRecordInfo approveRecordInfo);
+
+
+    /**
+     * 同步到BU
+     *
+     * @param approvalInfoMsgItemDTO 请求同步请求
+     * @return 状态
+     */
+    ResponseStatus syncToBu(ApprovalInfoMsgItemDTO approvalInfoMsgItemDTO);
+
 }

+ 9 - 0
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/business/INoticeBusiness.java

@@ -1,6 +1,7 @@
 package com.dragonsoft.dcuc.approve.business;
 
 import com.dragonsoft.dcuc.approve.model.req.v3.ZeroTrustApproveCallbackReqV3VO;
+import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncReqVO;
 import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
 
 /**
@@ -29,4 +30,12 @@ public interface INoticeBusiness {
      * @return 状态
      */
     ResponseStatus baseApproveNotification(String callbackUrl, ZeroTrustApproveCallbackReqV3VO zeroTrustApproveCallbackReqV3VO);
+
+    /**
+     * 请求同步到 BU
+     *
+     * @param buApproveSyncReqVO 同步请求
+     * @return 状态
+     */
+     ResponseStatus retryRequestSyncToBu(BuApproveSyncReqVO buApproveSyncReqVO);
 }

+ 17 - 102
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/business/impl/BuSyncBusinessImpl.java

@@ -1,40 +1,27 @@
 package com.dragonsoft.dcuc.approve.business.impl;
 
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.StrUtil;
 import com.dragoninfo.dcuc.common.annotation.CacheLock;
 import com.dragoninfo.dcuc.common.utils.ResponseUtil;
 import com.dragonsoft.dcuc.approve.business.IBuSyncBusiness;
-import com.dragonsoft.dcuc.approve.constants.ApproveBeanConstants;
+import com.dragonsoft.dcuc.approve.business.INoticeBusiness;
 import com.dragonsoft.dcuc.approve.constants.ApproveConstants;
 import com.dragonsoft.dcuc.approve.enumresources.bu.ApproveModeEnum;
-import com.dragonsoft.dcuc.approve.enumresources.bu.BuErrorCodeEnum;
 import com.dragonsoft.dcuc.approve.model.ApproveCirculationInfo;
 import com.dragonsoft.dcuc.approve.model.ApproveRecordInfo;
 import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncBizApproveReqVO;
 import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncBizDataReqVO;
 import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncReqVO;
-import com.dragonsoft.dcuc.approve.model.resp.v3.ZeroTrustResultDataVO;
-import com.dragoninfo.dcuc.common.utils.ValidUtil;
-import com.dragonsoft.dcuc.approve.properties.ApproveBuProperties;
+import com.dragonsoft.dcuc.approve.model.vo.ApprovalInfoMsgItemDTO;
 import com.dragonsoft.dcuc.approve.service.IApproveCirculationService;
 import com.dragonsoft.dcuc.approve.service.IApproveRecordService;
 import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
 import com.dragonsoft.duceap.base.enums.BooleanEnum;
-import com.dragonsoft.duceap.commons.util.json.JsonUtils;
-import com.dragonsoft.duceap.commons.util.string.StringUtils;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
-import org.springframework.web.client.RestTemplate;
 
 import java.util.List;
 
@@ -50,41 +37,25 @@ import java.util.List;
 @Component
 public class BuSyncBusinessImpl implements IBuSyncBusiness {
 
-    private RestTemplate restTemplate;
-
-    private ObjectMapper dragonObjectMapper;
-
-    private ApproveBuProperties approveBuProperties;
-
     private IApproveRecordService approveRecordService;
 
     private IApproveCirculationService approveCirculationService;
 
-    @Autowired
-    public void setApproveCirculationService(IApproveCirculationService approveCirculationService) {
-        this.approveCirculationService = approveCirculationService;
-    }
-
-    @Autowired
-    public void setApproveRecordService(IApproveRecordService approveRecordService) {
-        this.approveRecordService = approveRecordService;
-    }
+    private INoticeBusiness noticeBusiness;
 
     @Autowired
-    public void setApproveBuProperties(ApproveBuProperties approveBuProperties) {
-        this.approveBuProperties = approveBuProperties;
+    public void setNoticeBusiness(INoticeBusiness noticeBusiness) {
+        this.noticeBusiness = noticeBusiness;
     }
 
-    @Qualifier(ApproveBeanConstants.DRAGON_OBJECT_MAPPER)
     @Autowired
-    public void setDragonObjectMapper(ObjectMapper dragonObjectMapper) {
-        this.dragonObjectMapper = dragonObjectMapper;
+    public void setApproveCirculationService(IApproveCirculationService approveCirculationService) {
+        this.approveCirculationService = approveCirculationService;
     }
 
-    @Qualifier(ApproveBeanConstants.SKIP_SSL_REST_TEMPLATE)
     @Autowired
-    public void setRestTemplate(RestTemplate restTemplate) {
-        this.restTemplate = restTemplate;
+    public void setApproveRecordService(IApproveRecordService approveRecordService) {
+        this.approveRecordService = approveRecordService;
     }
 
     @Override
@@ -167,7 +138,7 @@ public class BuSyncBusinessImpl implements IBuSyncBusiness {
         buApproveSyncBizDataReqVO.setBizApprove(buApproveSyncBizApproveReqVO);
         buApproveSyncReqVO.setBizData(buApproveSyncBizDataReqVO);
 
-        ResponseStatus responseStatus = requestSyncToBu(buApproveSyncReqVO);
+        ResponseStatus responseStatus = noticeBusiness.retryRequestSyncToBu(buApproveSyncReqVO);
         if (ResponseUtil.isSuccess(responseStatus)) {
             approveRecordService.updateSyncFlag(id, BooleanEnum.TRUE);
             return ResponseStatus.success();
@@ -176,70 +147,14 @@ public class BuSyncBusinessImpl implements IBuSyncBusiness {
         return ResponseStatus.fail();
     }
 
-    /**
-     * 请求同步到 BU
-     *
-     * @param buApproveSyncReqVO 同步请求
-     * @return 状态
-     */
-    public ResponseStatus requestSyncToBu(BuApproveSyncReqVO buApproveSyncReqVO) {
-        if (!approveBuProperties.isEnable()) {
-            return ResponseStatus.success();
-        }
-        String syncTaskUrl = approveBuProperties.getSyncTaskUrl();
+    @Override
+    public ResponseStatus syncToBu(ApprovalInfoMsgItemDTO approvalInfoMsgItemDTO) {
+        ApproveRecordInfo approveRecordInfo = new ApproveRecordInfo();
+        BeanUtil.copyProperties(approvalInfoMsgItemDTO, approveRecordInfo);
+        // todo 转换
 
-        return baseReq(syncTaskUrl, buApproveSyncReqVO, "同步任务信息到BU");
+        return syncToBu(approveRecordInfo);
     }
 
-    /**
-     * 流程详情
-     *
-     * @param url                路径
-     * @param buApproveSyncReqVO 请求参数
-     * @return 返回内容
-     */
-    protected ResponseStatus baseReq(String url, BuApproveSyncReqVO buApproveSyncReqVO, String requestName) {
-        // 打印请求参数
-        log.info(requestName + " 请求路径:{} 请求参数:{}", url, JsonUtils.toJSONString(buApproveSyncReqVO));
-        String errorTip = requestName + "失败";
-        ResponseStatus responseStatus = ValidUtil.validReqVo(buApproveSyncReqVO);
-        if (ResponseUtil.isFail(responseStatus)) {
-            log.error("request vo valid error:{}", responseStatus.getMessage());
-            return ResponseUtil.dtoFail(responseStatus.getMessage());
-        }
-
-        HttpEntity<BuApproveSyncReqVO> entity = new HttpEntity<>(buApproveSyncReqVO);
-        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
 
-        if (!exchange.getStatusCode().is2xxSuccessful()) {
-            log.error("{} error. resp: {}", requestName, JsonUtils.toJSONString(exchange));
-            return ResponseUtil.dtoFail(errorTip);
-        }
-        String body = exchange.getBody();
-        if (StringUtils.isBlank(body)) {
-            log.error("{} error. resp: {}", requestName, JsonUtils.toJSONString(exchange));
-            return ResponseUtil.dtoFail(errorTip);
-        }
-
-        log.info("baseReq resp :{}", JsonUtils.toJSONString(exchange));
-        TypeReference<ZeroTrustResultDataVO<Object>> typeReference = new TypeReference<ZeroTrustResultDataVO<Object>>() {
-        };
-
-        ZeroTrustResultDataVO<Object> respDto;
-        try {
-            JsonNode jsonNode = dragonObjectMapper.readTree(body);
-            String statusCode = jsonNode.get("status_code").asText();
-            String message = jsonNode.get("message").asText();
-            if (BuErrorCodeEnum.SUCCESS.getValue().equals(statusCode)) {
-                respDto = dragonObjectMapper.readValue(body, typeReference);
-            } else {
-                return ResponseUtil.dtoFail(message);
-            }
-        } catch (JsonProcessingException e) {
-            log.error("parse error.", e);
-            return ResponseUtil.dtoFail("返回值解析失败");
-        }
-
-        return ResponseUtil.newInstance(respDto.getData());
-    }
 }

+ 102 - 2
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/business/impl/NoticeBusinessImpl.java

@@ -1,18 +1,29 @@
 package com.dragonsoft.dcuc.approve.business.impl;
 
 import cn.hutool.core.util.StrUtil;
+import com.dragoninfo.dcuc.common.utils.ResponseUtil;
+import com.dragoninfo.dcuc.common.utils.ValidUtil;
 import com.dragonsoft.dcuc.approve.business.INoticeBusiness;
 import com.dragonsoft.dcuc.approve.constants.ApproveBeanConstants;
+import com.dragonsoft.dcuc.approve.enumresources.bu.BuErrorCodeEnum;
 import com.dragonsoft.dcuc.approve.model.req.v3.ZeroTrustApproveCallbackReqV3VO;
+import com.dragonsoft.dcuc.approve.model.resp.bu.BuApproveSyncReqVO;
 import com.dragonsoft.dcuc.approve.model.resp.v3.ZeroTrustMessageRespVO;
+import com.dragonsoft.dcuc.approve.model.resp.v3.ZeroTrustResultDataVO;
+import com.dragonsoft.dcuc.approve.properties.ApproveBuProperties;
 import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
 import com.dragonsoft.duceap.commons.util.json.JsonUtils;
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.github.rholder.retry.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.RequestEntity;
 import org.springframework.http.ResponseEntity;
@@ -33,12 +44,27 @@ public class NoticeBusinessImpl implements INoticeBusiness {
 
     private RestTemplate restTemplate;
 
+    private ObjectMapper dragonObjectMapper;
+
+    private ApproveBuProperties approveBuProperties;
+
+
     @Qualifier(ApproveBeanConstants.SKIP_SSL_REST_TEMPLATE)
     @Autowired
     public void setRestTemplate(RestTemplate restTemplate) {
         this.restTemplate = restTemplate;
     }
 
+    @Qualifier(ApproveBeanConstants.DRAGON_OBJECT_MAPPER)
+    @Autowired
+    public void setDragonObjectMapper(ObjectMapper dragonObjectMapper) {
+        this.dragonObjectMapper = dragonObjectMapper;
+    }
+
+    @Autowired
+    public void setApproveBuProperties(ApproveBuProperties approveBuProperties) {
+        this.approveBuProperties = approveBuProperties;
+    }
 
     private static final Logger logger = LoggerFactory.getLogger(NoticeBusinessImpl.class);
 
@@ -91,8 +117,7 @@ public class NoticeBusinessImpl implements INoticeBusiness {
 
             if (responseEntity.getStatusCode().is2xxSuccessful()) {
                 String responseEntityBody = responseEntity.getBody();
-                ObjectMapper objectMapper = new ObjectMapper();
-                ZeroTrustMessageRespVO zeroTrustMessageRespVO = objectMapper.readValue(responseEntityBody, ZeroTrustMessageRespVO.class);
+                ZeroTrustMessageRespVO zeroTrustMessageRespVO = dragonObjectMapper.readValue(responseEntityBody, ZeroTrustMessageRespVO.class);
                 if (zeroTrustMessageRespVO == null) {
                     logger.info("打印状态变更通知返回值:" + responseEntity);
                     throw new RemoteAccessException(responseEntity.toString());
@@ -111,4 +136,79 @@ public class NoticeBusinessImpl implements INoticeBusiness {
         return responseStatus;
     }
 
+    /**
+     * 请求同步到 BU
+     *
+     * @param buApproveSyncReqVO 同步请求
+     * @return 状态
+     */
+    @Override
+    public ResponseStatus retryRequestSyncToBu(BuApproveSyncReqVO buApproveSyncReqVO) {
+        if (!approveBuProperties.isEnable()) {
+            return ResponseStatus.success();
+        }
+        String syncTaskUrl = approveBuProperties.getSyncTaskUrl();
+
+        ResponseStatus responseStatus;
+        try {
+            responseStatus = RESPONSE_STATUS_RETRYER.call(() -> syncToBuBaseReq(syncTaskUrl, buApproveSyncReqVO, "同步任务信息到BU"));
+        } catch (ExecutionException | RetryException e) {
+            logger.error("retry error", e);
+            responseStatus = ResponseStatus.fail(e.getMessage());
+        }
+
+        return responseStatus;
+    }
+
+    /**
+     * 流程详情
+     *
+     * @param url                路径
+     * @param buApproveSyncReqVO 请求参数
+     * @return 返回内容
+     */
+    protected ResponseStatus syncToBuBaseReq(String url, BuApproveSyncReqVO buApproveSyncReqVO, String requestName) {
+        // 打印请求参数
+        logger.info(requestName + " 请求路径:{} 请求参数:{}", url, JsonUtils.toJSONString(buApproveSyncReqVO));
+        String errorTip = requestName + "失败";
+        ResponseStatus responseStatus = ValidUtil.validReqVo(buApproveSyncReqVO);
+        if (ResponseUtil.isFail(responseStatus)) {
+            logger.error("request vo valid error:{}", responseStatus.getMessage());
+            return ResponseUtil.dtoFail(responseStatus.getMessage());
+        }
+
+        HttpEntity<BuApproveSyncReqVO> entity = new HttpEntity<>(buApproveSyncReqVO);
+        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
+
+        if (!exchange.getStatusCode().is2xxSuccessful()) {
+            logger.error("{} error. resp: {}", requestName, JsonUtils.toJSONString(exchange));
+            return ResponseUtil.dtoFail(errorTip);
+        }
+        String body = exchange.getBody();
+        if (StringUtils.isBlank(body)) {
+            logger.error("{} error. resp: {}", requestName, JsonUtils.toJSONString(exchange));
+            return ResponseUtil.dtoFail(errorTip);
+        }
+
+        logger.info("baseReq resp :{}", JsonUtils.toJSONString(exchange));
+        TypeReference<ZeroTrustResultDataVO<Object>> typeReference = new TypeReference<ZeroTrustResultDataVO<Object>>() {
+        };
+
+        ZeroTrustResultDataVO<Object> respDto;
+        try {
+            JsonNode jsonNode = dragonObjectMapper.readTree(body);
+            String statusCode = jsonNode.get("status_code").asText();
+            String message = jsonNode.get("message").asText();
+            if (BuErrorCodeEnum.SUCCESS.getValue().equals(statusCode)) {
+                respDto = dragonObjectMapper.readValue(body, typeReference);
+            } else {
+                return ResponseUtil.dtoFail(message);
+            }
+        } catch (JsonProcessingException e) {
+            logger.error("parse error.", e);
+            return ResponseUtil.dtoFail("返回值解析失败");
+        }
+
+        return ResponseUtil.newInstance(respDto.getData());
+    }
 }

+ 48 - 0
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/event/listener/SyncToBuSendFinalResultListener.java

@@ -0,0 +1,48 @@
+package com.dragonsoft.dcuc.approve.event.listener;
+
+import com.dragoninfo.dcuc.common.utils.ResponseUtil;
+import com.dragonsoft.dcuc.approve.business.IBuSyncBusiness;
+import com.dragonsoft.dcuc.approve.enumresources.ApproveWorkFlowOperationEnum;
+import com.dragonsoft.dcuc.approve.event.event.ApproveFinalResultEvent;
+import com.dragonsoft.dcuc.approve.model.vo.ApprovalInfoMsgItemDTO;
+import com.dragonsoft.dcuc.approve.model.vo.ApproveMessageInfoDTO;
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationListener;
+import org.springframework.stereotype.Component;
+
+/**
+ * <p>
+ * 审批结束后同步到BU
+ * </p>
+ *
+ * @author huangzqa
+ * @date 2023/11/13
+ */
+@Slf4j
+@Component
+public class SyncToBuSendFinalResultListener implements ApplicationListener<ApproveFinalResultEvent> {
+
+    private IBuSyncBusiness buSyncBusiness;
+
+    @Autowired
+    public void setBuSyncBusiness(IBuSyncBusiness buSyncBusiness) {
+        this.buSyncBusiness = buSyncBusiness;
+    }
+
+    @Override
+    public void onApplicationEvent(ApproveFinalResultEvent event) {
+        ApproveMessageInfoDTO approveMessageInfoDTO = event.getApproveMessageInfoDTO();
+        ApproveWorkFlowOperationEnum approveResult = approveMessageInfoDTO.getApproveResult();
+
+        ApprovalInfoMsgItemDTO approvalInfoMsgItemDTO = approveMessageInfoDTO.getApproveInfo();
+        if (approveResult.equals(ApproveWorkFlowOperationEnum.COMPLETE_AGREE)) {
+            ResponseStatus responseStatus = buSyncBusiness.syncToBu(approvalInfoMsgItemDTO);
+            if (ResponseUtil.isFail(responseStatus)) {
+                log.error("sync to bu error: {}", responseStatus.getMessage());
+            }
+        }
+
+    }
+}

+ 3 - 0
approve-core-service/src/main/java/com/dragonsoft/dcuc/approve/model/vo/ApprovalInfoMsgItemDTO.java

@@ -16,6 +16,9 @@ import java.util.Date;
 @ApiModel(description = "申请表单返回信息")
 public class ApprovalInfoMsgItemDTO {
 
+    @ApiModelProperty(value = "主键")
+    private String id;
+
     @ApiModelProperty(value = "流程标识码")
     private String businessCode;