Commit 51821c29 authored by neo.wang's avatar neo.wang

Merge branch 'dev_neo2' into 'dev'

[DEV] get file content upload file

See merge request root/atms!26
parents 00e5666c 2e04d14f
...@@ -24,4 +24,8 @@ public final class Constant { ...@@ -24,4 +24,8 @@ public final class Constant {
public static final UUID USER_ID_FOR_UPLOAD = UUID.fromString("64D39CF7-467E-4414-B334-AA4B55A4E2B3"); public static final UUID USER_ID_FOR_UPLOAD = UUID.fromString("64D39CF7-467E-4414-B334-AA4B55A4E2B3");
public static final String UPLOAD_FOLDER_NAME="Upload"; public static final String UPLOAD_FOLDER_NAME="Upload";
public static final String CURRENT_USER_HOME="~";
public static final String CURRENT_USER_UPLOAD_FOLDER=CURRENT_USER_HOME+File.separator+UPLOAD_FOLDER_NAME;
public static final boolean DEFAULT_RESULT=true;
} }
\ No newline at end of file
package pwc.taxtech.atms.constant;
public class VATCommon {
public static final String Const_ImportFileError = "文件为空或者格式不正确";
public static final int inValidNum = -1;
public static final String ValidationExisted = "validation existed";
public static final String ReValidate = "revalidate";
public static final int WholeYearPeriod = -1;
}
...@@ -3,8 +3,10 @@ package pwc.taxtech.atms.controller; ...@@ -3,8 +3,10 @@ package pwc.taxtech.atms.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import pwc.taxtech.atms.common.CommonUtils; import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.dto.OperationResultDto; import pwc.taxtech.atms.dto.OperationResultDto;
...@@ -14,8 +16,16 @@ import pwc.taxtech.atms.dto.vatdto.*; ...@@ -14,8 +16,16 @@ import pwc.taxtech.atms.dto.vatdto.*;
import pwc.taxtech.atms.service.DataImportService; import pwc.taxtech.atms.service.DataImportService;
import pwc.taxtech.atms.service.ICitTBDataImportService; import pwc.taxtech.atms.service.ICitTBDataImportService;
import pwc.taxtech.atms.service.IdentityService; import pwc.taxtech.atms.service.IdentityService;
import pwc.taxtech.atms.utils.NPOIHelper;
import pwc.taxtech.atms.vat.dao.DataImportModel;
import pwc.taxtech.atms.vat.service.TBDataImportService; import pwc.taxtech.atms.vat.service.TBDataImportService;
import pwc.taxtech.atms.vat.service.impl.FileUploadAdapter;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -36,6 +46,8 @@ public class TBDataImportController { ...@@ -36,6 +46,8 @@ public class TBDataImportController {
private DataImportService dataImportService; private DataImportService dataImportService;
@Autowired @Autowired
private IdentityService identityService; private IdentityService identityService;
@Autowired
private FileUploadAdapter fileUploadAdapter;
@RequestMapping(value = "GetBalanceDataForDisplay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "GetBalanceDataForDisplay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
...@@ -384,6 +396,7 @@ public class TBDataImportController { ...@@ -384,6 +396,7 @@ public class TBDataImportController {
identityService.getIdentityUser().getID() identityService.getIdentityUser().getID()
); );
} }
@RequestMapping(value = "GetParentCodesForDisplay", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "GetParentCodesForDisplay", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody public @ResponseBody
String GetParentCodesForDisplay(String category) { String GetParentCodesForDisplay(String category) {
...@@ -433,5 +446,28 @@ public class TBDataImportController { ...@@ -433,5 +446,28 @@ public class TBDataImportController {
initParentCodeTree(tempNode.getChildren(), jsonParentCodesLookup.getAcctCode(), sources); initParentCodeTree(tempNode.getChildren(), jsonParentCodesLookup.getAcctCode(), sources);
} }
} }
@ApiOperation(value = "getFileContent", notes = "")
@RequestMapping(value = "FileContent/{tempFileName}/{selectedSheetIndex}/{topRowNumber}", method = RequestMethod.GET)
public ResponseEntity getFileContent(@PathVariable String tempFileName, @PathVariable Integer selectedSheetIndex,
@PathVariable Integer topRowNumber) {
DataImportModel dataImportModel;
String fullFilePath = fileUploadAdapter.getLastFilePathByFileName(tempFileName);
if (fullFilePath != null) {
try(FileInputStream fis = new FileInputStream(new File(fullFilePath));) {
dataImportModel = NPOIHelper.renderTBDataTableFromExcel(fis, selectedSheetIndex, 0, true, topRowNumber);
dataImportModel.setSelectedSheetIndex(selectedSheetIndex);
return ResponseEntity.ok(dataImportModel);
} catch (FileNotFoundException e) {//TODO: replace exception filter (neo)
e.printStackTrace();
return ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
}
} else {
return ResponseEntity.ok().build();
}
}
} }
package pwc.taxtech.atms.utils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import pwc.taxtech.atms.constant.VATCommon;
import pwc.taxtech.atms.vat.dao.DataImportModel;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static pwc.taxtech.atms.constant.Constant.DEFAULT_RESULT;
public class NPOIHelper {
public static DataImportModel renderTBDataTableFromExcel(FileInputStream fis, Integer selectedSheetIndex,
int headerRowIndex, boolean haveHeader, Integer topRowNumber) {
DataImportModel dataImportModel = new DataImportModel();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(selectedSheetIndex);
dataImportModel.setDataList(new ArrayList<>());
dataImportModel.setSheetNameList(new ArrayList<>());
dataImportModel.setResult(DEFAULT_RESULT);
try {
//TODO:should be replace by biz exception (neo)
if (!(workbook instanceof XSSFWorkbook)) throw new RuntimeException("not support workbook type");
XSSFFormulaEvaluator eva = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
int numberOfSheets = workbook.getNumberOfSheets();
for (int index = 0; index < numberOfSheets; index++) {
dataImportModel.getSheetNameList().add(workbook.getSheetAt(index).getSheetName());
}
//判断文件是否为空
if (sheet.getPhysicalNumberOfRows() == 0) {
dataImportModel.setResult(false);
dataImportModel.setResultMsg(VATCommon.Const_ImportFileError);
} else {
//var headerRow = sheet.GetRow(HeaderRowIndex);
int cellCount = 0;
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row != null && row.getPhysicalNumberOfCells() > cellCount) {
cellCount = row.getPhysicalNumberOfCells();
}
}
List<String> rowList = new ArrayList<>();
//dataImportModel.dataList.Add(rowList);
dataImportModel.setLastRowIndex(sheet.getLastRowNum());
int RowStart = sheet.getFirstRowNum();
for (int i = RowStart; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row != null) {
rowList = getOutputInvoiceRowValueList(eva, row, cellCount);
dataImportModel.getDataList().add(rowList);
}
}
}
} catch (Exception ex) {
dataImportModel.setResult(false);
dataImportModel.setResultMsg(VATCommon.Const_ImportFileError);
}
if (topRowNumber > 0) {
//第一行为列头的话就多输出一行
int topNum = haveHeader ? topRowNumber + 1 : topRowNumber;
dataImportModel.setDataList(dataImportModel.getDataList().subList(0,topNum));
}
return dataImportModel;
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return null;
}
private static List<String> getOutputInvoiceRowValueList(XSSFFormulaEvaluator eva, Row row, int cellCount) {
List<String> rowList = new ArrayList<>();
for (int j = 0; j < cellCount; j++) {
if (row.getCell(j) != null) {
rowList.add(getStringCellValue(eva, row.getCell(j)));
} else {
rowList.add(null);
}
}
return rowList;
}
private static String getStringCellValue(XSSFFormulaEvaluator eva, Cell cell) {
// 获取单元格数据内容为字符串类型的数据
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strCell = "";
switch (cell.getCellTypeEnum()) {
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
strCell = sdf.format(cell.getDateCellValue());
} else {
strCell = cell.getNumericCellValue() + "";
}
break;
case FORMULA:
try {
if (eva.evaluate(cell).getCellTypeEnum() == NUMERIC) {
strCell = eva.evaluate(cell).getNumberValue() + "";
} else {
strCell = eva.evaluate(cell).getStringValue();
}
} catch (Exception ex) {
strCell = "#N/A";
}
break;
default:
strCell = cell.toString();
break;
}
return strCell;
}
}
package pwc.taxtech.atms.vat.dao;
import java.util.List;
public class DataImportModel {
private List<String> sheetNameList;
private List<List<String>> dataList;
private List<String> mappingResult;
private int selectedSheetIndex;
private int lastRowIndex;
private Boolean result;
private String resultMsg;
public List<String> getSheetNameList() {
return sheetNameList;
}
public void setSheetNameList(List<String> sheetNameList) {
this.sheetNameList = sheetNameList;
}
public List<List<String>> getDataList() {
return dataList;
}
public void setDataList(List<List<String>> dataList) {
this.dataList = dataList;
}
public List<String> getMappingResult() {
return mappingResult;
}
public void setMappingResult(List<String> mappingResult) {
this.mappingResult = mappingResult;
}
public int getSelectedSheetIndex() {
return selectedSheetIndex;
}
public void setSelectedSheetIndex(int selectedSheetIndex) {
this.selectedSheetIndex = selectedSheetIndex;
}
public int getLastRowIndex() {
return lastRowIndex;
}
public void setLastRowIndex(int lastRowIndex) {
this.lastRowIndex = lastRowIndex;
}
public Boolean getResult() {
return result;
}
public void setResult(Boolean result) {
this.result = result;
}
public String getResultMsg() {
return resultMsg;
}
public void setResultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}
}
...@@ -21,6 +21,7 @@ import java.util.Calendar; ...@@ -21,6 +21,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
import static pwc.taxtech.atms.constant.Constant.CURRENT_USER_UPLOAD_FOLDER;
import static pwc.taxtech.atms.constant.Constant.UPLOAD_FOLDER_NAME; import static pwc.taxtech.atms.constant.Constant.UPLOAD_FOLDER_NAME;
@Service @Service
...@@ -38,6 +39,17 @@ public class FileUploadAdapter { ...@@ -38,6 +39,17 @@ public class FileUploadAdapter {
private boolean containsSubFolder = true;//TODO this should not use val should use parameter(neo) private boolean containsSubFolder = true;//TODO this should not use val should use parameter(neo)
public String getLastFilePathByFileName(String tempFileName) {
String uploadFolder = combine(CURRENT_USER_UPLOAD_FOLDER, getSubFolder());
String fileFullPath = combine(uploadFolder, tempFileName);
if (new File(fileFullPath).exists()) {
return fileFullPath;
}
return null;
}
static class FileParamBean { static class FileParamBean {
String fileName; String fileName;
...@@ -127,13 +139,9 @@ public class FileUploadAdapter { ...@@ -127,13 +139,9 @@ public class FileUploadAdapter {
} }
private String prepareFolder() { private String prepareFolder() {
String uploadFolder = String.format("~"+File.separator+"%s", UPLOAD_FOLDER_NAME); String uploadFolder = CURRENT_USER_UPLOAD_FOLDER;
if (containsSubFolder) { if (containsSubFolder) {
Calendar now = Calendar.getInstance(); uploadFolder = combine(uploadFolder, getSubFolder());
now.setTime(new Date());
String subUploadFolder = String.format("%s" + File.separator + "%s", now.get(Calendar.MONTH), now.get(Calendar.DATE));
uploadFolder = String.format("%s" + File.separator + "%s", uploadFolder, subUploadFolder);
File file = new File(uploadFolder); File file = new File(uploadFolder);
if (!file.exists()) file.mkdirs(); if (!file.exists()) file.mkdirs();
...@@ -141,4 +149,14 @@ public class FileUploadAdapter { ...@@ -141,4 +149,14 @@ public class FileUploadAdapter {
return uploadFolder; return uploadFolder;
} }
private String getSubFolder() {
Calendar now = Calendar.getInstance();
now.setTime(new Date());
return String.format("%s" + File.separator + "%s", now.get(Calendar.MONTH), now.get(Calendar.DATE));
}
private String combine(String parent, String child) {
return String.format("%s" + File.separator + "%s", parent, child);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment