|
@@ -1,9 +1,11 @@
|
|
|
package com.goer.project.model.modelEntity.service.impl;
|
|
package com.goer.project.model.modelEntity.service.impl;
|
|
|
|
|
|
|
|
-import java.util.Date;
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.goer.common.chiner.model.ColumnField;
|
|
import com.goer.common.chiner.model.ColumnField;
|
|
@@ -13,7 +15,13 @@ import com.goer.common.command.ExecResult;
|
|
|
import com.goer.common.command.impl.DBReverseGetTableDDLImpl;
|
|
import com.goer.common.command.impl.DBReverseGetTableDDLImpl;
|
|
|
import com.goer.common.utils.DateUtils;
|
|
import com.goer.common.utils.DateUtils;
|
|
|
import com.goer.common.utils.StringUtils;
|
|
import com.goer.common.utils.StringUtils;
|
|
|
|
|
+import com.goer.common.utils.file.FileUtils;
|
|
|
import com.goer.common.utils.fisok.raw.kit.JSONKit;
|
|
import com.goer.common.utils.fisok.raw.kit.JSONKit;
|
|
|
|
|
+import com.goer.common.utils.poi.ExcelHandlerAdapter;
|
|
|
|
|
+import com.goer.common.utils.poi.ExcelUtil;
|
|
|
|
|
+import com.goer.common.utils.reflect.ReflectUtils;
|
|
|
|
|
+import com.goer.common.utils.security.ShiroUtils;
|
|
|
|
|
+import com.goer.framework.aspectj.lang.annotation.Excel;
|
|
|
import com.goer.project.model.dataType.domain.DataType;
|
|
import com.goer.project.model.dataType.domain.DataType;
|
|
|
import com.goer.project.model.dataType.mapper.DataTypeMapper;
|
|
import com.goer.project.model.dataType.mapper.DataTypeMapper;
|
|
|
import com.goer.project.model.modelDbs.domain.ModelDbs;
|
|
import com.goer.project.model.modelDbs.domain.ModelDbs;
|
|
@@ -21,6 +29,12 @@ import com.goer.project.model.modelDbs.mapper.ModelDbsMapper;
|
|
|
import com.goer.project.model.modelField.domain.ModelField;
|
|
import com.goer.project.model.modelField.domain.ModelField;
|
|
|
import com.goer.project.model.modelField.mapper.ModelFieldMapper;
|
|
import com.goer.project.model.modelField.mapper.ModelFieldMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.goer.project.model.modelEntity.mapper.ModelEntityMapper;
|
|
import com.goer.project.model.modelEntity.mapper.ModelEntityMapper;
|
|
@@ -28,6 +42,9 @@ import com.goer.project.model.modelEntity.domain.ModelEntity;
|
|
|
import com.goer.project.model.modelEntity.service.IModelEntityService;
|
|
import com.goer.project.model.modelEntity.service.IModelEntityService;
|
|
|
import com.goer.common.utils.text.Convert;
|
|
import com.goer.common.utils.text.Convert;
|
|
|
|
|
|
|
|
|
|
+import static com.goer.common.utils.poi.ExcelUtil.getSheetPictures03;
|
|
|
|
|
+import static com.goer.common.utils.poi.ExcelUtil.getSheetPictures07;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 数据Service业务层处理
|
|
* 数据Service业务层处理
|
|
|
*
|
|
*
|
|
@@ -308,4 +325,239 @@ public class ModelEntityServiceImpl implements IModelEntityService
|
|
|
}
|
|
}
|
|
|
return "0";
|
|
return "0";
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导入文件表结构,转换成list
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param is 输入流
|
|
|
|
|
+ * @return 转换后集合
|
|
|
|
|
+ */
|
|
|
|
|
+ public int importTables(InputStream is, Long projectId) throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ int rtn = 0;
|
|
|
|
|
+
|
|
|
|
|
+ List<ModelEntity> mes = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ Date nowDate = DateUtils.getNowDate();
|
|
|
|
|
+
|
|
|
|
|
+ Workbook wb = WorkbookFactory.create(is);
|
|
|
|
|
+ // 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
|
|
|
|
|
+ int sheetCnt = wb.getNumberOfSheets();
|
|
|
|
|
+
|
|
|
|
|
+ if (sheetCnt == 0) {
|
|
|
|
|
+ throw new IOException("文件无sheet");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Sheet sheet = null;
|
|
|
|
|
+ ModelEntity cond = new ModelEntity();
|
|
|
|
|
+ cond.setProjectId(projectId);
|
|
|
|
|
+ cond.setVersionId(0L);
|
|
|
|
|
+ int intEntitySort = modelEntityMapper.selectMaxEntitySort(cond) + 1;
|
|
|
|
|
+
|
|
|
|
|
+ for (int i=0;i<sheetCnt;i++) {
|
|
|
|
|
+
|
|
|
|
|
+ sheet = wb.getSheetAt(i);
|
|
|
|
|
+ // 获取表名称(中文)
|
|
|
|
|
+ Row row1 = sheet.getRow(1);
|
|
|
|
|
+ String tableName = null;
|
|
|
|
|
+ if (row1.getCell(2) != null) {
|
|
|
|
|
+ tableName = getCellValue(row1,2).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 获取表逻辑名(英文)
|
|
|
|
|
+ Row row2 = sheet.getRow(2);
|
|
|
|
|
+ String tableLogicName = null;
|
|
|
|
|
+ if (row2.getCell(2)!= null) {
|
|
|
|
|
+ tableLogicName = getCellValue(row2,2).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取描述(中文)
|
|
|
|
|
+ Row row3 = sheet.getRow(3);
|
|
|
|
|
+ String tableDesc = null;
|
|
|
|
|
+ if (row3.getCell(2)!= null) {
|
|
|
|
|
+ tableDesc = getCellValue(row3,2).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.isEmpty(tableName) || StringUtils.isEmpty(tableLogicName)) {
|
|
|
|
|
+ throw new IOException("[" + sheet.getSheetName() + "]sheet无表名。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ModelEntity me = new ModelEntity();
|
|
|
|
|
+ me.setProjectId(projectId);
|
|
|
|
|
+ me.setVersionId(0L);
|
|
|
|
|
+ me.setEntityName(tableName);
|
|
|
|
|
+ me.setEntityCode(tableLogicName);
|
|
|
|
|
+ me.setRemark(tableDesc);
|
|
|
|
|
+ me.setEntitySort(intEntitySort);
|
|
|
|
|
+ me.setParentVersionId(0L);
|
|
|
|
|
+ me.setCreateBy(ShiroUtils.getLoginName());
|
|
|
|
|
+ me.setCreateTime(nowDate);
|
|
|
|
|
+ me.setUpdateBy(ShiroUtils.getLoginName());
|
|
|
|
|
+ me.setUpdateTime(nowDate);
|
|
|
|
|
+
|
|
|
|
|
+ List<ModelField> mfs = new ArrayList<>();
|
|
|
|
|
+ int rowIdx = 6;
|
|
|
|
|
+ int sort = 1;
|
|
|
|
|
+ while (StringUtils.isNotEmpty(getCellValue(sheet.getRow(rowIdx),1).toString()))
|
|
|
|
|
+ {
|
|
|
|
|
+ ModelField mf = new ModelField();
|
|
|
|
|
+
|
|
|
|
|
+ // 字段名(中文)
|
|
|
|
|
+ String fieldName = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(2) != null) {
|
|
|
|
|
+ fieldName = getCellValue(sheet.getRow(rowIdx),2).toString();
|
|
|
|
|
+ };
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldName)) {
|
|
|
|
|
+ throw new IOException("[" + sheet.getSheetName() + "]sheet,第"+ (rowIdx + 1) + "行,字段名为空。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 逻辑名(英文)
|
|
|
|
|
+ String fieldLogicName = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(3) != null) {
|
|
|
|
|
+ fieldLogicName = getCellValue(sheet.getRow(rowIdx),3).toString();
|
|
|
|
|
+ };
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldLogicName)) {
|
|
|
|
|
+ throw new IOException("[" + sheet.getSheetName() + "]sheet,第"+ (rowIdx+1) + "行,逻辑名为空。");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 类型(请选择)
|
|
|
|
|
+ String fieldType = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(4) != null) {
|
|
|
|
|
+ fieldType = getCellValue(sheet.getRow(rowIdx),4).toString();
|
|
|
|
|
+ };
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldType)) {
|
|
|
|
|
+ throw new IOException("[" + sheet.getSheetName() + "]sheet,第"+ (rowIdx+1) + "行,类型为空。");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 主键(请选择)
|
|
|
|
|
+ String fieldIsKey = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(5) != null) {
|
|
|
|
|
+ fieldIsKey = getCellValue(sheet.getRow(rowIdx), 5).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldIsKey)) {
|
|
|
|
|
+ fieldIsKey = "N";
|
|
|
|
|
+ }
|
|
|
|
|
+ // 非空(请选择默认为N)
|
|
|
|
|
+ String fieldNotNull = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(6) != null) {
|
|
|
|
|
+ fieldNotNull = getCellValue(sheet.getRow(rowIdx), 6).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldNotNull)) {
|
|
|
|
|
+ fieldNotNull = "N";
|
|
|
|
|
+ }
|
|
|
|
|
+ // 自增(请选择默认为N)
|
|
|
|
|
+ String fieldAuto = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(7) != null) {
|
|
|
|
|
+ fieldAuto = getCellValue(sheet.getRow(rowIdx), 7).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(fieldAuto)) {
|
|
|
|
|
+ fieldAuto = "N";
|
|
|
|
|
+ }
|
|
|
|
|
+ // 默认值
|
|
|
|
|
+ String fieldDefaultValue = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(8) != null) {
|
|
|
|
|
+ fieldDefaultValue = getCellValue(sheet.getRow(rowIdx), 8).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 备注
|
|
|
|
|
+ String fieldRemark = null;
|
|
|
|
|
+ if (sheet.getRow(rowIdx).getCell(9) != null) {
|
|
|
|
|
+ fieldRemark = getCellValue(sheet.getRow(rowIdx), 9).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mf.setProjectId(projectId);
|
|
|
|
|
+ mf.setVersionId(0L);
|
|
|
|
|
+ mf.setFieldName(fieldName);
|
|
|
|
|
+ mf.setFieldCode(fieldLogicName);
|
|
|
|
|
+ mf.setFieldType(fieldType);
|
|
|
|
|
+ mf.setFieldPk(fieldIsKey);
|
|
|
|
|
+ mf.setNotNull(fieldNotNull);
|
|
|
|
|
+ mf.setAutoIncrement(fieldAuto);
|
|
|
|
|
+ mf.setDefaultValue(fieldDefaultValue);
|
|
|
|
|
+ mf.setFieldSort(sort);
|
|
|
|
|
+ mf.setRemark(fieldRemark);
|
|
|
|
|
+ mf.setParentVersionId(0L);
|
|
|
|
|
+ mf.setCreateBy(ShiroUtils.getLoginName());
|
|
|
|
|
+ mf.setCreateTime(nowDate);
|
|
|
|
|
+ mf.setUpdateBy(ShiroUtils.getLoginName());
|
|
|
|
|
+ mf.setUpdateTime(nowDate);
|
|
|
|
|
+
|
|
|
|
|
+ mfs.add(mf);
|
|
|
|
|
+ rowIdx = rowIdx +1;
|
|
|
|
|
+ sort = sort + 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ me.setFields(mfs);
|
|
|
|
|
+ intEntitySort = intEntitySort + 1;
|
|
|
|
|
+ mes.add(me);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (ModelEntity modelEntity : mes) {
|
|
|
|
|
+ rtn = rtn + modelEntityMapper.insertModelEntity(modelEntity);
|
|
|
|
|
+
|
|
|
|
|
+ for (ModelField modelField : modelEntity.getFields()) {
|
|
|
|
|
+ modelField.setEntityId(modelEntity.getId());
|
|
|
|
|
+ rtn = rtn + modelFieldMapper.insertModelField(modelField);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return rtn;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取单元格值
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param row 获取的行
|
|
|
|
|
+ * @param column 获取单元格列号
|
|
|
|
|
+ * @return 单元格值
|
|
|
|
|
+ */
|
|
|
|
|
+ public Object getCellValue(Row row, int column)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (row == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ Object val = "";
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ Cell cell = row.getCell(column);
|
|
|
|
|
+ if (StringUtils.isNotNull(cell))
|
|
|
|
|
+ {
|
|
|
|
|
+ if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = cell.getNumericCellValue();
|
|
|
|
|
+ if (DateUtil.isCellDateFormatted(cell))
|
|
|
|
|
+ {
|
|
|
|
|
+ val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ if ((Double) val % 1 != 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = new BigDecimal(val.toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ val = new DecimalFormat("0").format(val);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (cell.getCellType() == CellType.STRING)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = cell.getStringCellValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (cell.getCellType() == CellType.BOOLEAN)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = cell.getBooleanCellValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (cell.getCellType() == CellType.ERROR)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = cell.getErrorCellValue();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ return val;
|
|
|
|
|
+ }
|
|
|
|
|
+ return val;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|