shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.ishop.merchant.util;
 
import com.ishop.merchant.util.cache.MerProductCateSortComparator;
import com.ishop.merchant.util.cache.ProductCategorySortComparator;
import com.ishop.model.po.EbMerchantProductCategory;
import com.ishop.model.po.EbProductCategory;
import com.ishop.model.vo.ProductCategoryVo;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ProductCategoryUtils {
 
    protected static Logger logger = LoggerFactory.getLogger(ProductCategoryUtils.class);
 
    /**
     * 根据商品分类对象集合(平台)返回分类ID集合。
     * @param categoryList
     * @return
     * @date 2023-07-23
     */
    public static final List<Integer> acquireCategoryIdList(List<EbProductCategory> categoryList){
        if(StringUtils.isEmptyList(categoryList)){
            return null;
        }
        List<Integer> list = new ArrayList<>(categoryList.size());
        for(EbProductCategory category : categoryList){
            list.add(category.getId());
        }
        return list;
    }
 
    public static final List<ProductCategoryVo> acquireListTree(List<EbMerchantProductCategory> allTree, MerProductCateSortComparator sortComparator){
        // 重新排序
        Collections.sort(allTree, sortComparator);
        //
        List<ProductCategoryVo> treeList = new ArrayList<>();
        if(allTree.size() == 0){
            return treeList;
        }
        //
        for (EbMerchantProductCategory category : allTree) {
            treeList.add(new ProductCategoryVo(category));
        }
        return buildTree(treeList);
    }
 
    /**
     * 获取平台商品分类树列表。
     * @param allTree
     * @param sortComparator
     * @return
     * @date 2023-06-01
     */
    public static final List<ProductCategoryVo> acquireListTree(List<EbProductCategory> allTree, ProductCategorySortComparator sortComparator){
        // 重新排序
        Collections.sort(allTree, sortComparator);
        //
        List<ProductCategoryVo> treeList = new ArrayList<>();
        if(allTree.size() == 0){
            return treeList;
        }
 
        //
        for (EbProductCategory category : allTree) {
            treeList.add(new ProductCategoryVo(category));
        }
 
//        Map<Integer, ProductCategoryVo> map = new HashMap<>(treeList.size());
//        //ID 为 key 存储到map 中
//        for (ProductCategoryVo categoryTreeVo : treeList) {
//            map.put(categoryTreeVo.getId(), categoryTreeVo);
//        }
//
//        List<ProductCategoryVo> list = new ArrayList<>();
//        ProductCategoryVo parentTree = null;
//        for (ProductCategoryVo tree : treeList) {
//            //子集ID返回对象,有则添加。
//            parentTree = map.get(tree.getPid());
//            if (parentTree != null) {
//                parentTree.getChildList().add(tree);
//            } else {
//                list.add(tree);
//            }
//        }
//        return list;
        return buildTree(treeList);
    }
 
    private static List<ProductCategoryVo> buildTree(List<ProductCategoryVo> treeList){
        Map<Integer, ProductCategoryVo> map = new HashMap<>(treeList.size());
        //ID 为 key 存储到map 中
        for (ProductCategoryVo categoryTreeVo : treeList) {
            map.put(categoryTreeVo.getId(), categoryTreeVo);
        }
 
        List<ProductCategoryVo> list = new ArrayList<>();
        ProductCategoryVo parentTree = null;
        for (ProductCategoryVo tree : treeList) {
            //子集ID返回对象,有则添加。
            parentTree = map.get(tree.getPid());
            if (parentTree != null) {
                parentTree.getChildList().add(tree);
            } else {
                list.add(tree);
            }
        }
        return list;
    }
}