Kaynağa Gözat

fix 国任保险上传影像问题

liub 4 ay önce
ebeveyn
işleme
fbaf01fb06

+ 53 - 0
tenant/insurance/quotation-commons/src/main/java/com/jzg/quotation/commons/utils/HttpUtil.java

@@ -183,5 +183,58 @@ public class HttpUtil {
         return null;
     }
 
+    /**
+     * 根据URL下载文件内容为byte数组
+     * @param urlString URL地址
+     * @return byte数组
+     */
+    public static byte[] getUrlBytes(String urlString) {
+        if (urlString == null || urlString.isEmpty()) {
+            return null;
+        }
+        HttpURLConnection connection = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream outputStream = null;
+        try {
+            URL url = new URL(urlString);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("GET");
+            connection.setConnectTimeout(30000);
+            connection.setReadTimeout(30000);
+            connection.setDoInput(true);
+            connection.connect();
+
+            int responseCode = connection.getResponseCode();
+            if (responseCode == HttpURLConnection.HTTP_OK) {
+                inputStream = connection.getInputStream();
+                outputStream = new ByteArrayOutputStream();
+                byte[] buffer = new byte[4096];
+                int bytesRead;
+                while ((bytesRead = inputStream.read(buffer)) != -1) {
+                    outputStream.write(buffer, 0, bytesRead);
+                }
+                return outputStream.toByteArray();
+            }
+        } catch (Exception e) {
+            System.err.println("下载文件失败:" + e.getMessage());
+            e.printStackTrace();
+        } finally {
+            try {
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+                if (outputStream != null) {
+                    outputStream.close();
+                }
+                if (connection != null) {
+                    connection.disconnect();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
 
 }

+ 143 - 31
tenant/insurance/quotation-guoren/src/main/java/com/jzg/quotation/guoren/api/service/Impl/GuoRenApiRequestImpl.java

@@ -11,12 +11,14 @@ import com.jzg.commons.entity.agreement.po.PtlAgreementUrl;
 import com.jzg.commons.entity.orders.po.InsOrders;
 import com.jzg.commons.entity.orders.po.InsOrdersPolicy;
 import com.jzg.commons.entity.po.InsCompanyClause;
+import com.jzg.commons.entity.po.SysUploadFiles;
 import com.jzg.commons.entity.quote.vo.AccidentalDrivingVo;
 import com.jzg.commons.entity.quote.vo.CarInfoVo;
 import com.jzg.commons.entity.quote.vo.QuoteVo;
 import com.jzg.commons.entity.quote.vo.aggregated.*;
 import com.jzg.commons.exception.SystemException;
 import com.jzg.commons.util.AssertionUtils;
+import com.jzg.commons.util.MinioUtils;
 import com.jzg.commons.util.StringUtils;
 import com.jzg.quotation.commons.annotation.QuoteApiService;
 import com.jzg.quotation.commons.client.PlatformClient;
@@ -27,6 +29,7 @@ import com.jzg.commons.entity.quote.vo.image.InsuranceImageVo;
 import com.jzg.quotation.commons.standard.conversion.ConfigureParametersConversion;
 import com.jzg.quotation.commons.utils.DateTimeUtil;
 import com.jzg.quotation.commons.utils.HttpUtil;
+import com.jzg.quotation.commons.utils.TimeProcessing;
 import com.jzg.quotation.guoren.api.build.GuoRenApiQuoteResultsVoBuild;
 import com.jzg.quotation.guoren.api.component.GuoRenRequestApiComponent;
 import com.jzg.quotation.guoren.api.component.GuorenDuplicateInsuranceComponent;
@@ -608,72 +611,181 @@ public class GuoRenApiRequestImpl implements GuoRenApiRequest {
 
     /**
      * 影像上传 调用三方
+     * 请求参数:
+     * - businessNo: 投保单号(交商同保传交强)
+     * - operatorCode: 操作人代码
+     * - comCode: 机构代码
+     * - fileGroup: 影像类型(DZ—单证影像,YC-验车影像)
+     * - file: 图片对象
      */
     public void imageUpdate(GuoRenApiOrder guoRenApiOrder, GuoRenConfigureParameters parameters, InsuranceUploadImageVo insuranceUploadImageVo) {
+        // 获取交强险投保单号(交商同保传交强)
+        String businessNo = guoRenApiOrder.getJqApplyNo();
+        if (StringUtils.isEmpty(businessNo)) {
+            logger.error("国任上传影像失败,交强险投保单号为空");
+            throw new SystemException("国任,影像上传失败,投保单号为空");
+        }
 
-        //构造请求参数
-        Map<String, Object> formMap = new HashMap<>();
-        formMap.put("businessNo", StringUtils.isEmpty(guoRenApiOrder.getJqApplyNo()) ? guoRenApiOrder.getSyApplyNo() : guoRenApiOrder.getJqApplyNo());
-        formMap.put("operatorCode", parameters.getHandlerCode());
-        formMap.put("comCode", parameters.getComCode());
-        formMap.put("fileGroup", "DZ");
+        // 操作人代码
+        String operatorCode = parameters.getHandlerCode();
+        // 机构代码
+        String comCode = parameters.getComCode();
 
         List<InsuranceImageVo> imageVoList = insuranceUploadImageVo.getImageVoList();
-        Map<String, List<String>> map = new HashMap<>();
+        Map<String, List<String>> resultMap = new HashMap<>();
+
+        if (CollUtil.isEmpty(imageVoList)) {
+            logger.warn("国任上传影像失败,影像列表为空");
+            return;
+        }
+
+        for (InsuranceImageVo imageVo : imageVoList) {
+            if (imageVo == null) {
+                continue;
+            }
 
-        imageVoList.forEach(insuranceImageVo -> {
             List<String> imageIds = new ArrayList<>();
-            for (InsuranceImageVo.ImageDTO imageDTO : insuranceImageVo.getImageBase64List()) {
+            List<InsuranceImageVo.ImageDTO> imageList = imageVo.getImageBase64List();
+
+            if (CollUtil.isEmpty(imageList)) {
+                logger.warn("国任上传影像失败,影像Base64列表为空,类型:{}", imageVo.getImageType());
+                continue;
+            }
+
+            // 根据影像类型确定fileGroup
+            String fileGroup = getFileGroup(imageVo);
+
+            for (InsuranceImageVo.ImageDTO imageDTO : imageList) {
+                if (imageDTO == null) {
+                    logger.warn("国任上传影像失败,图片DTO为空");
+                    continue;
+                }
+
+                byte[] imageBytes = imageDTO.getImageByte();
+                String fileName = imageDTO.getImageName();
+
+                // 如果imageByte为空,根据imageId查询文件并下载
+                if (imageBytes == null && StringUtils.isNotEmpty(imageDTO.getImageId())) {
+                    try {
+                        // 根据imageId查询文件信息
+                        SysUploadFiles uploadFile = platformClient.getUploadFile(imageDTO.getImageId());
+                        if (uploadFile == null) {
+                            logger.error("国任上传影像失败,未找到文件信息,imageId:{}", imageDTO.getImageId());
+                            continue;
+                        }
+
+                        // 获取文件名
+                        fileName = uploadFile.getFileName();
+
+                        // 获取预签名URL
+                        String presignedUrl = MinioUtils.getPresignedObjectUrl(uploadFile.getBucketName(), fileName);
+                        if (StringUtils.isEmpty(presignedUrl)) {
+                            logger.error("国任上传影像失败,获取文件预签名URL失败,imageId:{}", imageDTO.getImageId());
+                            continue;
+                        }
+
+                        // 下载文件内容为byte[]
+                        imageBytes = HttpUtil.getUrlBytes(presignedUrl);
+                        if (imageBytes == null || imageBytes.length == 0) {
+                            logger.error("国任上传影像失败,文件内容为空,imageId:{}", imageDTO.getImageId());
+                            continue;
+                        }
+
+                        logger.info("国任下载影像成功,imageId:{},fileName:{}", imageDTO.getImageId(), fileName);
+                    } catch (Exception e) {
+                        logger.error("国任下载影像异常,imageId:{},error:{}", imageDTO.getImageId(), e.getMessage(), e);
+                        continue;
+                    }
+                }
+
+                if (imageBytes == null || imageBytes.length == 0) {
+                    logger.warn("国任上传影像失败,图片字节数组为空,imageId:{}", imageDTO.getImageId());
+                    continue;
+                }
+
                 try {
-                    //需要设置一个临时图片路径
-                    File tempFile = File.createTempFile("upload_temp_",  getFileSuffix(imageDTO.getImageName()));
-                    FilePart file1 = new FilePart("file", tempFile);
+                    // 生成临时文件
+                    String fileSuffix = getFileSuffix(fileName);
+                    File tempFile = File.createTempFile("upload_temp_", fileSuffix);
                     tempFile.deleteOnExit();
 
                     // 将字节数组写入临时文件
                     try (FileOutputStream fos = new FileOutputStream(tempFile)) {
-                        fos.write(imageDTO.getImageByte());
+                        fos.write(imageBytes);
                         fos.flush();
                     }
 
-                    Part[] parts = {file1, new StringPart("businessNo", formMap.get("businessNo").toString()),
-                            new StringPart("operatorCode",
-                                    formMap.get("operatorCode").toString()), new StringPart("comCode",
-                            formMap.get("comCode").toString()),
-                            new StringPart("fileGroup", formMap.get("fileGroup").toString())};
-
+                    // 构造请求参数
+                    FilePart filePart = new FilePart("file", tempFile);
+                    Part[] parts = {
+                            filePart,
+                            new StringPart("businessNo", businessNo),
+                            new StringPart("operatorCode", operatorCode),
+                            new StringPart("comCode", comCode),
+                            new StringPart("fileGroup", fileGroup)
+                    };
+
+                    // 发送请求
                     String result = HttpUtil.sendPostFromFile(parts, properties.getUploadUrl());
+
                     if (StringUtils.isEmpty(result)) {
+                        logger.error("国任上传影像失败,接口返回为空,imageId:{}", imageDTO.getImageId());
                         throw new SystemException("国任,影像上传失败");
                     }
 
                     JSONObject jsonObject = JSON.parseObject(result);
+                    Integer resultCode = StringUtils.toInt(jsonObject.get("resultCode"));
 
-                    if (StringUtils.toInt(jsonObject.get("resultCode")) != 0) {
-                        throw new SystemException("国任,影像上传失败");
+                    if (resultCode == null || resultCode != 0) {
+                        String resultMsg = jsonObject.getString("resultMsg");
+                        logger.error("国任上传影像失败,resultCode:{},resultMsg:{},imageId:{}", resultCode, resultMsg, imageDTO.getImageId());
+                        throw new SystemException("国任,影像上传失败:" + resultMsg);
                     }
+
                     imageIds.add(imageDTO.getImageId());
+                    logger.info("国任上传影像成功,imageId:{},businessNo:{}", imageDTO.getImageId(), businessNo);
+
                 } catch (Exception e) {
-                    logger.error("国任上传影像异常:{}", e.getMessage());
-                    // 上传的影像存入redis
-                    insImagesUploadCacheComponent.setUploadImageCache(insuranceUploadImageVo.getOrder().getInsuranceCompanyCode(), map);
-                    throw new SystemException("国任,影像上传失败,类型:" + insuranceImageVo.getImageType());
+                    logger.error("国任上传影像异常:{},imageId:{}", e.getMessage(), imageDTO.getImageId(), e);
+                    insImagesUploadCacheComponent.setUploadImageCache(insuranceUploadImageVo.getOrder().getInsuranceCompanyCode(), resultMap);
+                    throw new SystemException("国任,影像上传失败,类型:" + imageVo.getImageType());
                 }
             }
-            map.put(insuranceImageVo.getImageCode(), imageIds);
-        });
 
-        insImagesUploadCacheComponent.setUploadImageCache(insuranceUploadImageVo.getOrder().getInsuranceCompanyCode(), map);
+            if (CollUtil.isNotEmpty(imageIds)) {
+                resultMap.put(imageVo.getImageCode(), imageIds);
+            }
+        }
+
+        // 将上传成功的影像存入缓存
+        insImagesUploadCacheComponent.setUploadImageCache(insuranceUploadImageVo.getOrder().getInsuranceCompanyCode(), resultMap);
+    }
+
+    /**
+     * 根据影像类型获取fileGroup
+     * DZ—单证影像, YC-验车影像
+     */
+    private String getFileGroup(InsuranceImageVo imageVo) {
+        String imageType = imageVo.getImageType();
+        if (StringUtils.isEmpty(imageType)) {
+            return "DZ";
+        }
+        // 验车相关的影像类型使用YC
+        if (imageType.contains("验车") || imageType.contains("YC")) {
+            return "YC";
+        }
+        // 默认使用单证影像
+        return "DZ";
     }
 
     /**
      * 工具方法:从文件名中提取后缀(如 "test.txt" → "txt")
      */
     private static String getFileSuffix(String fileName) {
-        if (fileName.contains(".")) {
-            return fileName.substring(fileName.lastIndexOf("."));
+        if (fileName == null || !fileName.contains(".")) {
+            return "bin"; // 默认后缀(二进制文件)
         }
-        return "bin"; // 默认后缀(二进制文件)
+        return fileName.substring(fileName.lastIndexOf("."));
     }