From f3b6cdde7720ec77feb5dc3c2836890a675de1a7 Mon Sep 17 00:00:00 2001 From: Renpc-kilig <308442850@qq.com> Date: Thu, 27 Jan 2022 10:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E6=A0=A1=E6=89=B9=E9=87=8F=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD=E6=96=B0=E5=A2=9E=E3=80=82=20bug?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComprehensiveCenterController.java | 15 ++ .../api/school/SchoolController.java | 132 ++++++++++ .../listener/ImportExcelListener.java | 215 ++++++++++++++++ .../systembase/pojo/dtos/excel/ExcelDTO.java | 57 +++++ .../pojo/dtos/excel/ImportFailDto.java | 40 +++ .../pojo/dtos/excel/ImportResultDto.java | 53 ++++ .../pojo/dtos/excel/ImportSuccessDto.java | 17 ++ .../pojo/dtos/school/SchoolDTO.java | 20 ++ .../pojo/model/school/SchoolModel.java | 218 ++++++++++++++++ .../systembase/pojo/vos/school/SchoolVO.java | 20 ++ .../IHandleImportExcelService.java | 46 ++++ .../impl/HandleImportExcelServiceImpl.java | 71 ++++++ .../school/impl/SchoolServiceImpl.java | 2 +- .../tenlion/systembase/util/ExcelUtil.java | 77 ++++++ .../systembase/util/ImportErrorData.java | 15 ++ .../systembase/util/ImportExcelHelper.java | 235 ++++++++++++++++++ .../mybatis/mapper/school/school-mapper.xml | 21 ++ .../resources/templates/institution/save.html | 4 +- .../templates/institution/update.html | 13 +- src/main/resources/templates/school/list.html | 41 ++- 20 files changed, 1302 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cn/com/tenlion/systembase/listener/ImportExcelListener.java create mode 100644 src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ExcelDTO.java create mode 100644 src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportFailDto.java create mode 100644 src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportResultDto.java create mode 100644 src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportSuccessDto.java create mode 100644 src/main/java/cn/com/tenlion/systembase/pojo/model/school/SchoolModel.java create mode 100644 src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/IHandleImportExcelService.java create mode 100644 src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/impl/HandleImportExcelServiceImpl.java create mode 100644 src/main/java/cn/com/tenlion/systembase/util/ExcelUtil.java create mode 100644 src/main/java/cn/com/tenlion/systembase/util/ImportErrorData.java create mode 100644 src/main/java/cn/com/tenlion/systembase/util/ImportExcelHelper.java diff --git a/src/main/java/cn/com/tenlion/systembase/controller/api/comprehensivecenter/ComprehensiveCenterController.java b/src/main/java/cn/com/tenlion/systembase/controller/api/comprehensivecenter/ComprehensiveCenterController.java index 2650601..01a6331 100644 --- a/src/main/java/cn/com/tenlion/systembase/controller/api/comprehensivecenter/ComprehensiveCenterController.java +++ b/src/main/java/cn/com/tenlion/systembase/controller/api/comprehensivecenter/ComprehensiveCenterController.java @@ -108,4 +108,19 @@ public class ComprehensiveCenterController extends DefaultBaseController { return new SuccessResultData<>(comprehensiveCenterService.count(params)); } + public static void main(String[] args) { + int aa = 2022; + for(int i=0;i<1000;i++) { + for(int j=0;j<=2;j++) { + aa = aa - j; + for(int k=1;k<12;k++) { + System.out.println("" + aa + "---" + k); + } + System.out.println("tb"); + System.out.println("hb"); + System.out.println("zs"); + } + } + } + } \ No newline at end of file diff --git a/src/main/java/cn/com/tenlion/systembase/controller/api/school/SchoolController.java b/src/main/java/cn/com/tenlion/systembase/controller/api/school/SchoolController.java index 647f292..a765314 100644 --- a/src/main/java/cn/com/tenlion/systembase/controller/api/school/SchoolController.java +++ b/src/main/java/cn/com/tenlion/systembase/controller/api/school/SchoolController.java @@ -1,8 +1,17 @@ package cn.com.tenlion.systembase.controller.api.school; +import cn.com.tenlion.systembase.listener.ImportExcelListener; +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportFailDto; import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO; +import cn.com.tenlion.systembase.pojo.model.school.SchoolModel; import cn.com.tenlion.systembase.pojo.vos.school.SchoolVO; +import cn.com.tenlion.systembase.service.handleimportexcel.IHandleImportExcelService; import cn.com.tenlion.systembase.service.school.ISchoolService; +import cn.com.tenlion.systembase.util.ExcelUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelReader; +import com.alibaba.excel.read.builder.ExcelReaderBuilder; +import com.alibaba.excel.read.metadata.ReadSheet; import ink.wgink.annotation.CheckRequestBodyAnnotation; import ink.wgink.common.base.DefaultBaseController; import ink.wgink.interfaces.consts.ISystemConstant; @@ -12,9 +21,15 @@ import ink.wgink.pojo.result.SuccessResult; import ink.wgink.pojo.result.SuccessResultData; import ink.wgink.pojo.result.SuccessResultList; import io.swagger.annotations.*; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -33,6 +48,9 @@ public class SchoolController extends DefaultBaseController { @Autowired private ISchoolService schoolService; + @Autowired + private IHandleImportExcelService handleImportExcelService; + private List failDtoList; @ApiOperation(value = "新增学校", notes = "新增学校接口") @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @@ -108,4 +126,118 @@ public class SchoolController extends DefaultBaseController { return new SuccessResultData<>(schoolService.count(params)); } + @ApiOperation(value = "导入房屋信息", notes = "导入房屋信息接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("importexcel") + public SuccessResultData importExcel(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception { + ImportExcelListener listener = new ImportExcelListener(handleImportExcelService, SchoolModel.class); + File newFile = multipartFileToFile(file); + ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(newFile); + ExcelReader excelReader = excelReaderBuilder.build(); + List sheets = excelReader.excelExecutor().sheetList(); + for (ReadSheet sheet : sheets) { + // 读取数据 + ExcelUtil.readExcel(file, sheet.getSheetNo(), 1, listener, SchoolModel.class); + } + newFile.delete(); + + // 错误集 + List importFailDtoList = listener.getImportFailDtoList(); + if(!importFailDtoList.isEmpty()) { + failDtoList = importFailDtoList; + return new SuccessResultData(importFailDtoList.size()); + } + return new SuccessResultData("success"); + } + + /** + * 导出失败数据 + * @param response + * + */ + @ApiOperation(value = "导出房屋信息失败信息", notes = "导出房屋信息失败接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("exportexcel") + public void exportExcel(HttpServletResponse response) throws IOException { + String excelName = "出租房失败数据"; + String fileName = URLEncoder.encode(excelName, "UTF-8"); + response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); + + String [] headers = {"流水号", "批次号", "入库时间", "MD5校验码", "业务字段MD5", "分片号", + "记录状态", "处理状态", "学校名称", "学校类型", "建筑面积", "类别", "教职工人数", + "学校地址", "行政区划", "面积", "失败原因"}; + List> listHeader = new ArrayList<>(); + for(String item : headers) { + List title = new ArrayList<>(); + title.add(item); + listHeader.add(title); + } + + List> listData = new ArrayList<>(); + int i = 0; + for (ImportFailDto dto : failDtoList) { + i++; + List data = new ArrayList<>(); + SchoolModel schoolModel = new SchoolModel(); + BeanUtils.copyProperties(dto.getObject(), schoolModel); + data.add(i); + data.add(schoolModel.getSerialNumber()); + data.add(schoolModel.getBatchNumber()); + data.add(schoolModel.getInsertTime()); + data.add(schoolModel.getMd5Check()); + data.add(schoolModel.getBusinessMd5()); + data.add(schoolModel.getSliceNumber()); + data.add(schoolModel.getRecordStatus()); + data.add(schoolModel.getHandleStatus()); + data.add(schoolModel.getSchoolName()); + data.add(schoolModel.getRunningType()); + data.add(schoolModel.getBuildArea()); + data.add(schoolModel.getSchoolType()); + data.add(schoolModel.getWorkersCount()); + data.add(schoolModel.getSchoolAddress()); + data.add(schoolModel.getAreaName()); + data.add(schoolModel.getArea()); + data.add(dto.getErrMsg()); + listData.add(data); + } + EasyExcel.write(response.getOutputStream()).sheet("教育局-请求结果数据导入失败数据").head(listHeader).doWrite(listData); + } + + /** + * MultipartFile 转 File + * + * @param file + * @throws Exception + */ + public File multipartFileToFile(MultipartFile file) throws Exception { + + File toFile = null; + if (file.equals("") || file.getSize() <= 0) { + file = null; + } else { + InputStream ins = null; + ins = file.getInputStream(); + toFile = new File(file.getOriginalFilename()); + inputStreamToFile(ins, toFile); + ins.close(); + } + return toFile; + } + + //获取流文件 + private void inputStreamToFile(InputStream ins, File file) { + try { + OutputStream os = new FileOutputStream(file); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + } + os.close(); + ins.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } \ No newline at end of file diff --git a/src/main/java/cn/com/tenlion/systembase/listener/ImportExcelListener.java b/src/main/java/cn/com/tenlion/systembase/listener/ImportExcelListener.java new file mode 100644 index 0000000..9434405 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/listener/ImportExcelListener.java @@ -0,0 +1,215 @@ +package cn.com.tenlion.systembase.listener; + +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportFailDto; +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportSuccessDto; +import cn.com.tenlion.systembase.pojo.model.school.SchoolModel; +import cn.com.tenlion.systembase.service.handleimportexcel.IHandleImportExcelService; +import cn.com.tenlion.systembase.util.ImportExcelHelper; +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.exception.ExcelAnalysisException; + +import java.lang.reflect.Field; +import java.util.*; + +/** + * 导入excel的监听处理类 + * @author renpc + */ +public class ImportExcelListener extends AnalysisEventListener{ + + private static final int BATCH_COUNT = 1000; + + /** + * 导入成功的数据 + */ + private List importSuccessDtoList = new ArrayList<>(); + /** + * 导入失败的数据 + */ + private List importFailDtoList = new ArrayList<>(); + + /** + * 保存数据库专用list + */ + private List list = new ArrayList<>(); + + /** + * 去重专用list + */ + private List listForDistinct = new ArrayList<>(); + + /** + * 处理导入数据的逻辑 + */ + private IHandleImportExcelService handleImportExcelService; + + private Class tClass; + + public List getImportSuccessDtoList() { + return importSuccessDtoList; + } + + public void setImportSuccessDtoList(List importSuccessDtoList) { + this.importSuccessDtoList = importSuccessDtoList; + } + + public List getImportFailDtoList() { + return importFailDtoList; + } + + public void setImportFailDtoList(List importFailDtoList) { + this.importFailDtoList = importFailDtoList; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public IHandleImportExcelService getHandleImportExcelService() { + return handleImportExcelService; + } + + public void setHandleImportExcelService(IHandleImportExcelService handleImportExcelService) { + this.handleImportExcelService = handleImportExcelService; + } + + public Class gettClass() { + return tClass; + } + + public void settClass(Class tClass) { + this.tClass = tClass; + } + + public ImportExcelListener(IHandleImportExcelService handleImportExcelService) { + this.handleImportExcelService = handleImportExcelService; + } + + public ImportExcelListener(IHandleImportExcelService handleImportExcelService, Class tClass) { + this.handleImportExcelService = handleImportExcelService; + this.tClass = tClass; + } + + /** + * 导入数据的处理逻辑 + * @param t + * @param analysisContext + */ + @Override + public void invoke(T t, AnalysisContext analysisContext) { + // 错误提示 + String msg = ""; + try { + msg = ImportExcelHelper.checkDataValid(t); + }catch (Exception e){ + msg = "存在空数据"; + e.printStackTrace(); + } + + // 如果校验失败 + if(msg.length() != 0){ + ImportFailDto importFailDto = new ImportFailDto(t, msg); + importFailDtoList.add(importFailDto); + }else{ + ImportExcelHelper.checkDataFromDatabase(t); + list.add(t); + listForDistinct.add(t); + } + + if(list.size() > BATCH_COUNT){ + try { + insertData(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 所有数据处理完之后的处理方法 + * @param analysisContext + */ + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + // PS:之所以最后这里还进行处理是防止最后一次的数据量没有达到批量值 + if(list.size() > 0) { + try { + insertData(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 校验导入的表格的头是否匹配 + * @param headMap + * @param analysisContext + */ + @Override + public void invokeHeadMap(Map headMap, AnalysisContext analysisContext){ + super.invokeHeadMap(headMap, analysisContext); + if(tClass != null){ + try { + // 获取Excel导入实体的单元格内容 + Map indexNameMap = getIndexName(tClass); + Set keySet = indexNameMap.keySet(); + for (Integer key: keySet) { + // 头表是否存在空值 + if(headMap.get(key).length() == 0){ + throw new ExcelAnalysisException("Excel格式非法"); + } + // 对比导入Excel实体模板和当前上传Excel是否匹配 + /*if(!headMap.get(key).equals(indexNameMap.get(key))){ + throw new ExcelAnalysisException("Excel格式非法"); + }*/ + } + }catch (Exception e){ + e.printStackTrace(); + } + } + } + + private Map getIndexName(Class tClass) throws NoSuchFieldException { + Map result = new HashMap<>(); + Field[] declaredFields = tClass.getDeclaredFields(); + for (Field currentField: declaredFields) { + currentField.setAccessible(true); + ExcelProperty annotation = currentField.getAnnotation(ExcelProperty.class); + if(annotation != null){ + int index =annotation.index(); + String[] value = annotation.value(); + StringBuilder sb = new StringBuilder(); + for (String cur : value) { + sb.append(cur); + } + result.put(index, sb.toString()); + } + } + return result; + } + + /** + * 将数据插入数据库操作 + */ + private void insertData() throws Exception { + String value = ""; + // 房屋信息 + if(list.get(0) instanceof SchoolModel) { + List schoolModelList = (List) list; + handleImportExcelService.saveSchool(schoolModelList, importFailDtoList); + } + listForDistinct.clear(); + list.clear(); + } + + +} \ No newline at end of file diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ExcelDTO.java b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ExcelDTO.java new file mode 100644 index 0000000..fe59bc6 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ExcelDTO.java @@ -0,0 +1,57 @@ +package cn.com.tenlion.systembase.pojo.dtos.excel; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * + * @ClassName: ExcelDTO + * @Description: 导入excel相关DTO + * @Author: renpc + * @Date: 2021-02-28 15:38 + * @Version: 1.0 + **/ +@ApiModel +public class ExcelDTO { + + @ApiModelProperty(name = "dictId", value = "字典表ID") + private String dictId; + @ApiModelProperty(name = "dictName", value = "字典表名称") + private String dictName; + @ApiModelProperty(name = "areaId", value = "区域表ID") + private String areaId; + @ApiModelProperty(name = "areaName", value = "字典表名称") + private String areaName; + + public String getDictId() { + return dictId == null ? "" : dictId; + } + + public void setDictId(String dictId) { + this.dictId = dictId; + } + + public String getDictName() { + return dictName == null ? "" : dictName; + } + + public void setDictName(String dictName) { + this.dictName = dictName; + } + + public String getAreaId() { + return areaId == null ? "" : areaId; + } + + public void setAreaId(String areaId) { + this.areaId = areaId; + } + + public String getAreaName() { + return areaName == null ? "" : areaName; + } + + public void setAreaName(String areaName) { + this.areaName = areaName; + } +} diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportFailDto.java b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportFailDto.java new file mode 100644 index 0000000..3010b61 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportFailDto.java @@ -0,0 +1,40 @@ +package cn.com.tenlion.systembase.pojo.dtos.excel; + +import java.io.Serializable; + +/** + * 批量导入失败实体类 + * @author renpc + */ +public class ImportFailDto implements Serializable { + + /** + *导入实体信息 + */ + private Object object; + /** + * 导入错误提示 + */ + private String errMsg; + + public ImportFailDto(Object object, String errMsg) { + this.object = object; + this.errMsg = errMsg; + } + + public Object getObject() { + return object; + } + + public void setObject(Object object) { + this.object = object; + } + + public String getErrMsg() { + return errMsg == null ? "" : errMsg; + } + + public void setErrMsg(String errMsg) { + this.errMsg = errMsg; + } +} diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportResultDto.java b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportResultDto.java new file mode 100644 index 0000000..ffb836b --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportResultDto.java @@ -0,0 +1,53 @@ +package cn.com.tenlion.systembase.pojo.dtos.excel; + +import java.util.ArrayList; +import java.util.List; + +/** + * 批量导入结果实体类 + * @author renpc + */ +public class ImportResultDto { + + /** + * 导入成功的消息列表 + */ + private List successDtoList; + + /** + * 导入失败的消息列表 + */ + private List failDtoList; + + public ImportResultDto(List successDtoList, List failDtoList) { + this.successDtoList = successDtoList; + this.failDtoList = failDtoList; + } + + public ImportResultDto(List failDtoList) { + this.failDtoList = failDtoList; + this.successDtoList = new ArrayList<>(); + } + + public List getSuccessDtoList() { + if (successDtoList == null) { + return new ArrayList<>(); + } + return successDtoList; + } + + public void setSuccessDtoList(List successDtoList) { + this.successDtoList = successDtoList; + } + + public List getFailDtoList() { + if (failDtoList == null) { + return new ArrayList<>(); + } + return failDtoList; + } + + public void setFailDtoList(List failDtoList) { + this.failDtoList = failDtoList; + } +} diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportSuccessDto.java b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportSuccessDto.java new file mode 100644 index 0000000..0f1d099 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/excel/ImportSuccessDto.java @@ -0,0 +1,17 @@ +package cn.com.tenlion.systembase.pojo.dtos.excel; + +import java.io.Serializable; + +/** + * 批量导入成功实体类 + * @author renpc + */ +public class ImportSuccessDto implements Serializable { + + private Object object; + + public ImportSuccessDto(Object object) { + this.object = object; + } + +} diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/school/SchoolDTO.java b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/school/SchoolDTO.java index 13858b0..cfbc366 100644 --- a/src/main/java/cn/com/tenlion/systembase/pojo/dtos/school/SchoolDTO.java +++ b/src/main/java/cn/com/tenlion/systembase/pojo/dtos/school/SchoolDTO.java @@ -64,6 +64,10 @@ public class SchoolDTO { private String grid; @ApiModelProperty(name = "gridName", value = "所属网格") private String gridName; + @ApiModelProperty(name = "remake", value = "remake") + private String remake; + @ApiModelProperty(name = "dataFrom", value = "dataFrom") + private String dataFrom; public String getSchoolId() { return schoolId == null ? "" : schoolId.trim(); @@ -264,4 +268,20 @@ public class SchoolDTO { public void setGridName(String gridName) { this.gridName = gridName; } + + public String getRemake() { + return remake; + } + + public void setRemake(String remake) { + this.remake = remake; + } + + public String getDataFrom() { + return dataFrom; + } + + public void setDataFrom(String dataFrom) { + this.dataFrom = dataFrom; + } } diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/model/school/SchoolModel.java b/src/main/java/cn/com/tenlion/systembase/pojo/model/school/SchoolModel.java new file mode 100644 index 0000000..a133c9b --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/pojo/model/school/SchoolModel.java @@ -0,0 +1,218 @@ +package cn.com.tenlion.systembase.pojo.model.school; + +import com.alibaba.excel.annotation.ExcelProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * @author 29492 + */ +@ApiModel +public class SchoolModel { + + @ExcelProperty(index = 0) + private String serialNumber; + @ExcelProperty(index = 1) + private String batchNumber; + @ExcelProperty(index = 2) + private String insertTime; + @ExcelProperty(index = 3) + private String md5Check; + @ExcelProperty(index = 4) + private String businessMd5; + @ExcelProperty(index = 5) + private String sliceNumber; + @ExcelProperty(index = 6) + private String recordStatus; + @ExcelProperty(index = 7) + private String handleStatus; + @ExcelProperty(index = 8) + private String schoolName; + @ExcelProperty(index = 9) + private String runningType; + @ExcelProperty(index = 10) + private String buildArea; + @ExcelProperty(index = 11) + private String schoolType; + @ExcelProperty(index = 12) + private String workersCount; + @ExcelProperty(index = 13) + private String schoolAddress; + @ExcelProperty(index = 14) + private String areaName; + @ExcelProperty(index = 15) + private String area; + + String remake; + String areaCode; + private String runningTypeName; + private String runningTypeCode; + private String locationCode; + public String getSerialNumber() { + return serialNumber; + } + + public void setSerialNumber(String serialNumber) { + this.serialNumber = serialNumber; + } + + public String getBatchNumber() { + return batchNumber; + } + + public void setBatchNumber(String batchNumber) { + this.batchNumber = batchNumber; + } + + public String getInsertTime() { + return insertTime; + } + + public void setInsertTime(String insertTime) { + this.insertTime = insertTime; + } + + public String getMd5Check() { + return md5Check; + } + + public void setMd5Check(String md5Check) { + this.md5Check = md5Check; + } + + public String getBusinessMd5() { + return businessMd5; + } + + public void setBusinessMd5(String businessMd5) { + this.businessMd5 = businessMd5; + } + + public String getSliceNumber() { + return sliceNumber; + } + + public void setSliceNumber(String sliceNumber) { + this.sliceNumber = sliceNumber; + } + + public String getRecordStatus() { + return recordStatus; + } + + public void setRecordStatus(String recordStatus) { + this.recordStatus = recordStatus; + } + + public String getHandleStatus() { + return handleStatus; + } + + public void setHandleStatus(String handleStatus) { + this.handleStatus = handleStatus; + } + + public String getSchoolName() { + return schoolName; + } + + public void setSchoolName(String schoolName) { + this.schoolName = schoolName; + } + + public String getRunningType() { + return runningType; + } + + public void setRunningType(String runningType) { + this.runningType = runningType; + } + + public String getBuildArea() { + return buildArea; + } + + public void setBuildArea(String buildArea) { + this.buildArea = buildArea; + } + + public String getSchoolType() { + return schoolType; + } + + public void setSchoolType(String schoolType) { + this.schoolType = schoolType; + } + + public String getWorkersCount() { + return workersCount; + } + + public void setWorkersCount(String workersCount) { + this.workersCount = workersCount; + } + + public String getSchoolAddress() { + return schoolAddress; + } + + public void setSchoolAddress(String schoolAddress) { + this.schoolAddress = schoolAddress; + } + + public String getAreaName() { + return areaName; + } + + public void setAreaName(String areaName) { + this.areaName = areaName; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getRemake() { + return remake; + } + + public void setRemake(String remake) { + this.remake = remake; + } + + public String getAreaCode() { + return areaCode; + } + + public void setAreaCode(String areaCode) { + this.areaCode = areaCode; + } + + public String getRunningTypeName() { + return runningTypeName; + } + + public void setRunningTypeName(String runningTypeName) { + this.runningTypeName = runningTypeName; + } + + public String getRunningTypeCode() { + return runningTypeCode; + } + + public void setRunningTypeCode(String runningTypeCode) { + this.runningTypeCode = runningTypeCode; + } + + public String getLocationCode() { + return locationCode; + } + + public void setLocationCode(String locationCode) { + this.locationCode = locationCode; + } +} diff --git a/src/main/java/cn/com/tenlion/systembase/pojo/vos/school/SchoolVO.java b/src/main/java/cn/com/tenlion/systembase/pojo/vos/school/SchoolVO.java index d4fdd68..b47b2b4 100644 --- a/src/main/java/cn/com/tenlion/systembase/pojo/vos/school/SchoolVO.java +++ b/src/main/java/cn/com/tenlion/systembase/pojo/vos/school/SchoolVO.java @@ -63,6 +63,10 @@ public class SchoolVO { private String locationCode; @ApiModelProperty(name = "grid", value = "所属网格") private String grid; + @ApiModelProperty(name = "dataFrom", value = "数据来源(0:系统录入;1:批量导入)") + private String dataFrom; + @ApiModelProperty(name = "remake", value = "remake") + private String remake; public String getSchoolName() { return schoolName == null ? "" : schoolName.trim(); @@ -247,4 +251,20 @@ public class SchoolVO { public void setGrid(String grid) { this.grid = grid; } + + public String getDataFrom() { + return dataFrom; + } + + public void setDataFrom(String dataFrom) { + this.dataFrom = dataFrom; + } + + public String getRemake() { + return remake; + } + + public void setRemake(String remake) { + this.remake = remake; + } } diff --git a/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/IHandleImportExcelService.java b/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/IHandleImportExcelService.java new file mode 100644 index 0000000..a3002ca --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/IHandleImportExcelService.java @@ -0,0 +1,46 @@ +package cn.com.tenlion.systembase.service.handleimportexcel; + +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportFailDto; +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportResultDto; +import cn.com.tenlion.systembase.pojo.model.school.SchoolModel; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName: IHandleImportExcelService + * @Description: 导入excel service + * @Author: renpc + * @Date: 2021-02-27 14:40 + * @Version: 1.0 + **/ +public interface IHandleImportExcelService { + + /** + * 数据校验 + * + * @param list + * @return + * @throws Exception + */ + public ImportResultDto checkImportData(List list); + + /** + * 字段比对数据库(字典) + * @param params + */ + public void checkDataFromDict(Map params); + + /** + * 字段比对数据库(区域) + * @param params + */ + public void checkDataFromArea(Map params); + + /** + * 批量保存学校信息 + * @param schoolModelList + * @param importFailDtoList + */ + void saveSchool(List schoolModelList, List importFailDtoList) throws Exception; +} diff --git a/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/impl/HandleImportExcelServiceImpl.java b/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/impl/HandleImportExcelServiceImpl.java new file mode 100644 index 0000000..eaf7eef --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/service/handleimportexcel/impl/HandleImportExcelServiceImpl.java @@ -0,0 +1,71 @@ +package cn.com.tenlion.systembase.service.handleimportexcel.impl; + +import cn.com.tenlion.systembase.dao.school.ISchoolDao; +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportFailDto; +import cn.com.tenlion.systembase.pojo.dtos.excel.ImportResultDto; +import cn.com.tenlion.systembase.pojo.model.school.SchoolModel; +import cn.com.tenlion.systembase.service.handleimportexcel.IHandleImportExcelService; +import cn.com.tenlion.systembase.service.school.ISchoolService; +import com.google.common.util.concurrent.AbstractService; +import ink.wgink.common.base.DefaultBaseService; +import ink.wgink.util.UUIDUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName: HandleImportExcelServiceImpl + * @Description: 导入excel service + * @Author: renpc + * @Date: 2021-02-27 14:40 + * @Version: 1.0 + **/ +@Service +public class HandleImportExcelServiceImpl extends DefaultBaseService implements IHandleImportExcelService { + + @Autowired + private ISchoolDao schoolDao; + @Autowired + private ISchoolService schoolService; + + @Override + public ImportResultDto checkImportData(List list) { + return new ImportResultDto(null); + } + + @Override + public void checkDataFromDict(Map params) { + + } + + @Override + public void checkDataFromArea(Map params) { + + } + + @Override + public void saveSchool(List schoolModelList, List importFailDtoList) throws Exception { + if(null != schoolModelList && schoolModelList.size() > 0) { + Map params = new HashMap<>(); + for(SchoolModel schoolModel: schoolModelList) { + String schoolId = UUIDUtil.getUUID(); + params.put("schoolId", schoolId); + params.put("dataFrom", 1); + params.put("remake", schoolModel.getRemake()); + params.put("schoolName", schoolModel.getSchoolName()); + params.put("runningType", schoolModel.getRunningType()); + params.put("runningTypeCode", schoolModel.getRunningTypeCode()); + params.put("runningTypeName", schoolModel.getRunningTypeName()); + params.put("location", schoolModel.getAreaName()); + params.put("locationCode", schoolModel.getLocationCode()); + params.put("schoolAddress", schoolModel.getSchoolAddress()); + setSaveInfo(params); + + schoolDao.save(params); + } + } + } +} diff --git a/src/main/java/cn/com/tenlion/systembase/service/school/impl/SchoolServiceImpl.java b/src/main/java/cn/com/tenlion/systembase/service/school/impl/SchoolServiceImpl.java index 20dcdbf..bbd4821 100644 --- a/src/main/java/cn/com/tenlion/systembase/service/school/impl/SchoolServiceImpl.java +++ b/src/main/java/cn/com/tenlion/systembase/service/school/impl/SchoolServiceImpl.java @@ -120,7 +120,7 @@ public class SchoolServiceImpl extends DefaultBaseService implements ISchoolServ @Override public SchoolDTO get(Map params) { SchoolDTO schoolDTO = schoolDao.get(params); - if(null != schoolDTO) { + if(null != schoolDTO && !"1".equals(schoolDTO.getDataFrom())) { GridDTO gridDTO = gridService.get(schoolDTO.getGrid()); if(null != gridDTO) { schoolDTO.setGridName(gridDTO.getGridName()); diff --git a/src/main/java/cn/com/tenlion/systembase/util/ExcelUtil.java b/src/main/java/cn/com/tenlion/systembase/util/ExcelUtil.java new file mode 100644 index 0000000..87be863 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/util/ExcelUtil.java @@ -0,0 +1,77 @@ +package cn.com.tenlion.systembase.util; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.support.ExcelTypeEnum; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLEncoder; +import java.util.List; + +/** + * excel工具类 + * @author renpc + */ +public class ExcelUtil { + + /** + * 默认excel文件名和单元sheet名一样的 Excel文件导出 + * @param httpServletResponse + * @param data + * @param fileName + * @param clazz + * @throws IOException + */ + public static void writeExcel(HttpServletResponse httpServletResponse, List data, String fileName, Class clazz) throws IOException { + writeExcel(httpServletResponse, data, fileName, fileName, clazz); + } + + /** + * 导出数据为Excel文件 + * @param response 响应实体 + * @param data 导出数据 + * @param fileName 文件名 + * @param sheetName 单元格名 + * @param clazz 定义excel导出的实体 + * @throws IOException + */ + public static void writeExcel(HttpServletResponse response, List data, String fileName, String sheetName, Class clazz) throws IOException { + //防止中文乱码 + fileName = URLEncoder.encode(fileName, "UTF-8"); + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("utf-8"); + //防止导入excel文件名中文不乱码 + response.setHeader("Content-disposition", "attachment;fileName=" + fileName + ".xlsx" + ";fileName*=utf-8''" + fileName + ".xlsx"); + EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data); + } + + /** + * easyExcel处理上传的文件 + * @param sheetNo 单元格 + * @param rowNumber 行数 + * @param analysisEventListener 上传处理监听 + * @param clazz 上传处理excel类 + * @return + * @throws Exception + */ + public static List readExcel(MultipartFile file, Integer sheetNo, Integer rowNumber, AnalysisEventListener analysisEventListener, Class clazz) throws Exception { + if(null != file) { + String originalFilename = file.getOriginalFilename(); + if(StringUtils.isEmpty(originalFilename)){ + throw new Exception("上传的文件不能为空"); + } + if(!originalFilename.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()) && + !originalFilename.toLowerCase().endsWith(ExcelTypeEnum.XLSX.getValue())){ + throw new Exception("上传的文件格式不匹配"); + } + InputStream inputStream = file.getInputStream(); + return EasyExcel.read(inputStream, clazz, analysisEventListener).sheet(sheetNo).headRowNumber(rowNumber).doReadSync(); + } + throw new Exception("上传的文件不能为空"); + } + +} diff --git a/src/main/java/cn/com/tenlion/systembase/util/ImportErrorData.java b/src/main/java/cn/com/tenlion/systembase/util/ImportErrorData.java new file mode 100644 index 0000000..7d34274 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/util/ImportErrorData.java @@ -0,0 +1,15 @@ +package cn.com.tenlion.systembase.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author xwangs + * @create 2021-03-23 17:52 + * @description + */ +public class ImportErrorData { + + public static Map errorData = new HashMap<>(36); + +} diff --git a/src/main/java/cn/com/tenlion/systembase/util/ImportExcelHelper.java b/src/main/java/cn/com/tenlion/systembase/util/ImportExcelHelper.java new file mode 100644 index 0000000..d167b93 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systembase/util/ImportExcelHelper.java @@ -0,0 +1,235 @@ +package cn.com.tenlion.systembase.util; + +import cn.com.tenlion.systembase.dao.school.ISchoolDao; +import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO; +import cn.com.tenlion.systembase.pojo.model.school.SchoolModel; +import cn.com.tenlion.systembase.service.handleimportexcel.IHandleImportExcelService; +import ink.wgink.module.dictionary.pojo.dtos.AreaDTO; +import ink.wgink.module.dictionary.pojo.dtos.DataDTO; +import ink.wgink.mongo.module.dictionary.service.IMongoAreaService; +import ink.wgink.mongo.module.dictionary.service.IMongoDataService; +import ink.wgink.pojo.ListPage; +import ink.wgink.pojo.result.SuccessResultList; +import org.apache.commons.lang3.StringUtils; +import org.checkerframework.checker.units.qual.A; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 导入excel数据的校验类 + * @author renpc + */ +@Component +public class ImportExcelHelper { + + @Autowired + private IHandleImportExcelService handleImportExcelService; + @Autowired + private static IMongoDataService mongoDataService; + @Autowired + private static IMongoAreaService mongoAreaService; + @Autowired + private static ISchoolDao schoolDao; + + @Autowired + public void init(ISchoolDao schoolDao) { + ImportExcelHelper.schoolDao = schoolDao; + } + + @Autowired + public void init(IMongoDataService mongoDataService) { + ImportExcelHelper.mongoDataService = mongoDataService; + } + + @Autowired + public void init(IMongoAreaService mongoAreaService) { + ImportExcelHelper.mongoAreaService = mongoAreaService; + } + + public static String checkDataValid(T obj) { + if(null == obj) { + return null; + } + String returnStr = isMustInput(obj); + return returnStr; + } + + /** + * 检查数据是否必填项缺失 + * @param obj + * @param + * @return + */ + private static String isMustInput(T obj) { + // 房屋信息 + if(obj instanceof SchoolModel) { + Object temp = obj; + SchoolModel schoolModel = (SchoolModel) temp; + return checkSchoolData(schoolModel); + } + return ""; + } + + /** + * 检查数据是否必填项缺失(房屋管理) + * @param schoolModel + * @return + */ + private static String checkSchoolData(SchoolModel schoolModel) { + if(StringUtils.isEmpty(schoolModel.getSchoolName())) { + return "学校名称必填"; + } + if(StringUtils.isEmpty(schoolModel.getSchoolAddress())) { + return "学校地址必填"; + } + if(StringUtils.isEmpty(schoolModel.getAreaName())) { + return "行政区划"; + } + Map params = new HashMap<>(); + params.put("schoolName", schoolModel.getSchoolName()); + params.put("schoolAddress", schoolModel.getSchoolAddress()); + List schoolDTOS = schoolDao.list(params); + if(null != schoolDTOS && schoolDTOS.size() > 0) { + return "数据已存在"; + } + return ""; + } + + /** + * 从数据库比对数据,并转换为数据库数据 + * @param obj + * @param + */ + public static void checkDataFromDatabase(T obj) { + if(obj instanceof SchoolModel) { + Object temp = obj; + SchoolModel schoolModel = (SchoolModel) temp; + schoolCheck(schoolModel); + temp = schoolModel; + obj = (T) temp; + } + } + + /** + * 从数据库比对数据,并转换为数据库数据(房屋管理) + * @param schoolModel + */ + private static void schoolCheck(SchoolModel schoolModel) { + // 行政区划筛选 + if("市直".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("市辖区"); + } + if("乌兰察布市察哈尔右翼前旗".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("察哈尔右翼前旗"); + } + if("乌兰察布市教育局".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("乌兰察布市"); + } + if("12000".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("乌兰察布市"); + } + if("市直单位".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("乌兰察布市"); + } + if("察右中旗".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("察哈尔右翼中旗"); + } + if("察右后旗".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("察哈尔右翼后旗"); + } + if("乌兰察布市兴和县".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("兴和县"); + } + if("内蒙古自治区乌兰察布市兴和县".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("兴和县"); + } + if("兴和县城关镇".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("城关镇"); + } + if("兴和县赛乌素镇".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("赛乌素镇"); + } + if("兴和县大库联".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("大库联乡"); + } + if("兴和县团结乡".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("民族团结乡"); + } + if("兴和县张皋镇".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("张皋镇"); + } + if("兴和县大同夭".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("大同夭乡"); + } + if("兴和县店子镇".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("店子镇"); + } + if("内蒙古自治区乌兰察布市凉城县".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("凉城县"); + } + if("察右前旗".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("察哈尔右翼前旗"); + } + if("县市级".equals(schoolModel.getAreaName())) { + schoolModel.setAreaName("丰镇市"); + } + + // 办学类型筛选 + if("中等职业技术学校".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("中等职业学校"); + } + if("九年一贯制学校(初中)".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("九年一贯制学校"); + } + if("九年一贯制".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("九年一贯制学校"); + } + if("职高".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("职业高中"); + } + if("民办".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("民办的其他高等教育机构"); + } + if("九年一贯".equals(schoolModel.getRunningType())) { + schoolModel.setAreaName("九年一贯制学校"); + } + + String remake = "源系统数据:\n"; + remake += "流水号:" + schoolModel.getSliceNumber() + "\n"; + remake += "批次号:" + schoolModel.getBatchNumber() + "\n"; + remake += "入库时间:" + schoolModel.getInsertTime() + "\n"; + remake += "MD5校验码:" + schoolModel.getMd5Check() + "\n"; + remake += "业务字段MD5:" + schoolModel.getBusinessMd5() + "\n"; + remake += "分片号:" + schoolModel.getSerialNumber() + "\n"; + remake += "记录状态:" + schoolModel.getRecordStatus() + "\n"; + remake += "处理状态:" + schoolModel.getHandleStatus() + "\n"; + remake += "建筑面积:" + schoolModel.getBuildArea() + "\n"; + remake += "类别:" + schoolModel.getSchoolType() + "\n"; + remake += "教职工人数:" + schoolModel.getWorkersCount() + "\n"; + remake += "面积:" + schoolModel.getArea() + "\n"; + schoolModel.setRemake(remake); + + // 由于无法通过地区名称查找地区编码和ID,所以暂时注释掉 + /*AreaDTO areaDTO = mongoAreaService.get(schoolModel.getAreaName()); + schoolModel.setAreaName(areaDTO.getAreaName()); + schoolModel.setLocationCode(areaDTO.getAreaCode());*/ + + Map params = new HashMap<>(); + params.put("keywords", schoolModel.getRunningType()); + params.put("dataParentId", "e5624543-4f8e-4550-a8e8-49235f23b287"); + ListPage page = new ListPage(); + page.setParams(params); + SuccessResultList> runningTypeDTO = mongoDataService.listPage(page); + if(null != runningTypeDTO && null != runningTypeDTO.getRows() && runningTypeDTO.getRows().size() > 0) { + DataDTO dataDTO = runningTypeDTO.getRows().get(0); + schoolModel.setRunningType(dataDTO.getDataId()); + schoolModel.setRunningTypeName(dataDTO.getDataName()); + schoolModel.setRunningTypeCode(dataDTO.getDataCode()); + } + } + +} diff --git a/src/main/resources/mybatis/mapper/school/school-mapper.xml b/src/main/resources/mybatis/mapper/school/school-mapper.xml index 3564a00..e8a6c6c 100644 --- a/src/main/resources/mybatis/mapper/school/school-mapper.xml +++ b/src/main/resources/mybatis/mapper/school/school-mapper.xml @@ -27,6 +27,8 @@ + + @@ -104,6 +106,8 @@ location, location_code, grid, + data_from, + remake, creator, gmt_create, modifier, @@ -134,6 +138,8 @@ #{location}, #{locationCode}, #{grid}, + #{dataFrom}, + #{remake}, #{creator}, #{gmtCreate}, #{modifier}, @@ -242,6 +248,9 @@ grid = #{grid}, + + remake = #{remake}, + gmt_modified = #{gmtModified}, modifier = #{modifier}, school_id = school_id @@ -275,6 +284,8 @@ t1.location, t1.location_code, t1.grid, + t1.data_from, + t1.remake, t1.school_id FROM data_school t1 @@ -399,6 +410,14 @@ AND LEFT(t1.gmt_create, 10) #{endTime} + + AND + t1.school_name = #{schoolName} + + + AND + t1.school_address = #{schoolAddress} + AND t1.school_id IN @@ -406,6 +425,8 @@ #{schoolIds[${index}]} + ORDER BY + t1.gmt_create DESC diff --git a/src/main/resources/templates/institution/save.html b/src/main/resources/templates/institution/save.html index 4fe1567..9027a3e 100644 --- a/src/main/resources/templates/institution/save.html +++ b/src/main/resources/templates/institution/save.html @@ -59,14 +59,14 @@
- +