From f6fae6f4629db14a9f2da5e8d13f9d231119b5fd Mon Sep 17 00:00:00 2001 From: wj <1285151836@qq.com> Date: Fri, 21 Apr 2023 15:25:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B1=E5=86=85=E5=90=91=E5=A4=96=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sale/admin/config/TaskPoolConfig.java | 38 ++++++++++ .../admin/constant/BasicExportTypeEnum.java | 5 ++ .../sync/SpsSyncDataController.java | 74 ++++++++++++++++++- .../admin/req/sync/SyncUpLoadRequest.java | 19 +++++ 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java create mode 100644 api-admin/src/main/java/com/glxp/sale/admin/req/sync/SyncUpLoadRequest.java diff --git a/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java b/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java new file mode 100644 index 0000000..bd9ddb3 --- /dev/null +++ b/api-admin/src/main/java/com/glxp/sale/admin/config/TaskPoolConfig.java @@ -0,0 +1,38 @@ +package com.glxp.sale.admin.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 配置线程池 + */ +@Configuration +@EnableAsync +public class TaskPoolConfig { + + @Bean(name = "taskExecutor") + public ThreadPoolTaskExecutor taskExecutor() { + // 获取当前主机的cpu核心数 + int threadCount = Runtime.getRuntime().availableProcessors(); + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + //核心池的大小 + taskExecutor.setCorePoolSize(threadCount); + //线程池最大线程数 + taskExecutor.setMaxPoolSize(threadCount * 2); + //队列最大长度 + taskExecutor.setQueueCapacity(200); + //线程空闲时间 + taskExecutor.setKeepAliveSeconds(60); + //配置线程前缀 + taskExecutor.setThreadNamePrefix("custom_executor"); + //配置拒绝策略 + taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + //执行初始化 + taskExecutor.initialize(); + return taskExecutor; + } +} diff --git a/api-admin/src/main/java/com/glxp/sale/admin/constant/BasicExportTypeEnum.java b/api-admin/src/main/java/com/glxp/sale/admin/constant/BasicExportTypeEnum.java index ffb81d2..b6cef9d 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/constant/BasicExportTypeEnum.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/constant/BasicExportTypeEnum.java @@ -17,6 +17,11 @@ public enum BasicExportTypeEnum { */ COUNTRY_DI_DATA("country_di_data", "国家库DI数据"), + /** + * 单据类型 + */ + DOCUMENT_TYPE_DATA("document_type_data", "单据类型"), + /** * 其他数据 */ diff --git a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SpsSyncDataController.java b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SpsSyncDataController.java index b4428f6..e251064 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SpsSyncDataController.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SpsSyncDataController.java @@ -1,15 +1,17 @@ package com.glxp.sale.admin.controller.sync; -import cn.hutool.core.lang.Dict; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.glxp.sale.admin.constant.*; import com.glxp.sale.admin.entity.param.SystemParamConfigEntity; import com.glxp.sale.admin.entity.sync.*; import com.glxp.sale.admin.http.SpGetHttp; import com.glxp.sale.admin.http.SpPostHttp; +import com.glxp.sale.admin.idc.utils.UriUtils; import com.glxp.sale.admin.req.sync.SpsSyncDataRequest; +import com.glxp.sale.admin.req.sync.SyncUpLoadRequest; import com.glxp.sale.admin.res.sync.*; import com.glxp.sale.admin.service.param.SystemParamConfigService; import com.glxp.sale.admin.service.sync.BasicExportService; @@ -22,22 +24,28 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Date; -import java.util.List; @RestController @RequiredArgsConstructor public class SpsSyncDataController { + private final UriUtils uriUtils; @Resource RedisUtil redisUtil; @Resource @@ -291,5 +299,67 @@ public class SpsSyncDataController { return ResultVOUtils.error(500, "更新失败!"); } + @PostMapping("sps/sync/upload/data") + public Object uploadData(HttpServletRequest request, @RequestBody SyncUpLoadRequest upLoadRequest) { + String uri = uriUtils.parseUri(request.getRequestURL().toString()); + String upperServerHost = systemParamConfigService.selectValueByParamKey("upper_server_ip"); + if (StrUtil.isNotEmpty(upperServerHost)) { + uri = upperServerHost + "/" + uri; + } + JSONObject jsonObject = redirect(request, uri, upLoadRequest.getExportType()); + return jsonObject; + } + + private JSONObject redirect(HttpServletRequest request, String uri, BasicExportTypeEnum exportType) { + 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(), exportType); + responseBody = restTemplate.postForEntity(uri, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("GET")) { + writeUploadLog(taskId, httpEntity.getBody(), exportType); + responseBody = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, JSONObject.class); + } + JSONObject result = responseBody.getBody(); + return result; + } + + + @Async + public void writeUploadLog(String taskId, String content, BasicExportTypeEnum exportType) { + String key = gennerOrderUtils.getOrders(); + BasicUploadStatusEntity basicUploadStatusEntity = new BasicUploadStatusEntity(); + basicUploadStatusEntity.setId(key); + basicUploadStatusEntity.setTaskId(taskId); + basicUploadStatusEntity.setStartTime(DateUtil.getDateTime()); + basicUploadStatusEntity.setIdDatas(exportType.getKey()); + basicUploadStatusEntity.setType(exportType.getRemark()); + basicUploadStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); + basicUploadService.insertDownloadStatus(basicUploadStatusEntity); + FileUtils.makeDirectory(filePath + "upload/"); + String fileName = filePath + "upload/" + exportType.getRemark() + "-" + taskId + ".udi"; + FileUtils.SaveFileAs(content, fileName); + } + + @Async + public void writeDownloadLog(String taskId, String content, BasicExportTypeEnum exportType) { + String key = gennerOrderUtils.getOrders(); + BasicExportStatusEntity myEntity = new BasicExportStatusEntity(); + myEntity.setId(key); + myEntity.setUpdateTime(new Date()); + myEntity.setType(exportType.getRemark()); + myEntity.setIdDatas(exportType.getKey()); + 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/req/sync/SyncUpLoadRequest.java b/api-admin/src/main/java/com/glxp/sale/admin/req/sync/SyncUpLoadRequest.java new file mode 100644 index 0000000..a3da66b --- /dev/null +++ b/api-admin/src/main/java/com/glxp/sale/admin/req/sync/SyncUpLoadRequest.java @@ -0,0 +1,19 @@ +package com.glxp.sale.admin.req.sync; + +import com.glxp.sale.admin.constant.BasicExportTypeEnum; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class SyncUpLoadRequest { + + private BasicExportTypeEnum exportType; + + private Object data; + +}