ccj 2 năm trước cách đây
mục cha
commit
c208a262d5

+ 4 - 0
pom.xml

@@ -55,6 +55,10 @@
                 <enabled>true</enabled>
             </snapshots>
         </repository>
+        <repository>
+            <id>com.e-iceblue</id>
+            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
+        </repository>
     </repositories>
     <pluginRepositories>
         <pluginRepository>

BIN
templates/tmp.pdf


+ 2 - 2
winsea-haixin-platform-backend/src/main/resources/application.yml

@@ -14,7 +14,7 @@ spring:
     serialization:
       write_dates_as_timestamps: false
   profiles:
-    active: prod
+    active: test
   resources:
     static-locations: file:///winsea/static/
   thymeleaf:
@@ -27,7 +27,7 @@ spring:
 #运行环境 1.开发环境 2.测试环境 3.生产环境
 com:
   changyuntong:
-    env: 3
+    env: 1
 
 auth:
   anon-methods:

+ 8 - 2
winsea-haixin-plugin-wangluohuoyun/pom.xml

@@ -177,8 +177,14 @@
             <artifactId>junit</artifactId>
         </dependency>
         <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
+            <groupId>e-iceblue</groupId>
+            <artifactId>spire.pdf.free</artifactId>
+            <version>5.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.pdfbox</groupId>
+            <artifactId>pdfbox</artifactId>
+            <version>2.0.9</version>
         </dependency>
     </dependencies>
 

+ 42 - 25
winsea-haixin-plugin-wangluohuoyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/service/impl/OrderInfoServiceImpl.java

@@ -1128,7 +1128,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
     @Override
     @Transactional(rollbackFor = Exception.class)
     public String batchSubmission(OrderInfo orderInfo) throws IOException {
-        if ("3".equals(ENV)) {
+//        if ("3".equals(ENV)) {
             //获取token
             String token = EntityAnalyse.GetJTToken(ENV);
             if (CollectionUtils.isNotEmpty(orderInfo.getOrderInfoList())) {
@@ -1206,34 +1206,51 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
                             orderInfo1.setRoadTransportBusinessLicenseNo(hyDriverInfo.getActualCarrierBusinessLicense());
                         }
                     }
-                    new Thread(new Runnable() {
-                        @SneakyThrows
-                        @Override
-                        public void run() {
-                            try {
-                                String result = EntityAnalyse.uploadWaybill(orderInfo1,ENV);
-                                if ("1001".equals(result)) {
-                                    //更改上报状态
-                                    orderInfo1.setEscalationStatusKey(StatusEnum.UNDER_REVIEW.getFlag());
-                                    orderInfo1.setEscalationStatus(StatusEnum.UNDER_REVIEW.getName());
-                                    orderInfoService.updateById(orderInfo1);
-                                } else {
-                                    //更改上报状态
-                                    orderInfo1.setEscalationStatusKey(StatusEnum.NOT_ADOPT.getFlag());
-                                    orderInfo1.setEscalationStatus(StatusEnum.NOT_ADOPT.getName());
-                                    orderInfo1.setEscalationFailureReason(result);
-                                    orderInfoService.updateById(orderInfo1);
-                                }
-                            } catch (Exception e) {
-                                e.printStackTrace();
-                            }
+//                    new Thread(new Runnable() {
+//                        @SneakyThrows
+//                        @Override
+//                        public void run() {
+//                            try {
+//                                String result = EntityAnalyse.uploadWaybill(orderInfo1,ENV);
+//                                if ("1001".equals(result)) {
+//                                    //更改上报状态
+//                                    orderInfo1.setEscalationStatusKey(StatusEnum.UNDER_REVIEW.getFlag());
+//                                    orderInfo1.setEscalationStatus(StatusEnum.UNDER_REVIEW.getName());
+//                                    orderInfoService.updateById(orderInfo1);
+//                                } else {
+//                                    //更改上报状态
+//                                    orderInfo1.setEscalationStatusKey(StatusEnum.NOT_ADOPT.getFlag());
+//                                    orderInfo1.setEscalationStatus(StatusEnum.NOT_ADOPT.getName());
+//                                    orderInfo1.setEscalationFailureReason(result);
+//                                    orderInfoService.updateById(orderInfo1);
+//                                }
+//                            } catch (Exception e) {
+//                                e.printStackTrace();
+//                            }
+//                        }
+//                    }).start();
+                     try {
+                        String result = EntityAnalyse.uploadWaybill(orderInfo1,ENV);
+                        if ("1001".equals(result)) {
+                            //更改上报状态
+                            orderInfo1.setEscalationStatusKey(StatusEnum.UNDER_REVIEW.getFlag());
+                            orderInfo1.setEscalationStatus(StatusEnum.UNDER_REVIEW.getName());
+                            orderInfoService.updateById(orderInfo1);
+                        } else {
+                            //更改上报状态
+                            orderInfo1.setEscalationStatusKey(StatusEnum.NOT_ADOPT.getFlag());
+                            orderInfo1.setEscalationStatus(StatusEnum.NOT_ADOPT.getName());
+                            orderInfo1.setEscalationFailureReason(result);
+                            orderInfoService.updateById(orderInfo1);
                         }
-                    }).start();
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
                 }
             }
             return "ok";
-        }
-        return "NG";
+//        }
+//        return "NG";
     }
 
     /**

+ 199 - 40
winsea-haixin-plugin-wangluohuoyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/util/EntityAnalyse.java

@@ -5,8 +5,8 @@ import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.ServiceException;
 import com.yh.saas.plugin.yiliangyiyun.entity.*;
 import com.yh.saas.plugin.yiliangyiyun.entity.view.DistinguishView;
+import net.coobird.thumbnailator.Thumbnails;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.collections.CollectionUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
@@ -22,8 +22,13 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
-import org.springframework.beans.factory.annotation.Value;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.rendering.PDFRenderer;
+import sun.awt.image.BufferedImageGraphicsConfig;
 
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
@@ -33,6 +38,7 @@ import java.text.DecimalFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.List;
 
 public class EntityAnalyse {
 
@@ -505,9 +511,9 @@ public class EntityAnalyse {
         params.put("insuranceInformation", insuranceInformation);
 
         params.put("remark", orderInfo.getRemark() != null ? orderInfo.getRemark() : "");
-        String[] file1 = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getDriverContractUrl())), orderInfo.getToken(), ENV)};
-        params.put("file", file1);
-        String[] ownersContractFile = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getContractUrl())), orderInfo.getToken(), ENV)};
+//        String[] file1 = {EntityAnalyse.uploadImage(EntityAnalyse.urlToPdfFile(new URL(orderInfo.getDriverContractUrl())), orderInfo.getToken(), ENV)};
+//        params.put("file", file1);
+        String[] ownersContractFile = {EntityAnalyse.uploadImage(EntityAnalyse.urlToPdfFile(new URL(orderInfo.getContractUrl())), orderInfo.getToken(), ENV)};
         params.put("ownersContractFile", ownersContractFile);
         String[] backDocumentImages = {EntityAnalyse.uploadImage(EntityAnalyse.urlToFile(new URL(orderInfo.getReceiptImg())), orderInfo.getToken(), ENV)};
         params.put("backDocumentImages", backDocumentImages);
@@ -1332,42 +1338,173 @@ public class EntityAnalyse {
      * @return
      */
     public static File urlToFile(URL url) {
-        return writeImageToDisk(getImageFromNetByUrl(url));
-//        InputStream is = null;
-//        File file = null;
-//        FileOutputStream fos = null;
-//        try {
-//            String tmp = "templates" + File.separator + "tmp.jpg";
-//            file = new File( new String(tmp.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
-////            file = File.createTempFile("tmp", null);
-//            URLConnection urlConn = null;
-//            urlConn = url.openConnection();
-//            is = urlConn.getInputStream();
-//            fos = new FileOutputStream(file);
-//            byte[] buffer = new byte[4096];
-//            int length;
-//            while ((length = is.read(buffer)) > 0) {
-//                fos.write(buffer, 0, length);
-//            }
-//            return file;
-//        } catch (IOException e) {
-//            return null;
-//        } finally {
-//            if (is != null) {
-//                try {
-//                    is.close();
-//                } catch (IOException e) {
-//                }
-//            }
-//            if (fos != null) {
-//                try {
-//                    fos.close();
-//                } catch (IOException e) {
-//                }
-//            }
-//        }
+        File file = writeImageToDisk(getImageFromNetByUrl(url));
+        return file;
+    }
+    /**
+     * url资源转化为file流
+     *
+     * @param url
+     * @return
+     */
+    public static File urlToPdfFile(URL url) {
+        byte[] bytes = getImageFromNetByUrl(url);
+        File file = writePDFToDisk(bytes);
+        try {
+            PDDocument doc = PDDocument.load(file);
+            PDFRenderer renderer = new PDFRenderer(doc);
+            int pageCount = doc.getNumberOfPages();
+            BufferedImage image = null;
+            for (int i = 0; i < pageCount; i++) {
+                if(i == 0){
+                    image = renderer.renderImageWithDPI(i, 72); // Windows native DPI
+                }
+                else{
+                    BufferedImage tmpImage = renderer.renderImageWithDPI(i, 72);
+                    image = mergeImage(image,tmpImage,false,true,true,0,null);
+                }
+                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
+            }
+            file = new File("templates" + File.separator + "tmpPdf.png");
+            ImageIO.write(image, "png", file);
+//            Thumbnails.of(file) //原图片
+//                    .scale(0.4) //分辨率比例
+//                    .outputQuality(1) //图片质量
+//                    .outputFormat("JPEG") //目标文件格式
+//                    .toFile(file); //目标图片
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+//        bytes = ImgCompressUtil.compressPicForScale(getImageStream(file),2000);
+//        file = readBin2Image(bytes,file);
+        return file;
+    }
+    public static byte[] getImageStream(File file) {
+        byte[] buffer = null;
+        FileInputStream fis;
+        try {
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            fis = new FileInputStream(file);
+            byte[] b = new byte[1024];
+            int n;
+            while ((n = fis.read(b)) != -1) {
+                bos.write(b, 0, n);
+            }
+            fis.close();
+            bos.close();
+            buffer = bos.toByteArray();
+            if(file.exists()) {
+                file.delete();
+            }
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buffer;
+    }
+
+    public static File readBin2Image(byte[] byteArray, File file) {
+        InputStream in = new ByteArrayInputStream(byteArray);
+        FileOutputStream fos = null;
+        try {
+            fos = new FileOutputStream(file);
+            int len = 0;
+            byte[] buf = new byte[1024];
+            while ((len = in.read(buf)) != -1) {
+                fos.write(buf, 0, len);
+            }
+            fos.flush();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (null != fos) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return file;
     }
 
+    /**
+     * 合并两个图片
+     * @param first 图片1
+     * @param second 图片2
+     * @param horizontal 等于true,则两个图片水平合并显示, 否则两个图片垂直合并显示
+     * @param center 图片是否水平居中, horizontal 等于 false 垂直合并才有效
+     * @param transparent 合并后的图片背景是否透明色
+     * @param gap 图片之间的间距
+     * @param color 图片的背景颜色
+     * @return 返回合并之后的图片
+     */
+    public static BufferedImage mergeImage(BufferedImage first, BufferedImage second, boolean horizontal, boolean center, boolean transparent, int gap, Color color) throws IOException {
+        if (first == null) {
+            return second;
+        } else if (second == null) {
+            return first;
+        }
+        // 获取原始图片宽度
+        int firstWidth = first.getWidth();
+        int firstHeight = first.getHeight();
+        int secondWidth = second.getWidth();
+        int secondHeight = second.getHeight();
+        int picGap = gap * 2;
+        // 合并后的图片宽高
+        int mergeWidth = Math.max(firstWidth, secondWidth) + picGap;
+        int mergeHeight = firstHeight + secondHeight + picGap;
+        if (horizontal) {
+            mergeWidth = firstWidth + secondWidth + picGap;
+            mergeHeight = Math.max(firstHeight, secondHeight) + picGap;
+        }
+
+        // 创建目标图片对象
+        BufferedImage target = new BufferedImage(mergeWidth, mergeHeight, BufferedImage.TYPE_INT_RGB);
+        if (transparent) {
+            // 设置图片背景为透明的
+            BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(target);
+            target = config.createCompatibleImage(mergeWidth, mergeHeight, Transparency.TRANSLUCENT);
+        }
+
+        // 创建绘制目标图片对象
+        Graphics2D graphics = target.createGraphics();
+        int x1, y1;
+        int x2, y2;
+        if (horizontal) {
+            // 水平合并
+            x1 = gap;
+            y1 = gap;
+            x2 = firstWidth + gap;
+            y2 = gap;
+        } else {
+            // 垂直合并
+            if (center) {
+                // 计算居中位置
+                x1 = (mergeWidth - firstWidth) / 2;
+                x2 = (mergeWidth - secondWidth) / 2;
+            } else {
+                x1 = gap;
+                x2 = gap;
+            }
+            y1 = gap;
+            y2 = firstHeight + gap;
+        }
+
+        // 图片的背景颜色
+        if (color != null) {
+            graphics.setColor(color);
+            graphics.fillRect(0, 0, mergeWidth, mergeHeight);
+        }
+
+        // 按照顺序绘制图片
+        graphics.drawImage(first, x1, y1, firstWidth, firstHeight, null);
+        graphics.drawImage(second, x2, y2, secondWidth, secondHeight, null);
+        graphics.dispose();
+        // 返回合并后的图片对象
+        return target;
+    }
     /**
      * 将获取的字节数组保存为文件写入硬盘
      *
@@ -1391,7 +1528,29 @@ public class EntityAnalyse {
         }
         return null;
     }
-
+    /**
+     * 将获取的字节数组保存为文件写入硬盘
+     *
+     * @param data
+     */
+    public static File writePDFToDisk(byte[] data) {
+        try {
+            File file = new File("templates" + File.separator + "tmp.pdf"); // 本地目录
+            File fileParent = file.getParentFile();
+            if (!fileParent.exists()) {
+                fileParent.mkdirs();
+                file.createNewFile();
+            }
+            FileOutputStream fops = new FileOutputStream(file);
+            fops.write(data);
+            fops.flush();
+            fops.close();
+            return file;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
     /**
      * 获取远程http地址视图片

+ 75 - 0
winsea-haixin-plugin-wangluohuoyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/util/ImgCompressUtil.java

@@ -0,0 +1,75 @@
+package com.yh.saas.plugin.yiliangyiyun.util;
+
+import net.coobird.thumbnailator.Thumbnails;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+/**
+ * @Author:chengchangjiang
+ * @Description:
+ * @Date:Created in 13:51 2023/2/18
+ */
+public class ImgCompressUtil {
+    private static final Integer ZERO = 0;
+    private static final Integer ONE_ZERO_TWO_FOUR = 1024;
+    private static final Integer NINE_ZERO_ZERO = 900;
+    private static final Integer THREE_TWO_SEVEN_FIVE = 3275;
+    private static final Integer TWO_ZERO_FOUR_SEVEN = 2047;
+    private static final Double ZERO_EIGHT_FIVE = 0.85;
+    private static final Double ZERO_SIX = 0.6;
+    private static final Double ZERO_FOUR_FOUR = 0.44;
+    private static final Double ZERO_FOUR = 0.4;
+    private static final String JPG = ".jpg";
+
+    /**
+     * 根据指定大小压缩图片
+     *
+     * @param imageBytes  源图片字节数组
+     * @param desFileSize 指定图片大小,单位kb
+     * @return 压缩质量后的图片字节数组
+     */
+    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
+        if (imageBytes == null || imageBytes.length <= ZERO || imageBytes.length < desFileSize * ONE_ZERO_TWO_FOUR) {
+            return imageBytes;
+        }
+        long srcSize = imageBytes.length;
+//        double accuracy = getAccuracy(srcSize / ONE_ZERO_TWO_FOUR);
+        double accuracy = 0.85;
+        try {
+            while (imageBytes.length > desFileSize * ONE_ZERO_TWO_FOUR) {
+                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
+                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
+                Thumbnails.of(inputStream)
+                        .scale(accuracy)
+                        .outputQuality(accuracy)
+                        .toOutputStream(outputStream);
+                imageBytes = outputStream.toByteArray();
+            }
+            System.out.println("图片原大小={}kb | 压缩后大小=kb"+srcSize / ONE_ZERO_TWO_FOUR+ imageBytes.length / ONE_ZERO_TWO_FOUR);
+        } catch (Exception e) {
+            System.out.println("【图片压缩】msg=图片压缩失败!"+e);
+        }
+        return imageBytes;
+    }
+
+    /**
+     * 自动调节精度(经验数值)
+     *
+     * @param size 源图片大小
+     * @return 图片压缩质量比
+     */
+    private static double getAccuracy(long size) {
+        double accuracy;
+        if (size < NINE_ZERO_ZERO) {
+            accuracy = ZERO_EIGHT_FIVE;
+        } else if (size < TWO_ZERO_FOUR_SEVEN) {
+            accuracy = ZERO_SIX;
+        } else if (size < THREE_TWO_SEVEN_FIVE) {
+            accuracy = ZERO_FOUR_FOUR;
+        } else {
+            accuracy = ZERO_FOUR;
+        }
+        return accuracy;
+    }
+}