ResourceServiceImpl.java 2.97 KB
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.TimeUtil;
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 vion
 * @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", TimeUtil.formatNormal(v.getCreateTime()))
                    .put("updateTime", TimeUtil.formatNormal(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);
    }
}