学校批量导入功能新增。

bug修改。
This commit is contained in:
Renpc-kilig 2022-01-27 10:49:23 +08:00
parent f756564754
commit f3b6cdde77
20 changed files with 1302 additions and 10 deletions

View File

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

View File

@ -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<ImportFailDto> 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<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
for (ReadSheet sheet : sheets) {
// 读取数据
ExcelUtil.readExcel(file, sheet.getSheetNo(), 1, listener, SchoolModel.class);
}
newFile.delete();
// 错误集
List<ImportFailDto> 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<List<String>> listHeader = new ArrayList<>();
for(String item : headers) {
List<String> title = new ArrayList<>();
title.add(item);
listHeader.add(title);
}
List<List<Object>> listData = new ArrayList<>();
int i = 0;
for (ImportFailDto dto : failDtoList) {
i++;
List<Object> 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();
}
}
}

View File

@ -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<T> extends AnalysisEventListener<T>{
private static final int BATCH_COUNT = 1000;
/**
* 导入成功的数据
*/
private List<ImportSuccessDto> importSuccessDtoList = new ArrayList<>();
/**
* 导入失败的数据
*/
private List<ImportFailDto> importFailDtoList = new ArrayList<>();
/**
* 保存数据库专用list
*/
private List<T> list = new ArrayList<>();
/**
* 去重专用list
*/
private List<T> listForDistinct = new ArrayList<>();
/**
* 处理导入数据的逻辑
*/
private IHandleImportExcelService handleImportExcelService;
private Class<T> tClass;
public List<ImportSuccessDto> getImportSuccessDtoList() {
return importSuccessDtoList;
}
public void setImportSuccessDtoList(List<ImportSuccessDto> importSuccessDtoList) {
this.importSuccessDtoList = importSuccessDtoList;
}
public List<ImportFailDto> getImportFailDtoList() {
return importFailDtoList;
}
public void setImportFailDtoList(List<ImportFailDto> importFailDtoList) {
this.importFailDtoList = importFailDtoList;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public IHandleImportExcelService getHandleImportExcelService() {
return handleImportExcelService;
}
public void setHandleImportExcelService(IHandleImportExcelService handleImportExcelService) {
this.handleImportExcelService = handleImportExcelService;
}
public Class<T> gettClass() {
return tClass;
}
public void settClass(Class<T> tClass) {
this.tClass = tClass;
}
public ImportExcelListener(IHandleImportExcelService handleImportExcelService) {
this.handleImportExcelService = handleImportExcelService;
}
public ImportExcelListener(IHandleImportExcelService handleImportExcelService, Class<T> 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<Integer, String> headMap, AnalysisContext analysisContext){
super.invokeHeadMap(headMap, analysisContext);
if(tClass != null){
try {
// 获取Excel导入实体的单元格内容
Map<Integer, String> indexNameMap = getIndexName(tClass);
Set<Integer> 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<Integer, String> getIndexName(Class<T> tClass) throws NoSuchFieldException {
Map<Integer, String> 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<SchoolModel> schoolModelList = (List<SchoolModel>) list;
handleImportExcelService.saveSchool(schoolModelList, importFailDtoList);
}
listForDistinct.clear();
list.clear();
}
}

View File

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

View File

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

View File

@ -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<ImportSuccessDto> successDtoList;
/**
* 导入失败的消息列表
*/
private List<ImportFailDto> failDtoList;
public ImportResultDto(List<ImportSuccessDto> successDtoList, List<ImportFailDto> failDtoList) {
this.successDtoList = successDtoList;
this.failDtoList = failDtoList;
}
public ImportResultDto(List<ImportFailDto> failDtoList) {
this.failDtoList = failDtoList;
this.successDtoList = new ArrayList<>();
}
public List<ImportSuccessDto> getSuccessDtoList() {
if (successDtoList == null) {
return new ArrayList<>();
}
return successDtoList;
}
public void setSuccessDtoList(List<ImportSuccessDto> successDtoList) {
this.successDtoList = successDtoList;
}
public List<ImportFailDto> getFailDtoList() {
if (failDtoList == null) {
return new ArrayList<>();
}
return failDtoList;
}
public void setFailDtoList(List<ImportFailDto> failDtoList) {
this.failDtoList = failDtoList;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <T> ImportResultDto checkImportData(List<T> list);
/**
* 字段比对数据库字典
* @param params
*/
public void checkDataFromDict(Map<String, Object> params);
/**
* 字段比对数据库区域
* @param params
*/
public void checkDataFromArea(Map<String, Object> params);
/**
* 批量保存学校信息
* @param schoolModelList
* @param importFailDtoList
*/
void saveSchool(List<SchoolModel> schoolModelList, List<ImportFailDto> importFailDtoList) throws Exception;
}

View File

@ -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 <T> ImportResultDto checkImportData(List<T> list) {
return new ImportResultDto(null);
}
@Override
public void checkDataFromDict(Map<String, Object> params) {
}
@Override
public void checkDataFromArea(Map<String, Object> params) {
}
@Override
public void saveSchool(List<SchoolModel> schoolModelList, List<ImportFailDto> importFailDtoList) throws Exception {
if(null != schoolModelList && schoolModelList.size() > 0) {
Map<String, Object> 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);
}
}
}
}

View File

@ -120,7 +120,7 @@ public class SchoolServiceImpl extends DefaultBaseService implements ISchoolServ
@Override
public SchoolDTO get(Map<String, Object> 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());

View File

@ -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<Object> 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("上传的文件不能为空");
}
}

View File

@ -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<String, Object> errorData = new HashMap<>(36);
}

View File

@ -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 <T> String checkDataValid(T obj) {
if(null == obj) {
return null;
}
String returnStr = isMustInput(obj);
return returnStr;
}
/**
* 检查数据是否必填项缺失
* @param obj
* @param <T>
* @return
*/
private static <T> 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<String, Object> params = new HashMap<>();
params.put("schoolName", schoolModel.getSchoolName());
params.put("schoolAddress", schoolModel.getSchoolAddress());
List<SchoolDTO> schoolDTOS = schoolDao.list(params);
if(null != schoolDTOS && schoolDTOS.size() > 0) {
return "数据已存在";
}
return "";
}
/**
* 从数据库比对数据并转换为数据库数据
* @param obj
* @param <T>
*/
public static <T> 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<String, Object> params = new HashMap<>();
params.put("keywords", schoolModel.getRunningType());
params.put("dataParentId", "e5624543-4f8e-4550-a8e8-49235f23b287");
ListPage page = new ListPage();
page.setParams(params);
SuccessResultList<List<DataDTO>> 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());
}
}
}

View File

@ -27,6 +27,8 @@
<result column="location" property="location"/>
<result column="location_code" property="locationCode"/>
<result column="grid" property="grid"/>
<result column="data_from" property="dataFrom"/>
<result column="remake" property="remake"/>
</resultMap>
<resultMap id="schoolBO" type="cn.com.tenlion.systembase.pojo.bos.school.SchoolBO">
@ -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 @@
<if test="grid != null and grid != ''">
grid = #{grid},
</if>
<if test="remake != null and remake != ''">
remake = #{remake},
</if>
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) <![CDATA[ <= ]]> #{endTime}
</if>
<if test="schoolName != null and schoolName != ''">
AND
t1.school_name = #{schoolName}
</if>
<if test="schoolAddress != null and schoolAddress != ''">
AND
t1.school_address = #{schoolAddress}
</if>
<if test="schoolIds != null and schoolIds.size > 0">
AND
t1.school_id IN
@ -406,6 +425,8 @@
#{schoolIds[${index}]}
</foreach>
</if>
ORDER BY
t1.gmt_create DESC
</select>
<!-- 学校列表 -->

View File

@ -59,14 +59,14 @@
<input type="hidden" id="locationId" name="locationId" class="layui-input" value="" placeholder="请输入一级区域ID" maxlength="36">
<input type="hidden" id="locationCode" name="locationCode" class="layui-input" value="" placeholder="请输入一级区域名称" maxlength="255">
<div class="layui-input-block">
<input type="text" id="location" name="location" class="layui-input" value="" placeholder="点击选择所在地" maxlength="11">
<input type="text" id="location" name="location" class="layui-input" value="" placeholder="点击选择所在地" maxlength="11" lay-verify="required">
</div>
</div>
<div class="layui-col-lg6">
<label class="layui-form-label">所属网格</label>
<div class="layui-input-block layui-form" id="gridSelectTemplateBox" lay-filter="gridSelectTemplateBox"></div>
<script id="gridSelectTemplate" type="text/html">
<select id="grid" name="grid" lay-filter="grid">
<select id="grid" name="grid" lay-filter="grid" lay-verify="required">
<option value="">请选择网格</option>
{{# for(var i = 0, item; item = d[i++];) { }}
<option value="{{item.gridId}}">{{item.gridName}}</option>

View File

@ -25,14 +25,15 @@
<div class="layui-form-item">
<label class="layui-form-label">机构名称</label>
<div class="layui-input-block">
<input type="text" id="institutionName" name="institutionName" class="layui-input" value="" placeholder="请输入机构名称" maxlength="100">
<input type="text" id="institutionName" name="institutionName" class="layui-input" value=""
placeholder="请输入机构名称" maxlength="100" lay-verify="required">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">机构类型</label>
<div class="layui-input-block layui-form" id="institutionTypeIdSelectTemplateBox" lay-filter="institutionTypeIdSelectTemplateBox"></div>
<script id="institutionTypeIdSelectTemplate" type="text/html">
<select id="institutionTypeId" name="institutionTypeId">
<select id="institutionTypeId" name="institutionTypeId" lay-verify="required">
<option value="">请选择机构类型</option>
{{# for(var i = 0, item; item = d[i++];) { }}
<option value="{{item.dataId}}">{{item.dataName}}</option>
@ -44,7 +45,7 @@
<label class="layui-form-label">机构等级</label>
<div class="layui-input-block layui-form" id="institutionLevelIdSelectTemplateBox" lay-filter="institutionLevelIdSelectTemplateBox"></div>
<script id="institutionLevelIdSelectTemplate" type="text/html">
<select id="institutionLevelId" name="institutionLevelId">
<select id="institutionLevelId" name="institutionLevelId" lay-verify="required">
<option value="">请选择机构等级</option>
{{# for(var i = 0, item; item = d[i++];) { }}
<option value="{{item.dataId}}">{{item.dataName}}</option>
@ -58,14 +59,14 @@
<input type="hidden" id="locationId" name="locationId" class="layui-input" value="" placeholder="请输入一级区域ID" maxlength="36">
<input type="hidden" id="locationCode" name="locationCode" class="layui-input" value="" placeholder="请输入一级区域名称" maxlength="255">
<div class="layui-input-block">
<input type="text" id="location" name="location" class="layui-input" value="" placeholder="点击选择所在地" maxlength="11">
<input type="text" id="location" name="location" class="layui-input" value="" placeholder="点击选择所在地" maxlength="11" lay-verify="required">
</div>
</div>
<div class="layui-col-lg6">
<label class="layui-form-label">所属网格</label>
<div class="layui-input-block layui-form" id="gridSelectTemplateBox" lay-filter="gridSelectTemplateBox"></div>
<script id="gridSelectTemplate" type="text/html">
<select id="grid" name="grid" lay-filter="grid">
<select id="grid" name="grid" lay-filter="grid" lay-verify="required">
<option value="">请选择网格</option>
{{# for(var i = 0, item; item = d[i++];) { }}
<option value="{{item.gridId}}">{{item.gridName}}</option>
@ -77,7 +78,7 @@
<div class="layui-form-item layui-layout-admin">
<div class="layui-input-block">
<div class="layui-footer" style="left: 0;">
<button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交编辑</button>
<button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交新增</button>
<button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
</div>
</div>

View File

@ -29,6 +29,10 @@
<button type="button" id="search" class="layui-btn layui-btn-sm">
<i class="fa fa-lg fa-search"></i> 搜索
</button>
<button type="button" id="importExcel" class="layui-btn layui-btn-sm">
<i class="layui-icon">&#xe67c;</i> 导入
</button>
</div>
<table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
<!-- 表头按钮组 -->
@ -57,7 +61,7 @@
base: 'assets/layuiadmin/'
}).extend({
index: 'lib/index'
}).use(['index', 'table', 'laydate', 'common'], function() {
}).use(['index', 'table', 'laydate', 'common','upload'], function() {
var $ = layui.$;
var $win = $(window);
var table = layui.table;
@ -67,6 +71,41 @@
var resizeTimeout = null;
var tableUrl = 'api/school/listpage';
// 导入功能
function importExcel() {
var uploadLoading;
layui.upload.render({
elem: '#importExcel'
,url: 'api/school/importexcel'
,accept: 'file'
,exts: 'xls|xlsx'
,before: function() {
uploadLoading = layer.msg('正在上传,请稍后...', {icon: 16, time: 0, shade: 0.3});
}
,done: function(data) {
layer.close(uploadLoading);
if(data.data != 'success') {
layer.msg('导入成功,存在' + data.data + '条失败数据', {time: 2000}, function() {
window.open(top.restAjax.path('api/school/exportexcel', []));
});
} else {
layer.msg('导入成功', {time: 2000}, function() {
closeBox();
reloadTable();
});
}
}
,error: function(data, index){
layer.close(uploadLoading);
if(data != null) {
top.dialog.msg(data.msg);
}
},
});
}
importExcel()
// 初始化表格
function initTable() {
table.render({