浏览代码

加了个一次性的导入方法

jz.kai 4 年之前
父节点
当前提交
114c090ab8

+ 2 - 1
web/src/main/java/com/jpsoft/insurance/modules/open/ReportApiController.java

@@ -96,7 +96,8 @@ public class ReportApiController {
 
             if (affectCount > 0) {
                 msgResult.setResult(true);
-                msgResult.setData(report);
+                msgResult.setMessage("数据库添加成功");
+//                msgResult.setData(report);
             } else {
                 msgResult.setResult(false);
                 msgResult.setMessage("数据库添加失败");

+ 96 - 0
web/src/main/java/com/jpsoft/insurance/modules/sys/controller/DataDictionaryController.java

@@ -1,21 +1,31 @@
 package com.jpsoft.insurance.modules.sys.controller;
 
 import com.github.pagehelper.Page;
+import com.jpsoft.insurance.config.OSSConfig;
 import com.jpsoft.insurance.modules.common.dto.MessageResult;
 import com.jpsoft.insurance.modules.common.dto.Sort;
+import com.jpsoft.insurance.modules.common.utils.OSSUtil;
+import com.jpsoft.insurance.modules.common.utils.POIUtils;
 import com.jpsoft.insurance.modules.common.utils.PojoUtils;
 import com.jpsoft.insurance.modules.sys.entity.DataDictionary;
+import com.jpsoft.insurance.modules.sys.entity.User;
 import com.jpsoft.insurance.modules.sys.service.DataDictionaryService;
+import com.jpsoft.insurance.modules.sys.service.UserService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.util.*;
 
 @RestController
@@ -26,6 +36,10 @@ public class DataDictionaryController {
 
     @Autowired
     private DataDictionaryService dataDictionaryService;
+    @Autowired
+    private UserService userService;
+    @Autowired
+    private OSSConfig ossConfig;
 
     @ApiOperation(value="创建空记录")
     @GetMapping("create")
@@ -306,4 +320,86 @@ public class DataDictionaryController {
 
         return messageResult;
     }
+
+    @ApiOperation(value="导入用户")
+    @PostMapping("importXls")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "uploadFile",value = "上传文件", required = true,paramType="form", dataType = "__file")
+    })
+    public MessageResult<String> importXls(MultipartFile uploadFile, @RequestAttribute String subject){
+        User user = userService.get(subject);
+
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            POIUtils poiUtils = new POIUtils(uploadFile.getInputStream());
+            int sheetIndex = 0;
+            Sheet sheet1 = poiUtils.getSheetAt(sheetIndex);
+
+            int affectCount = 0;
+            int failCount = 0;
+            int validateColIndex = 4;
+
+            for(int rowIndex=1; rowIndex<=sheet1.getLastRowNum(); rowIndex++){
+                try {
+                    String sortNo = (String)poiUtils.getCellValue(sheetIndex,rowIndex,0).toString().replace(" ","");
+                    String value = (String)poiUtils.getCellValue(sheetIndex,rowIndex,1).toString().replace(" ","");
+//                    String position = (String)poiUtils.getCellValue(sheetIndex,rowIndex,2).toString().replace(" ","");
+                    String name = (String)poiUtils.getCellValue(sheetIndex,rowIndex,3).toString().replace(" ","");
+
+                    DataDictionary dataDictionary = new DataDictionary();
+                    dataDictionary.setId(UUID.randomUUID().toString());
+                    dataDictionary.setName(name);
+                    dataDictionary.setValue(value);
+                    dataDictionary.setSortNo(Integer.parseInt(sortNo));
+                    dataDictionary.setParentId("1775997f-b69d-41ea-83ac-83f345be5d97");
+                    dataDictionary.setDataType("2");
+                    dataDictionary.setDelFlag(false);
+                    dataDictionary.setCreateBy(subject);
+                    dataDictionary.setCreateDate(new Date());
+                    dataDictionaryService.insert(dataDictionary);
+
+                    affectCount++;
+                }
+                catch(Exception innerEx){
+                    sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("错误!");
+                    failCount++;
+                    logger.error(innerEx.getMessage(),innerEx);
+                }
+            }
+
+            if (failCount>0){
+                //有导入失败的记录
+                msgResult.setResult(false);
+                msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。");
+
+                //todo 只保留错误数据的sheet
+                Workbook wb = poiUtils.exportErrorXls(sheetIndex,validateColIndex,1 + affectCount + failCount);
+
+                //todo 将wb保存到oss
+                ByteArrayOutputStream output = new ByteArrayOutputStream();
+                wb.write(output);
+
+                byte[] buffer = output.toByteArray();
+                ByteArrayInputStream input = new ByteArrayInputStream(buffer);
+
+                String downloadUrl = OSSUtil.upload(ossConfig,"import","error.xls",input);
+
+                //todo 返回导入失败报表下载链接
+                msgResult.setData(downloadUrl);
+            }
+            else{
+                msgResult.setResult(true );
+                msgResult.setMessage("数据成功导入" + affectCount + "条");
+            }
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
 }