Commit aae9f6a9 authored by neo's avatar neo

[Optimial] fixed gen data to async

parent 178b5b36
......@@ -32,22 +32,11 @@ public class ReportController {
return reportService.getReportTemplate(projectId, EnumServiceType.getEnumByCode(serviceType), period);
}
@RequestMapping(value = "updateConfig/{projectId}/{ifDeleteManualDataSource}/{period}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public OperationResultDto updateConfig(@PathVariable String projectId,
@PathVariable Boolean ifDeleteManualDataSource,
@PathVariable Integer period,
@RequestParam String generator,
@RequestParam Boolean mergeManual) {
return reportService.updateConfig(projectId, period, ifDeleteManualDataSource, generator, mergeManual);
// OperationResultDto operationResultDto = new OperationResultDto();
// operationResultDto.setResult(true);
// return operationResultDto;
}
@RequestMapping(value = "generateByTotal/{projectId}/{ifDeleteManualDataSource}/{period}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public OperationResultDto generateAllData(@PathVariable String projectId, @PathVariable Boolean ifDeleteManualDataSource,
@PathVariable Integer period, @RequestParam Optional<String> generator) {
return reportService.generateData(projectId, EnumServiceType.VAT, ifDeleteManualDataSource, period, null, generator);
@PathVariable Integer period, @RequestParam Optional<String> generator,
@RequestParam Boolean mergeManual) {
return reportService.generateData(projectId, EnumServiceType.VAT, mergeManual, period, null, generator);
}
@RequestMapping(value = "templateReferences/{period}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
......
package pwc.taxtech.atms.dto.vatdto;
import lombok.Getter;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.entity.Project;
import pwc.taxtech.atms.entity.TemplateGroup;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.service.impl.Message;
import pwc.taxtech.atms.vat.entity.PeriodCellTemplate;
import pwc.taxtech.atms.vat.entity.PeriodCellTemplateConfig;
import pwc.taxtech.atms.vat.entity.PeriodTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Getter
public class PeriodResources {
Project project;
List<PeriodTemplate> periodTemplates = new ArrayList<>();
List<PeriodCellTemplate> periodCellTemplates = new ArrayList<>();
List<PeriodCellTemplateConfig> periodCellTemplateConfigs = new ArrayList<>();
public void setProject(Project project) {
this.project = project;
}
public void putAllTemplate(List<PeriodTemplate> periodTemplates) {
periodTemplates.addAll(periodTemplates);
}
public List<Long> getTemolateIds() {
MyAsserts.assertNotEmpty(periodTemplates, Exceptions.NOT_FOUND_TEMPLATE_EXCEPTION);
return periodTemplates.stream()
.map(PeriodTemplate::getTemplateId)
.collect(Collectors.toList());
}
public void putAllCellTemplate(List<PeriodCellTemplate> periodCellTemplates) {
periodCellTemplates.addAll(periodCellTemplates);
}
public void putAllCellTemplateConfig(List<PeriodCellTemplateConfig> periodCellTemplateConfigs) {
periodCellTemplateConfigs.addAll(periodCellTemplateConfigs);
}
public Long getTemplateGroupId(){
return periodTemplates.get(0).getTemplateGroupId();
}
}
package pwc.taxtech.atms.dto.vatdto;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.entity.Project;
import pwc.taxtech.atms.entity.Template;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.PeriodJob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class WrapPeriodJobDto {
public static final String STATUS_BEGIN = "Begin";
public static final String STATUS_CANCEL = "Cancel";
public static final String STATUS_ERROR = "Error";
public static final String STATUS_END = "End";
public static final String STEP_UPDATE_CONFIG = "UpdateConfig";
public static PeriodJob createReportGenJob(String projectId, Integer period, List<Template> templates) {
MyAsserts.assertNotEmpty(templates, Exceptions.NOT_FOUND_TEMPLATE_EXCEPTION);
PeriodJob job = new PeriodJob();
job.setCreateTime(new Date());
job.setName("Gen All Report");
job.setProjectId(projectId);
job.setPeriod(period);
job.setId(UUID.randomUUID().toString());
job.setStatus(STATUS_BEGIN);
job.setCurrentStep(STEP_UPDATE_CONFIG);
StringBuilder builder = new StringBuilder(STEP_UPDATE_CONFIG);
templates.forEach(m -> {
builder.append(",").append(m.getCode());
});
job.setStepsCode(builder.toString());
return job;
}
}
package pwc.taxtech.atms.exception;
import org.springframework.http.ResponseEntity;
import javax.net.ssl.SSLEngineResult;
import static org.springframework.http.HttpStatus.CONFLICT;
public class ConflictException extends ApiException {
public ConflictException() {
super();
}
public ConflictException(String message) {
super(message);
}
public ConflictException(String message, Throwable cause) {
super(message, cause);
}
public ConflictException(Throwable cause) {
super(cause);
}
@Override
public <Object> ResponseEntity handle() {
return ResponseEntity.status(CONFLICT).build();
}
}
......@@ -10,6 +10,11 @@ public class Exceptions {
public static final ApiException EMPTY_PROJECT_PARAM = new BadParameterException("project is empty");
public static final ApiException EMPTY_PRIODDATE_PARAM = new BadParameterException("period data is empty");
public static final ApiException NOT_FOUND_REPORT_EXCEPTION = new NotFoundException("not found report");
public static final ApiException NOT_FOUND_TEMPLATE_EXCEPTION = new NotFoundException("not found template");
public static final ApiException REPORT_HAS_COMMIT_EXCEPTION = new AlreadyExistsException("report approval has commit");
public static final ApiException SERVER_ERROR_EXCEPTION= new ServerErrorException("server error exception");
public static final ApiException SERVER_ERROR_EXCEPTION = new ServerErrorException("server error exception");
public static final ApiException TASK_HAS_BEGINNING = new ConflictException("task has beginning ..");
public static final ApiException PROJECT_EMPTY_EXCEPTION = new BadParameterException("projectId is empty");
public static final ApiException PROJECT_PROJECT_EXCEPTION = new NotFoundException("not found project");
public static final ApiException NOT_FOUND_TEMPLATE_GROUP_EXCEPTION = new NotFoundException("not found template group");
}
......@@ -20,7 +20,9 @@ import pwc.taxtech.atms.constant.enums.CellDataSourceType;
import pwc.taxtech.atms.constant.enums.FormulaDataSourceType;
import pwc.taxtech.atms.dao.ProjectMapper;
import pwc.taxtech.atms.dto.vatdto.CellCalcInfoDto;
import pwc.taxtech.atms.entity.Project;
import pwc.taxtech.atms.dto.vatdto.PeriodResources;
import pwc.taxtech.atms.dto.vatdto.WrapPeriodJobDto;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.service.impl.DistributedIdService;
import pwc.taxtech.atms.service.impl.HttpFileService;
import pwc.taxtech.atms.vat.dao.*;
......@@ -63,50 +65,33 @@ public class ReportGeneratorImpl {
private PeriodTemplateMapper periodTemplateMapper;
@Autowired
private DistributedIdService distributedIdService;
@Autowired
private PeriodJobMapper periodJobMapper;
public String generateData(String projectId, List<Long> templateIds, Boolean ifDeleteManualDataSource,
List<String> exceptCellTemplates, Integer periodParam, Optional<String> generator) {
int period = periodParam != null ? periodParam : 0;
Project project = projectMapper.selectByPrimaryKey(projectId);
if (templateIds.isEmpty()) templateIds.add(Long.MAX_VALUE);
List<PeriodTemplate> periodTemplateList = queryPeriodTemplates(projectId, period, templateIds);
List<Long> periodTemplateIdList = periodTemplateList.stream()
.map(PeriodTemplate::getTemplateId)
.collect(Collectors.toList());
Long templateGroupId = periodTemplateList.size() > 0 ? periodTemplateList.get(0).getTemplateGroupId() : 0;
List<PeriodCellTemplate> periodCellTemplateList = queryPeriodCellTemplates(projectId, period, periodTemplateIdList);
List<PeriodCellTemplateConfig> periodCellTemplateConfigList = queryPeriodCellTemplateConfigs(projectId, period, periodTemplateIdList);
Optional<Workbook> workbook = createWorkBookWithExcelFileList(periodTemplateList);
if (workbook.isPresent()) {
Workbook newWorkbook = workbook.get();
FormulaContext formulaContext = FormulaContext.extractContextFromProject(project).fixedFormula(period, templateGroupId, formulaAgent);
addFunctionsToWorkbook(newWorkbook, formulaContext);
setConfigAndDataToWorkBook(newWorkbook, periodTemplateList, periodCellTemplateList, periodCellTemplateConfigList);
FormulaEvaluator evaluator = newWorkbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
List<PeriodReport> reports = createReportsByTemplates(workbook.get(), periodTemplateList, projectId, period);
updateWorkbookCaclsValueToDb(projectId,period,workbook.get(),reports,periodTemplateList,periodCellTemplateList,periodCellTemplateConfigList);
return "generate report successful";
} else {
return "GenerateReport failed";
public FormulaContext initContext(PeriodResources resources, Integer period) {
return FormulaContext.extractContextFromProject(resources.getProject()).fixedFormula(period, resources.getTemplateGroupId(),
formulaAgent);
}
public PeriodResources getPeriodResources(String projectId, Integer period, List<Long> templateIds) {
PeriodResources periodResources = new PeriodResources();
periodResources.putAllTemplate(queryPeriodTemplates(projectId, period, templateIds));
periodResources.putAllCellTemplate(queryPeriodCellTemplates(projectId, period, periodResources.getTemolateIds()));
periodResources.putAllCellTemplateConfig(queryPeriodCellTemplateConfigs(projectId, period, periodResources.getTemolateIds()));
periodResources.setProject(projectMapper.selectByPrimaryKey(projectId));
return periodResources;
}
private void updateWorkbookCaclsValueToDb(String projectId,Integer period,Workbook workbook,List<PeriodReport> periodReports,
List<PeriodTemplate> periodTemplateList,
List<PeriodCellTemplate> periodCellTemplateList,List<PeriodCellTemplateConfig> periodCellTemplateConfigList){
public void updateWorkbookCaclsValueToDb(String projectId, Integer period, Workbook workbook, PeriodResources resources, PeriodJob job) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
String code = sheet.getSheetName();
Optional<PeriodTemplate> periodTemplate = periodTemplateList.stream()
Optional<PeriodTemplate> periodTemplate = resources.getPeriodTemplates().stream()
.filter(a -> a.getCode().equals(code))
.findFirst();
try {
Long templateId;
if (periodTemplate.isPresent()) {
templateId = periodTemplate.get().getTemplateId();
......@@ -114,23 +99,25 @@ public class ReportGeneratorImpl {
templateId = 0L;
}
Optional<PeriodReport> periodReport = periodReports.stream()
.filter(a -> a.getTemplateId().equals(templateId))
.findFirst();
Long reportId;
if (periodReport.isPresent()) {
reportId = periodReport.get().getId();
} else {
reportId = 0L;
}
if (templateId > 0) {
List<PeriodCellTemplateConfig> periodCellTemplateConfigs = periodCellTemplateConfigList.stream()
PeriodReport report = new PeriodReport();
Long reportId = distributedIdService.nextId();
report.setId(reportId);
report.setTemplateId(templateId);
report.setPeriod(period);
report.setProjectId(projectId);
report.setCreateBy("Admin");
report.setCreateTime(new Date());
report.setUpdateBy("Admin");
report.setUpdateTime(new Date());
report.setProjectId(projectId);
reportMapper.insertSelective(report);
List<PeriodCellTemplateConfig> periodCellTemplateConfigs = resources.getPeriodCellTemplateConfigs().stream()
.filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.collect(Collectors.toList());
List<PeriodCellTemplateConfig> keyInCellTemplateConfigs = periodCellTemplateConfigList.stream()
List<PeriodCellTemplateConfig> keyInCellTemplateConfigs = resources.getPeriodCellTemplateConfigs().stream()
.filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.KeyIn.getCode()))
.collect(Collectors.toList());
......@@ -262,7 +249,7 @@ public class ReportGeneratorImpl {
}
}
Optional<PeriodCellTemplate> tempPeriodCellTemplate = periodCellTemplateList.stream()
Optional<PeriodCellTemplate> tempPeriodCellTemplate = resources.getPeriodCellTemplates().stream()
.filter(a -> a.getCellTemplateId().equals(periodCellTemplateConfig.getCellTemplateId()))
.findFirst();
if (tempPeriodCellTemplate.isPresent()) {
......@@ -345,6 +332,16 @@ public class ReportGeneratorImpl {
}
}
}
} catch (Exception e) {
e.printStackTrace();
job.setStatus(WrapPeriodJobDto.STATUS_ERROR);
job.setErrorMsg("cacals report for code "+code +" failed");
}finally {
job.setCurrentStep(code);
periodJobMapper.updateByPrimaryKey(job);
}
}
}
......@@ -383,14 +380,12 @@ public class ReportGeneratorImpl {
return reports;
}
private void setConfigAndDataToWorkBook(Workbook workbook, List<PeriodTemplate> periodTemplates,
List<PeriodCellTemplate> periodCellTemplates,
List<PeriodCellTemplateConfig> periodCellTemplateConfigs) {
public void setConfigAndDataToWorkBook(Workbook workbook, PeriodResources resources) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
String code = sheet.getSheetName();
Optional<PeriodTemplate> periodTemplate = periodTemplates.stream()
Optional<PeriodTemplate> periodTemplate = resources.getPeriodTemplates().stream()
.filter(a -> a.getCode().equals(code))
.findFirst();
Long templateId;
......@@ -403,11 +398,11 @@ public class ReportGeneratorImpl {
if (templateId > 0) {
//get cell template and cell template config with templateId
Map<PeriodCellTemplate, PeriodCellTemplateConfig> cellTemplatePeriodCellTemplateConfigMap = new HashMap<>();
List<PeriodCellTemplate> tempPeriodCellTemplateList = periodCellTemplates.stream()
List<PeriodCellTemplate> tempPeriodCellTemplateList = resources.getPeriodCellTemplates().stream()
.filter(a -> a.getReportTemplateId().equals(templateId))
.collect(Collectors.toList());
tempPeriodCellTemplateList.forEach(a -> {
Optional<PeriodCellTemplateConfig> tempCellTemplateConfig = periodCellTemplateConfigs.stream()
Optional<PeriodCellTemplateConfig> tempCellTemplateConfig = resources.getPeriodCellTemplateConfigs().stream()
.filter(item -> item.getCellTemplateId().equals(a.getCellTemplateId())
&& item.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.findFirst();
......@@ -480,7 +475,7 @@ public class ReportGeneratorImpl {
* @param templates 模板code和模板路径 键值对
* @return 工作簿workbook
*/
private Optional<Workbook> createWorkBookWithExcelFileList(List<PeriodTemplate> templates) {
public Workbook createWorkBookByPeriodTemplate(List<PeriodTemplate> templates, PeriodJob periodJob) {
Workbook workbook = new XSSFWorkbook();
try {
String filePath = this.getClass().getResource("").toURI().getPath();
......@@ -516,11 +511,14 @@ public class ReportGeneratorImpl {
}
POIUtil.cloneSheet(tWorkbook.getSheetAt(0), workbook.createSheet(a.getCode()));
});
return Optional.of(workbook);
return workbook;
} catch (Exception e) {
logger.error("createWorkBookWithExcelFileList error.", e);
logger.error("createWorkBookByPeriodTemplate error.", e);
periodJob.setStatus(WrapPeriodJobDto.STATUS_ERROR);
periodJob.setErrorMsg("error for gen excel , redo it later!!1");
periodJobMapper.updateByPrimaryKey(periodJob);
throw Exceptions.SERVER_ERROR_EXCEPTION;
}
return Optional.empty();
}
/**
......@@ -528,12 +526,11 @@ public class ReportGeneratorImpl {
*
* @param workbook 工作簿
*/
private void addFunctionsToWorkbook(Workbook workbook, FormulaContext formulaContext) {
String[] functionNames = {"SGSR", "FSJZ", "ND", "BB", "XXFP", "GZSD", "ProjectContext", "JXFPMX", "JXFP"};
public void addFunctionsAndContext(Workbook workbook, String[] functions, FormulaContext formulaContext) {
FreeRefFunction[] functionImpls = {new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext),
new BB(formulaContext), new XXFP(formulaContext), new GZSD(formulaContext), new ProjectContext(formulaContext)
, new JXFPMX(formulaContext), new JXFP(formulaContext)};
UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
UDFFinder udfs = new DefaultUDFFinder(functions, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
workbook.addToolPack(udfToolpack);
}
......
......@@ -2,8 +2,9 @@ package pwc.taxtech.atms.vat.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.grapecity.documents.excel.Workbook;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.reflections.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -12,15 +13,14 @@ import org.springframework.stereotype.Component;
import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.common.util.SpringContextUtil;
import pwc.taxtech.atms.constant.Constant;
import pwc.taxtech.atms.constant.enums.*;
import pwc.taxtech.atms.dao.*;
import pwc.taxtech.atms.dpo.ReportDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.vatdto.*;
import pwc.taxtech.atms.entity.*;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.exception.NotFoundException;
import pwc.taxtech.atms.exception.NotSupportedException;
import pwc.taxtech.atms.service.impl.CellConfigTranslater;
import pwc.taxtech.atms.service.impl.DistributedIdService;
import pwc.taxtech.atms.vat.dao.*;
......@@ -28,14 +28,14 @@ import pwc.taxtech.atms.vat.dpo.DataSourceCellDataDto;
import pwc.taxtech.atms.vat.dpo.DataSourceExtendDto;
import pwc.taxtech.atms.vat.dpo.InputVATInvoiceItemExtendDto;
import pwc.taxtech.atms.vat.entity.*;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -43,6 +43,8 @@ import java.util.stream.Collectors;
@Component
public class ReportServiceImpl {
private final static Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);
private BlockingQueue<PeriodJob> queue = new LinkedBlockingQueue<>();
private final static String[] functions = {"SGSR", "FSJZ", "ND", "BB", "XXFP", "GZSD", "ProjectContext", "JXFPMX", "JXFP"};
@Autowired
private ReportGeneratorImpl reportGenerator;
......@@ -96,6 +98,8 @@ public class ReportServiceImpl {
private ProjectMapper projectMapper;
@Autowired
private DistributedIdService distributedIdService;
@Autowired
private PeriodJobMapper periodJobMapper;
public OperationResultDto<List<ReportDto>> getReportTemplate(String projectId, EnumServiceType serviceType, Integer periodParam) {
int period = periodParam != null ? periodParam : 0;
......@@ -255,39 +259,33 @@ public class ReportServiceImpl {
}
public OperationResultDto updateConfig(String projectId, Integer period, Boolean ifDeleteManualDataSource,
String generator, Boolean isMergeManualData) {
OperationResultDto result = new OperationResultDto();
private void updateConfig(String projectId, Integer period, Boolean isMergeManualData, List<Template> templates, PeriodJob job) {
try {
if (period == null) {
result.setResultMsg("peirod is null");
return result;
}
Project project = projectMapper.selectByPrimaryKey(projectId);
if (project == null) {
result.setResultMsg("NoProject");
return result;
}
List<Long> exceptTemplateIds = templateMapper.getIdsForExceptTemplate();
Long templateGroupId = projectMapper.getTemplateGroupIdByProject(projectId, EnumServiceType.VAT.getCode());
if (templateGroupId != null && templateGroupId != 0) {
// 先进行数据清理,包括period开头的所有报表配置表 条件Period
clearPeriodData(projectId, period, exceptTemplateIds, isMergeManualData);
copyTemplateAndConfigFromAdmin(projectId, templateGroupId, period);
} else {
result.setResult(true);
result.setResultMsg("there is no templateGroup");
return result;
}
result.setResult(true);
copyTemplateAndConfigFromAdmin(projectId, templates, period);
} catch (Exception ex) {
result.setResult(false);
job.setStatus(WrapPeriodJobDto.STATUS_ERROR);
job.setErrorMsg("error update config with projectId " + projectId + " period" + period);
periodJobMapper.updateByPrimaryKey(job);
logger.error(ex.getMessage(), ex);
}
return result;
}
private List<Template> getTemplatesByProjectId(String projectId) {
MyAsserts.assertEmpty(projectId, Exceptions.PROJECT_PROJECT_EXCEPTION);
Project project = projectMapper.selectByPrimaryKey(projectId);
MyAsserts.assertNotNull(project, Exceptions.NOT_FOUND_REPORT_EXCEPTION);
Long templateGroupId = projectMapper.getTemplateGroupIdByProject(projectId, EnumServiceType.VAT.getCode());
MyAsserts.assertNotNull(templateGroupId, Exceptions.NOT_FOUND_TEMPLATE_GROUP_EXCEPTION);
List<Template> templates = queryTemplateByGroup(templateGroupId);
MyAsserts.assertNotEmpty(templates, Exceptions.NOT_FOUND_TEMPLATE_EXCEPTION);
templates.sort(Comparator.comparing(Template::getOrderIndex));
return templates;
}
private void copyPeriodConfigFromCellTemplateConfig(String projectId, Long templateId, Integer period) {
......@@ -421,11 +419,13 @@ public class ReportServiceImpl {
periodCellTemplateMapper.batchInsert(periodCellTemplateList);
}
private void copyTemplateAndConfigFromAdmin(String projectId, Long templateGroupId, Integer period) {
private List<Template> queryTemplateByGroup(Long templateGroupId) {
TemplateExample example = new TemplateExample();
example.createCriteria().andTemplateGroupIdEqualTo(templateGroupId).andIsActiveAssociationEqualTo(true);
List<Template> templates = templateMapper.selectByExample(example);
return templateMapper.selectByExample(example);
}
private void copyTemplateAndConfigFromAdmin(String projectId, List<Template> templates, Integer period) {
List<PeriodTemplate> periodTemplateList = new ArrayList<>();
for (Template template : templates) {
Long startTime = System.currentTimeMillis();
......@@ -458,7 +458,8 @@ public class ReportServiceImpl {
periodTemplateMapper.batchInsert(periodTemplateList);
}
public OperationResultDto generateData(String projectId, EnumServiceType serviceType, Boolean ifDeleteManualDataSource,
public OperationResultDto generateData(String projectId, EnumServiceType serviceType, Boolean isMergeManualData,
Integer periodParam, Integer reportType, Optional<String> generator) {
OperationResultDto operationResultDto = new OperationResultDto();
try {
......@@ -468,13 +469,38 @@ public class ReportServiceImpl {
}
MyAsserts.assertEq(serviceType, EnumServiceType.VAT, new NotFoundException());
List<Long> templateIds = queryTemplates(projectId, periodParam, reportType, serviceType.getCode().toString());
PeriodJobExample example = new PeriodJobExample();
example.createCriteria().andProjectIdEqualTo(projectId).andPeriodEqualTo(periodParam)
.andStatusEqualTo(WrapPeriodJobDto.STATUS_BEGIN);
MyAsserts.assertEmpty(periodJobMapper.selectByExample(example), Exceptions.TASK_HAS_BEGINNING);
String rslt = reportGenerator.generateData(projectId, templateIds, ifDeleteManualDataSource, null, periodParam, generator);
if (StringUtils.isBlank(rslt)) {
operationResultDto.setResultMsg("ReportGenerateFailed!");
return operationResultDto;
List<Template> templates = getTemplatesByProjectId(projectId);
PeriodJob genJob = WrapPeriodJobDto.createReportGenJob(projectId, periodParam, templates);
periodJobMapper.insert(genJob);
new Thread(new Runnable() {
@Override
public void run() {
updateConfig(projectId, periodParam, isMergeManualData, templates, genJob);
PeriodResources resources = reportGenerator.getPeriodResources(projectId, periodParam,
templates.stream().map(Template::getId).collect(Collectors.toList()));
Workbook workbook = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob);
reportGenerator.addFunctionsAndContext(workbook,functions, reportGenerator.initContext(resources, periodParam));
reportGenerator.setConfigAndDataToWorkBook(workbook,resources);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
reportGenerator.updateWorkbookCaclsValueToDb(projectId,periodParam,workbook,resources,genJob);
genJob.setStatus(WrapPeriodJobDto.STATUS_END);
periodJobMapper.updateByPrimaryKey(genJob);
}
}).start();
operationResultDto.setData(genJob.getId());
operationResultDto.setResult(true);
} catch (Exception ex) {
operationResultDto.setResult(false);
......@@ -483,35 +509,6 @@ public class ReportServiceImpl {
return operationResultDto;
}
private List<Long> queryTemplates(String projectId, Integer period, Integer reportType, String serviceTypeStr) {
ProjectServiceTypeExample projectServiceTypeExample = new ProjectServiceTypeExample();
projectServiceTypeExample.createCriteria().andServiceTypeIdEqualTo(serviceTypeStr).andProjectIdEqualTo(projectId);
Optional<Long> templateGroupId = projectServiceTypeMapper.selectByExample(projectServiceTypeExample).stream()
.map(ProjectServiceType::getTemplateGroupId).findFirst();
MyAsserts.assertNotNull(templateGroupId, new NotFoundException("not found group"));
List<Long> templateIds;
if (reportType != null) {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria()
.andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period)
.andIsActiveAssociationEqualTo(true)
.andReportTypeEqualTo(reportType);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
} else {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria().andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period).andIsActiveAssociationEqualTo(true);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
}
return templateIds;
}
public List<CellTemplateReferenceDto> getTemplateReferences(int period) {
return new ArrayList<>();
}
......@@ -1221,7 +1218,7 @@ public class ReportServiceImpl {
public String export(String reportData, String serverPath) {
String filePath = String.format("GeneratedReports\tt_{0}.xlsx", UUID.randomUUID().toString());
String fullFilePath = combine(serverPath, filePath);
Workbook workbook = new Workbook();
com.grapecity.documents.excel.Workbook workbook = new com.grapecity.documents.excel.Workbook();
workbook.fromJson(reportData);
workbook.save(fullFilePath);
return filePath;
......
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