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