ResourceServiceImpl.java
2.98 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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.tree.MapTree;
import org.dromara.hutool.core.tree.TreeNode;
import org.dromara.hutool.core.tree.TreeUtil;
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<MapTree<Long>> listTree(Long id) {
List<Resource> resourceList = this.lambdaQuery()
.orderByAsc(Resource::getSortCode)
.list();
List<TreeNode<Long>> nodeList = ListUtil.zero();
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<MapTree<Long>> wholeTree = TreeUtil.build(nodeList, 0L);
return Opt.ofNullable(id).map(d -> {
List<MapTree<Long>> resTree = new ArrayList<>();
wholeTree.forEach(t ->
t.walk(tmp -> {
if (tmp.getId().equals(d)) {
resTree.add(tmp);
}
}));
return resTree;
}).orElse(wholeTree);
}
}