Pārlūkot izejas kodu

签章文件回调处理

徐滕 1 nedēļu atpakaļ
vecāks
revīzija
3dd92cb9a9

+ 120 - 20
jeeplus-modules/jeeplus-finance/src/main/java/com/jeeplus/finance/projectReport/controller/CwProjectReportSignatureCallBackController.java

@@ -57,6 +57,8 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.lang.reflect.Type;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.*;
 import java.util.stream.Collectors;
 //import org.springframework.mock.web.MockMultipartFile;
@@ -232,10 +234,17 @@ public class CwProjectReportSignatureCallBackController {
 
 
     @RequestMapping(value = "/getApprovalCallBackAccomplish", method= RequestMethod.POST)
+    @ResponseBody
     public String getApprovalCallBackAccomplish(PresignCallBack presignCallBack, HttpServletResponse response) {
         //根据contractId查询对应的报告信息
         CwProjectReportData projectReportData = cwProjectReportService.queryByContractId(presignCallBack.getContractId());
+        if (projectReportData == null) {
+            return "签章回调失败:根据contractId[" + presignCallBack.getContractId() + "]未查询到对应的报告信息";
+        }
         UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getById (projectReportData.getCreateById());
+        if (userDTO == null) {
+            return "签章回调失败:根据createById[" + projectReportData.getCreateById() + "]未查询到对应的用户信息";
+        }
         //对文件回调路径进行处理并保存
         String signatureUrl = null;
         if(StringUtils.isNotBlank(presignCallBack.getStoragePath())){
@@ -258,23 +267,68 @@ public class CwProjectReportSignatureCallBackController {
             }else{
                 path = "/attachment-file/";
             }
-            String aliyunUrl = "http://cdn.gangwaninfo.com";
-            String aliDownloadUrl = "http://oss.gangwaninfo.com";
-
 
-            String file = aliyunUrl + signatureUrl;
-            file = file.replace("amp;","");
-            String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());
-            String cons = "";
-            if (file.contains(aliyunUrl)){
-                cons = aliyunUrl;
-            }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
-                cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
-            }else {
-                cons = aliDownloadUrl;
+            String fileName;
+            if (StringUtils.isNotBlank(presignCallBack.getStoragePath())) {
+                // 原有逻辑:storagePath 有值,从 OSS 下载
+                String aliyunUrl = "http://cdn.gangwaninfo.com";
+                String aliDownloadUrl = "http://oss.gangwaninfo.com";
+
+                String file = aliyunUrl + signatureUrl;
+                file = file.replace("amp;","");
+                fileName = file.substring(file.lastIndexOf("/") + 1, file.length());
+                String cons = "";
+                if (file.contains(aliyunUrl)){
+                    cons = aliyunUrl;
+                }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
+                    cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
+                }else {
+                    cons = aliDownloadUrl;
+                }
+                String ossKey = file.split(cons+"/")[1];
+                SpringUtil.getBean ( IWorkAttachmentApi.class ).downQzByStreamSaveLocal(ossKey,fileName,path+fileName);
+            } else {
+                // 兜底逻辑:storagePath 为空(如更换契约锁服务后),通过契约锁下载合同API直接获取文件
+                System.out.println("storagePath为空,通过契约锁API下载合同,contractId:" + presignCallBack.getContractId());
+                String downloadApiUrl = Global.getConfig("signature_http_top") + "/contract/download?contractId=" + presignCallBack.getContractId() + "&downloadItems=NORMAL";
+                System.out.println("调用 " + downloadApiUrl);
+
+                // 通过HTTP GET调用契约锁下载接口,直接获取二进制zip文件流
+                HttpURLConnection httpConn = null;
+                try {
+                    URL urlObj = new URL(downloadApiUrl);
+                    httpConn = (HttpURLConnection) urlObj.openConnection();
+                    httpConn.setRequestMethod("GET");
+                    httpConn.setRequestProperty("x-qys-accesstoken", Global.getConfig("apptoken"));
+                    httpConn.setRequestProperty("x-qys-signature", Global.getConfig("signature"));
+                    httpConn.setRequestProperty("x-qys-timestamp", "0");
+                    httpConn.setConnectTimeout(35000);
+                    httpConn.setReadTimeout(120000);
+
+                    int responseCode = httpConn.getResponseCode();
+                    System.out.println("契约锁下载接口响应码:" + responseCode);
+                    if (responseCode != 200) {
+                        return "签章处理异常:契约锁下载接口返回非200,responseCode=" + responseCode + ",contractId:" + presignCallBack.getContractId();
+                    }
+                    // 读取二进制响应(zip文件)
+                    InputStream inputStream = httpConn.getInputStream();
+                    fileName = presignCallBack.getContractId() + ".zip";
+                    File localFile = new File(path + fileName);
+                    FileOutputStream fos = new FileOutputStream(localFile);
+                    byte[] buffer = new byte[4096];
+                    int len;
+                    while ((len = inputStream.read(buffer)) != -1) {
+                        fos.write(buffer, 0, len);
+                    }
+                    fos.close();
+                    inputStream.close();
+                    System.out.println("通过契约锁download API下载合同成功:" + path + fileName);
+                } finally {
+                    if (httpConn != null) {
+                        httpConn.disconnect();
+                    }
+                }
             }
-            String ossKey = file.split(cons+"/")[1];
-            SpringUtil.getBean ( IWorkAttachmentApi.class ).downQzByStreamSaveLocal(ossKey,fileName,path+fileName);
 //            ossClientService.downQzByStreamSaveLocal(ossKey,fileName,path+fileName);
             //将下载下来的文件转换为file文件
             File srcFile = new File(path+fileName);
@@ -319,24 +373,30 @@ public class CwProjectReportSignatureCallBackController {
                     //取得上传文件
                     //将文件上传到oss云盘中
                     MultipartFile cMultiFile = new MockMultipartFile("file", f.getName(), null, new FileInputStream(f));
+                    System.out.println("文件大小:" + f.length() + ",文件名:" + f.getName());
                     if (!cMultiFile.isEmpty()) {
                         // 文件保存路径
                         String realPath =DIRECTORY.replaceFirst("/","")+"/cw_project_report_file"+SpringUtil.getBean ( IWorkAttachmentApi.class ).datePath()+"/"+ System.currentTimeMillis();
                         //文件原名称
                         String newName = cMultiFile.getOriginalFilename();
                         if(StringUtils.isNotBlank(newName)){
-                            newName = newName.substring(13, newName.length());
+                            if (newName.length() > 13) {
+                                newName = newName.substring(13, newName.length());
+                            }
                             //SpringUtil.getBean ( IWorkAttachmentApi.class ).uploadFile2OSS(cMultiFile.getInputStream(),realPath,newName);
                             SpringUtil.getBean ( IWorkAttachmentApi.class ).uploadFileSignatureOSS(f.getPath(),realPath,newName);
                             filepath = "/" + realPath + newName;
+                            System.out.println("文件上传OSS成功,filepath:" + filepath);
                         }
+                    } else {
+                        System.out.println("文件为空,跳过上传");
                     }
 
                     System.out.println(f.getName());
                     //截取文件后缀名
                     String substring = f.getName().substring(f.getName().lastIndexOf(".")+1, f.getName().length());
 
-                    String newFileName = f.getName().substring(13, f.getName().length());
+                    String newFileName = f.getName().length() > 13 ? f.getName().substring(13, f.getName().length()) : f.getName();
 
                     //将签章完成的附件进行保存到附件表中
                     WorkAttachmentInfo workattachment = new WorkAttachmentInfo();
@@ -370,12 +430,14 @@ public class CwProjectReportSignatureCallBackController {
                     }
                     System.out.println("byAttachmentIdAndUrl:" + byAttachmentIdAndUrl);
                     if (byAttachmentIdAndUrl == null || byAttachmentIdAndUrl.size() == 0) {
+                        System.out.println("附件不存在,开始保存。filepath:" + filepath + ",newFileName:" + newFileName);
                         Map<String,String> map = new HashMap<>();
                         String workAttachment = JSON.toJSONString((workattachment));
                         String userDTOInfo = JSON.toJSONString((userDTO));
                         map.put("workAttachment",workAttachment);
                         map.put("userDTO",userDTOInfo);
                         SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
+                        System.out.println("work_attachment 保存完成");
 //                        ossServiceMapper.insert(workattachment);
 
                         //将附件信息添加到签章附件信息表中
@@ -389,6 +451,37 @@ public class CwProjectReportSignatureCallBackController {
                         cwProjectReportFile.setReportFileId(id);
                         cwProjectReportFile.setFileAffiliation("1");
                         cwProjectReportService.insertProjectReportFile(cwProjectReportFile);
+                        System.out.println("cw_project_report_file 保存完成,reportFileId:" + id);
+                    } else {
+                        // 附件已存在但URL可能已变化(重试回调),删除旧记录重新保存
+                        WorkAttachmentInfo existAttachment = byAttachmentIdAndUrl.get(0);
+                        System.out.println("附件已存在,旧URL:" + existAttachment.getUrl() + ",新URL:" + filepath + ",旧ID:" + existAttachment.getId());
+                        // 删除旧的 work_attachment 记录
+                        SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteById(existAttachment.getId());
+                        // 删除旧的 cw_project_report_file 记录
+                        attachmentMapper.delete(new QueryWrapper<CwProjectReportFile>().lambda()
+                                .eq(CwProjectReportFile::getReportFileId, existAttachment.getId()));
+                        System.out.println("旧记录已删除,重新保存");
+
+                        Map<String,String> map = new HashMap<>();
+                        String workAttachment = JSON.toJSONString((workattachment));
+                        String userDTOInfo = JSON.toJSONString((userDTO));
+                        map.put("workAttachment",workAttachment);
+                        map.put("userDTO",userDTOInfo);
+                        SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
+                        System.out.println("work_attachment 重新保存完成");
+
+                        CwProjectReportFile cwProjectReportFile = new CwProjectReportFile();
+                        cwProjectReportFile.setDelFlag(0);
+                        cwProjectReportFile.setId(UUID.randomUUID().toString().replace("-", ""));
+                        cwProjectReportFile.setCreateById(projectReportData.getCreateById());
+                        cwProjectReportFile.setUpdateById(projectReportData.getCreateById());
+                        cwProjectReportFile.setCreateTime(new Date());
+                        cwProjectReportFile.setUpdateTime(new Date());
+                        cwProjectReportFile.setReportFileId(id);
+                        cwProjectReportFile.setFileAffiliation("1");
+                        cwProjectReportService.insertProjectReportFile(cwProjectReportFile);
+                        System.out.println("cw_project_report_file 重新保存完成,reportFileId:" + id);
                     }
 
                     //遍历该contractId对应的报告信息,将报告信息中的附件地址进行替换
@@ -417,6 +510,9 @@ public class CwProjectReportSignatureCallBackController {
         }catch (Exception e){
             System.out.println("签章处理错误1:" + e.getMessage());
             e.printStackTrace();
+            StringWriter sw = new StringWriter();
+            e.printStackTrace(new PrintWriter(sw));
+            return "签章处理异常:" + e.getClass().getName() + " - " + sw.toString();
         }finally {
             if(StringUtils.isNotBlank(deleteFile)){
                 //根据路径创建文件对象
@@ -426,11 +522,15 @@ public class CwProjectReportSignatureCallBackController {
                     file.delete();
                 }
             }
-            File file = new File(unzipFileStr);
-            deleteFile(file);
+            if (StringUtils.isNotBlank(unzipFileStr)) {
+                File file = new File(unzipFileStr);
+                if (file.exists()) {
+                    deleteFile(file);
+                }
+            }
         }
 
-        return null;
+        return "success";
     }