Commit 703733d3 authored by neo's avatar neo

[dev] financeDataUpload impl and importFinancedata impl

parent 1119f37d
package pwc.taxtech.atms.common.util;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.exception.ApiException;
import pwc.taxtech.atms.exception.FormulaException;
......
......@@ -6,6 +6,8 @@ public class VatErpImportValidation {
public static final String BalanceTb = "Balance";
public static final String AccountRemapTb = "AccountRemap";
public static final String VoucherTbName = "Voucher";
public static final String ImportMethod_OneClick = "oneClick";
public static final String ImportMethod_Universal = "universal";
public static final String Const_ComBalanceAcctValidate = "试算平衡表科目存在于企业科目列表中";
public static final String Const_ComBalanceAcctUnValidate = "试算平衡表中出现的科目不在科目表中";
......
package pwc.taxtech.atms.constant.enums;
public enum ImportType {
Override(0),
Append(1);
private Integer code;
ImportType(Integer code) {
this.code = code;
}
public Integer getCode() {
return code;
}
}
package pwc.taxtech.atms.controller;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
......@@ -11,6 +13,9 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.thymeleaf.util.StringUtils;
import pwc.taxtech.atms.constant.DataImport;
import pwc.taxtech.atms.constant.VatErpImportValidation;
import pwc.taxtech.atms.constant.enums.ImportType;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.vatdto.ImportDataDto;
import pwc.taxtech.atms.service.IdentityService;
......@@ -22,8 +27,10 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
......@@ -36,6 +43,8 @@ public class FinanceDataImportController {
private final static String UniversalUploadExcelPath = "~/FinanceDataUpload/UniversalExcelFile";
private final static String UniversalUploadCsvPath = "~/FinanceDataUpload/UniversalCsvlFile";
private static Logger logger = LoggerFactory.getLogger(FinanceDataImportController.class);
@Autowired
private FinanceDataImportServiceImpl financeDataImport;
@Autowired
......@@ -106,118 +115,89 @@ public class FinanceDataImportController {
}
@RequestMapping(value = "importData/importFinancedata", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity FinanceImportData() {
// string userId = _identityService.IdentityUser.ID;
// string importMethod = HttpContext.Current.Request.Params["importMethod"];
// string fileName = HttpContext.Current.Request.Params["fileName"];
// string importTypeStr = HttpContext.Current.Request.Params["importType"];
// string fileGuid = HttpContext.Current.Request.Params["fileGuid"];
// string periodStr = HttpContext.Current.Request.Params["period"];
// string fileExtension = HttpContext.Current.Request.Params["fileExtension"];
// string strProjectYear = HttpContext.Current.Request.Params["projectYear"];
// #region 参数为空即返回
// if (string.IsNullOrEmpty(importMethod))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "importMethod is empty" });
// }
//
// if (string.IsNullOrEmpty(fileName))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "fileName is empty" });
// }
//
// if (string.IsNullOrEmpty(importTypeStr))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "importType is empty" });
// }
//
// if (string.IsNullOrEmpty(fileGuid))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "fileGuid is empty" });
// }
//
// if (string.IsNullOrEmpty(periodStr))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "periodStr is empty" });
// }
//
// if (string.IsNullOrEmpty(fileExtension))
// {
// return this.Ok(new OperationResultDto() { Result = false, ResultMsg = "fileExtension is empty" });
// }
//
// #endregion
// int importType = int.Parse(importTypeStr);
// int projectYear = !string.IsNullOrEmpty(strProjectYear) ? int.Parse(strProjectYear) : -1;
// int period = int.Parse(periodStr);
// int importFileCount = 0;
// var mappedPath = SetUploadFolderPath(importMethod, fileExtension);
// OperationResultDto<List<ImportDataDto>> result = new OperationResultDto<List<ImportDataDto>>();
// List<ImportDataDto> importResults = new List<ImportDataDto>();
// List<string> fileList = fileName.Split(',').ToList();
// List<string> toDeleteFilesPath = new List<string>();
// #region 导入数据
//
// if (importType == (int)DataImport.ImportType.Override)
// {
// //如果导入文件只有voucher的话,那么就不用删除CustBalance表中当前期间的数据
// bool isClearCustTb = fileList.Count == 1 && fileList.First().ToLower().StartsWith(VatGlobal.VoucherTbName.ToLower()) ? false : true;
// //1. 删除TB相关表的数据
// _financeDataImport.ClearTable(period,isClearCustTb);
// }
// //2. 删除ValidationInfo中Erp相关的数据
// _financeDataImport.DeleteErpValidation();
//
// //3. 开始导入数据
// foreach (var f in fileList)
// {
// var fileFullPath = Path.Combine(mappedPath, fileGuid + "_" + Path.GetFileName(f));
// var schemaPath = HostingEnvironment.MapPath("~/Resource");
// toDeleteFilesPath.Add(fileFullPath);
// var importResult = _financeDataImport.ImportData(projectYear, importMethod, importType, period, fileFullPath, f, schemaPath, userId);
// if (importResult.Result)
// {
// importFileCount++;
// }
// else
// {
// importResults.Add(importResult);
// }
// }
//
// if (importFileCount == fileList.Count)
// {
// _financeDataImport.UpdateCompanyBalanceNullValue();
// result.Result = true;
// result.ResultMsg = string.Empty;
// result.Data = importResults;
// toDeleteFilesPath.ForEach(f =>
// {
// if (File.Exists(f))
// {
// File.Delete(f);
// _logger.Log(LogLevel.Info, "成功删除财务数据导入文件:" + f);
// }
// });
//
// }
// else
// {
// result.Result = false;
// result.Data = importResults;
// result.ResultMsg = "导入数据出错: " + "\r\n";
// foreach (var r in importResults)
// {
// result.ResultMsg += r.FileName + "\r\n";
// }
// }
//
// #endregion
//
// return Ok(result);
return ResponseEntity.ok().build();
@RequestMapping(value = "importData/importFinancedata", method = RequestMethod.POST, produces = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity FinanceImportData(@RequestParam String importMethod, @RequestParam String fileName,
@RequestParam String importType, @RequestParam String fileGuid,
@RequestParam Integer period, @RequestParam String fileExtension,
@RequestParam String projectYear, MultipartHttpServletRequest request) {
String userId = identityService.getIdentityUser().getID();
if (StringUtils.isEmpty(importMethod)) {
return ResponseEntity.ok(new OperationResultDto(false, "importMethod is empty"));
}
if (StringUtils.isEmpty(fileName)) {
return ResponseEntity.ok(new OperationResultDto(false, "fileName is empty"));
}
if (StringUtils.isEmpty(importType)) {
return ResponseEntity.ok(new OperationResultDto(false, "importType is empty"));
}
if (StringUtils.isEmpty(fileGuid)) {
return ResponseEntity.ok(new OperationResultDto(false, "fileGuid is empty"));
}
if (period == null) {
return ResponseEntity.ok(new OperationResultDto(false, "periodStr is empty"));
}
if (StringUtils.isEmpty(fileExtension)) {
return ResponseEntity.ok(new OperationResultDto(false, "fileExtension is empty"));
}
Integer projectYearInt = !StringUtils.isEmpty(projectYear) ? Integer.parseInt(projectYear) : -1;
int importFileCount = 0;
String mappedPath = SetUploadFolderPath(importMethod, fileExtension);
OperationResultDto<List<ImportDataDto>> result = new OperationResultDto<>();
List<ImportDataDto> importResults = new ArrayList<>();
String[] fileList = fileName.split(",");
List<String> toDeleteFilesPath = new ArrayList<>();
if (importType.equals(ImportType.Override.getCode() + "")) {
//如果导入文件只有voucher的话,那么就不用删除CustBalance表中当前期间的数据
Boolean isClearCustTb = fileList.length == 1 && fileList[0].toLowerCase().startsWith(VatErpImportValidation.VoucherTbName.toLowerCase()) ? false : true;
//1. 删除TB相关表的数据
financeDataImport.ClearTable(period, isClearCustTb);
}
//2. 删除ValidationInfo中Erp相关的数据
financeDataImport.DeleteErpValidation();
//3. 开始导入数据
for (MultipartFile f : request.getFileMap().values()) {
String fileFullPath = Paths.get(mappedPath, fileGuid + "_" + f.getName()).toString();
String schemaPath = "~/Resource";
toDeleteFilesPath.add(fileFullPath);
ImportDataDto importResult = financeDataImport.ImportData(projectYear, importMethod, importType, period, fileFullPath, f, schemaPath, userId);
if (importResult.Result) {
importFileCount++;
} else {
importResults.add(importResult);
}
}
if (importFileCount == fileList.length) {
financeDataImport.UpdateCompanyBalanceNullValue();
result.setResult(true);
result.setResultMsg("");
result.setData(importResults);
toDeleteFilesPath.forEach(f ->
{
if (new File(f).exists()) {
new File(f).delete();
logger.info("成功删除财务数据导入文件:" + f);
}
});
} else {
result.setResult(false);
result.setData(importResults);
String msg = "导入数据出错: " + "\r\n";
for (ImportDataDto m : importResults)
msg += (m.FileName + "\r\n");
result.setResultMsg(msg);
}
return ResponseEntity.ok(result);
}
private Boolean IsSameFileExtenstion(Map<String, MultipartFile> files) {
......
package pwc.taxtech.atms.vat.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.constant.VatErpImportValidation;
import pwc.taxtech.atms.constant.enums.EnumValidationType;
import pwc.taxtech.atms.constant.enums.ErpCheckType;
import pwc.taxtech.atms.constant.enums.ImageType;
import pwc.taxtech.atms.dto.FieldsMapper;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.vatdto.DataCheckDto;
import pwc.taxtech.atms.dto.vatdto.ImportDataDto;
import pwc.taxtech.atms.dto.vatdto.VoucherAccountMto;
import pwc.taxtech.atms.dto.vatdto.VoucherBalanceDto;
import pwc.taxtech.atms.dto.vatdto.VoucherDto;
......@@ -21,10 +30,13 @@ import pwc.taxtech.atms.vat.entity.VoucherExample;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.sound.sampled.DataLine;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Comparator;
......@@ -45,6 +57,9 @@ public class FinanceDataImportServiceImpl {
private ValidationInfoMapper validationInfoMapper;
@Autowired
private VoucherMapper voucherMapper;
@Autowired
private SqlSessionTemplate dynamicSqlSessionTemplate;
private Logger logger = LoggerFactory.getLogger(FinanceDataImportServiceImpl.class);
public OperationResultDto<List<DataCheckDto>> getBasicValidationResults(String userId) {
Integer number = 1;
......@@ -322,4 +337,105 @@ public class FinanceDataImportServiceImpl {
private List<ValidationInfo> getValidationData(Integer typeId) {
return getValidationData(typeId, null);
}
public Boolean ClearTable(int period, Boolean isClearCustTb) {
if (isClearCustTb == null) isClearCustTb = true;
ClearCustTable(period, VatErpImportValidation.CompanyBalanceTbName);
if (isClearCustTb) {
ClearCustTable(period, VatErpImportValidation.CustBalanceTb);
}
ClearCustTable(period, VatErpImportValidation.BalanceTb);
ClearVoucherTable(period);
ClearRemapTable(period);
return true;
}
private void ClearVoucherTable(int period) {
String command = IsImportByPeriod(period)
? "DELETE FROM " + VatErpImportValidation.VoucherTbName + " WHERE Period = " + period
: "TRUNCATE TABLE " + VatErpImportValidation.VoucherTbName;
exeCommand(command);
}
private boolean IsImportByPeriod(int period) {
return period >= 0;
}
private void ClearRemapTable(int period) {
String command = IsImportByPeriod(period)
? "DELETE FROM " + VatErpImportValidation.AccountRemapTb + " WHERE Period = " + period
: "TRUNCATE TABLE " + VatErpImportValidation.AccountRemapTb;
exeCommand(command);
}
private void ClearCustTable(int period, String tbName) {
String command = "";
if (IsImportByPeriod(period) && tbName != VatErpImportValidation.BalanceTb) {
if (tbName == VatErpImportValidation.CompanyBalanceTbName) {
command = String.format("DELETE FROM %s WHERE PeriodId = %s", tbName, period);
}
if (tbName == VatErpImportValidation.CustBalanceTb) {
command = String.format("DELETE FROM %s WHERE PeriodId >= %s", tbName, period);
}
} else {
command = "TRUNCATE TABLE " + tbName;
}
exeCommand(command);
}
private void exeCommand(String command) {
try (SqlSession sqlSession = dynamicSqlSessionTemplate.getSqlSessionFactory().openSession();) {
ScriptRunner scriptRunner = new ScriptRunner(sqlSession.getConnection());
scriptRunner.setAutoCommit(true);
scriptRunner.runScript(new StringReader(command));
}
}
public void DeleteErpValidation() {
List<Integer> deleteTypes = Lists.<Integer>newArrayList(EnumValidationType.ErpBasicCheck.getCode(),
EnumValidationType.ErpCorrect.getCode(),
EnumValidationType.ErpDuplicate.getCode());
ValidationInfoExample example = new ValidationInfoExample();
example.createCriteria().andImportTypeIdIn(deleteTypes);
validationInfoMapper.deleteByExample(example);
}
public ImportDataDto ImportData(String projectYear, String importMethod, String importType, Integer period,
String fileFullPath, MultipartFile f, String schemaPath, String userId) {
logger.info("开始导入数据: importMethod: {0}, importType: {1}", importMethod, importType);
ImportDataDto importResult = new ImportDataDto();
String ZipFileFolder = "";
try {
if (importMethod == VatErpImportValidation.ImportMethod_OneClick) {
// UnZipDataFile(fileFullPath, schemaPath);//todo: import data tobe continue(neo)
// ImportBalance(ZipFileFolder, period);
// ImportVoucher(ZipFileFolder, period);
}
if (importMethod == VatErpImportValidation.ImportMethod_Universal) {
// UniversalImportData(period, fileFullPath, f.getName(), schemaPath);
}
// SynAccountFromTaxAdmin(period, userId);
importResult.Result = true;
} catch (Exception ex) {
importResult.Result = false;
}
return importResult;
}
public void UpdateCompanyBalanceNullValue() {
String command = "UPDATE CompanyBalance SET BegDebitBal = 0 WHERE BegDebitBal IS NULL"
+ " UPDATE CompanyBalance SET BegCreditBal = 0 WHERE BegCreditBal IS NULL"
+ " UPDATE CompanyBalance SET BegBal = 0 WHERE BegBal IS NULL"
+ " UPDATE CompanyBalance SET EndBal = 0 WHERE EndBal IS NULL"
+ " UPDATE CompanyBalance SET EndDebitBal = 0 WHERE EndDebitBal IS NULL"
+ " UPDATE CompanyBalance SET EndCreditBal = 0 WHERE EndCreditBal IS NULL"
+ " UPDATE CompanyBalance SET DebitBal = 0 WHERE DebitBal IS NULL"
+ " UPDATE CompanyBalance SET CreditBal = 0 WHERE CreditBal IS NULL";
exeCommand(command);
}
}
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