From 9cf4ac9c3f8301f5697f6d12ac4bf32855f0b044 Mon Sep 17 00:00:00 2001
From: yangyang
Date: 星期一, 17 三月 2025 07:54:10 +0800
Subject: [PATCH] #新增 添加质检单管理

---
 rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java |  131 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 124 insertions(+), 7 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java b/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
index ebffe54..146149b 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/common/utils/ExcelUtil.java
@@ -1,26 +1,41 @@
 package com.vincent.rsf.server.common.utils;
 
+import cn.afterturn.easypoi.excel.annotation.Excel;
+import cn.afterturn.easypoi.excel.entity.ImportParams;
+import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
+import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
+import com.google.common.collect.Lists;
+import com.vincent.rsf.common.domain.BeanValidators;
 import com.vincent.rsf.framework.common.Cools;
+import com.vincent.rsf.server.common.handler.AggregationDataHandler;
+import com.vincent.rsf.server.common.handler.ExcelDictHandlerImpl;
+import com.vincent.rsf.server.manager.entity.excel.annotation.ExcelComment;
 import io.swagger.annotations.ApiModelProperty;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
 import java.io.IOException;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * Created by vincent on 2/17/2024
  */
+@Slf4j
 public class ExcelUtil {
-
     public static void build(Workbook workbook, HttpServletResponse response) {
         response.reset();
         Http.cors(response);
@@ -32,6 +47,10 @@
     }
 
     public static <T> Workbook create(List<T> list, Class<T> clz) {
+        return  create(list, clz, false);
+    }
+
+    public static <T> Workbook create(List<T> list, Class<T> clz, boolean flagTemplate) {
         HSSFWorkbook workbook = new HSSFWorkbook();
         Sheet sheet = workbook.createSheet(clz.getSimpleName());
 
@@ -45,6 +64,15 @@
                 continue;
             }
             String memo = "Undefined";
+            if (flagTemplate) {
+                if (field.isAnnotationPresent(ExcelComment.class)) {
+                    memo = field.getAnnotation(ExcelComment.class).value();
+                }
+            } else {
+                if (field.isAnnotationPresent(Excel.class)) {
+                    memo = field.getAnnotation(Excel.class).name();
+                }
+            }
             if (field.isAnnotationPresent(ApiModelProperty.class)) {
                 memo = field.getAnnotation(ApiModelProperty.class).value();
             }
@@ -64,7 +92,8 @@
                     continue;
                 }
 
-                field.setAccessible(true);  // 姝よ寰堥噸瑕侊紝鐗瑰埆鏄瓧娈典负private鏃�
+                // 姝よ寰堥噸瑕侊紝鐗瑰埆鏄瓧娈典负private鏃�
+                field.setAccessible(true);
                 Object value = null;
                 try {
                     value = field.get(t);
@@ -90,6 +119,94 @@
         return workbook;
     }
 
+    /**
+     * 娣诲姞瀵煎叆excel閰嶇疆鍙傛暟
+     * 娉細榛樿閰嶇疆鍙弧瓒冲綋鍓嶉渶姹�
+     * @return
+     */
+    public static ImportParams getDefaultImportParams() {
+        ImportParams importParams = new ImportParams();
+        importParams.setTitleRows(0);
+        importParams.setHeadRows(1);
+        importParams.setSheetNum(1);
+        return importParams;
+    }
+
+    /**
+     * 鏍规嵁 {@code tClass} 鐩稿叧鎴愬憳鍙橀噺鐨� {@link ExcelComment#example()} 瀛楁鍒涘缓妯℃嫙鏁版嵁锛屾殏涓嶆敮鎸� 澶嶆潅绫诲瀷
+     * @param tClass
+     * @return
+     */
+    public static <T> T mockData(Class<T> tClass) {
+        if (tClass == null) {
+            return null;
+        }
+        T instance = null;
+
+        try {
+            instance = tClass.newInstance();
+            Field[] declaredFields = tClass.getDeclaredFields();
+            for (Field declaredField : declaredFields) {
+                ExcelComment comment = declaredField.getAnnotation(ExcelComment.class);
+                if (comment == null) {
+                    continue;
+                }
+                declaredField.setAccessible(true);
+                Class<?> fieldType = declaredField.getType();
+
+                String exampleValue = comment.example();
+                Object value = null;
+
+                if (fieldType == int.class || fieldType == Integer.class) {
+                    value = StringUtils.isBlank(exampleValue) ? 0 : Integer.parseInt(exampleValue);
+                } else if (fieldType == short.class || fieldType == Short.class) {
+                    value = StringUtils.isBlank(exampleValue) ? 0 : Short.parseShort(exampleValue);
+                } else if (fieldType == long.class || fieldType == Long.class) {
+                    value = StringUtils.isBlank(exampleValue) ? 0 : Long.parseLong(exampleValue);
+                } else if (fieldType == double.class || fieldType == Double.class) {
+                    value = StringUtils.isBlank(exampleValue) ? 0 : Double.parseDouble(exampleValue);
+                } else if (fieldType == boolean.class || fieldType == Boolean.class) {
+                    value = StringUtils.isNotBlank(exampleValue) && Boolean.parseBoolean(exampleValue);
+                } else if (fieldType == String.class) {
+                    value = exampleValue;
+                } else if (fieldType == Date.class) {
+                    value = DateUtils.parse(exampleValue);
+                }
+
+                if (value == null && !isBaseType(fieldType)) {
+                    declaredField.set(instance, null);
+                } else {
+                    declaredField.set(instance, value);
+                }
+            }
+        } catch (Exception e) {
+            log.error("鏁版嵁鏋勯�犲け璐ワ紝璇锋煡璇㈣缁嗕俊鎭�", e);
+            return instance;
+        }
+
+        return instance;
+    }
+
+
+    /**
+     * 鏄惁鏄熀纭�鏁版嵁绫诲瀷
+     *
+     * @param className
+     * @return
+     */
+    private static boolean isBaseType(Class<?> className) {
+        if (className.equals(java.lang.Integer.class) ||
+                className.equals(java.lang.Byte.class) ||
+                className.equals(java.lang.Long.class) ||
+                className.equals(java.lang.Double.class) ||
+                className.equals(java.lang.Float.class) ||
+                className.equals(java.lang.Character.class) ||
+                className.equals(java.lang.Short.class) ||
+                className.equals(java.lang.Boolean.class)) {
+            return true;
+        }
+        return false;
+    }
 
 
 }

--
Gitblit v1.9.1