From 68696c83a00fdf7a0846b989c8692fff7427b721 Mon Sep 17 00:00:00 2001 From: lenovo Date: Fri, 18 Nov 2022 16:19:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=A5=E6=94=B9=E7=9A=84=E9=83=BD=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gmh/controller/ExcelController.java | 72 ++++++++++++------- .../controller/GlobalExceptionController.java | 11 ++- src/main/java/com/gmh/dao/ExcelDao.java | 13 ++++ .../entity/query/DataSourceImportQuery.java | 15 ++++ .../com/gmh/entity/vo/ExcelDownloadVo.java | 21 ++++++ .../com/gmh/exception/ServiceException.java | 64 +++++++++++++++++ .../java/com/gmh/service/ExcelService.java | 10 +++ .../gmh/service/impl/ExcelServiceImpl.java | 22 ++++++ src/main/resources/mapper/ExcelDao.xml | 25 +++++++ 9 files changed, 225 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/gmh/dao/ExcelDao.java create mode 100644 src/main/java/com/gmh/entity/query/DataSourceImportQuery.java create mode 100644 src/main/java/com/gmh/entity/vo/ExcelDownloadVo.java create mode 100644 src/main/java/com/gmh/exception/ServiceException.java create mode 100644 src/main/java/com/gmh/service/ExcelService.java create mode 100644 src/main/java/com/gmh/service/impl/ExcelServiceImpl.java create mode 100644 src/main/resources/mapper/ExcelDao.xml diff --git a/src/main/java/com/gmh/controller/ExcelController.java b/src/main/java/com/gmh/controller/ExcelController.java index e4fe9a8..9e7ecae 100644 --- a/src/main/java/com/gmh/controller/ExcelController.java +++ b/src/main/java/com/gmh/controller/ExcelController.java @@ -4,10 +4,14 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gmh.entity.vo.ExcelDownloadVo; +import com.gmh.exception.ServiceException; import com.gmh.entity.*; import com.gmh.entity.properties.FileServerProperties; +import com.gmh.entity.query.DataSourceImportQuery; import com.gmh.entity.query.ExecuteTemplateQuery; import com.gmh.entity.vo.ReadNameVo; +import com.gmh.service.ExcelService; import com.gmh.service.FileService; import com.gmh.service.GmhFileLogService; import com.gmh.service.SysBaijiaxingService; @@ -51,33 +55,29 @@ public class ExcelController { @Autowired private HttpServletRequest request; - /** - * HashMap的Key为SessionId - * - * 通过SessionId为每一个会话保存自己的对象副本 - * - * 不适用于集群环境 - */ - private final Map excelInfoMap = new HashMap<>(); + @Autowired + private ExcelService excelService; private static final String KEY_XINGMING = "姓名"; + private static final String EXCEL_INFO_KEY = "excel_info"; + @RequestMapping("/t01") public R test01() { return R.ok("ok"); } @RequestMapping("/dataSourceImport") - public R dataSourceImport(MultipartFile file, String dataSourceImportModel) throws IOException { - excelInfoMap.put(request.getSession().getId(), new ExcelInfo()); - DataSourceImportModel importModel = DataSourceImportModel.findEnum(dataSourceImportModel); + public R dataSourceImport(MultipartFile file, DataSourceImportQuery query) throws IOException { + request.getSession().setAttribute(EXCEL_INFO_KEY, new ExcelInfo()); + DataSourceImportModel importModel = DataSourceImportModel.findEnum(query.getDataSourceImportModel()); Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); FileEntity fileEntity = fileService.uploadFile(file); gmhFileLogService.recordLog(fileEntity, GmhFileLog.OperateType.D_S_IMP); if (DataSourceImportModel.AUTO_NAME.equals(importModel)) { List readNameVoList = new ArrayList<>(); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { - ReadNameVo readNameVo = new ReadNameVo(); + ReadNameVo readNameVo; Sheet sheet = workbook.getSheetAt(i); List> excelMaps = POIExcelUtil.toListMap(sheet); if (excelMaps.isEmpty()) { @@ -96,6 +96,7 @@ public class ExcelController { } readNameVo = getReadNameDataForNameKey(excelMaps, xmKey); } else { + // map中存的是可能是姓名的列数据,key为列头,value为该列数据中姓名的个数 Map map = new HashMap<>(); for (Map excelMap : excelMaps) { for (String key : excelMap.keySet()) { @@ -106,21 +107,25 @@ public class ExcelController { } } } - List> list = new ArrayList<>(map.entrySet()); + if (map.size() == 0) { + throw new ServiceException("该Excel文件中未发现姓名列"); + } + List> list = new ArrayList<>(map.entrySet()); // list.sort(Comparator.comparingInt(Map.Entry::getValue)); //升序 - list.sort((o1, o2) -> (o2.getValue() - o1.getValue())); + list.sort((o1, o2) -> (o2.getValue() - o1.getValue())); // 降序 + // 取第一个元素,也就是可能是姓名列中权重最大的列,即最有可能是姓名的一列 String key = list.get(0).getKey(); readNameVo = getReadNameDataForNameKey(excelMaps, key); } readNameVo.setSheetName(sheet.getSheetName()); readNameVoList.add(readNameVo); } - ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); + ExcelInfo excelInfo = (ExcelInfo) request.getSession().getAttribute(EXCEL_INFO_KEY); excelInfo.setReadNameVoList(readNameVoList); return R.ok().setData(readNameVoList); } - return R.error(); + return R.error("这个功能还没做"); } @@ -148,19 +153,20 @@ public class ExcelController { /** * 模板导入 - * @param file excel文件 + * + * @param file excel文件 * @param templateSheetNum 模板所在Sheet页 * @return * @throws IOException */ @RequestMapping("/template") - public R templateUpload(MultipartFile file, String templateSheetNum) throws IOException { + public R templateUpload(MultipartFile file, Integer templateSheetNum) throws IOException { Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename()); FileEntity fileEntity = fileService.uploadFile(file); gmhFileLogService.recordLog(fileEntity, GmhFileLog.OperateType.TEMPL_IMP); - ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); + ExcelInfo excelInfo = (ExcelInfo) request.getSession().getAttribute(EXCEL_INFO_KEY); excelInfo.setTemplateOriginalFilename(file.getOriginalFilename()); - excelInfo.setTemplateSheetNum(Integer.parseInt(templateSheetNum) - 1); + excelInfo.setTemplateSheetNum(templateSheetNum - 1); Sheet sheet = workbook.getSheetAt(excelInfo.getTemplateSheetNum()); if (sheet == null) { throw new RuntimeException("sheet页不存在"); @@ -171,7 +177,7 @@ public class ExcelController { @RequestMapping("/cellNumSearch") public R analysisCell(@Validated RowCellNum rowCellNum) { - ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); + ExcelInfo excelInfo = (ExcelInfo) request.getSession().getAttribute(EXCEL_INFO_KEY); if (excelInfo.getTemplateWorkbook() == null) { return R.error("模板文件出问题了,请重新上传"); } @@ -197,13 +203,17 @@ public class ExcelController { public R executeTemplate(@RequestBody ExecuteTemplateQuery executeTemplateQuery) throws IOException { // TODO 有表达式和无表达式兼容处理 String newCellData = executeTemplateQuery.getNewCellData(); + if (!newCellData.contains("@{")) { + return R.error("没有表达式,我无法为你工作"); + } + String expr = newCellData.substring(newCellData.indexOf("@{"), newCellData.indexOf("}") + 1); String exprVal = newCellData.substring(newCellData.indexOf("@{") + 2, newCellData.indexOf("}")); String[] exprArr = exprVal.split("\\."); String sheetName = exprArr[0]; String field = exprArr[1]; - ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); + ExcelInfo excelInfo = (ExcelInfo) request.getSession().getAttribute(EXCEL_INFO_KEY); Optional first = excelInfo.getReadNameVoList() .stream() .filter(item -> item.getSheetName().equalsIgnoreCase(sheetName)) @@ -212,7 +222,9 @@ public class ExcelController { throw new RuntimeException("未找到sheet页,可能是表达式错误"); } ReadNameVo readNameVo = first.get(); + // 删除除了模板页以外的所有sheet POIExcelUtil.removeModelSheet(excelInfo.getTemplateWorkbook(), excelInfo.getTemplateSheetNum()); + // 根据目标数据数量(姓名数量,也就是人数)复制相应数量的sheet页,一人一页 POIExcelUtil.batchCloneSheet(excelInfo.getTemplateWorkbook(), excelInfo.getTemplateSheetNum(), readNameVo.getNameList().size()); ArrayList gmhUsers = new ArrayList<>(readNameVo.getNameList()); RowCellNum rowCellNum = POIExcelUtil.analysisCellRowNum(executeTemplateQuery.getCellRowStr()); @@ -243,7 +255,7 @@ public class ExcelController { } fileEntity.setContentType(MediaType.ALL_VALUE) .setOriginalFilename(originalFilename) - .setFileSize(2222L); + .setFileSize(0L); fileEntity.setFileType(fileProperties.getType()); String nowDate = DateUtil.format(new Date(), "yyyyMMddHHmmss"); fileEntity.setFileName(nowDate + "." + extName); @@ -276,17 +288,25 @@ public class ExcelController { @GetMapping("/gotoDownload") public R gotoDownload() { - ExcelInfo excelInfo = excelInfoMap.get(request.getSession().getId()); + ExcelInfo excelInfo = (ExcelInfo) request.getSession().getAttribute(EXCEL_INFO_KEY); try { - excelInfo.getTemplateWorkbook().close(); + if (excelInfo.getTemplateWorkbook() != null) { + excelInfo.getTemplateWorkbook().close(); + } } catch (IOException e) { throw new RuntimeException(e); } finally { - excelInfoMap.remove(request.getSession().getId()); + request.getSession().removeAttribute(EXCEL_INFO_KEY); } return R.ok(); } + @GetMapping("/getDownloadData") + public R getDownloadData() { + List excelDownloadVos = excelService.queryExcelDownloadVo(); + return R.ok().setData(excelDownloadVos); + } + private boolean isChinaName(String val) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.apply("{0} LIKE CONCAT(xingshi,'%')", val); diff --git a/src/main/java/com/gmh/controller/GlobalExceptionController.java b/src/main/java/com/gmh/controller/GlobalExceptionController.java index dcf60df..3389ec0 100644 --- a/src/main/java/com/gmh/controller/GlobalExceptionController.java +++ b/src/main/java/com/gmh/controller/GlobalExceptionController.java @@ -1,13 +1,13 @@ package com.gmh.controller; +import com.gmh.exception.ServiceException; import com.gmh.entity.R; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindException; -import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import java.util.List; +import javax.servlet.http.HttpServletResponse; @Slf4j @@ -20,4 +20,11 @@ public class GlobalExceptionController { return R.error(e.getAllErrors().get(0).getDefaultMessage()); } + @ExceptionHandler(ServiceException.class) + public R handleServiceException(ServiceException e, HttpServletResponse response) { + log.error(e.getMessage()); + response.setStatus(500); + return R.error(e.getMessage()); + } + } diff --git a/src/main/java/com/gmh/dao/ExcelDao.java b/src/main/java/com/gmh/dao/ExcelDao.java new file mode 100644 index 0000000..aec3cc5 --- /dev/null +++ b/src/main/java/com/gmh/dao/ExcelDao.java @@ -0,0 +1,13 @@ +package com.gmh.dao; + +import com.gmh.entity.vo.ExcelDownloadVo; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface ExcelDao { + + List queryExcelDownloadVo(); + +} diff --git a/src/main/java/com/gmh/entity/query/DataSourceImportQuery.java b/src/main/java/com/gmh/entity/query/DataSourceImportQuery.java new file mode 100644 index 0000000..d8bb6cb --- /dev/null +++ b/src/main/java/com/gmh/entity/query/DataSourceImportQuery.java @@ -0,0 +1,15 @@ +package com.gmh.entity.query; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DataSourceImportQuery { + + private String dataSourceImportModel; + + private Integer titleRowNum; + + private Integer removeRepeat; +} diff --git a/src/main/java/com/gmh/entity/vo/ExcelDownloadVo.java b/src/main/java/com/gmh/entity/vo/ExcelDownloadVo.java new file mode 100644 index 0000000..f4c43b3 --- /dev/null +++ b/src/main/java/com/gmh/entity/vo/ExcelDownloadVo.java @@ -0,0 +1,21 @@ +package com.gmh.entity.vo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +public class ExcelDownloadVo { + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private LocalDateTime createTime; + + private String fileId; + + private String originalName; + + private String fileName; +} diff --git a/src/main/java/com/gmh/exception/ServiceException.java b/src/main/java/com/gmh/exception/ServiceException.java new file mode 100644 index 0000000..ce977fa --- /dev/null +++ b/src/main/java/com/gmh/exception/ServiceException.java @@ -0,0 +1,64 @@ +package com.gmh.exception; + +/** + * 业务异常 + * + * @author callCenter + */ +public final class ServiceException extends RuntimeException { + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + private Integer code; + + /** + * 错误提示 + */ + private String message; + + /** + * 错误明细,内部调试错误 + *

+ * 和 {@link CommonResult#getDetailMessage()} 一致的设计 + */ + private String detailMessage; + + /** + * 空构造方法,避免反序列化问题 + */ + public ServiceException() { + } + + public ServiceException(String message) { + this.message = message; + } + + public ServiceException(String message, Integer code) { + this.message = message; + this.code = code; + } + + public String getDetailMessage() { + return detailMessage; + } + + public String getMessage() { + return message; + } + + public Integer getCode() { + return code; + } + + public ServiceException setMessage(String message) { + this.message = message; + return this; + } + + public ServiceException setDetailMessage(String detailMessage) { + this.detailMessage = detailMessage; + return this; + } +} diff --git a/src/main/java/com/gmh/service/ExcelService.java b/src/main/java/com/gmh/service/ExcelService.java new file mode 100644 index 0000000..18d6ad9 --- /dev/null +++ b/src/main/java/com/gmh/service/ExcelService.java @@ -0,0 +1,10 @@ +package com.gmh.service; + +import com.gmh.entity.vo.ExcelDownloadVo; + +import java.util.List; + +public interface ExcelService { + + List queryExcelDownloadVo(); +} diff --git a/src/main/java/com/gmh/service/impl/ExcelServiceImpl.java b/src/main/java/com/gmh/service/impl/ExcelServiceImpl.java new file mode 100644 index 0000000..7e2ec92 --- /dev/null +++ b/src/main/java/com/gmh/service/impl/ExcelServiceImpl.java @@ -0,0 +1,22 @@ +package com.gmh.service.impl; + +import com.gmh.dao.ExcelDao; +import com.gmh.entity.vo.ExcelDownloadVo; +import com.gmh.service.ExcelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ExcelServiceImpl implements ExcelService { + + @Autowired + private ExcelDao excelDao; + + + @Override + public List queryExcelDownloadVo() { + return excelDao.queryExcelDownloadVo(); + } +} diff --git a/src/main/resources/mapper/ExcelDao.xml b/src/main/resources/mapper/ExcelDao.xml new file mode 100644 index 0000000..94e3047 --- /dev/null +++ b/src/main/resources/mapper/ExcelDao.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + +