支持部门树

This commit is contained in:
2024-03-16 23:56:24 +08:00
parent aef241821a
commit f48319f5d8
8 changed files with 81 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.department.dto.CreateDepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentTreeDto;
import cn.lihongjie.coal.department.dto.UpdateDepartmentDto;
import cn.lihongjie.coal.department.service.DepartmentService;
@@ -16,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/department")
@RestController
@SysLog(module = "部门管理")
@@ -52,4 +55,16 @@ public class DepartmentController {
public DepartmentDto getById(@RequestBody IdRequest dto) {
return this.service.getById(dto.getId());
}
@PostMapping("/roots")
public List<DepartmentTreeDto> roots() {
return this.service.getRoots();
}
@PostMapping("/treeByIds")
public List<DepartmentTreeDto> roots(@RequestBody IdRequest request) {
return this.service.getTreeByIds(request);
}
}

View File

@@ -5,4 +5,6 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreateDepartmentDto extends OrgCommonDto {}
public class CreateDepartmentDto extends OrgCommonDto {
private String parent;
}

View File

@@ -5,4 +5,6 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class DepartmentDto extends OrgCommonDto {}
public class DepartmentDto extends OrgCommonDto {
private String parent;
}

View File

@@ -0,0 +1,12 @@
package cn.lihongjie.coal.department.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import java.util.List;
@Data
public class DepartmentTreeDto extends OrgCommonDto {
private List<DepartmentTreeDto> children;
}

View File

@@ -5,4 +5,6 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdateDepartmentDto extends OrgCommonDto {}
public class UpdateDepartmentDto extends OrgCommonDto {
private String parent;
}

View File

@@ -2,15 +2,28 @@ package cn.lihongjie.coal.department.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Comment;
import java.util.List;
@Entity
@Comment("部门信息")
@Getter
@Setter
public class DepartmentEntity extends OrgCommonEntity {}
public class DepartmentEntity extends OrgCommonEntity {
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<DepartmentEntity> children;
@ManyToOne
private DepartmentEntity parent;
}

View File

@@ -3,6 +3,7 @@ package cn.lihongjie.coal.department.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.department.dto.CreateDepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentTreeDto;
import cn.lihongjie.coal.department.dto.UpdateDepartmentDto;
import cn.lihongjie.coal.department.entity.DepartmentEntity;
@@ -16,4 +17,7 @@ import org.mapstruct.control.DeepClone;
mappingControl = DeepClone.class)
public interface DepartmentMapper
extends BaseMapper<
DepartmentEntity, DepartmentDto, CreateDepartmentDto, UpdateDepartmentDto> {}
DepartmentEntity, DepartmentDto, CreateDepartmentDto, UpdateDepartmentDto> {
DepartmentTreeDto toTreeDto(DepartmentEntity departmentEntity);
}

View File

@@ -5,6 +5,7 @@ import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.department.dto.CreateDepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentDto;
import cn.lihongjie.coal.department.dto.DepartmentTreeDto;
import cn.lihongjie.coal.department.dto.UpdateDepartmentDto;
import cn.lihongjie.coal.department.entity.DepartmentEntity;
import cn.lihongjie.coal.department.mapper.DepartmentMapper;
@@ -22,6 +23,10 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional
@@ -65,6 +70,27 @@ class DepartmentService extends BaseService<DepartmentEntity, DepartmentReposito
return mapper.toDto(entity);
}
public List<DepartmentTreeDto> getRoots() {
List<DepartmentEntity> roots = this.repository.findAll( (root, query, criteriaBuilder) -> criteriaBuilder.isNull(root.get("parent")));
return roots.stream().map(de -> this.mapper.toTreeDto(de)).collect(Collectors.toList());
}
public List<DepartmentTreeDto> getTreeByIds(IdRequest id) {
if (id.getIds().isEmpty()) {
return new ArrayList<>();
}
List<DepartmentEntity> roots = this.repository.findAll((root, query, criteriaBuilder) -> root.get("id").in(id.getIds()));
return roots.stream().map(de -> this.mapper.toTreeDto(de)).collect(Collectors.toList());
}
public Page<DepartmentDto> list(CommonQuery query) {
Page<DepartmentEntity> page =