From 37287cb5032782fbae257e42b5ce92773f6dfa7b Mon Sep 17 00:00:00 2001 From: anthonywj Date: Thu, 30 Mar 2023 17:22:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E6=8D=AE=E5=90=8C=E6=AD=A5=E7=9B=B4?= =?UTF-8?q?=E8=BF=9E=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/sync/SyncController.java | 104 +++++++++++++++++- .../admin/idc/controller/IdcController.java | 66 +---------- .../glxp/sale/admin/idc/utils/UriUtils.java | 77 +++++++++++++ 3 files changed, 186 insertions(+), 61 deletions(-) create mode 100644 api-admin/src/main/java/com/glxp/sale/admin/idc/utils/UriUtils.java diff --git a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java index 15af49d..8ae08df 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java @@ -1,17 +1,58 @@ package com.glxp.sale.admin.controller.sync; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import com.glxp.sale.admin.constant.BasicProcessStatus; +import com.glxp.sale.admin.constant.ConstantStatus; +import com.glxp.sale.admin.entity.sync.BasicExportStatusEntity; +import com.glxp.sale.admin.entity.sync.BasicUploadStatusEntity; +import com.glxp.sale.admin.idc.utils.UriUtils; +import com.glxp.sale.admin.service.param.SystemParamConfigService; +import com.glxp.sale.admin.service.sync.BasicExportService; +import com.glxp.sale.admin.service.sync.BasicUploadService; +import com.glxp.sale.admin.util.Constant; +import com.glxp.sale.admin.util.DateUtil; +import com.glxp.sale.admin.util.FileUtils; +import com.glxp.sale.admin.util.GennerOrderUtils; import com.glxp.sale.common.res.BaseResponse; import com.glxp.sale.common.util.ResultVOUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Date; /** - * 中继服务接口 + * 中继服务接口 直连 */ +@Slf4j @RestController public class SyncController { + @Resource + UriUtils uriUtils; + @Resource + SystemParamConfigService systemParamConfigService; + @Resource + private GennerOrderUtils gennerOrderUtils; + @Resource + private BasicUploadService basicUploadService; + @Value("${file_path}") + private String filePath; + @Resource + BasicExportService basicExportService; + /** * 测试连通性 * @@ -22,5 +63,66 @@ public class SyncController { return ResultVOUtils.success("连接成功"); } + //单据接口直连 + @RequestMapping("/orderApi/**") + public Object orderApi(HttpServletRequest request, HttpServletResponse httpServletResponse) { + String uri = uriUtils.parseUri(request.getRequestURL().toString()); + log.info(uri); + JSONObject jsonObject = redirect(request, uri); + return jsonObject; + } + + + private JSONObject redirect(HttpServletRequest request, String uri) { + RestTemplate restTemplate = new RestTemplate(); + HttpEntity httpEntity = uriUtils.buildHeader(request); + String taskId = request.getHeader("taskId"); + ResponseEntity responseBody = null; + if (request.getMethod().equals("POST")) { + //写入日志 + writeUploadLog(taskId, httpEntity.getBody()); + responseBody = restTemplate.postForEntity(uri, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("GET")) { + writeDownloadLog(taskId, httpEntity.getBody()); + responseBody = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, JSONObject.class); + + } + JSONObject result = responseBody.getBody(); + return result; + } + + @Async + public void writeUploadLog(String taskId, String content) { + String key = gennerOrderUtils.getOrders(); + BasicUploadStatusEntity basicUploadStatusEntity = new BasicUploadStatusEntity(); + basicUploadStatusEntity.setId(key); + basicUploadStatusEntity.setTaskId(taskId); + basicUploadStatusEntity.setStartTime(DateUtil.getDateTime()); + basicUploadStatusEntity.setIdDatas(ConstantStatus.SYNC_SCAN_ORDER); + basicUploadStatusEntity.setType(BasicProcessStatus.NEW_ALL_ORDER); + basicUploadStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); + basicUploadService.insertDownloadStatus(basicUploadStatusEntity); + FileUtils.makeDirectory(filePath + "upload/"); + String fileName = filePath + "upload/" + BasicProcessStatus.NEW_ALL_ORDER + "-" + taskId + ".udi"; + FileUtils.SaveFileAs(content, fileName); + } + + @Async + public void writeDownloadLog(String taskId, String content) { + String key = gennerOrderUtils.getOrders(); + BasicExportStatusEntity myEntity = new BasicExportStatusEntity(); + myEntity.setId(key); + myEntity.setUpdateTime(new Date()); + myEntity.setType(BasicProcessStatus.NEW_ALL_DATA); + myEntity.setIdDatas(ConstantStatus.SYNC_SCAN_ORDER); + myEntity.setStartTime(DateUtil.getDateTime()); + myEntity.setTaskId(taskId); + myEntity.setStatus(Constant.SYNC_STATUS_WAIT); + basicExportService.insertExportStatus(myEntity); + FileUtils.makeDirectory(filePath + "download/"); + String fileName = filePath + "/download/" + BasicProcessStatus.NEW_ALL_ORDER + "-" + taskId + ".udi"; + FileUtils.SaveFileAs(content, fileName); + } + } diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java index 2b0c7c9..199a299 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java @@ -19,6 +19,7 @@ import javax.servlet.http.HttpServletResponse; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import com.glxp.sale.admin.idc.service.DownloadRestTemplate; +import com.glxp.sale.admin.idc.utils.UriUtils; import com.glxp.sale.admin.service.param.SystemParamConfigService; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.util.http.fileupload.IOUtils; @@ -54,6 +55,8 @@ public class IdcController { IdcService idcService; @Resource DbDao dbDao; + @Resource + UriUtils uriUtils; /*通用上传接口*/ //@Log("中继服务数据接收,下级往上级上传") @@ -62,7 +65,7 @@ public class IdcController { @RequestParam("content") String content, @RequestParam(value = "files", required = false) MultipartFile[] files) { // - return idcService.receive( request, content, files); + return idcService.receive(request, content, files); } @@ -95,7 +98,7 @@ public class IdcController { @RequestMapping("/mapi/**") public Object mapi(HttpServletRequest request, HttpServletResponse httpServletResponse) { - String uri = parseUri(request.getRequestURL().toString()); + String uri = uriUtils.parseUri(request.getRequestURL().toString()); log.info(uri); String isTopService = systemParamConfigService.selectValueByParamKey("is_top_service"); @@ -115,48 +118,6 @@ public class IdcController { } - HttpEntity buildHeader(HttpServletRequest request) { - - log.info(request.getMethod()); - - String bodyContent = ""; - Map headerParam = new HashMap<>(); - - if (request.getMethod().equals(HttpMethod.GET)) { - Map bodyParam = new HashMap(); - Enumeration pNames = request.getParameterNames(); - while (pNames.hasMoreElements()) { - String name = (String) pNames.nextElement(); - String value = request.getParameter(name); - bodyParam.put(name, value); - } - - bodyContent = JSON.toJSONString(bodyParam); - } else { - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(request.getInputStream())); - bodyContent = IoUtil.read(reader); - log.info(bodyContent); - } catch (IOException e) { - e.printStackTrace(); - } - } - - - HttpHeaders headers = new HttpHeaders(); - Enumeration enumeration = request.getHeaderNames(); - while (enumeration.hasMoreElements()) { - String name = enumeration.nextElement(); - String value = request.getHeader(name); - headerParam.put(name, value); - headers.add(name, value); - } - //headers.add("Content-Type", "application/json;charset=UTF-8"); - HttpEntity http = new HttpEntity<>(bodyContent, headers); - return http; - } - private byte[] redirectIamge(HttpServletRequest request, HttpServletResponse httpServletResponse, String uri) { RestTemplate restTemplate = new RestTemplate(); // HttpEntity httpEntity = buildHeader(request); @@ -207,11 +168,10 @@ public class IdcController { @Resource SystemParamConfigService systemParamConfigService; - private JSONObject redirect(HttpServletRequest request, String uri) { RestTemplate restTemplate = new RestTemplate(); - HttpEntity httpEntity = buildHeader(request); + HttpEntity httpEntity = uriUtils.buildHeader(request); ResponseEntity responseBody = null; if (request.getMethod().equals("POST")) @@ -228,18 +188,4 @@ public class IdcController { } - private String parseUri(String url) { - String uri = url; - int a1 = uri.indexOf("://"); - int a2 = 0; - if (a1 > 0) { - a2 = uri.indexOf("/", a1 + 4); - if (a2 > 0) { - uri = uri.substring(a2 + 1); - } - } - return uri; - } - - } diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/utils/UriUtils.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/utils/UriUtils.java new file mode 100644 index 0000000..a03361e --- /dev/null +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/utils/UriUtils.java @@ -0,0 +1,77 @@ +package com.glxp.sale.admin.idc.utils; + +import cn.hutool.core.io.IoUtil; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@Service +public class UriUtils { + + public String parseUri(String url) { + String uri = url; + int a1 = uri.indexOf("://"); + int a2 = 0; + if (a1 > 0) { + a2 = uri.indexOf("/", a1 + 4); + if (a2 > 0) { + uri = uri.substring(a2 + 1); + } + } + return uri; + } + + + public HttpEntity buildHeader(HttpServletRequest request) { + + log.info(request.getMethod()); + String bodyContent = ""; + Map headerParam = new HashMap<>(); + if (request.getMethod().equals(HttpMethod.GET)) { + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + } + + bodyContent = JSON.toJSONString(bodyParam); + } else { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(request.getInputStream())); + bodyContent = IoUtil.read(reader); + log.info(bodyContent); + } catch (IOException e) { + e.printStackTrace(); + } + } + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String name = enumeration.nextElement(); + String value = request.getHeader(name); + headerParam.put(name, value); + headers.add(name, value); + } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(bodyContent, headers); + return http; + } + + +}