李白
20 小时以前 5024682a739495a9546803188af8b203c6ad8a18
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ProductServiceImpl.java
@@ -1,10 +1,24 @@
package com.by4cloud.platformx.business.service.impl;
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.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.business.entity.Product;
import com.by4cloud.platformx.business.mapper.ProductMapper;
import com.by4cloud.platformx.business.service.ProductService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * 产品信息
 *
@@ -13,4 +27,53 @@
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Override
    public List<Tree<Long>> treeList(Long parentId, String productName) {
        Long parent = parentId == null ? 0l : parentId;
        List<TreeNode<Long>> collect = baseMapper
                .selectList(Wrappers.<Product>lambdaQuery()
                        .like(StrUtil.isNotBlank(productName), Product::getProductName, productName)
                        .orderByAsc(Product::getCreateTime))
                .stream().map(getNodeFunction()).collect(Collectors.toList());
        // 模糊查询 不组装树结构 直接返回 表格方便编辑
        if (StrUtil.isNotBlank(productName)) {
            return collect.stream().map(node -> {
                Tree<Long> tree = new Tree<>();
                tree.putAll(node.getExtra());
                BeanUtils.copyProperties(node, tree);
                return tree;
            }).collect(Collectors.toList());
        }
        return TreeUtil.build(collect, parent);
    }
    @NotNull
    private Function<Product, TreeNode<Long>> getNodeFunction() {
        return product -> {
            TreeNode<Long> node = new TreeNode<>();
            node.setId(product.getId());
            node.setName(product.getProductName());
            node.setParentId(product.getParentId());
            node.setWeight(product.getParentId());
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
//            extra.put("picPath", category.getPicPath());
//            extra.put("Type", category.getType());
//            extra.put("isShow", category.getIsShow());
//            extra.put("iconPicPath", category.getIconPath());
//            extra.put("status", category.getStatus());
            // 适配 vue3
            Map<String, Object> meta = new HashMap<>();
            meta.put("title", product.getProductName());
            meta.put("icon", product.getPrice());
            extra.put("meta", meta);
            node.setExtra(extra);
            return node;
        };
    }
}