- 部门表与职位表是一对多的关系:
增加或查看职位都需要装配对应的部门;删除部门则需要删除其对应的职位;
- 增加职位
@Override
public PostVo createPost(PostDto postDto) {
Post post = BeanUtil.toBean(postDto, Post.class);
String postNo = createPostNo(post.getDeptNo());
post.setPostNo(postNo);
int flag = postMapper.insert(post);
if (flag==0){
throw new RuntimeException("保存职位信息出错");
}
PostVo postVoResult = BeanConv.toBean(post, PostVo.class);
DeptDto deptDto = DeptDto.builder()
.dataState(SuperConstant.DATA_STATE_0)
.parentDeptNo(postDto.getDeptNo()).build();
List<Dept> deptList = deptMapper.selectList(deptDto);
if (!EmptyUtil.isNullOrEmpty(deptList)){
postVoResult.setDeptVo(BeanConv.toBean(deptList.get(0),DeptVo.class));
}
return postVoResult;
}
- 分页查询职位
@Override
public PageResponse<PostVo> findPostPage(PostDto postDto, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
if (EmptyUtil.isNullOrEmpty(postDto.getDeptNo())){
throw new BaseException("部门不能为空");
}
Page<List<Post>> page = postMapper.selectPage(postDto);
PageResponse<PostVo> pageResponse = PageResponse.of(page, PostVo.class);
if (!EmptyUtil.isNullOrEmpty(pageResponse.getRecords())){
List<String> deptNos = pageResponse.getRecords().stream().map(PostVo::getDeptNo).collect(Collectors.toList());
List<DeptVo> deptVoList = deptService.findDeptInDeptNos(deptNos);
pageResponse.getRecords().forEach(n->{
n.setCreateDay(LocalDateTimeUtil.format(n.getCreateTime(), "yyyy-MM-dd"));
deptVoList.forEach(d->{
if (n.getDeptNo().equals(d.getDeptNo())){
n.setDeptVo(BeanConv.toBean(d,DeptVo.class));
}
});
});
}
return pageResponse;
}