diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/manage/AnnualPlanLibManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/manage/AnnualPlanLibManage.java index fb83502..0391c86 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/manage/AnnualPlanLibManage.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/manage/AnnualPlanLibManage.java @@ -1,8 +1,8 @@ package com.ningdatech.pmapi.projectlib.manage; -import cn.hutool.poi.excel.ExcelUtil; import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.ExcelReader; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -10,10 +10,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ningdatech.basic.exception.BizException; import com.ningdatech.basic.model.PageVo; import com.ningdatech.basic.util.CollUtils; +import com.ningdatech.basic.util.ValidUtil; import com.ningdatech.pmapi.common.statemachine.event.ProjectStatusChangeEvent; import com.ningdatech.pmapi.common.statemachine.util.StateMachineUtils; import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum; import com.ningdatech.pmapi.projectlib.helper.ProjectHelper; +import com.ningdatech.pmapi.projectlib.model.dto.AnnualLibImportDTO; import com.ningdatech.pmapi.projectlib.model.entity.Project; import com.ningdatech.pmapi.projectlib.model.req.ProjectApprovedReq; import com.ningdatech.pmapi.projectlib.model.req.ProjectIdReq; @@ -31,7 +33,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import static com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum.*; @@ -139,7 +140,60 @@ public class AnnualPlanLibManage { @Transactional(rollbackFor = Exception.class) public void importAnnualPlan(MultipartFile file) { try (InputStream inputStream = file.getInputStream()) { - EasyExcel.read(inputStream).sheet(0); + EasyExcel.read(inputStream, new AnalysisEventListener() { + + private List records = new ArrayList<>(); + + @Override + public void onException(Exception exception, AnalysisContext context) throws Exception { + super.onException(exception, context); + throw BizException.wrap("导入年度计划解析失败"); + } + + @Override + public void invoke(AnnualLibImportDTO data, AnalysisContext context) { + ValidUtil.valid(data); + Project project = new Project(); + project.setId(data.getProjectId()); + project.setProjectName(data.getProjectName()); + project.setProjectRemarks(data.getProjectRemarks()); + project.setEngineeringSpeedOne(data.getEngineeringSpeedOne()); + project.setEngineeringSpeedTwo(data.getEngineeringSpeedTwo()); + project.setEngineeringSpeedThree(data.getEngineeringSpeedThree()); + project.setEngineeringSpeedFour(data.getEngineeringSpeedFour()); + project.setAnnualPlanAmount(data.getAnnualPlanAmount()); + project.setDeclareOtherAmount(data.getDeclareOtherAmount()); + project.setBuildBasis(data.getBuildBasis()); + project.setBuildOrgName(data.getBuildUnitName()); + project.setDeclareBankLendingAmount(data.getDeclareBankLendingAmount()); + project.setDeclareGovOwnFinanceAmount(data.getDeclareGovOwnFinanceAmount()); + project.setDeclareGovSuperiorFinanceAmount(data.getDeclareGovSuperiorFinanceAmount()); + project.setDeclareHaveAmount(data.getDeclareHaveAmount()); + project.setDeclareOtherAmount(data.getDeclareOtherAmount()); + project.setContactName(data.getContactName()); + project.setResponsibleMan(data.getResponsibleMan()); + String[] dataArr = data.getBuildCycle().split("至"); + project.setBeginTime(dataArr[0].trim()); + project.setEndTime(dataArr[1].trim()); + project.setProjectIntroduction(data.getProjectIntroduction()); + project.setIsFirst(data.getIsFirst().equals("新建") ? 1 : 0); + records.add(project); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + if (records.isEmpty()) { + throw BizException.wrap("导入年度计划为空"); + } + List projectIds = CollUtils.fieldList(records, Project::getId); + long count = projectService.count(Wrappers.lambdaQuery(Project.class) + .in(Project::getId, projectIds)); + if (count != records.size()) { + throw BizException.wrap("请确保所有项目都存在"); + } + projectService.updateBatchById(records); + } + }).sheet(0).doReadSync(); } catch (IOException e) { throw BizException.wrap("导入年度计划失败"); } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/model/dto/AnnualLibImportDTO.java b/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/model/dto/AnnualLibImportDTO.java index 6b40bed..fbfa348 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/model/dto/AnnualLibImportDTO.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/projectlib/model/dto/AnnualLibImportDTO.java @@ -52,7 +52,7 @@ public class AnnualLibImportDTO { @NotNull(message = "年度投资额不能为空") @ExcelProperty("年度投资额") - private BigDecimal annualPlanAMount; + private BigDecimal annualPlanAmount; @ExcelProperty("自有资金") @NotNull(message = "自由资金不能为空") diff --git a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java index 4ed9825..78af962 100644 --- a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java +++ b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java @@ -1,9 +1,15 @@ package com.ningdatech.pmapi.sys.service; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.db.Db; import cn.hutool.db.Entity; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; import com.ningdatech.pmapi.AppTests; import com.ningdatech.pmapi.sys.model.entity.Menu; +import com.ningdatech.pmapi.sys.model.entity.RoleMenu; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -34,4 +40,45 @@ class IMenuServiceTest extends AppTests { }); } + + @Autowired + private IRoleMenuService roleMenuService; + + @Test + public void initMenu() { + /*String str = FileUtil.readString("/Users/wendy/Desktop/long_text_2023-02-13-15-28-42.txt", "UTF-8"); + List obj = JSONUtil.toList(str, JSONObject.class); + save(obj, 0);*/ + menuService.list().forEach(w -> { + roleMenuService.save(new RoleMenu(){{ + setRoleId(1L); + setMenuId(w.getId()); + }}); + }); + } + + public void save(List objs, long parentId) { + objs.forEach(w -> { + Menu menu = new Menu(); + menu.setActiveMenu(w.getStr("activeMenu")); + menu.setComponent(w.getStr("component")); + menu.setIcon(w.getStr("icon")); + menu.setPath(w.getStr("path")); + menu.setName(w.getStr("path").replace("/", "")); + menu.setTopMenu(w.getStr("topMenu")); + menu.setTitle(w.getStr("title")); + menu.setHidden(w.getBool("hidden")); + menu.setPid(parentId); + menu.setRedirect(w.getStr("redirect")); + menu.setSort(w.getInt("sort")); + menu.setDataScopeOption(""); + menu.setHasDataScope(false); + menuService.save(menu); + List children = w.getBeanList("children", JSONObject.class); + if (CollUtil.isNotEmpty(children)) { + save(children, menu.getId()); + } + }); + } + } \ No newline at end of file