Commit d0051f1b authored by chenyuefang's avatar chenyuefang

成本汇总导入修改

parent 7d0f01ac
......@@ -134,7 +134,7 @@ public class CbSummaryController extends BaseController {
*/
@PostMapping("/analysisData")
public void analysisData(Long projectId){
cbSummaryService.importCbProject(projectId);
cbSummaryService.importCbSummary(projectId);
}
}
......@@ -53,6 +53,12 @@ public class CbSummaryCostAccountImportListener extends AnalysisEventListener<Cb
failureMsg.append("<br/>").append("第" + sort + "条数据序号为空");
return;
}
//成本科目不能为空
if (StringUtil.isBlank(importVo.getCbName())) {
failureNum++;
failureMsg.append("<br/>").append("第" + sort + "条数据成本科目为空");
return;
}
//父级数据处理
if (sort == 1) {
......@@ -74,7 +80,7 @@ public class CbSummaryCostAccountImportListener extends AnalysisEventListener<Cb
@Override
public String getAnalysis() {
if (failureNum > 0) {
failureMsg.insert(0, "失败原因:共 " + failureNum + " 条数据格式不正确,错误如下:");
failureMsg.insert(0, "共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "成本科目汇总数据已全部解析成功!共 " + getList().size() + " 条");
......
package com.dsk.cscec.listener;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dsk.common.excel.ExcelListener;
......@@ -42,8 +43,12 @@ public class CbSummaryProjectImportListener extends AnalysisEventListener<CbSumm
@Override
public void invoke(CbSummaryProjectImportVo importVo, AnalysisContext context) {
//第一个序号不能为空? todo
//名称不能为空
if (StringUtil.isBlank(importVo.getCbName())) {
failureNum++;
failureMsg.append("<br/>").append("第" + sort + "条数据名称为空");
return;
}
//添加父级数据
if (sort == 0) {
CbSummaryProjectImportVo firstVo = new CbSummaryProjectImportVo();
......@@ -63,8 +68,12 @@ public class CbSummaryProjectImportListener extends AnalysisEventListener<CbSumm
//按sort倒序取number不为空的最大level
Object[] objects = resultList.stream().sorted(Comparator.comparingInt(CbSummaryProjectImportVo::getSort).reversed())
.filter(vo -> StringUtil.isNotBlank(vo.getNumber())).limit(1).map(CbSummaryProjectImportVo::getLevel).toArray();
//数据序号错误 todo
//数据序号错误
if (ObjectUtil.isEmpty(objects)) {
failureNum++;
failureMsg.append("<br/>").append("第" + sort + "条数据序号错误");
return;
}
Integer lastLevel = (Integer) objects[0];
importVo.setLevel(lastLevel + 1);
......@@ -75,8 +84,6 @@ public class CbSummaryProjectImportListener extends AnalysisEventListener<CbSumm
}
// failureNum++;
// failureMsg.append("<br/>").append(failureNum);
resultList.add(importVo);
}
......@@ -93,7 +100,7 @@ public class CbSummaryProjectImportListener extends AnalysisEventListener<CbSumm
@Override
public String getAnalysis() {
if (failureNum > 0) {
failureMsg.insert(0, "失败原因:共 " + failureNum + " 条数据格式不正确,错误如下:");
failureMsg.insert(0, "共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "项目汇总数据已全部解析成功!共 " + getList().size() + " 条");
......
......@@ -16,5 +16,6 @@ public interface CbProjectFileMapper extends BaseMapper<CbProjectFile> {
List<CbProjectFile> selectAnalysisList(@Param("projectId") Long projectId,@Param("cbType") Integer cbType,@Param("cbStage") Integer cbStage);
int removeById(@Param("id") Long id);
}
......@@ -2,7 +2,6 @@ package com.dsk.cscec.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dsk.common.core.domain.R;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.CbSummary;
import com.dsk.cscec.domain.bo.CbSummaryActualBo;
import com.dsk.cscec.domain.bo.CbSummaryActualListBo;
......@@ -26,7 +25,7 @@ public interface CbSummaryService extends IService<CbSummary> {
* 项目汇总导入
* @param projectId
*/
void importCbProject(Long projectId);
void importCbSummary(Long projectId);
/**
* 获取一级名称/成本科目列表
......
......@@ -28,9 +28,14 @@ import com.dsk.system.service.ISysOssService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
......@@ -59,43 +64,52 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
@Autowired
private ISysOssService ossService;
@Autowired
private TransactionTemplate transactionTemplate;
@Async
@Override
public void importCbProject(Long projectId) {
//获取del_flag为1的文件,若存在则根据fileId删除相关基础数据,并修改文件del_flag为2 todo
public void importCbSummary(Long projectId) {
//获取文件信息
List<CbProjectFile> cbProjectFiles = cbProjectFileMapper.selectList(new LambdaQueryWrapper<CbProjectFile>()
.eq(CbProjectFile::getProjectId, projectId)
// .eq(CbProjectFile::getCbStage, cbStage)
.eq(CbProjectFile::getCbType, CbProjectConstants.CB_TYPE_SUMMARY)
.eq(CbProjectFile::getFileParseStatus,CbProjectConstants.PROJECT_FILE_STATUS_PREPARING)
);
//获取待解析和待删除文件信息
List<CbProjectFile> cbProjectFiles = cbProjectFileMapper.selectAnalysisList(projectId, CbProjectConstants.CB_TYPE_SUMMARY, null);
//解析数据
cbProjectFiles.forEach(cbProjectFile -> {
if (cbProjectFile.getDelFlag() == 1) {
//根据fileId删除相关基础数据,并删除文件
baseMapper.delete(new LambdaQueryWrapper<CbSummary>().eq(CbSummary::getCbProjectFileId, cbProjectFile.getId()));
cbProjectFileMapper.removeById(cbProjectFile.getId());
} else {
try {
if (cbProjectFile.getFileName().contains("项目")) {
saveCbSummaryProject(projectId,cbProjectFile);
saveCbSummaryProject(projectId, cbProjectFile);
} else if (cbProjectFile.getFileName().contains("成本科目")) {
saveCbSummaryCostAccount(projectId,cbProjectFile);
saveCbSummaryCostAccount(projectId, cbProjectFile);
} else {
throw new ServiceException("文件名错误");
}
} catch (Exception e) {
//修改文件状态
cbProjectFile.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_FAIL);
cbProjectFile.setFailRemark("失败原因:文件名错误");
cbProjectFile.setFailRemark("失败原因:" + e.getMessage());
cbProjectFileMapper.updateById(cbProjectFile);
}
}
});
}
/**
* 成本汇总-项目汇总导入
*
* @param projectId
* @param file
* @return
*/
boolean saveCbSummaryProject(Long projectId, CbProjectFile file){
boolean saveCbSummaryProject(Long projectId, CbProjectFile file) {
//修改文件状态为解析中
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSING);
cbProjectFileMapper.updateById(file);
......@@ -103,18 +117,24 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
//文件下载
InputStream inputStream = ossService.downFileIO(file.getFileOssId());
if (ObjectUtil.isNull(inputStream)) {
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_FAIL);
file.setFailRemark("失败原因:文件数据不存在");
cbProjectFileMapper.updateById(file);
return false;
throw new ServiceException("文件数据不存在!");
}
//解析数据
ExcelResult<CbSummaryProjectImportVo> importVoExcelResult = ExcelUtil.importExcel(inputStream, CbSummaryProjectImportVo.class,new CbSummaryProjectImportListener(projectId,file.getId()));
ExcelResult<CbSummaryProjectImportVo> importVoExcelResult = ExcelUtil.importExcel(inputStream, CbSummaryProjectImportVo.class, new CbSummaryProjectImportListener(projectId, file.getId()));
log.info(importVoExcelResult.getAnalysis());
if (CollectionUtil.isEmpty(importVoExcelResult.getList())) {
throw new ServiceException("文件中不存在待导入数据!");
}
List<CbSummary> addList = BeanUtil.copyToList(importVoExcelResult.getList(), CbSummary.class);
this.saveBatch(addList);
transactionTemplate.execute(status -> {
try {
boolean flag = this.saveBatch(addList);
if (!flag) {
throw new ServiceException("导入数据失败");
}
//处理父级id
List<CbSummary> cbSummaryList = baseMapper.selectList(
......@@ -123,34 +143,46 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
.eq(CbSummary::getCbType, 1)
.orderByAsc(CbSummary::getSort)
);
for (CbSummary cbSummary:cbSummaryList) {
if(cbSummary.getLevel()==0){
for (CbSummary cbSummary : cbSummaryList) {
if (cbSummary.getLevel() == 0) {
continue;
}
//获取父级id
Integer parentLevel = cbSummary.getLevel()-1;
CbSummary parent = cbSummaryList.stream().filter(summary -> summary.getSort()<cbSummary.getSort()&&summary.getLevel()==parentLevel)
Integer parentLevel = cbSummary.getLevel() - 1;
CbSummary parent = cbSummaryList.stream().filter(summary -> summary.getSort() < cbSummary.getSort() && summary.getLevel() == parentLevel)
.max(Comparator.comparing(CbSummary::getSort)).get();
cbSummary.setParentId(parent.getId());
}
this.updateBatchById(cbSummaryList);
flag = this.updateBatchById(cbSummaryList);
if (!flag) {
throw new ServiceException("更新数据失败");
}
//修改文件状态为解析成功
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_SUCCESS);
cbProjectFileMapper.updateById(file);
flag = cbProjectFileMapper.updateById(file) > 0;
if (!flag) {
throw new ServiceException("更新文件状态失败");
}
} catch (Exception e) {
status.setRollbackOnly();
throw e;
}
return Boolean.TRUE;
});
return true;
}
/**
* 成本汇总-成本科目汇总导入
*
* @param projectId
* @param file
* @return
*/
boolean saveCbSummaryCostAccount(Long projectId,CbProjectFile file) {
boolean saveCbSummaryCostAccount(Long projectId, CbProjectFile file) {
//修改文件状态为解析中
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSING);
cbProjectFileMapper.updateById(file);
......@@ -158,19 +190,24 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
//文件下载
InputStream inputStream = ossService.downFileIO(file.getFileOssId());
if (ObjectUtil.isNull(inputStream)) {
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_FAIL);
file.setFailRemark("失败原因:文件数据不存在");
cbProjectFileMapper.updateById(file);
return false;
throw new ServiceException("文件数据不存在");
}
//解析数据
ExcelResult<CbSummaryCostAccountImportVo> importVoExcelResult = ExcelUtil.importExcel(inputStream, CbSummaryCostAccountImportVo.class,new CbSummaryCostAccountImportListener(projectId,file.getId()));
ExcelResult<CbSummaryCostAccountImportVo> importVoExcelResult = ExcelUtil.importExcel(inputStream, CbSummaryCostAccountImportVo.class, new CbSummaryCostAccountImportListener(projectId, file.getId()));
log.info(importVoExcelResult.getAnalysis());
if (CollectionUtil.isEmpty(importVoExcelResult.getList())) {
throw new ServiceException("文件中不存在待导入数据!");
}
List<CbSummary> addList = BeanUtil.copyToList(importVoExcelResult.getList(), CbSummary.class);
this.saveBatch(addList);
transactionTemplate.execute(status -> {
try {
boolean flag = this.saveBatch(addList);
if (!flag) {
throw new ServiceException("导入数据失败");
}
//处理父级id
List<CbSummary> cbSummaryList = baseMapper.selectList(
......@@ -179,13 +216,13 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
.eq(CbSummary::getCbType, 1)
.orderByAsc(CbSummary::getSort)
);
for (CbSummary cbSummary:cbSummaryList) {
if(cbSummary.getLevel()==0){
for (CbSummary cbSummary : cbSummaryList) {
if (cbSummary.getLevel() == 0) {
continue;
}
//获取父级id
Integer parentLevel = cbSummary.getLevel()-1;
CbSummary parent = cbSummaryList.stream().filter(summary -> summary.getSort()<cbSummary.getSort()&&summary.getLevel()==parentLevel)
Integer parentLevel = cbSummary.getLevel() - 1;
CbSummary parent = cbSummaryList.stream().filter(summary -> summary.getSort() < cbSummary.getSort() && summary.getLevel() == parentLevel)
.max(Comparator.comparing(CbSummary::getSort)).get();
cbSummary.setParentId(parent.getId());
......@@ -194,7 +231,16 @@ public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary
this.updateBatchById(cbSummaryList);
//修改文件状态为解析成功
file.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_SUCCESS);
cbProjectFileMapper.updateById(file);
flag = cbProjectFileMapper.updateById(file) > 0;
if (!flag) {
throw new ServiceException("更新文件状态失败");
}
} catch (Exception e) {
status.setRollbackOnly();
throw e;
}
return Boolean.TRUE;
});
return true;
}
......
......@@ -7,6 +7,14 @@
del_flag, create_by, create_time, update_by, update_time
FROM cb_project_file
WHERE ((del_flag = 0 and file_parse_status in (1,3)) or del_flag = 1)
and project_id=#{projectId} and cb_stage =#{cbStage} and cb_type = #{cbType}
and project_id=#{projectId}
<if test="cbStage != null">
and cb_stage =#{cbStage}
</if>
and cb_type = #{cbType}
</select>
<update id="removeById">
UPDATE cb_project_file SET del_flag=2 WHERE id=#{id}
</update>
</mapper>
\ No newline at end of file
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