ResourceServiceImpl.java 2.93 KB
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);
    }
}