流程 批量导入数据

This commit is contained in:
yuezhihang
2022-03-01 18:01:13 +08:00
parent 9921c6aa9b
commit 3c03a1659f
4 changed files with 279 additions and 41 deletions
@@ -5,10 +5,9 @@ import com.adc.da.excel.poi.excel.entity.enums.ExcelType;
import com.adc.da.slrs.msgDynamicInfo.common.ReadExcel;
import com.adc.da.slrs.processModel.entity.exportExcel;
import com.adc.da.slrs.processModel.entity.importExcelDTO;
import com.adc.da.slrs.processModel.service.IExportService;
import com.adc.da.slrs.processModel.service.impl.IExportServiceImpl;
import com.adc.da.slrs.processModel.utils.ExcelExportUtilByWk;
import com.adc.da.slrs.processModel.utils.ExcelImportUtilByWk;
import com.adc.da.slrs.standardSplit.entity.SarFileSplitInfoEO;
import com.adc.da.slrs.standardSplit.entity.SarFileSplitItemsEO;
import com.adc.da.sys.service.IDicTypeEOService;
import com.adc.da.util.exception.AdcDaBaseException;
import com.adc.da.util.http.ResponseMessage;
@@ -25,7 +24,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -37,6 +35,8 @@ public class exportWkExcel {
@Autowired
private IDicTypeEOService dicTypeEOService;
@Autowired
private IExportServiceImpl exportService;
@ApiOperation(value = "|SysCorpEO|导出excel")
@PostMapping(value = "/exportqbzxdjh")
@@ -67,11 +67,13 @@ public class exportWkExcel {
@ApiOperation(value = "|SarFileSplitItemsEO|导入")
@PostMapping(value = "/importData")
public ResponseMessage<List<importExcelDTO>> importSplitItemsZip(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
List<importExcelDTO> res =new ArrayList<>();
Map<String, Object> dicTypeEO = dicTypeEOService.getDicTypeListCode();
ExcelImportUtilByWk.readExcel(file);
return Result.success(res);
StringBuilder stringBuilder=new StringBuilder();
List<importExcelDTO> res=exportService.delWithWkData(file,stringBuilder);
if (stringBuilder.toString().length()>0){
return Result.error(stringBuilder.toString());
}else {
return Result.success(res);
}
}
}
@@ -0,0 +1,12 @@
package com.adc.da.slrs.processModel.service;
import com.adc.da.slrs.processModel.entity.importExcelDTO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface IExportService {
List<importExcelDTO> delWithWkData(MultipartFile file ,StringBuilder stringBuilder);
}
@@ -0,0 +1,109 @@
package com.adc.da.slrs.processModel.service.impl;
import com.adc.da.slrs.processModel.entity.importExcelDTO;
import com.adc.da.slrs.processModel.service.IExportService;
import com.adc.da.slrs.processModel.utils.DateUtil;
import com.adc.da.slrs.processModel.utils.ExcelImportUtilByWk;
import com.adc.da.slrs.sarInstitution.entity.TsInstitution;
import com.adc.da.slrs.sarInstitution.service.ITsInstitutionService;
import com.adc.da.slrs.sarUser.entity.TsUser;
import com.adc.da.slrs.sarUser.service.impl.TsUserServiceImpl;
import com.adc.da.sys.service.IDicTypeEOService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class IExportServiceImpl implements IExportService {
//查询数据字典
@Autowired
private IDicTypeEOService dicTypeEOService;
//机构数据
@Autowired
private ITsInstitutionService tsInstitutionService;
//人员数据
@Autowired
private TsUserServiceImpl tsUserService;
@Override
public List<importExcelDTO> delWithWkData(MultipartFile file ,StringBuilder stringBuilder) {
// //获取数据字典数据
// Map<String, Object> dicTypeEO = dicTypeEOService.getDicTypeListCode();
//获取机构数据
List<TsInstitution> tsInstitutions=tsInstitutionService.list();
//获取人员数据
QueryWrapper<TsUser> tsUserQueryWrapper=new QueryWrapper<>();
tsUserQueryWrapper.eq("VALID_FLAG","0");
List<TsUser> tsUsers=tsUserService.list(tsUserQueryWrapper);
//获取institution工具集合
Map<String,String> tsInstitutionMap=new HashMap<>();
tsInstitutions.forEach(tsInstitution -> {
tsInstitutionMap.put(tsInstitution.getName(),tsInstitution.getId());
});
//获取user工具集合
Map<String,String> tsUserMap=new HashMap<>();
tsUsers.forEach(tsUser -> {
tsUserMap.put(tsUser.getUserId(),tsUser.getUname());
});
List<importExcelDTO> res= ExcelImportUtilByWk.readExcel(file);
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
int i=1;
for (importExcelDTO importExcelDTO: res) {
importExcelDTO.setWgLeaderName(tsUserMap.get(importExcelDTO.getWgLeader()));
if (null!=importExcelDTO.getWgLeader() && null==tsUserMap.get(importExcelDTO.getWgLeader())){
stringBuilder.append(""+i+"行第7列工作组组长填写不符合格式要求请检查;");
}
importExcelDTO.setUnitName(tsInstitutionMap.get(importExcelDTO.getUnit()));
if (null!=importExcelDTO.getUnit() && null==tsInstitutionMap.get(importExcelDTO.getUnit())){
stringBuilder.append(""+i+"行第4列起草责任单位填写不符合格式要求请检查;");
}
importExcelDTO.setDrafterName(tsUserMap.get(importExcelDTO.getDrafter()));
if (null!=importExcelDTO.getDrafter() && null==tsUserMap.get(importExcelDTO.getDrafter())){
stringBuilder.append(""+i+"行第5列起草人填写不符合格式要求请检查;");
}
importExcelDTO.setResPersonName(tsUserMap.get(importExcelDTO.getResPerson()));
if (null!=importExcelDTO.getResPerson() && null==tsUserMap.get(importExcelDTO.getResPerson())){
stringBuilder.append(""+i+"行第6列评审责任人填写不符合格式要求请检查;");
}
try {
if (null!=importExcelDTO.getEndTime()) {
importExcelDTO.setEndTime(formater.format(HSSFDateUtil.getJavaDate(Double.valueOf(importExcelDTO.getEndTime()))));
}
}catch (Exception e){
stringBuilder.append(""+i+"行第10列立项完成时间填写不符合格式要求请检查;");
}
try {
if (null!=importExcelDTO.getReleaseTime()) {
importExcelDTO.setReleaseTime(formater.format(HSSFDateUtil.getJavaDate(Double.valueOf(importExcelDTO.getReleaseTime()))));
}
}catch (Exception e){
stringBuilder.append(""+i+"行第9列计划标准发布时间填写不符合格式要求请检查;");
}
try {
if (null!=importExcelDTO.getDraftTime()) {
importExcelDTO.setDraftTime(formater.format(HSSFDateUtil.getJavaDate(Double.valueOf(importExcelDTO.getDraftTime()))));
}
}catch (Exception e){
stringBuilder.append(""+i+"行第8列计划初稿完成日期填写不符合格式要求请检查;");
}
i++;
}
return res;
}
}
@@ -30,7 +30,7 @@ public class ExcelImportUtilByWk {
* @param file 上传的Excel文件
* @return 读取结果列表,读取失败时返回null
*/
public static List<importExcelDTO> readExcel(MultipartFile file) {
public static List<Class<?>> readExcelPbulic(MultipartFile file) {
Workbook workbook = null;
@@ -47,7 +47,8 @@ public class ExcelImportUtilByWk {
workbook = getWorkbook(file.getInputStream(), fileType);
// 读取excel中的数据
List<importExcelDTO> resultDataList = parseExcel(workbook);
//不同需求条件下可以 在此处修改 来获取不同的对象
List<Class<?>> resultDataList = parseExcelPublic(workbook);
return resultDataList;
} catch (Exception e) {
@@ -65,7 +66,6 @@ public class ExcelImportUtilByWk {
}
}
/**
* 根据文件后缀名类型获取对应的工作簿对象
* @param inputStream 读取文件的输入流
@@ -84,6 +84,120 @@ public class ExcelImportUtilByWk {
}
/**
* 解析Excel数据
* @param workbook Excel工作簿对象
* @return 解析结果
*/
private static List<Class<?>> parseExcelPublic(Workbook workbook) {
List<Class<?>> resultDataList = new ArrayList<>();
// 解析sheet
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
Sheet sheet = workbook.getSheetAt(sheetNum);
if(!"hidden".equals(sheet.getSheetName())) {
// 校验sheet是否合法
if (sheet == null) {
continue;
}
// 获取第一行数据
int firstRowNum = sheet.getFirstRowNum();
Row firstRow = sheet.getRow(firstRowNum);
if (null == firstRow) {
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
}
// 解析每一行的数据,构造数据对象
int rowStart = firstRowNum + 1;
int rowEnd = sheet.getPhysicalNumberOfRows();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row row = sheet.getRow(rowNum);
if (null == row) {
continue;
}
Class<?> resultData = convertRowToDataPublic(row);
if (null == resultData) {
log.warn("" + row.getRowNum() + "行数据不合法,已忽略!");
continue;
}
resultDataList.add(resultData);
}
}
}
return resultDataList;
}
/**
* 提取每一行中需要的数据,构造成为一个结果数据对象
*
* 当该行中有单元格的数据为空或不合法时,忽略该行的数据
*
* @param row 行数据
* @return 解析后的行数据对象,行数据错误时返回null
*/
private static Class<?> convertRowToDataPublic(Row row) {
Class<?> resultData = null;
Cell cell;
int cellNum = 0;
// 获取企标类型
cell = row.getCell(cellNum++);
String bussSort = convertCellValueToString(cell);
// resultData.setBussSort(bussSort);
return resultData;
}
/**
* 读取Excel文件内容
* @param file 上传的Excel文件
* @return 读取结果列表,读取失败时返回null
*/
public static List<importExcelDTO> readExcel(MultipartFile file) {
Workbook workbook = null;
try {
// 获取Excel后缀名
String fileName = file.getOriginalFilename();
if (fileName == null || fileName.isEmpty() || fileName.lastIndexOf(".") < 0) {
log.warn("解析Excel失败,因为获取到的Excel文件名非法!");
return null;
}
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
// 获取Excel工作簿
workbook = getWorkbook(file.getInputStream(), fileType);
// 读取excel中的数据
//不同需求条件下可以 在此处修改 来获取不同的对象
List<importExcelDTO> resultDataList = parseExcel(workbook);
return resultDataList;
} catch (Exception e) {
log.warn("解析Excel失败,文件名:" + file.getOriginalFilename() + " 错误信息:" + e.getMessage());
return null;
} finally {
try {
if (null != workbook) {
workbook.close();
}
} catch (Exception e) {
log.warn("关闭数据流出错!错误信息:" + e.getMessage());
return null;
}
}
}
/**
* 解析Excel数据
* @param workbook Excel工作簿对象
@@ -94,35 +208,36 @@ public class ExcelImportUtilByWk {
// 解析sheet
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
Sheet sheet = workbook.getSheetAt(sheetNum);
// 校验sheet是否合法
if (sheet == null) {
continue;
}
// 获取第一行数据
int firstRowNum = sheet.getFirstRowNum();
Row firstRow = sheet.getRow(firstRowNum);
if (null == firstRow) {
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
}
// 解析每一行的数据,构造数据对象
int rowStart = firstRowNum + 1;
int rowEnd = sheet.getPhysicalNumberOfRows();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row row = sheet.getRow(rowNum);
if (null == row) {
if(!"hidden".equals(sheet.getSheetName())) {
// 校验sheet是否合法
if (sheet == null) {
continue;
}
importExcelDTO resultData = convertRowToData(row);
if (null == resultData) {
log.warn("" + row.getRowNum() + "行数据不合法,已忽略!");
continue;
// 获取第一行数据
int firstRowNum = sheet.getFirstRowNum();
Row firstRow = sheet.getRow(firstRowNum);
if (null == firstRow) {
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
}
// 解析每一行的数据,构造数据对象
int rowStart = firstRowNum + 1;
int rowEnd = sheet.getPhysicalNumberOfRows();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row row = sheet.getRow(rowNum);
if (null == row) {
continue;
}
importExcelDTO resultData = convertRowToData(row);
if (null == resultData) {
log.warn("" + row.getRowNum() + "行数据不合法,已忽略!");
continue;
}
resultDataList.add(resultData);
}
resultDataList.add(resultData);
}
}
@@ -158,19 +273,19 @@ public class ExcelImportUtilByWk {
//获取起草责任单位
cell = row.getCell(cellNum++);
String unitName = convertCellValueToString(cell);
resultData.setUnitName(unitName);
resultData.setUnit(unitName);
//获取起草人
cell = row.getCell(cellNum++);
String drafterName = convertCellValueToString(cell);
resultData.setDrafterName(drafterName);
resultData.setDrafter(drafterName);
//获取评审责任人
cell = row.getCell(cellNum++);
String resPersonName = convertCellValueToString(cell);
resultData.setResPersonName(resPersonName);
resultData.setResPerson(resPersonName);
//获取工作组组长
cell = row.getCell(cellNum++);
String wgLeaderName = convertCellValueToString(cell);
resultData.setWgLeaderName(wgLeaderName);
resultData.setWgLeader(wgLeaderName);
//获取计划初稿完成日期
cell = row.getCell(cellNum++);
String draftTime = convertCellValueToString(cell);