该改的都改

master
lenovo 2 years ago
parent e428e56fc8
commit 68696c83a0

@ -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;
/**
* HashMapKeySessionId
*
* SessionId
*
*
*/
private final Map<String, ExcelInfo> 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<ReadNameVo> readNameVoList = new ArrayList<>();
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
ReadNameVo readNameVo = new ReadNameVo();
ReadNameVo readNameVo;
Sheet sheet = workbook.getSheetAt(i);
List<Map<String, String>> excelMaps = POIExcelUtil.toListMap(sheet);
if (excelMaps.isEmpty()) {
@ -96,6 +96,7 @@ public class ExcelController {
}
readNameVo = getReadNameDataForNameKey(excelMaps, xmKey);
} else {
// map中存的是可能是姓名的列数据key为列头value为该列数据中姓名的个数
Map<String, Integer> map = new HashMap<>();
for (Map<String, String> excelMap : excelMaps) {
for (String key : excelMap.keySet()) {
@ -106,21 +107,25 @@ public class ExcelController {
}
}
}
List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
if (map.size() == 0) {
throw new ServiceException("该Excel文件中未发现姓名列");
}
List<Map.Entry<String, Integer>> 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<ReadNameVo> 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<GmhUser> 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<ExcelDownloadVo> excelDownloadVos = excelService.queryExcelDownloadVo();
return R.ok().setData(excelDownloadVos);
}
private boolean isChinaName(String val) {
QueryWrapper<SysBaijiaxing> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("{0} LIKE CONCAT(xingshi,'%')", val);

@ -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());
}
}

@ -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<ExcelDownloadVo> queryExcelDownloadVo();
}

@ -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;
}

@ -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;
}

@ -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;
/**
*
* <p>
* {@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;
}
}

@ -0,0 +1,10 @@
package com.gmh.service;
import com.gmh.entity.vo.ExcelDownloadVo;
import java.util.List;
public interface ExcelService {
List<ExcelDownloadVo> queryExcelDownloadVo();
}

@ -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<ExcelDownloadVo> queryExcelDownloadVo() {
return excelDao.queryExcelDownloadVo();
}
}

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gmh.dao.ExcelDao">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.gmh.entity.vo.ExcelDownloadVo">
<result column="create_time" property="createTime"/>
<result column="file_id" property="fileId"/>
<result column="original_filename" property="originalName"/>
<result column="file_name" property="fileName"/>
</resultMap>
<select id="queryExcelDownloadVo" resultMap="BaseResultMap">
SELECT
a.create_time,
a.file_id,
b.original_filename,
b.file_name
FROM
`gmh_file_log` a
LEFT JOIN tbl_file b ON a.file_id = b.id
WHERE a.operate_type = '2'
ORDER BY a.create_time DESC
</select>
</mapper>
Loading…
Cancel
Save