Преглед на файлове

fix:修改excel对应接口地址,以及导入数据的校验,只会存储主表的数据,导出模版限制

韩帛霖 преди 1 година
родител
ревизия
6323518567

+ 0 - 18
ruoyi-admin/src/main/java/com/ruoyi/web/controller/dragForm/CommonController.java

@@ -75,25 +75,7 @@ public class CommonController extends BaseController {
 
     }
 
-    /**
-     * 通用导出excel
-     */
-    @PostMapping("/export")
-    public void export(HttpServletResponse response, CommonEntity commonEntity) throws Exception {
-        commonService.export(response, commonEntity);
-    }
 
-    /**
-     * 通用导出excel模版
-     *
-     * @param response
-     * @param tableName
-     * @throws Exception
-     */
-    @PostMapping("/exportTemplate")
-    public void exportTemplate(HttpServletResponse response, String tableName, String sqlkey) throws Exception {
-        commonService.exportTemplate(response, tableName, sqlkey);
-    }
 
     /**
      * 通用动态表单详情

+ 45 - 7
ruoyi-admin/src/main/java/com/ruoyi/web/controller/dragForm/CommonFileController.java

@@ -45,10 +45,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import org.apache.poi.ss.usermodel.*;
 
@@ -79,6 +76,26 @@ public class CommonFileController {
 
     private static final String FILE_DELIMETER = ",";
 
+    /**
+     * 通用导出excel
+     */
+    @PostMapping("/export")
+    public void export(HttpServletResponse response, CommonEntity commonEntity) throws Exception {
+        commonService.export(response, commonEntity);
+    }
+
+    /**
+     * 通用导出excel模版
+     *
+     * @param response
+     * @param tableName
+     * @throws Exception
+     */
+    @PostMapping("/exportTemplate")
+    public void exportTemplate(HttpServletResponse response, String tableName, String sqlkey) throws Exception {
+        commonService.exportTemplate(response, tableName, sqlkey);
+    }
+
     /**
      * 通用下载请求
      *
@@ -194,16 +211,38 @@ public class CommonFileController {
         try {
             // 创建一个工作簿对象 解析上传的excel表数据
             List<Map<String, String>> listMap = convertList(EasyExcel.read(file.getInputStream()).sheet().headRowNumber(0).doReadSync());
+
+            if (listMap.size() <= 1) {
+                return AjaxResult.warn("请检查表格中的数据!");
+            }
+
             // 得到当前导出入数据的格式k/v
             TableSql tableSql = iTableSqlService.selectTableSqlByTSqlKey(sqlKey);
             // 存储 字段描述:字段列名
             Map<String, Object> fieldMap = (Map<String, Object>) JSON.parse(tableSql.getTableExportField());
             Map<String, Object> endFieldMap = new HashMap<>();
+            // 处理导入title对应问题,双重校验
             fieldMap.forEach((mKey, mVal) -> {
-                endFieldMap.put(mVal.toString(), mKey);
+                if (!mKey.contains("@")) {
+                    endFieldMap.put(mVal.toString(), mKey);
+                } else {
+                    // 定义新的title map集合
+                    Map<String, String> map = listMap.get(0);
+                    // 根据val值删除当前map中的k/v(此处是title,理论上不会有重复的val存在)
+                    map.values().removeIf(value -> value.contains(mVal.toString()));
+                    // 删除旧title
+                    listMap.remove(0);
+                    // 替换新title
+                    listMap.add(0, map);
+                }
             });
+            if (listMap.get(0).size() != listMap.get(2).size()) {
+                return AjaxResult.error("请检查表格中的数据是否正确!");
+            }
             listMap.get(0).forEach((mKey, mVal) -> {
-                if (mVal != null) listMap.get(0).put(mKey, endFieldMap.get(mVal).toString());
+                if (mVal != null)
+                    if (endFieldMap.get(mVal) != null)
+                        listMap.get(0).put(mKey, endFieldMap.get(mVal).toString());
             });
             // 挣个excel文件数据
             listMap.forEach(item -> {
@@ -225,7 +264,6 @@ public class CommonFileController {
         }
     }
 
-
     // map类型转换
     public static List<Map<String, String>> convertList(List<Map<Integer, String>> inputList) {
         List<Map<String, String>> resultList = new ArrayList<>();

+ 4 - 2
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CommonServiceImpl.java

@@ -180,7 +180,7 @@ public class CommonServiceImpl implements ICommonService {
         //字段
         List<String> fieldList = new ArrayList<>();
         for (Map.Entry<String, String> entry : map.entrySet()) {
-            fieldList.add(entry.getKey());
+            fieldList.add(entry.getKey().replace("@", "_"));
             titleList.add(entry.getValue() == null ? "" : entry.getValue());
         }
         // 创建表头
@@ -217,7 +217,9 @@ public class CommonServiceImpl implements ICommonService {
         XSSFSheet sheet = workbook.createSheet("Sheet1");
         List<String> titleList = new ArrayList<>();
         for (Map.Entry<String, String> entry : map.entrySet()) {
-            titleList.add(entry.getValue() == null ? "" : entry.getValue());
+            if (!entry.getKey().contains("@")) {
+                titleList.add(entry.getValue() == null ? "" : entry.getValue());
+            }
         }
         // 创建表头
         XSSFRow header = sheet.createRow(0);