Browse Source

BI-71455 fix: 根据attachId可能会找不到excel附件,干脆直接根据excel的抽数数据来导入数据库

upgrade
Zhenfei.Li 5 years ago
parent
commit
2a6bf0d85a
  1. 24
      src/main/java/com/fr/plugin/decision/HWUtils.java
  2. 72
      src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java

24
src/main/java/com/fr/plugin/decision/HWUtils.java

@ -1,9 +1,10 @@
package com.fr.plugin.decision;
import com.finebi.burger.api.bean.configuration.table.field.FineBusinessField;
import com.finebi.foundation.api.structure.result.BIDetailCell;
import com.finebi.foundation.api.structure.result.BIDetailResult;
import com.fr.cache.Attachment;
import com.fr.cache.AttachmentSource;
import com.fr.general.CommonDateUtils;
import com.fr.general.DateUtils;
import com.fr.general.Inter;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
@ -107,6 +108,25 @@ public class HWUtils {
return result;
}
public static Map<String, List> getValues(List<FineBusinessField> fields, BIDetailResult detailResult) {
Map<String, List> result = new HashMap<String, List>();
while (detailResult.hasNext()) {
List<BIDetailCell> cells = detailResult.next();
for (int i = 0; i < fields.size(); ++i) {
String fieldName = fields.get(i).getName();
Object cellValue = cells.get(i).getData() != null ? cells.get(i).getData() : StringUtils.EMPTY;
if (result.containsKey(fieldName)) {
result.get(fieldName).add(cellValue.toString());
} else {
List<Object> rowList = new ArrayList<>();
rowList.add(cellValue.toString());
result.put(fieldName, rowList);
}
}
}
return result;
}
public static String getCellValue(XSSFSheet sheet, List<CellRangeAddress> combineCell, int row, int col, XSSFCell cell) {
int firstC = 0;
int lastC = 0;

72
src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java

@ -1,8 +1,9 @@
package com.fr.plugin.decision.request.handler;
import com.finebi.activate.foundation.StableManager;
import com.finebi.burger.api.bean.configuration.table.attachment.FineAttachment;
import com.finebi.burger.api.bean.configuration.table.field.FineBusinessField;
import com.finebi.burger.api.bean.configuration.table.page.ConfLimitInfo;
import com.finebi.burger.api.bean.configuration.table.page.ConfPageInfo;
import com.finebi.burger.api.request.configuration.table.DataBaseAddTableItemRequestBean;
import com.finebi.burger.api.request.configuration.table.DataBaseAddTableRequestBean;
import com.finebi.burger.api.response.configuration.table.TableAddResponseBean;
@ -12,9 +13,11 @@ import com.finebi.common.impl.vo.table.FineDBBusinessTable;
import com.finebi.direct.burger.api.service.FineTableService;
import com.finebi.direct.burger.api.service.FineUserInfoService;
import com.finebi.common.api.cell.user.authority.UserAuthority;
import com.finebi.direct.common.impl.engine.BurgerTableDataCellImpl;
import com.finebi.foundation.api.reponse.FineRespond;
import com.finebi.foundation.api.service.FineService;
import com.finebi.foundation.api.service.context.BaseContext;
import com.finebi.foundation.api.structure.result.BIDetailResult;
import com.finebi.utils.CompareUtils;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.json.JSONArray;
@ -30,11 +33,6 @@ import com.fr.stable.StringUtils;
import com.fr.third.fasterxml.jackson.core.JsonGenerationException;
import com.fr.third.fasterxml.jackson.databind.JsonMappingException;
import com.fr.third.fasterxml.jackson.databind.ObjectMapper;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.fr.third.v2.org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFSheet;
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.finebi.direct.common.api.cell.context.CellCreator;
import com.finebi.common.impl.vo.table.FineBusinessTable;
import com.finebi.common.impl.vo.table.FineExcelBusinessTable;
@ -56,6 +54,7 @@ public class UpgradeHandler extends HWAbstractHandler {
protected void deal(HttpServletRequest req, HttpServletResponse res, JSONObject para, JSONObject result) throws Exception {
String connection = para.getString("connection");
UserAuthority authorityUser = getFineUserInfoService().getUserAuthority(req);
Connection conn = JdbcUtils.getConnection(connection);
@ -75,57 +74,22 @@ public class UpgradeHandler extends HWAbstractHandler {
allTables.stream().filter(fineBusinessTable -> fineBusinessTable instanceof FineExcelBusinessTable).limit(15).forEach(fineBusinessTable -> {
Map<String, List> values = new HashMap<String, List>();
JSONArray items = JSONArray.create();
FineAttachment sheetInfo = ((FineExcelBusinessTable) fineBusinessTable).getBaseAttach();
String attachId = sheetInfo.getId();
String fileName = sheetInfo.getFileName();
int sheetNo = 0;
List<FineBusinessField> fields = ((FineExcelBusinessTable) fineBusinessTable).getExcelFields();
try {
if (fileName.endsWith(".xlsx")) {
XSSFWorkbook excelWork = HWUtils.getExcelWork(attachId);
if(excelWork != null){
SXSSFWorkbook swb = new SXSSFWorkbook(excelWork,100);
XSSFWorkbook xssfWorkbook = swb.getXSSFWorkbook();
XSSFSheet sheet = xssfWorkbook.getSheetAt(sheetNo);
JSONObject tableCol = JSONObject.create();
for(int i = 0; i < fields.size(); i++){
FineBusinessField field = fields.get(i);
tableCol.put(field.getName(), i);
JSONObject item = new JSONObject();
item.put("colLength", "8000");
item.put("colName", field.getName());
item.put("colType", this.getFieldSqlType(field.getType()));
item.put("excelCol", i);
item.put("id", field.getId());
items.put(item);
}
values = HWUtils.getSheetContent(sheet, 0, tableCol);
}
} else if (fileName.endsWith(".xls")) {
HSSFWorkbook excel2003Work = HWUtils.getExcel2003Work(attachId);
if(excel2003Work != null){
HSSFSheet sheetAt = excel2003Work.getSheetAt(sheetNo);
JSONObject tableCol = JSONObject.create();
for(int i = 0; i < fields.size(); i++){
FineBusinessField field = fields.get(i);
tableCol.put(field.getName(), i);
JSONObject item = new JSONObject();
item.put("colLength", "8000");
item.put("colName", field.getName());
item.put("colType", this.getFieldSqlType(field.getType()));
item.put("excelCol", i);
item.put("id", field.getId());
items.put(item);
}
values = HWUtils.get2003SheetContent(sheetAt,0, tableCol);
}
Optional<BIDetailResult> optionalBIDetailResult = new BurgerTableDataCellImpl().getRealDataWithAuth(fineBusinessTable, new ConfLimitInfo(500000, new ConfPageInfo(500000, 1)), authorityUser);
BIDetailResult detailResult = optionalBIDetailResult.get();
values = HWUtils.getValues(fields, detailResult);
for(int i = 0; i < fields.size(); i++){
FineBusinessField field = fields.get(i);
JSONObject item = new JSONObject();
item.put("colLength", "8000");
item.put("colName", field.getName());
item.put("colType", this.getFieldSqlType(field.getType()));
item.put("excelCol", i);
item.put("id", field.getId());
items.put(item);
}
} catch (Exception e) {
e.printStackTrace();
@ -153,8 +117,6 @@ public class UpgradeHandler extends HWAbstractHandler {
JSONArray tables = JSONArray.create().put(table);
JSONObject tablesBean = JSONObject.create().put("tables", tables);
try{
UserAuthority authorityUser = getFineUserInfoService().getUserAuthority(req);
Optional<FineBusinessPackage> packageIdWithoutAuth = CellCreator.getCellProvider().packageCell().getPackageByTableNameWithoutAuth(tableName);
FineBusinessPackage businessPackage = packageIdWithoutAuth.get();
String packId = businessPackage.getId();

Loading…
Cancel
Save