ResourceServiceImpl.java
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package vion.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.dto.ResourceDTO;
import vion.mapper.ResourceMapper;
import vion.model.Resource;
import vion.service.IResourceService;
import vion.vo.ResourceVO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author HlQ
* @date 2023/11/28
*/
@Service
@RequiredArgsConstructor
public class ResourceServiceImpl extends MPJBaseServiceImpl<ResourceMapper, Resource> implements IResourceService {
private final Converter converter;
@Override
public Page<ResourceVO> list(ResourceDTO dto) {
Page<Resource> resourcePage = this
.lambdaQuery(converter.convert(dto, Resource.class))
.page(Page.of(dto.getPageNum(), dto.getPageSize()));
List<ResourceVO> resourceVOList = converter.convert(resourcePage.getRecords(), ResourceVO.class);
return Page.<ResourceVO>of(resourcePage.getCurrent(), resourcePage.getSize(), resourcePage.getTotal()).setRecords(resourceVOList);
}
@Override
public List<Tree<Long>> listTree(Long id) {
List<Resource> resourceList = this.lambdaQuery()
.orderByAsc(Resource::getSortCode)
.list();
List<TreeNode<Long>> nodeList = CollUtil.newArrayList();
resourceList.forEach(v -> {
TreeNode<Long> node = new TreeNode<>(v.getId(), v.getParentId(), v.getName(), 1);
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("path", v.getPath())
.put("component", v.getComponent())
.put("perm", v.getPerm())
.put("sortCode", v.getSortCode())
.put("type", v.getType())
.put("remark", v.getRemark())
.put("createTime", DateUtil.formatDateTime(v.getCreateTime()))
.put("updateTime", DateUtil.formatDateTime(v.getUpdateTime()))
.build();
node.setExtra(map);
nodeList.add(node);
});
List<Tree<Long>> wholeTree = TreeUtil.build(nodeList, 0L);
return Opt.ofNullable(id).map(d -> {
List<Tree<Long>> resTree = new ArrayList<>();
wholeTree.forEach(t ->
t.walk(tmp -> {
if (tmp.getId().equals(d)) {
resTree.add(tmp);
}
}));
return resTree;
}).orElse(wholeTree);
}
}