From f5041d3a0a706809bc351ff6d0694935f33abc9f Mon Sep 17 00:00:00 2001
From: value <z1292839451@163.com>
Date: 星期一, 15 四月 2024 07:17:11 +0800
Subject: [PATCH] 新增报告插入二维码

---
 system-run/src/main/resources/application-dev.yml                                      |    7 ++-
 framework/pom.xml                                                                      |    6 +++
 framework/src/main/java/com/yuanchu/mom/utils/MatrixToImageWriter.java                 |   72 ++++++++++++++++++++++++++++++++++++
 inspect-server/src/main/resources/mapper/InsOrderMapper.xml                            |    8 ++--
 inspect-server/src/main/resources/static/report-template.docx                          |    0 
 inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java |   20 +++++++++
 6 files changed, 105 insertions(+), 8 deletions(-)

diff --git a/framework/pom.xml b/framework/pom.xml
index f69481d..c56bafc 100644
--- a/framework/pom.xml
+++ b/framework/pom.xml
@@ -39,5 +39,11 @@
             <artifactId>java-jwt</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.3.3</version>
+        </dependency>
+
     </dependencies>
 </project>
diff --git a/framework/src/main/java/com/yuanchu/mom/utils/MatrixToImageWriter.java b/framework/src/main/java/com/yuanchu/mom/utils/MatrixToImageWriter.java
new file mode 100644
index 0000000..cd217c2
--- /dev/null
+++ b/framework/src/main/java/com/yuanchu/mom/utils/MatrixToImageWriter.java
@@ -0,0 +1,72 @@
+package com.yuanchu.mom.utils;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.imageio.ImageIO;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+import com.yuanchu.mom.exception.ErrorException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+/**
+ * 閰嶇疆鍥惧儚鍐欏叆鍣�
+ *
+ * @author z1292
+ *
+ */
+public class MatrixToImageWriter {
+    private final int BLACK = 0xFF000000;
+    private final int WHITE = 0xFFFFFFFF;
+
+    private BufferedImage toBufferedImage(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        for (int x = 0; x < width; x++) {
+            for (int y = 0; y < height; y++) {
+                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
+            }
+        }
+        return image;
+    }
+
+    private void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
+        BufferedImage image = toBufferedImage(matrix);
+        if (!ImageIO.write(image, format, file)) {
+            throw new ErrorException("Could not write an image of format " + format + " to " + file);
+        }
+    }
+
+    public String code(String content, String path) {
+        try {
+            String codeName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy_MM_dd&HH_mm_ss"));// 浜岀淮鐮佺殑鍥剧墖鍚�
+            String imageType = "jpg";// 鍥剧墖绫诲瀷
+            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
+            Map<EncodeHintType, Object> hints = new HashMap<>();
+            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
+            hints.put(EncodeHintType.MARGIN, 0);
+            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
+            File file1 = new File(path, codeName + "." + imageType);
+            writeToFile(bitMatrix, imageType, file1);
+            return file1.getPath();
+        } catch (WriterException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        throw new ErrorException("浜岀淮鐮佺敓鎴愬け璐�");
+    }
+
+}
\ No newline at end of file
diff --git a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java
index 36c8f4a..41312ec 100644
--- a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java
+++ b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java
@@ -27,6 +27,8 @@
 import com.yuanchu.mom.service.InsOrderService;
 import com.yuanchu.mom.service.StandardTemplateService;
 import com.yuanchu.mom.utils.GiveCode;
+import com.yuanchu.mom.utils.JackSonUtil;
+import com.yuanchu.mom.utils.MatrixToImageWriter;
 import com.yuanchu.mom.utils.QueryWrappers;
 import com.yuanchu.mom.vo.InsOrderPlanVO;
 import org.apache.poi.xwpf.usermodel.TableRowAlign;
@@ -76,6 +78,9 @@
 
     @Value("${wordUrl}")
     private String wordUrl;
+
+    @Value("${twoCode}")
+    private String twoCode;
 
     @Resource
     private GiveCode giveCode;
@@ -419,6 +424,18 @@
                 table.put("tableSize", tables.size() + 1);
             });
             List<Map<String, String>> deviceList = insOrderMapper.selectDeviceList(deviceSet);
+            Map<String, String> codeStr = new HashMap<>();
+            codeStr.put("鎶ュ憡缂栧彿", insReport.getCode());
+            codeStr.put("鏍峰搧鍚嶇О", insOrder.getSample());
+            codeStr.put("瑙勬牸鍨嬪彿", samples.get(0).getModel());
+            codeStr.put("鍙戞斁鏃ユ湡", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            String codePath;
+            try {
+                codePath = new MatrixToImageWriter().code(JackSonUtil.marshal(codeStr).replaceAll("\\{","")
+                        .replaceAll("}","").replaceAll(",","").replaceAll("\"",""), twoCode);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
             ConfigureBuilder builder = Configure.builder();
             builder.useSpringEL(true);
             XWPFTemplate template = XWPFTemplate.compile(url, builder.build()).render(
@@ -432,9 +449,10 @@
                         put("tableSize", tables.size() + 1);
                         put("standardMethod", standardMethod2);
                         put("deviceList", deviceList);
+                        put("twoCode", Pictures.ofLocal(codePath).create());
                     }});
             try {
-                String name = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy_MM_dd_HH_mm_ss")) + ".docx";
+                String name = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy_MM_dd&HH_mm_ss")) + ".docx";
                 template.writeAndClose(Files.newOutputStream(Paths.get(wordUrl + "/" + name)));
                 insReport.setUrl("/word/" + name);
                 insReportMapper.insert(insReport);
diff --git a/inspect-server/src/main/resources/mapper/InsOrderMapper.xml b/inspect-server/src/main/resources/mapper/InsOrderMapper.xml
index 77a58f3..b11f9b9 100644
--- a/inspect-server/src/main/resources/mapper/InsOrderMapper.xml
+++ b/inspect-server/src/main/resources/mapper/InsOrderMapper.xml
@@ -142,11 +142,11 @@
             ${ew.customSqlSegment}
         </if>
     </select>
-    <select id="selectDeviceList" resultType="java.util.Map" parameterType="java.util.Set">
-        select device_name,specification_model,factory_no,latest_traceability from device
+    <select id="selectDeviceList" resultType="java.util.Map">
+        select device_name,specification_model,factory_no,date_format(latest_traceability, '%Y-%m-%d') latest_traceability from device
         where device_name in
-        <foreach collection="array" open="(" separator="," close=")" item="val">
-            ${val}
+        <foreach collection="names" index="index" open="(" separator="," close=")" item="val">
+            #{val}
         </foreach>
     </select>
 
diff --git a/inspect-server/src/main/resources/static/report-template.docx b/inspect-server/src/main/resources/static/report-template.docx
index 38de9fa..451e7e9 100644
--- a/inspect-server/src/main/resources/static/report-template.docx
+++ b/inspect-server/src/main/resources/static/report-template.docx
Binary files differ
diff --git a/system-run/src/main/resources/application-dev.yml b/system-run/src/main/resources/application-dev.yml
index 2b36d65..7b63943 100644
--- a/system-run/src/main/resources/application-dev.yml
+++ b/system-run/src/main/resources/application-dev.yml
@@ -5,7 +5,7 @@
 logging:
   config: classpath:logback-spring.xml
   # 鏃ュ織瀛樺偍璺緞+++++++++++++++++++++++++++杩愮淮闇�瑕侀厤缃�+++++++++++++++++++++++++++
-  file-location: D:\Download\log
+  file-location: D:\椤圭洰鏂囦欢瀛樺偍\log
 
 # 鏁版嵁搴撳浠借矾寰�
 backup:
@@ -18,11 +18,12 @@
 
 # 鐓х墖瀛樺偍璺緞+++++++++++++++++++++++++++杩愮淮闇�瑕侀厤缃�+++++++++++++++++++++++++++
 file:
-  path: D:\Download\鍏徃瀛樺偍鍦板潃
+  path: D:\椤圭洰鏂囦欢瀛樺偍\image
   # 涓婁紶鏂囦欢鍏佽鐨勬墿灞曞悕
   allowed: png,jpg,jpeg,gif
 
-wordUrl: D:\Download\鍏徃瀛樺偍鍦板潃
+wordUrl: D:\椤圭洰鏂囦欢瀛樺偍\word
+twoCode: D:\椤圭洰鏂囦欢瀛樺偍\two_code
 
 mybatis-plus:
   type-aliases-package: com.yuanchu.mom.pojo

--
Gitblit v1.9.3