package com.ycl.listener.excel;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.util.StrUtil;
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.ycl.common.exception.ServiceException;
|
import com.ycl.common.exception.base.BaseException;
|
import com.ycl.common.utils.excel.core.ExcelListener;
|
import com.ycl.common.utils.excel.core.ExcelResult;
|
import com.ycl.common.utils.spring.SpringUtils;
|
import com.ycl.domain.entity.*;
|
import com.ycl.domain.excel.ProjectExcelTemplate;
|
import com.ycl.service.ProjectInvestmentFundingService;
|
import com.ycl.service.ProjectInvestmentInfoService;
|
import com.ycl.service.ProjectInvestmentPolicyComplianceService;
|
import com.ycl.service.ProjectUnitRegistrationInfoService;
|
import com.ycl.service.impl.ProjectInfoServiceImpl;
|
import lombok.Data;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Data
|
//@NoArgsConstructor
|
public class ProjectImportListener extends AnalysisEventListener<ProjectExcelTemplate> implements ExcelListener<ProjectExcelTemplate> {
|
|
|
private final ProjectInfoServiceImpl projectInfoService;
|
private final ProjectInvestmentInfoService projectInvestmentInfoService;
|
private final ProjectInvestmentFundingService projectInvestmentFundingService;
|
private final ProjectUnitRegistrationInfoService projectUnitRegistrationInfoService;
|
private final ProjectInvestmentPolicyComplianceService projectPolicyComplianceService;
|
|
private int successNum = 0;
|
private int failureNum = 0;
|
private final StringBuilder successMsg = new StringBuilder();
|
private final StringBuilder failureMsg = new StringBuilder();
|
private boolean hasData = false;
|
|
private Map<String, Long> projectInfoMap = new HashMap<>();
|
private Map<String, Long> investmentProjectPolicyComplianceMap = new HashMap<>();
|
private Map<String, Long> documentsMap = new HashMap<>();
|
|
|
public ProjectImportListener() {
|
this.projectInfoService = SpringUtils.getBean(ProjectInfoServiceImpl.class);
|
this.projectInvestmentInfoService = SpringUtils.getBean(ProjectInvestmentInfoService.class);
|
this.projectInvestmentFundingService = SpringUtils.getBean(ProjectInvestmentFundingService.class);
|
this.projectUnitRegistrationInfoService = SpringUtils.getBean(ProjectUnitRegistrationInfoService.class);
|
this.projectPolicyComplianceService = SpringUtils.getBean(ProjectInvestmentPolicyComplianceService.class);
|
|
}
|
|
@Override
|
public ExcelResult<ProjectExcelTemplate> getExcelResult() {
|
return new ExcelResult<>() {
|
|
@Override
|
public String getAnalysis() {
|
if (failureNum > 0) {
|
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
throw new ServiceException(failureMsg.toString());
|
} else {
|
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
}
|
return successMsg.toString();
|
}
|
|
@Override
|
public List<ProjectExcelTemplate> getList() {
|
return null;
|
}
|
|
@Override
|
public List<String> getErrorList() {
|
return null;
|
}
|
};
|
}
|
|
@Override
|
public void invoke(ProjectExcelTemplate projectExcelTemplate, AnalysisContext analysisContext) {
|
hasData = true;
|
//处理项目基础信息
|
ProjectInfo projectInfo = BeanUtil.toBean(projectExcelTemplate, ProjectInfo.class);
|
projectInfoService.checkProjectNameUnique(projectInfo);
|
projectInfoService.save(projectInfo);
|
//审批计划书
|
if (StrUtil.isNotBlank(projectExcelTemplate.getApprovalPlan())) {
|
String[] names = projectExcelTemplate.getApprovalPlan().split(",");
|
for (String name : names) {
|
projectInfoMap.put(name, projectInfo.getId());
|
}
|
}
|
|
//处理投资管理信息
|
ProjectInvestmentInfo projectInvestmentInfo = BeanUtil.toBean(projectExcelTemplate, ProjectInvestmentInfo.class);
|
projectInvestmentInfo.setProjectId(projectInfo.getId());
|
projectInvestmentInfoService.save(projectInvestmentInfo);
|
|
//处理项目投资及资金来源
|
ProjectInvestmentFunding projectInvestmentFunding = BeanUtil.toBean(projectExcelTemplate, ProjectInvestmentFunding.class);
|
projectInvestmentFunding.setProjectId(projectInfo.getId());
|
projectInvestmentFundingService.save(projectInvestmentFunding);
|
|
// 处理项目法人单位信息
|
ProjectUnitRegistrationInfo projectUnitRegistrationInfo = BeanUtil.toBean(projectExcelTemplate, ProjectUnitRegistrationInfo.class);
|
projectUnitRegistrationInfo.setProjectId(projectInfo.getId());
|
projectUnitRegistrationInfoService.save(projectUnitRegistrationInfo);
|
|
//处理投资产业政策符合情况
|
ProjectInvestmentPolicyCompliance investmentProjectPolicyCompliance = BeanUtil.toBean(projectExcelTemplate, ProjectInvestmentPolicyCompliance.class);
|
investmentProjectPolicyCompliance.setProjectId(projectInfo.getId());
|
projectPolicyComplianceService.save(investmentProjectPolicyCompliance);
|
//符合政策附件
|
if (StrUtil.isNotBlank(projectExcelTemplate.getPolicyComplianceAttachment())) {
|
String[] names2 = projectExcelTemplate.getPolicyComplianceAttachment().split(",");
|
for (String name : names2) {
|
investmentProjectPolicyComplianceMap.put(name, investmentProjectPolicyCompliance.getId());
|
}
|
}
|
|
// 相关文书
|
if (StrUtil.isNotBlank(projectExcelTemplate.getDocuments())) {
|
String[] documentNames = projectExcelTemplate.getDocuments().split(",");
|
for (String name : documentNames) {
|
documentsMap.put(name, projectInfo.getId());
|
}
|
}
|
}
|
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
if (!hasData) {
|
throw new BaseException("excel为空,请填入数据后导入");
|
}
|
}
|
|
|
}
|