Jelajahi Sumber

1、新增srm单据上传附件到发票附件

wanghaiwu 3 minggu lalu
induk
melakukan
d133b1e74e

+ 179 - 0
code/jyyy/nckd-jimin-jyyy-fi/src/main/java/nckd/jimin/jyyy/fi/webapi/SRMSynAttacmentApiPlugin.java

@@ -0,0 +1,179 @@
+package nckd.jimin.jyyy.fi.webapi;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import kd.bos.dataentity.resource.ResManager;
+import kd.bos.dataentity.utils.StringUtils;
+import kd.bos.logging.Log;
+import kd.bos.logging.LogFactory;
+import kd.bos.openapi.common.custom.annotation.ApiController;
+import kd.bos.openapi.common.custom.annotation.ApiParam;
+import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
+import kd.bos.openapi.common.result.CustomApiResult;
+import kd.imc.rim.common.message.exception.MsgException;
+import kd.imc.rim.common.utils.FileUploadUtils;
+import kd.imc.rim.common.utils.FileUtils;
+import kd.imc.rim.common.utils.MD5;
+import nckd.jimin.jyyy.fi.plugin.operate.SRMHelperUtils;
+
+import javax.validation.Valid;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Base64;
+import java.util.Map;
+import java.util.UUID;
+
+@ApiController(
+        desc = "对接SRM预付款、付款附件,上传到发票",
+        value = "SRM"
+)
+public class SRMSynAttacmentApiPlugin implements Serializable {
+    private static final long serialVersionUID = 8975925104443178270L;
+
+    private static final Log logger = LogFactory.getLog(SRMSynAttacmentApiPlugin.class);
+
+    @ApiPostMapping("synAttacmenthForSRM")
+    public CustomApiResult<JSONObject> synAttacmenthForSRM(
+            @Valid @ApiParam(value = "附件xml") String request) {
+
+        //获取token
+        Map<String,String> tokenMap = SRMHelperUtils.getSRMToken();
+        if (!"0".equals(tokenMap.get("code"))) {//未获取到token
+            logger.info("get srm token fail" + tokenMap.get("msg"));
+
+            return returnResult("E", "失败,获取token失败," + tokenMap.get("msg"), null);
+        }
+        String token = tokenMap.get("msg").toString();
+        JSONObject returnData = new JSONObject();
+
+        String downloadUrl = "https://gateway.test.isrm.going-link.com/hfle/v1/19694/files/download?bucketName=private-bucket&url=https://isrm-test-private-bucket.obs.cn-east-2.myhuaweicloud.com:443/2025-05/cusz-attachment-default/19694/744f2e7425f7468283578f48879bb4be@%E5%B1%B1%E7%A6%BE%E8%AE%A2%E5%8D%95%E6%A8%A1%E6%9D%BF.pdf";
+        downloadUrl = downloadUrl + "&access_token=" + token;
+
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(downloadUrl);
+            //这里没有使用 封装后的ResponseEntity 就是也是因为这里不适合一次性的拿到结果,放不下content,会造成内存溢出
+            HttpURLConnection connection =(HttpURLConnection) url.openConnection();
+
+            connection.getContentLength();
+
+            //使用bufferedInputStream 缓存流的方式来获取下载文件,不然大文件会出现内存溢出的情况
+            inputStream = new BufferedInputStream(connection.getInputStream());
+
+            String base64 = InputStreamToBase64String(inputStream);
+
+            String fileName = fileName("1");
+            String fileUrl = upFileOfBase64(base64, fileName, "测试srm上传发票附件");
+
+            returnData.put("fileUrl", fileUrl == null ? "" : fileUrl);
+            returnData.put("downloadUrl", downloadUrl);
+
+            connection.disconnect();
+            inputStream.close();
+        } catch (Exception e){
+            e.printStackTrace();
+        } finally {
+            // 5、关闭资源
+//        	IOUtils.closeQuietly(outputStream);
+//            IOUtils.closeQuietly(inputStream);
+        }
+
+        return returnResult("S", "成功", returnData);
+    }
+
+    private String upFileOfBase64(String base64, String fileType, String fileName) {
+        String url = FileUploadUtils.getInvoiceDir("invoice") + fileName;
+        String fileId = MD5.SHA256Hex(fileType + base64);
+        String path = FileUtils.queryFilePath(fileId);
+        if (StringUtils.isEmpty(path)) {
+            path = FileUploadUtils.uploadBase64(url, fileName, base64);
+            FileUtils.saveFilePath("", path, fileId, fileType);
+        }
+
+        logger.info("SRMSynAttacmentApiPlugin 上传服务器:fileName:" + fileName + ", url:" + url + ", fileId: " + fileId);
+        logger.info("SRMSynAttacmentApiPlugin:" + (path == null ? "path is null" : path));
+
+        return path;
+    }
+
+    private String fileName(String fileType) {
+        String fileName = String.valueOf(UUID.randomUUID());
+        String filePix = "";
+        if (StringUtils.equals(fileType, "1")) {
+            filePix = ".pdf";
+        } else if (StringUtils.equals(fileType, "2")) {
+            filePix = ".png";
+        } else if (StringUtils.equals(fileType, "4")) {
+            filePix = ".ofd";
+        } else if (StringUtils.equalsIgnoreCase(fileType, "pdf")) {
+            filePix = ".pdf";
+        } else if (StringUtils.equalsIgnoreCase(fileType, "ofd")) {
+            filePix = ".ofd";
+        } else if (FileUtils.isImage(fileType)) {
+            filePix = ".jpg";
+        }
+
+        if (StringUtils.isEmpty(filePix)) {
+            throw new MsgException("0009", ResManager.loadKDString("文件类型错误", "UploadFileService_1", "imc-rim-formplugin", new Object[0]));
+        } else {
+            return fileName + filePix;
+        }
+    }
+
+    /**
+     * InputStream转成base64String
+     * @param inputStream
+     * @return
+     * @throws IOException
+     */
+    private String InputStreamToBase64String(InputStream inputStream){
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+//        byte[] buffer = new byte[1024]; // 创建一个缓冲区,大小可以根据需要调整
+        byte[] buffer = new byte[1024 * 1024 * 5];// 5MB
+        int bytesRead;
+        try {
+            // 读取数据到缓冲区,直到没有更多数据可读
+            while (true) {
+                try {
+                    if (!((bytesRead = inputStream.read(buffer)) != -1)) break;
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+                byteArrayOutputStream.write(buffer, 0, bytesRead);
+            }
+
+            // 将ByteArrayOutputStream的内容转换成byte数组
+            byte[] bytes = byteArrayOutputStream.toByteArray();
+
+//            String base64encode = new BASE64Encoder().encode(bytes);
+            String base64String = Base64.getEncoder().encodeToString(bytes);
+//去掉空格,直接转的话会有换行问题,在前端展示会导致出不来          base64encode = base64encode.replaceAll("[\\s*\t\n\r]", "");
+            byteArrayOutputStream.close();
+
+            return base64String;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+
+    }
+
+
+    /**
+     * 自定义返回data对象
+     * @param code  编码
+     * @param message  错误信息
+     * @return   CustomApiResult  返回
+     */
+    public CustomApiResult<JSONObject> returnResult(String code, String message, JSONObject responseData){
+        JSONObject reslutData = new JSONObject();
+
+        reslutData.put("message", message);
+        reslutData.put("code", code);
+
+        reslutData.put("data", responseData);
+
+        return CustomApiResult.success(reslutData);
+    }
+}