|
|
|
@ -11,7 +11,10 @@ import com.aliyuncs.utils.StringUtils; |
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow; |
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet; |
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
|
import org.apache.poi.ss.usermodel.Row; |
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRow; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
|
@ -31,6 +34,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.jeecg.modules.tablex.entity.Tablex; |
|
|
|
|
import org.jeecg.modules.tablex.service.ITablexService; |
|
|
|
|
import org.jeecg.common.system.base.controller.JeecgController; |
|
|
|
|
import org.jsoup.helper.StringUtil; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
@ -301,15 +305,97 @@ public class FieldxController extends JeecgController<Fieldx, IFieldxService> { |
|
|
|
|
return Result.error("文件格式上传有误"); |
|
|
|
|
} |
|
|
|
|
//根据格式读取
|
|
|
|
|
InputStream inputStream=file.getInputStream(); |
|
|
|
|
Workbook workbook=null; |
|
|
|
|
if (path.endsWith(".xls")) { |
|
|
|
|
workbook=new HSSFWorkbook(inputStream); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
workbook = new XSSFWorkbook(inputStream); |
|
|
|
|
} |
|
|
|
|
//读取表
|
|
|
|
|
List<Fieldx222> list = new ArrayList<>(); |
|
|
|
|
//获取excel的所有页
|
|
|
|
|
int numberofSheets= workbook.getNumberOfSheets(); |
|
|
|
|
for (int i=0;i< numberofSheets;i++) |
|
|
|
|
{ |
|
|
|
|
Sheet Sheet=workbook.getSheetAt(i); |
|
|
|
|
//判断表结构
|
|
|
|
|
Row row=Sheet.getRow(3); |
|
|
|
|
if(row==null) |
|
|
|
|
return Result.error("表结构不能为空"); |
|
|
|
|
|
|
|
|
|
if (path.endsWith(".xls")) { |
|
|
|
|
list = readXLS(file); |
|
|
|
|
} else list = readXLSX(file); |
|
|
|
|
for(int r=3;r<=Sheet.getLastRowNum();r++) { |
|
|
|
|
Row cells = Sheet.getRow(r); |
|
|
|
|
String cell = cells.getCell(0).getStringCellValue(); |
|
|
|
|
if (StringUtils.isEmpty(cell)) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
ExcelInfo excelInfo = new ExcelInfo(); |
|
|
|
|
excelInfo.setcells(cells); |
|
|
|
|
Fieldx222 fieldx222 = new Fieldx222(); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getTableId())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的表名不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setTableId(excelInfo.getTableId()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getFieldName())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的中文名称不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setFieldName(excelInfo.getFieldName()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getFieldEnName())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的英文名称不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setFieldEnName(excelInfo.getFieldEnName()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getType())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的数据类型不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setType(excelInfo.getType()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getLength())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的数据长度不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setLength(excelInfo.getLength()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getIsPk())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的是否主键不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setIsPk(excelInfo.getIsPk()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getIsFk())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的是否外键不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setIsFk(excelInfo.getIsFk()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getIsNull())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的是否为空不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setIsNull(excelInfo.getIsNull()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getIsUniqueness())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的是否重复不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setIsUniqueness(excelInfo.getIsUniqueness()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getVerisonStatus())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的字段状态不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setVerisonStatus(excelInfo.getVerisonStatus()); |
|
|
|
|
if (StringUtil.isBlank(excelInfo.getVerison())) { |
|
|
|
|
return Result.error("序号为" + (r - 2) + "的版本状态不能为空"); |
|
|
|
|
} |
|
|
|
|
fieldx222.setVerison(excelInfo.getVerison()); |
|
|
|
|
|
|
|
|
|
if (!StringUtils.isEmpty(excelInfo.getAccuracy())) |
|
|
|
|
fieldx222.setAccuracy(excelInfo.getAccuracy()); |
|
|
|
|
if (!StringUtils.isEmpty(excelInfo.getFormat())) |
|
|
|
|
fieldx222.setFormat(excelInfo.getFormat()); |
|
|
|
|
if (!StringUtils.isEmpty(excelInfo.getAssociateTable())) |
|
|
|
|
fieldx222.setAssociateTable(excelInfo.getAssociateTable()); |
|
|
|
|
if (!StringUtils.isEmpty(excelInfo.getAssociateField())) |
|
|
|
|
fieldx222.setAssociateField(excelInfo.getAssociateField()); |
|
|
|
|
if (!StringUtils.isEmpty(excelInfo.getRule())) |
|
|
|
|
fieldx222.setRull(excelInfo.getRule()); |
|
|
|
|
list.add(fieldx222); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fieldx222Service.saveBatch(list); |
|
|
|
|
List<Fieldx222> list222 = fieldx222Service.list(); |
|
|
|
|
fieldx222Service.remove(null); |
|
|
|
|
if(list222==null) |
|
|
|
|
return Result.error("文件上传失败"); |
|
|
|
|
List<Fieldx> list1= list222.stream().map((item)->{ |
|
|
|
@ -338,86 +424,10 @@ public class FieldxController extends JeecgController<Fieldx, IFieldxService> { |
|
|
|
|
fieldx.setRule(item.getRull()); |
|
|
|
|
return fieldx; |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
fieldx222Service.remove(null); |
|
|
|
|
fieldxService.saveBatch(list1); |
|
|
|
|
return Result.OK("文件上传成功"); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
public List<Fieldx222> readXLS(MultipartFile file) throws IOException { |
|
|
|
|
List<Fieldx222> list=new ArrayList<>(); |
|
|
|
|
InputStream inputStream=file.getInputStream(); |
|
|
|
|
HSSFWorkbook workbook=new HSSFWorkbook(inputStream); |
|
|
|
|
//读取表
|
|
|
|
|
HSSFSheet hssfSheet=workbook.getSheetAt(0); |
|
|
|
|
for(int r=3;r<=hssfSheet.getLastRowNum();r++){ |
|
|
|
|
HSSFRow cells =hssfSheet.getRow(r); |
|
|
|
|
String cell=cells.getCell(0).getStringCellValue(); |
|
|
|
|
if(StringUtils.isEmpty(cell)){ |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
ExcelInfo excelInfo = new ExcelInfo(); |
|
|
|
|
excelInfo.setcells(cells); |
|
|
|
|
Fieldx222 fieldx=copy(excelInfo); |
|
|
|
|
list.add(fieldx); |
|
|
|
|
} |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
public List<Fieldx222> readXLSX(MultipartFile file) throws IOException { |
|
|
|
|
List<Fieldx222> list=new ArrayList<>(); |
|
|
|
|
InputStream inputStream=file.getInputStream(); |
|
|
|
|
XSSFWorkbook workbook=new XSSFWorkbook(inputStream); |
|
|
|
|
//读取表
|
|
|
|
|
XSSFSheet hssfSheet=workbook.getSheetAt(0); |
|
|
|
|
for(int r=3;r<=hssfSheet.getLastRowNum();r++){ |
|
|
|
|
XSSFRow cells =hssfSheet.getRow(r); |
|
|
|
|
String cell=cells.getCell(0).getStringCellValue(); |
|
|
|
|
if(StringUtils.isEmpty(cell)){ |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
ExcelInfo excelInfo=new ExcelInfo(); |
|
|
|
|
excelInfo.setcells(cells); |
|
|
|
|
Fieldx222 fieldx=copy(excelInfo); |
|
|
|
|
list.add(fieldx); |
|
|
|
|
} |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
private Fieldx222 copy(ExcelInfo excelInfo){ |
|
|
|
|
Fieldx222 fieldx222=new Fieldx222(); |
|
|
|
|
if(pdNull(excelInfo.getTableId())||pdNull((excelInfo.getFieldName()))||pdNull((excelInfo.getFieldEnName()))|| |
|
|
|
|
pdNull((excelInfo.getType()))||pdNull((excelInfo.getLength()))||pdNull((excelInfo.getIsPk()))|| |
|
|
|
|
pdNull((excelInfo.getIsFk()))||pdNull((excelInfo.getIsNull()))||pdNull((excelInfo.getIsUniqueness()))|| |
|
|
|
|
pdNull((excelInfo.getVerison()))||pdNull((excelInfo.getVerison()))) |
|
|
|
|
return null; |
|
|
|
|
fieldx222.setTableId(excelInfo.getTableId()); |
|
|
|
|
fieldx222.setFieldName(excelInfo.getFieldName()); |
|
|
|
|
fieldx222.setFieldEnName(excelInfo.getFieldEnName()); |
|
|
|
|
fieldx222.setType(excelInfo.getType()); |
|
|
|
|
fieldx222.setLength(excelInfo.getLength()); |
|
|
|
|
fieldx222.setIsPk(excelInfo.getIsPk()); |
|
|
|
|
fieldx222.setIsFk(excelInfo.getIsFk()); |
|
|
|
|
fieldx222.setIsNull(excelInfo.getIsNull()); |
|
|
|
|
fieldx222.setIsUniqueness(excelInfo.getIsUniqueness()); |
|
|
|
|
fieldx222.setVerisonStatus(excelInfo.getVerisonStatus()); |
|
|
|
|
fieldx222.setVerison(excelInfo.getVerison()); |
|
|
|
|
if(!StringUtils.isEmpty(excelInfo.getAccuracy())) |
|
|
|
|
fieldx222.setAccuracy(excelInfo.getAccuracy()); |
|
|
|
|
if(!StringUtils.isEmpty(excelInfo.getFormat())) |
|
|
|
|
fieldx222.setFormat(excelInfo.getFormat()); |
|
|
|
|
if(!StringUtils.isEmpty(excelInfo.getAssociateTable())) |
|
|
|
|
fieldx222.setAssociateTable(excelInfo.getAssociateTable()); |
|
|
|
|
if(!StringUtils.isEmpty(excelInfo.getAssociateField())) |
|
|
|
|
fieldx222.setAssociateField(excelInfo.getAssociateField()); |
|
|
|
|
if(!StringUtils.isEmpty(excelInfo.getRule())) |
|
|
|
|
fieldx222.setRull(excelInfo.getRule()); |
|
|
|
|
return fieldx222; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean pdNull(Object object){ |
|
|
|
|
if (object == null) |
|
|
|
|
return true; |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 生成sql
|
|
|
|
|
public String createSql(String tableId){ |
|
|
|
|