|
@@ -8,20 +8,22 @@ import com.google.common.base.CaseFormat;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
import com.persagy.common.enums.ResponseCode;
|
|
import com.persagy.common.enums.ResponseCode;
|
|
import com.persagy.common.exception.BusinessException;
|
|
import com.persagy.common.exception.BusinessException;
|
|
-import com.persagy.fm.department.model.ConvertDepartmentTool;
|
|
|
|
-import com.persagy.fm.department.model.dto.AddDepartmentDTO;
|
|
|
|
-import com.persagy.fm.department.service.IDepartmentService;
|
|
|
|
import com.persagy.fm.common.constant.enums.ValidEnum;
|
|
import com.persagy.fm.common.constant.enums.ValidEnum;
|
|
import com.persagy.fm.common.model.RequiredParamsStorage;
|
|
import com.persagy.fm.common.model.RequiredParamsStorage;
|
|
import com.persagy.fm.common.model.dto.Sort;
|
|
import com.persagy.fm.common.model.dto.Sort;
|
|
|
|
+import com.persagy.fm.department.constant.enums.DepTypeEnum;
|
|
import com.persagy.fm.department.constant.enums.ResourceFromEnum;
|
|
import com.persagy.fm.department.constant.enums.ResourceFromEnum;
|
|
import com.persagy.fm.department.dao.DepartmentMapper;
|
|
import com.persagy.fm.department.dao.DepartmentMapper;
|
|
|
|
+import com.persagy.fm.department.model.ConvertDepartmentTool;
|
|
import com.persagy.fm.department.model.Department;
|
|
import com.persagy.fm.department.model.Department;
|
|
|
|
+import com.persagy.fm.department.model.dto.AddDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.PageQueryDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.PageQueryDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.QueryDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.QueryDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.UpdateDepartmentDTO;
|
|
import com.persagy.fm.department.model.dto.UpdateDepartmentDTO;
|
|
|
|
+import com.persagy.fm.department.service.IDepartmentService;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -38,20 +40,51 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
implements IDepartmentService {
|
|
implements IDepartmentService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
+ @Transactional
|
|
public Long createDepartment(AddDepartmentDTO addDepartmentDTO) {
|
|
public Long createDepartment(AddDepartmentDTO addDepartmentDTO) {
|
|
// 校验部门名称重复
|
|
// 校验部门名称重复
|
|
checkDuplicateDeptName(addDepartmentDTO);
|
|
checkDuplicateDeptName(addDepartmentDTO);
|
|
|
|
+ // 同一集团下只能有一个类型为集团的部门
|
|
|
|
+ checkDuplicateGroup(addDepartmentDTO);
|
|
|
|
|
|
Department department = ConvertDepartmentTool.INSTANCE.convertAddDto2Entity(addDepartmentDTO);
|
|
Department department = ConvertDepartmentTool.INSTANCE.convertAddDto2Entity(addDepartmentDTO);
|
|
|
|
|
|
// 设置默认值
|
|
// 设置默认值
|
|
setDefaultValue(department);
|
|
setDefaultValue(department);
|
|
|
|
|
|
|
|
+ // 获取部门的上级部门,拼装部门的全路径
|
|
|
|
+ Long parentId = department.getParentId();
|
|
|
|
+ if (parentId == 0L) {
|
|
|
|
+ department.setFullPath(department.getName());
|
|
|
|
+ } else {
|
|
|
|
+ Department parentDep = queryDepartmentDetail(parentId);
|
|
|
|
+ department.setFullPath(parentDep.getFullPath() + "-" + department.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
save(department);
|
|
save(department);
|
|
return department.getId();
|
|
return department.getId();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 校验同一集团下是否只有一个类型为集团的部门
|
|
|
|
+ *
|
|
|
|
+ * @param addDepartmentDTO 创建部门入参
|
|
|
|
+ * @author lixing
|
|
|
|
+ * @version V1.0 2021/3/18 7:06 下午
|
|
|
|
+ */
|
|
|
|
+ private void checkDuplicateGroup(AddDepartmentDTO addDepartmentDTO) {
|
|
|
|
+ if (DepTypeEnum.GROUP.getType().equals(addDepartmentDTO.getType())) {
|
|
|
|
+ QueryDepartmentDTO queryDepartmentDTO = new QueryDepartmentDTO();
|
|
|
|
+ queryDepartmentDTO.setType(DepTypeEnum.GROUP.getType());
|
|
|
|
+ List<Department> departments = queryDepartmentList(queryDepartmentDTO);
|
|
|
|
+ if (!CollectionUtils.isEmpty(departments)) {
|
|
|
|
+ throw new BusinessException(ResponseCode.B0300.getCode(),
|
|
|
|
+ "创建部门失败,同一集团下只能存在一个类型为集团的部门");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 如果某些字段没有赋值,使用默认的值
|
|
* 如果某些字段没有赋值,使用默认的值
|
|
*
|
|
*
|
|
* @param department 部门实体
|
|
* @param department 部门实体
|
|
@@ -101,14 +134,55 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
+ @Transactional
|
|
public void updateDepartment(UpdateDepartmentDTO updateDepartmentDTO) {
|
|
public void updateDepartment(UpdateDepartmentDTO updateDepartmentDTO) {
|
|
|
|
+ // TODO: 2021/3/22 上级部门只能选择大于等于当前部门等级的部门,通过fullPath包含的分隔符个数判断
|
|
Department department = getById(updateDepartmentDTO.getId());
|
|
Department department = getById(updateDepartmentDTO.getId());
|
|
|
|
+ String nameBeforeChange = department.getName();
|
|
department = ConvertDepartmentTool.INSTANCE.convertUpdateDto2Entity(department, updateDepartmentDTO);
|
|
department = ConvertDepartmentTool.INSTANCE.convertUpdateDto2Entity(department, updateDepartmentDTO);
|
|
|
|
+ // 如果部门名称发生变化,级联更新自己和子部门的全路径
|
|
|
|
+ if (!nameBeforeChange.equals(updateDepartmentDTO.getName())) {
|
|
|
|
+ cascadeUpdateFullPathWhenNameChange(department, nameBeforeChange);
|
|
|
|
+ }
|
|
department.setModifier(RequiredParamsStorage.getUserId());
|
|
department.setModifier(RequiredParamsStorage.getUserId());
|
|
updateById(department);
|
|
updateById(department);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 部门名称发生变化,级联更新自己和子部门的全路径
|
|
|
|
+ *
|
|
|
|
+ * @param department 名称发生变更的部门
|
|
|
|
+ * @param nameBeforeChange 修改之前的部门名称
|
|
|
|
+ * @author lixing
|
|
|
|
+ * @version V1.0 2021/3/19 10:53 上午
|
|
|
|
+ */
|
|
|
|
+ private void cascadeUpdateFullPathWhenNameChange(Department department, String nameBeforeChange) {
|
|
|
|
+ String fullPathBeforeChange = department.getFullPath();
|
|
|
|
+
|
|
|
|
+ // 根据部门全路径查询子部门
|
|
|
|
+ QueryDepartmentDTO queryDepartmentDTO = new QueryDepartmentDTO();
|
|
|
|
+ queryDepartmentDTO.setFullPath(fullPathBeforeChange + "-");
|
|
|
|
+ List<Department> sonDeps = queryDepartmentList(queryDepartmentDTO);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ String fullPathAfterChange = fullPathBeforeChange;
|
|
|
|
+ int i = fullPathBeforeChange.lastIndexOf(nameBeforeChange);
|
|
|
|
+ if (i != -1) {
|
|
|
|
+ fullPathAfterChange = fullPathBeforeChange.substring(0, i) + department.getName();
|
|
|
|
+ department.setFullPath(fullPathAfterChange);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 修改子部门全路径
|
|
|
|
+ for (Department sonDep : sonDeps) {
|
|
|
|
+ String tmpFullPath = sonDep.getFullPath();
|
|
|
|
+ tmpFullPath = tmpFullPath.replaceFirst(fullPathBeforeChange + "-", fullPathAfterChange + "-");
|
|
|
|
+ sonDep.setFullPath(tmpFullPath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ updateBatchById(sonDeps);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 校验部门是否可删除
|
|
* 校验部门是否可删除
|
|
*
|
|
*
|
|
* @param id 部门主键
|
|
* @param id 部门主键
|
|
@@ -118,7 +192,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
*/
|
|
*/
|
|
public Department checkDeletable(Long id) {
|
|
public Department checkDeletable(Long id) {
|
|
if (id == null) {
|
|
if (id == null) {
|
|
- throw new IllegalArgumentException("删除Department时发生异常,主键为空");
|
|
|
|
|
|
+ throw new IllegalArgumentException("删除Department时发生异常,主键为空!");
|
|
}
|
|
}
|
|
|
|
|
|
Department department = getById(id);
|
|
Department department = getById(id);
|
|
@@ -127,13 +201,17 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
throw new IllegalArgumentException("删除Department时发生异常,找不到要删除的数据,id:" + id);
|
|
throw new IllegalArgumentException("删除Department时发生异常,找不到要删除的数据,id:" + id);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (DepTypeEnum.GROUP.getType().equals(department.getType())) {
|
|
|
|
+ throw new IllegalArgumentException("集团不能删除!");
|
|
|
|
+ }
|
|
|
|
+
|
|
// 查询部门是否有子部门
|
|
// 查询部门是否有子部门
|
|
QueryDepartmentDTO queryDepartmentDTO = new QueryDepartmentDTO();
|
|
QueryDepartmentDTO queryDepartmentDTO = new QueryDepartmentDTO();
|
|
queryDepartmentDTO.setParentId(id);
|
|
queryDepartmentDTO.setParentId(id);
|
|
List<Department> departments = queryDepartmentList(queryDepartmentDTO);
|
|
List<Department> departments = queryDepartmentList(queryDepartmentDTO);
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(departments)) {
|
|
if (!CollectionUtils.isEmpty(departments)) {
|
|
- throw new BusinessException(ResponseCode.A10000.getCode(), "当前部门包含子部门,不可删除");
|
|
|
|
|
|
+ throw new IllegalArgumentException("当前部门包含子部门,不可删除");
|
|
}
|
|
}
|
|
|
|
|
|
// TODO: 2021/3/9 部门包含员工时,不可删除
|
|
// TODO: 2021/3/9 部门包含员工时,不可删除
|
|
@@ -142,6 +220,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
+ @Transactional
|
|
public void deleteDepartment(Long id) {
|
|
public void deleteDepartment(Long id) {
|
|
// 校验部门是否可删除
|
|
// 校验部门是否可删除
|
|
Department department = checkDeletable(id);
|
|
Department department = checkDeletable(id);
|
|
@@ -173,6 +252,10 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
if (StringUtils.isNotEmpty(queryDepartmentDTO.getResourceFrom())) {
|
|
if (StringUtils.isNotEmpty(queryDepartmentDTO.getResourceFrom())) {
|
|
queryWrapper.eq(Department.PROP_RESOURCE_FROM, queryDepartmentDTO.getResourceFrom());
|
|
queryWrapper.eq(Department.PROP_RESOURCE_FROM, queryDepartmentDTO.getResourceFrom());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isNotEmpty(queryDepartmentDTO.getFullPath())) {
|
|
|
|
+ queryWrapper.like(Department.PROP_FULL_PATH, queryDepartmentDTO.getFullPath());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return list(queryWrapper);
|
|
return list(queryWrapper);
|
|
}
|
|
}
|