单据同步直连修改

version1
anthonywj 2 years ago
parent ca7dbf3834
commit 37287cb503

@ -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<String> httpEntity = uriUtils.buildHeader(request);
String taskId = request.getHeader("taskId");
ResponseEntity<JSONObject> 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);
}
}

@ -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<String> buildHeader(HttpServletRequest request) {
log.info(request.getMethod());
String bodyContent = "";
Map<String, Object> headerParam = new HashMap<>();
if (request.getMethod().equals(HttpMethod.GET)) {
Map<String, Object> bodyParam = new HashMap<String, Object>();
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<String> 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<String> http = new HttpEntity<>(bodyContent, headers);
return http;
}
private byte[] redirectIamge(HttpServletRequest request, HttpServletResponse httpServletResponse, String uri) {
RestTemplate restTemplate = new RestTemplate();
// HttpEntity<String> 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<String> httpEntity = buildHeader(request);
HttpEntity<String> httpEntity = uriUtils.buildHeader(request);
ResponseEntity<JSONObject> 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;
}
}

@ -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<String> buildHeader(HttpServletRequest request) {
log.info(request.getMethod());
String bodyContent = "";
Map<String, Object> headerParam = new HashMap<>();
if (request.getMethod().equals(HttpMethod.GET)) {
Map<String, Object> bodyParam = new HashMap<String, Object>();
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<String> 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<String> http = new HttpEntity<>(bodyContent, headers);
return http;
}
}
Loading…
Cancel
Save