From 31d1c52a30b38819a07fcce60372e2f4726a5795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=98=8E=E6=A2=81?= <2429105222@qq.com> Date: Thu, 15 Jun 2023 18:59:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E6=8D=AE=E4=B8=8A=E4=BC=A0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 + .../controller/inout/PlatformController.java | 19 ++ .../dao/inout/UnitMaintainPlatformDao.java | 2 + .../api/req/inout/FilterOrderRequest.java | 2 + .../api/res/inout/IoUploadOrderResponse.java | 19 ++ .../api/res/inout/PlatformLinkResponse.java | 2 + .../api/service/inout/PlatformService.java | 8 + .../inout/impl/PlatformServiceImpl.java | 179 ++++++++++++++++-- .../com/glxp/api/util/MultipartFileTest.java | 79 ++++++++ .../java/com/glxp/api/util/OkHttpCli.java | 1 + .../mybatis/mapper/inout/IoOrderDao.xml | 5 +- .../mapper/inout/UnitMaintainPlatformDao.xml | 20 ++ 12 files changed, 324 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/glxp/api/res/inout/IoUploadOrderResponse.java create mode 100644 src/main/java/com/glxp/api/util/MultipartFileTest.java diff --git a/pom.xml b/pom.xml index caecc425..2ee2eb4f 100644 --- a/pom.xml +++ b/pom.xml @@ -314,6 +314,12 @@ 1.0.47_1 + + commons-fileupload + commons-fileupload + 1.3.1 + + diff --git a/src/main/java/com/glxp/api/controller/inout/PlatformController.java b/src/main/java/com/glxp/api/controller/inout/PlatformController.java index 56ca604c..3326e6ea 100644 --- a/src/main/java/com/glxp/api/controller/inout/PlatformController.java +++ b/src/main/java/com/glxp/api/controller/inout/PlatformController.java @@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageInfo; import com.glxp.api.annotation.AuthRuleAnnotation; @@ -229,4 +230,22 @@ public class PlatformController { return ResultVOUtils.success(pageSimpleResponse); } + /** + * 上传单据到自助 + * + * @return + */ + @AuthRuleAnnotation("") + @GetMapping("/udiwms/platform/uploadOrder") + public BaseResponse uploadOrder(String orderId) throws JsonProcessingException { + if (StrUtil.isBlank(orderId)) { + return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL); + } + return platformService.uploadOrder(orderId); + } + + + + + } diff --git a/src/main/java/com/glxp/api/dao/inout/UnitMaintainPlatformDao.java b/src/main/java/com/glxp/api/dao/inout/UnitMaintainPlatformDao.java index 35688b55..f70ad117 100644 --- a/src/main/java/com/glxp/api/dao/inout/UnitMaintainPlatformDao.java +++ b/src/main/java/com/glxp/api/dao/inout/UnitMaintainPlatformDao.java @@ -43,4 +43,6 @@ public interface UnitMaintainPlatformDao { Long selectCount(@Param("unitId") String unitId, @Param("action") String action); List selectDelectList(@Param("platformId") String platformId); + + PlatformLinkResponse selectByUnitld(@Param("unitld") String unitld); } \ No newline at end of file diff --git a/src/main/java/com/glxp/api/req/inout/FilterOrderRequest.java b/src/main/java/com/glxp/api/req/inout/FilterOrderRequest.java index b84784f3..b1b3c9e9 100644 --- a/src/main/java/com/glxp/api/req/inout/FilterOrderRequest.java +++ b/src/main/java/com/glxp/api/req/inout/FilterOrderRequest.java @@ -190,4 +190,6 @@ public class FilterOrderRequest extends ListPageRequest { private List invoiceActions2; private String orderBy; + private String uploadKey; + } diff --git a/src/main/java/com/glxp/api/res/inout/IoUploadOrderResponse.java b/src/main/java/com/glxp/api/res/inout/IoUploadOrderResponse.java new file mode 100644 index 00000000..a9849c64 --- /dev/null +++ b/src/main/java/com/glxp/api/res/inout/IoUploadOrderResponse.java @@ -0,0 +1,19 @@ +package com.glxp.api.res.inout; + +import com.glxp.api.entity.inout.*; +import lombok.Data; + +import java.util.List; + +@Data +public class IoUploadOrderResponse { + + private List ioOrderDetailCodeEntityList; + private List ioCodeTempEntityList; + private IoOrderEntity ioOrderEntity; + private PlatformLinkResponse platformLinkRespons; + private List ioOrderDetailBizEntityList; + private List ioOrderDetailResultEntityList; + private List ioOrderInvoiceEntityList; + +} diff --git a/src/main/java/com/glxp/api/res/inout/PlatformLinkResponse.java b/src/main/java/com/glxp/api/res/inout/PlatformLinkResponse.java index e6a5ba09..29c43fdd 100644 --- a/src/main/java/com/glxp/api/res/inout/PlatformLinkResponse.java +++ b/src/main/java/com/glxp/api/res/inout/PlatformLinkResponse.java @@ -39,4 +39,6 @@ public class PlatformLinkResponse { private String name; private String host; + private String appid; + private String secretKey; } diff --git a/src/main/java/com/glxp/api/service/inout/PlatformService.java b/src/main/java/com/glxp/api/service/inout/PlatformService.java index 845f181a..7b598b76 100644 --- a/src/main/java/com/glxp/api/service/inout/PlatformService.java +++ b/src/main/java/com/glxp/api/service/inout/PlatformService.java @@ -2,6 +2,7 @@ package com.glxp.api.service.inout; +import com.fasterxml.jackson.core.JsonProcessingException; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.entity.inout.UnitMaintainPlatformEntity; import com.glxp.api.entity.system.PlatformEntity; @@ -105,5 +106,12 @@ public interface PlatformService { */ List selectDelectList(PlatformLinkRequest platformLinkRequest); + /** + * 上传单据到自助 + * + * @return + */ + BaseResponse uploadOrder(String orderId) throws JsonProcessingException; + } diff --git a/src/main/java/com/glxp/api/service/inout/impl/PlatformServiceImpl.java b/src/main/java/com/glxp/api/service/inout/impl/PlatformServiceImpl.java index 5f27bfe0..4deeda1a 100644 --- a/src/main/java/com/glxp/api/service/inout/impl/PlatformServiceImpl.java +++ b/src/main/java/com/glxp/api/service/inout/impl/PlatformServiceImpl.java @@ -5,31 +5,38 @@ import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; -import com.alibaba.fastjson.JSON; + +import com.alibaba.fastjson2.JSON; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageHelper; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; -import com.glxp.api.dao.inout.PlatformDao; -import com.glxp.api.dao.inout.UnitMaintainPlatformDao; -import com.glxp.api.entity.inout.UnitMaintainPlatformEntity; +import com.glxp.api.dao.inout.*; +import com.glxp.api.entity.inout.*; import com.glxp.api.entity.system.PlatformEntity; +import com.glxp.api.idc.service.FileService; import com.glxp.api.req.inout.PlatformLinkRequest; +import com.glxp.api.res.inout.IoUploadOrderResponse; import com.glxp.api.res.inout.PlatformLinkResponse; -import com.glxp.api.service.inout.PlatformService; -import com.glxp.api.util.HttpClient; +import com.glxp.api.service.inout.*; import com.glxp.api.util.OkHttpCli; import lombok.extern.slf4j.Slf4j; +import okhttp3.*; import org.apache.commons.lang3.StringUtils; +import org.apache.tools.ant.util.DateUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.TimeUnit; @Slf4j @Service @@ -42,6 +49,24 @@ public class PlatformServiceImpl implements PlatformService { private UnitMaintainPlatformDao unitMaintainPlatformDao; @Resource private OkHttpCli okHttpCli; + @Resource + IoOrderService ioOrderService; + @Resource + IoOrderDetailCodeService ioOrderDetailCodeService; + @Resource + IoCodeTempService ioCodeTempService; + @Resource + IoOrderDetailBizDao ioOrderDetailBizDao; + @Resource + IoOrderDetailResultDao ioOrderDetailResultDao; + @Resource + IoOrderInvoiceMapper ioOrderInvoiceMapper; + @Value("${file_path}") + private String filePath; + @Value("${API_KEY}") + private String apiKey; + @Value("${API_SECRET}") + private String apiSecret; public int save(PlatformEntity platformEntity) { if (StringUtils.isEmpty(platformEntity.getId())) { @@ -125,7 +150,7 @@ public class PlatformServiceImpl implements PlatformService { * @param platformId * @return */ - public BaseResponse getTargetActions(String platformId, String invSubCode,String apiKey,String apiSecret) { + public BaseResponse getTargetActions(String platformId, String invSubCode, String apiKey, String apiSecret) { PlatformEntity platformEntity = platformDao.get(platformId); if (null == platformEntity) { return ResultVOUtils.success(); @@ -133,7 +158,7 @@ public class PlatformServiceImpl implements PlatformService { String host = platformEntity.getHost(); Map paramMap = new HashMap<>(); paramMap.put("invSubCode", invSubCode); - String resp = okHttpCli.doGet(host + "/udiwms/bussinessType/udimsFilter",paramMap,buildHeader(apiKey,apiSecret)); + String resp = okHttpCli.doGet(host + "/udiwms/bussinessType/udimsFilter", paramMap, buildHeader(apiKey, apiSecret)); if (StrUtil.isNotBlank(resp) && resp.contains("20000")) { return JSON.parseObject(resp, BaseResponse.class); } else { @@ -143,7 +168,7 @@ public class PlatformServiceImpl implements PlatformService { } //获取自助平台一级仓库 - public BaseResponse getTargetInv(String platformId,String apiKey,String apiSecret) { + public BaseResponse getTargetInv(String platformId, String apiKey, String apiSecret) { PlatformEntity platformEntity = platformDao.get(platformId); if (null == platformEntity) { return ResultVOUtils.success(); @@ -151,7 +176,7 @@ public class PlatformServiceImpl implements PlatformService { String host = platformEntity.getHost(); String url = host + "/spms/inv/warehouse/filterInv/forUdims"; log.info("拉取自助平台仓库数据:" + url); - String resp = okHttpCli.doGet(url,buildHeader(apiKey,apiSecret)); + String resp = okHttpCli.doGet(url, buildHeader(apiKey, apiSecret)); log.info("拉取结果:" + resp); if (StrUtil.isNotBlank(resp) && resp.contains("20000")) { try { @@ -168,7 +193,7 @@ public class PlatformServiceImpl implements PlatformService { //获取自助平台一级仓库所属分库 - public BaseResponse getTargetSubInv(String platformId, String invCode,String apiKey,String apiSecret) { + public BaseResponse getTargetSubInv(String platformId, String invCode, String apiKey, String apiSecret) { PlatformEntity platformEntity = platformDao.get(platformId); if (null == platformEntity) { return ResultVOUtils.success(); @@ -176,7 +201,7 @@ public class PlatformServiceImpl implements PlatformService { String host = platformEntity.getHost(); Map paramMap = new HashMap<>(); paramMap.put("invCode", invCode); - String resp = okHttpCli.doGet(host + "/spms/sub/inv/warehouse/getSubInvForUdims",paramMap,buildHeader(apiKey,apiSecret)); + String resp = okHttpCli.doGet(host + "/spms/sub/inv/warehouse/getSubInvForUdims", paramMap, buildHeader(apiKey, apiSecret)); if (StrUtil.isNotBlank(resp) && resp.contains("20000")) { return JSON.parseObject(resp, BaseResponse.class); } else { @@ -284,8 +309,128 @@ public class PlatformServiceImpl implements PlatformService { return unitMaintainPlatformDao.selectDelectList(platformLinkRequest.getPlatformId()); } - public String[] buildHeader(String apiKey,String apiSecret) { + @Override + public BaseResponse uploadOrder(String orderId) throws JsonProcessingException { + + //查询单据信息 + IoOrderEntity ioOrderEntity = ioOrderService.findByBillNo(orderId); + if (ioOrderEntity == null) { + return ResultVOUtils.error(999, "该单据不存在!"); + } + //查询客户关联信息 + PlatformLinkResponse platformLinkRespons = unitMaintainPlatformDao.selectByUnitld(ioOrderEntity.getFromCorp()); + if(platformLinkRespons == null){ + return ResultVOUtils.error(999, "该单没有设置补单条件!"); + } + //获取单据详情 + List ioOrderDetailCodeEntityList=ioOrderDetailCodeService.findByOrderId(orderId); + List ioCodeTempEntityList = ioCodeTempService.findByOrderId(orderId); + for (IoCodeTempEntity ioCodeTempEntity : ioCodeTempEntityList) { + Map jsonMap = new HashMap<>(); + jsonMap.put("code", ioCodeTempEntity.getCode()); + ObjectMapper mapper = new ObjectMapper(); + String json = mapper.writeValueAsString(jsonMap); + ioCodeTempEntity.setCode(json); + } + //获取业务单 + QueryWrapper ew=new QueryWrapper<>(); + ew.eq("orderIdFk",orderId); + List ioOrderDetailBizEntityList=ioOrderDetailBizDao.selectList(ew); + + //获取结果单 + QueryWrapper ew1=new QueryWrapper<>(); + ew1.eq("orderIdFk",orderId); + List ioOrderDetailResultEntityList=ioOrderDetailResultDao.selectList(ew1); + + //获取发票 + QueryWrapper ew2=new QueryWrapper<>(); + ew2.eq("orderIdFk",orderId); + List ioOrderInvoiceEntityList=ioOrderInvoiceMapper.selectList(ew2); + ArrayList list=new ArrayList<>(); + for (IoOrderInvoiceEntity ioOrderInvoiceEntity : ioOrderInvoiceEntityList) { + if(StrUtil.isNotBlank(ioOrderInvoiceEntity.getLicenseUrl())){ + list.add(filePath + "/register/image2/" + ioOrderInvoiceEntity.getLicenseUrl()); + ioOrderInvoiceEntity.setLicenseUrl("as" + ioOrderInvoiceEntity.getLicenseUrl()); + } + } + relayFile(list,platformLinkRespons.getHost()); + + //组装数据 + String host = platformLinkRespons.getHost(); + IoUploadOrderResponse ioUploadOrderResponse=new IoUploadOrderResponse(); + ioUploadOrderResponse.setIoOrderEntity(ioOrderEntity); + ioUploadOrderResponse.setPlatformLinkRespons(platformLinkRespons); + ioUploadOrderResponse.setIoCodeTempEntityList(ioCodeTempEntityList); + ioUploadOrderResponse.setIoOrderDetailCodeEntityList(ioOrderDetailCodeEntityList); + ioUploadOrderResponse.setIoOrderDetailResultEntityList(ioOrderDetailResultEntityList); + ioUploadOrderResponse.setIoOrderDetailBizEntityList(ioOrderDetailBizEntityList); + ioUploadOrderResponse.setIoOrderInvoiceEntityList(ioOrderInvoiceEntityList); + String resp = okHttpCli.doPostJson(host + "/spms/sub/inv/warehouse/addOrder", + JSON.toJSONString(ioUploadOrderResponse), buildHeader(platformLinkRespons.getAppid(), platformLinkRespons.getSecretKey())); + //回调结果 返回信息 + if (StrUtil.isNotBlank(resp) && resp.contains("20000")) { + return JSON.parseObject(resp, BaseResponse.class); + } else { + log.error("获取自助平台分库失败"); + return ResultVOUtils.error(500, "获取自助平台接口异常"); + } + } + + public String[] buildHeader(String apiKey, String apiSecret) { String[] headers = {"api_key", apiKey, "secret_key", apiSecret}; return headers; } + + + /*转发图片*/ + private String relayFile(ArrayList files, String ip) { + String host = ip; + String result = ""; + + if (!StringUtils.isEmpty(host)) { + host += "/udiwms/file/uploadFile"; + OkHttpClient client = new OkHttpClient().newBuilder() + .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间 + .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间 + .build(); + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + + MultipartBody.Builder builder = new MultipartBody.Builder(); + builder.setType(MultipartBody.FORM); + String fileType = "application/octet-stream"; + if (files != null && files.size() > 0) { + for (int i = 0; i < files.size(); i++) { + if (!StringUtils.isEmpty(files.get(i))) { + File file = new File(files.get(i)); + String fileName = files.get(i); + fileName = "as"+ fileName.substring(fileName.lastIndexOf("/")+1); + builder.addFormDataPart("files", fileName, + RequestBody.create(MediaType.parse(fileType), file)); + } + } + } + + RequestBody body = builder.build(); + + Request req = new Request.Builder() + .url(host) + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("format", "json") + .addHeader("apiKey", apiKey) + .addHeader("secretKey", apiSecret) + .addHeader("timestamp", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) + .addHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept") + .build(); + try { + Response response = client.newCall(req).execute(); + result = response.body().string(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return result; + } + } diff --git a/src/main/java/com/glxp/api/util/MultipartFileTest.java b/src/main/java/com/glxp/api/util/MultipartFileTest.java new file mode 100644 index 00000000..a3f32642 --- /dev/null +++ b/src/main/java/com/glxp/api/util/MultipartFileTest.java @@ -0,0 +1,79 @@ +package com.glxp.api.util; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + + +import java.io.*; + +public class MultipartFileTest { + + private static final Logger log = LoggerFactory.getLogger(MultipartFileTest.class); + + private MultipartFileTest() { } + + + public static void main(String[] args) { + // 本地文件转为MultipartFile类型 + String fileName="D:\\1s\\a6294b6b58de15136bb827dd1efdc76.jpg"; + fileName = fileName.substring(fileName.lastIndexOf("/")); + } + + public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) { + FileItem fileItem = createFileItem(inputStream, fileName); + return new CommonsMultipartFile(fileItem); + } + + + public static MultipartFile[] getMultipartFiles(InputStream[] inputStream, String fileName) { + // 多文件转换 + int length = inputStream.length; + MultipartFile[] multipartFiles = new MultipartFile[length]; + for (int i = 0; i < length; i++) { + FileItem fileItem = createFileItem(inputStream[i], fileName); + multipartFiles[i] = new CommonsMultipartFile(fileItem); + } + return multipartFiles; + } + + + public static FileItem createFileItem(InputStream inputStream, String fileName) { + FileItemFactory factory = new DiskFileItemFactory(16, null); + FileItem fileItem = factory.createItem("file", MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName); + int read = 0; + OutputStream os = null; + byte[] buffer = new byte[10 * 1024 * 1024]; + try { + os = fileItem.getOutputStream(); + while ((read = inputStream.read(buffer, 0, 4096)) != -1) { + os.write(buffer, 0, read); + } + inputStream.close(); + } catch (IOException e) { + log.error("os write exception", e); + throw new IllegalArgumentException("文件流输出失败"); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + log.error("stream os close exception", e); + } + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.error("stream inputStream close exception", e); + } + } + } + return fileItem; + } +} diff --git a/src/main/java/com/glxp/api/util/OkHttpCli.java b/src/main/java/com/glxp/api/util/OkHttpCli.java index 25bc943a..a853f71d 100644 --- a/src/main/java/com/glxp/api/util/OkHttpCli.java +++ b/src/main/java/com/glxp/api/util/OkHttpCli.java @@ -117,6 +117,7 @@ public class OkHttpCli { } + /** * post 请求, 请求数据为 json 的字符串 * diff --git a/src/main/resources/mybatis/mapper/inout/IoOrderDao.xml b/src/main/resources/mybatis/mapper/inout/IoOrderDao.xml index aa1e90cb..5e417cb0 100644 --- a/src/main/resources/mybatis/mapper/inout/IoOrderDao.xml +++ b/src/main/resources/mybatis/mapper/inout/IoOrderDao.xml @@ -15,6 +15,9 @@ (select name from basic_corp bc where bc.erpId = io.fromCorp) fromCorpName, (select name from basic_corp bc where bc.erpId = io.customerId) customerName from io_order as io + + INNER JOIN io_unit_maintain_platform a2 on io.action = a2.sourceAction AND io.fromCorp = a2.unitId + AND id = #{id} @@ -42,7 +45,7 @@ AND deptCode = #{deptCode} - AND invCode = #{invCode} + AND io.invCode = #{invCode} AND busType = #{busType} diff --git a/src/main/resources/mybatis/mapper/inout/UnitMaintainPlatformDao.xml b/src/main/resources/mybatis/mapper/inout/UnitMaintainPlatformDao.xml index cb9cdedd..cf1c52b7 100644 --- a/src/main/resources/mybatis/mapper/inout/UnitMaintainPlatformDao.xml +++ b/src/main/resources/mybatis/mapper/inout/UnitMaintainPlatformDao.xml @@ -165,4 +165,24 @@ LEFT JOIN basic_bussiness_type b1 ON b1.action = io.sourceAction where io.platformId = #{platformId} + + \ No newline at end of file