|
|
@@ -10,8 +10,12 @@ import cn.start.tz.framework.mybatis.core.query.LambdaQueryWrapperX;
|
|
|
import cn.start.tz.framework.mybatis.core.query.QueryWrapperX;
|
|
|
import cn.start.tz.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
|
|
|
import cn.start.tz.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
|
|
|
+import cn.start.tz.module.system.controller.admin.dept.vo.dept.DeptImportExcelVO;
|
|
|
+import cn.start.tz.module.system.controller.admin.dept.vo.dept.DeptImportRespVO;
|
|
|
import cn.start.tz.module.system.dal.dataobject.dept.DeptDO;
|
|
|
+import cn.start.tz.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
import cn.start.tz.module.system.dal.mysql.dept.DeptMapper;
|
|
|
+import cn.start.tz.module.system.dal.mysql.user.AdminUserMapper;
|
|
|
import cn.start.tz.module.system.dal.redis.RedisKeyConstants;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
@@ -20,6 +24,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import java.util.*;
|
|
|
@@ -41,6 +46,9 @@ public class DeptServiceImpl implements DeptService {
|
|
|
@Resource
|
|
|
private DeptMapper deptMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private AdminUserMapper adminUserMapper;
|
|
|
+
|
|
|
@Override
|
|
|
@CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST,
|
|
|
allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存
|
|
|
@@ -258,4 +266,91 @@ public class DeptServiceImpl implements DeptService {
|
|
|
return deptDO;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public DeptImportRespVO importDeptList(List<DeptImportExcelVO> importDepts) {
|
|
|
+ if (CollUtil.isEmpty(importDepts)) {
|
|
|
+ throw exception(DEPT_IMPORT_LIST_IS_EMPTY);
|
|
|
+ }
|
|
|
+
|
|
|
+ DeptImportRespVO respVO = DeptImportRespVO.builder()
|
|
|
+ .createDeptNames(new ArrayList<>())
|
|
|
+ .failureDeptNames(new LinkedHashMap<>())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ importDepts.forEach(importDept -> {
|
|
|
+ try {
|
|
|
+ // 1. 基本字段非空校验
|
|
|
+ if (importDept.getName() == null || importDept.getName().trim().isEmpty()) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName() == null ? "未知" : importDept.getName(), "部门名称不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (importDept.getCode() == null || importDept.getCode().trim().isEmpty()) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "部门代码不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 部门代码唯一性校验
|
|
|
+ DeptDO existByCode = deptMapper.selectByCode(importDept.getCode().trim());
|
|
|
+ if (existByCode != null) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "部门代码已存在: " + importDept.getCode());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 解析父部门
|
|
|
+ String parentId = DeptDO.PARENT_ID_ROOT;
|
|
|
+ if (importDept.getParentName() != null && !importDept.getParentName().trim().isEmpty()) {
|
|
|
+ DeptDO parentDept = deptMapper.selectOne(
|
|
|
+ new LambdaQueryWrapperX<DeptDO>()
|
|
|
+ .eq(DeptDO::getName, importDept.getParentName().trim()), false);
|
|
|
+ if (parentDept == null) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "父部门不存在: " + importDept.getParentName());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ parentId = parentDept.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 同父部门下名称唯一性校验
|
|
|
+ DeptDO existByName = deptMapper.selectByParentIdAndName(parentId, importDept.getName().trim());
|
|
|
+ if (existByName != null) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "同父部门下部门名称已存在");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5.负责人账号
|
|
|
+ String leaderUserId = "";
|
|
|
+ if (importDept.getLeader() != null && !importDept.getLeader().trim().isEmpty()){
|
|
|
+ AdminUserDO leaderUser = adminUserMapper.selectByUsername(importDept.getLeader());
|
|
|
+ if (leaderUser == null) {
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "负责人用户名不存在");
|
|
|
+ return;
|
|
|
+ }else{
|
|
|
+ leaderUserId = leaderUser.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 构建并插入
|
|
|
+ DeptDO dept = new DeptDO();
|
|
|
+ dept.setName(importDept.getName().trim());
|
|
|
+ dept.setCode(importDept.getCode().trim());
|
|
|
+ dept.setParentId(parentId);
|
|
|
+ dept.setSort(importDept.getSort() != null ? importDept.getSort() : 0);
|
|
|
+ dept.setPhone(importDept.getPhone());
|
|
|
+ dept.setEmail(importDept.getEmail());
|
|
|
+ dept.setTenantId(importDept.getTenantId());
|
|
|
+ dept.setLeaderUserId(leaderUserId);
|
|
|
+ dept.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认启用
|
|
|
+
|
|
|
+ deptMapper.insert(dept);
|
|
|
+ respVO.getCreateDeptNames().add(importDept.getName());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[importDeptList] 导入部门失败: {}", importDept.getName(), e);
|
|
|
+ respVO.getFailureDeptNames().put(importDept.getName(), "系统异常: " + e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
}
|