| | |
| | | package cn.iocoder.yudao.module.system.service.dept; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | |
| | | } |
| | | PostImportRespVO respVO = PostImportRespVO.builder().createPostNames(new ArrayList<>()) |
| | | .updatePostNames(new ArrayList<>()).failurePostNames(new LinkedHashMap<>()).build(); |
| | | importPosts.forEach(importPost -> { |
| | | // 使用 for 下标遍历,便于把「空值」失败原因定位到 Excel 的具体行号(表头占第 1 行,数据从第 2 行开始) |
| | | for (int i = 0; i < importPosts.size(); i++) { |
| | | PostImportExcelVO importPost = importPosts.get(i); |
| | | // 必填校验:Excel 中对应列缺失或未填写时,若放任空值落库,会触发 system_post 的非空约束 |
| | | // (code、name、sort、status 均为 NOT NULL 且无默认值),整批导入直接抛「系统异常」 |
| | | String name = importPost.getName(); |
| | | if (StrUtil.isBlank(name)) { |
| | | respVO.getFailurePostNames().put(StrUtil.format("第 {} 行", i + 2), "岗位名称不能为空"); |
| | | continue; |
| | | } |
| | | if (StrUtil.isBlank(importPost.getCode())) { |
| | | respVO.getFailurePostNames().put(name, "岗位编码不能为空"); |
| | | continue; |
| | | } |
| | | if (importPost.getSort() == null) { |
| | | respVO.getFailurePostNames().put(name, "岗位排序不能为空"); |
| | | continue; |
| | | } |
| | | if (importPost.getStatus() == null) { |
| | | respVO.getFailurePostNames().put(name, "状态不能为空,且只能填写「开启」或「停用」"); |
| | | continue; |
| | | } |
| | | |
| | | // 校验,判断是否有不符合的原因 |
| | | try { |
| | | validatePostForCreateOrUpdate(null, importPost.getName(), importPost.getCode()); |
| | |
| | | // ignore |
| | | } else { |
| | | respVO.getFailurePostNames().put(importPost.getName(), ex.getMessage()); |
| | | return; |
| | | continue; |
| | | } |
| | | } |
| | | |
| | |
| | | PostDO post = BeanUtils.toBean(importPost, PostDO.class); |
| | | postMapper.insert(post); |
| | | respVO.getCreatePostNames().add(importPost.getName()); |
| | | return; |
| | | continue; |
| | | } |
| | | |
| | | // 如果存在,判断是否允许更新 |
| | | if (!isUpdateSupport) { |
| | | respVO.getFailurePostNames().put(importPost.getName(), "岗位名或编码已经存在"); |
| | | return; |
| | | continue; |
| | | } |
| | | |
| | | // 更新岗位 |
| | |
| | | updatePost.setId(existPost.getId()); |
| | | postMapper.updateById(updatePost); |
| | | respVO.getUpdatePostNames().add(importPost.getName()); |
| | | }); |
| | | } |
| | | // 记录导入结果汇总,供 @LogRecord 的 success 文案引用 |
| | | OperateLogUtils.putImportSummary(respVO.getCreatePostNames(), respVO.getUpdatePostNames(), |
| | | respVO.getFailurePostNames()); |