提交职务管理代码

feature/fujiajun_branch
1545558448 1 year ago
parent 3bb1aafd68
commit 38ef791f47

@ -0,0 +1,148 @@
package com.intelligentHealthCare.service.impl;
import com.intelligentHealthCare.dto.OfficeDto;
import com.intelligentHealthCare.entity.Department;
import com.intelligentHealthCare.entity.DepartmentOffice;
import com.intelligentHealthCare.entity.Office;
import com.intelligentHealthCare.exception.BizException;
import com.intelligentHealthCare.repository.DepartmentOfficeRepository;
import com.intelligentHealthCare.repository.OfficeRepository;
import com.intelligentHealthCare.service.DepartmentOfficeService;
import com.intelligentHealthCare.service.OfficeService;
import com.intelligentHealthCare.service.OfficeStaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class OfficeServiceImpl extends AbstractSimpleJpaService<Office, OfficeRepository> implements OfficeService {
private final DepartmentOfficeService departmentOfficeService;
private final DepartmentOfficeRepository departmentOfficeRepository;
@Override
public List<Office> getTree() {
List<Office> officeList = repository.findAll();
ConvertTree(officeList);
List<String> officeIds = officeList.stream().map(Office::getId).collect(Collectors.toList());
List<DepartmentOffice> departmentOffices = departmentOfficeRepository.getDepartmentOfficeByOfficeIdIn(officeIds);
departmentOffices.forEach(d -> {
officeList.forEach(o -> {
if(o.getId().equals(d.getOffice().getId())){
if(o.getDepartments() == null){
o.setDepartments(new LinkedList<>());
}
o.getDepartments().add(d.getDepartment());
}
});
});
return ConvertTree(officeList);
}
@Override
@Transactional
public boolean delete(String id) {
List<Office> childrenList = repository.findAllByOfficeParent(id);
if(!childrenList.isEmpty()){
throw new BizException("请先删除子级职务");
}
//删除和部门之间的关系
int i = departmentOfficeRepository.deleteAllByOfficeId(id);
repository.deleteById(id);
return true;
}
@Override
@Transactional
public OfficeDto saveOfficeAndDepartmentOffice(OfficeDto officeDto) {
Office office = new Office();
BeanUtils.copyProperties(officeDto, office);
//保存职务
Office saved = repository.saveAndFlush(office);
saved.setOfficeTree(StringUtils.hasText(saved.getOfficeTree()) ? saved.getOfficeTree() + "-" + saved.getId()
: saved.getId());
//保存职务与部门关系,并且过滤出数据库没有的数据
List<DepartmentOffice> allDepartmentOffice = departmentOfficeService.getAll();
List<DepartmentOffice> departmentOffices = officeDto.getDepartmentIds().stream().filter(p -> allDepartmentOffice.stream().noneMatch(a ->{
return p.equals(a.getDepartment().getId()) && a.getOffice().getId().equals(saved.getId());
})).map(i -> {
return DepartmentOffice.builder().office(saved).department(Department.builder().id(i).build()).build();
}).collect(Collectors.toList());
List<DepartmentOffice> savedDepartmentOffices = departmentOfficeService.saveAll(departmentOffices);
OfficeDto returnOfficeDto = new OfficeDto();
BeanUtils.copyProperties(saved, returnOfficeDto);
returnOfficeDto.setDepartmentOffices(savedDepartmentOffices);
return returnOfficeDto;
}
@Override
@Transactional
public OfficeDto updateDto(OfficeDto officeDto) {
if(officeDto.getId() != null && officeDto.getOfficeParent() != null && officeDto.getId().equals(officeDto.getOfficeParent())){
throw new BizException("上级职务不能是自己");
}
String oldOfficeId = this.getById(officeDto.getId()).getId();
//删除职务和部门关系
departmentOfficeRepository.deleteAllByOfficeId(officeDto.getId());
//修改数据
OfficeDto savedOfficeDto = this.saveOfficeAndDepartmentOffice(officeDto);
List<Office> childrenOffice = repository.findAllByOfficeParent(officeDto.getId());
if(!childrenOffice.isEmpty() && !oldOfficeId.equals(savedOfficeDto.getOfficeParent())){
//更新所有子级的officeTree
List<Office> allOffice = this.getAll();
Office parent = new Office();
BeanUtils.copyProperties(savedOfficeDto, parent);
List<Office> updateOffices = new LinkedList<>();
updateChildrenTree(parent, allOffice, updateOffices);
this.saveAll(updateOffices);
}
return savedOfficeDto;
}
private List<Office> ConvertTree(List<Office> officeList) {
ArrayList<Office> list = new ArrayList<>();
for (Office office : officeList) {
if (office.getOfficeParent().equals("0")) {
list.add(childrenTree(office, officeList));
}
}
return list;
}
private Office childrenTree(Office office, List<Office> officeList) {
office.setChildren(new ArrayList<>()); // 创建一个新的子节点列表
for (Office item : officeList) {
if (office.getId().equals(item.getOfficeParent())){
office.getChildren().add(childrenTree(item, officeList));
}
}
return office;
}
private void updateChildrenTree(Office parent, List<Office> allOffice, List<Office> updateOffices){
//递归终止条件,集合中没有子级为止
List<Office> childrenList = allOffice.stream().filter(office -> office.getOfficeParent().equals(parent.getId())).collect(Collectors.toList());
if(childrenList.isEmpty()){
return;
}
//更新所有子级tree
childrenList.forEach(i -> {
i.setOfficeTree(parent.getOfficeTree() + "-" + i.getId());
updateOffices.add(i);
//递归更新
updateChildrenTree(i, allOffice, updateOffices);
});
}
}
Loading…
Cancel
Save