add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.iotechn.unimall.biz.service.category;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.Const;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.annotation.AspectCommonCache;
import com.dobbinsoft.fw.support.component.CacheComponent;
import com.iotechn.unimall.data.constant.CacheConst;
import com.iotechn.unimall.data.domain.CategoryDO;
import com.iotechn.unimall.data.dto.CategoryDTO;
import com.iotechn.unimall.data.enums.CategoryLevelType;
import com.iotechn.unimall.data.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by rize on 2019/7/12.
 */
@Service
public class CategoryBizService {
 
    @Autowired
    private CategoryMapper categoryMapper;
 
    @Autowired
    private CacheComponent cacheComponent;
 
    /**
     * 获取一棵两级类目树
     */
    @AspectCommonCache(value = CacheConst.CATEGORY_SECOND_LEVEL_TREE)
    public List<CategoryDTO> categorySecondLevelTree() throws ServiceException {
        List<CategoryDO> categoryDOS = categoryMapper.selectList(new QueryWrapper<CategoryDO>()
                .eq("level",0)
                .or()
                .eq("level",1)
                .orderByAsc("level"));
 
        List<CategoryDO> firstLevelList = categoryDOS.stream().filter(item -> item.getLevel().intValue() == CategoryLevelType.ONE.getCode()).collect(Collectors.toList());
        List<CategoryDO> secondLevelList = categoryDOS.stream().filter(item -> item.getLevel().intValue() == CategoryLevelType.TWO.getCode()).collect(Collectors.toList());
 
        // 以ID为键,组装后的DTO为值,提升组装速度
        HashMap<Long, CategoryDTO> speedUp = new HashMap<>();
 
        // 组装一级类目
        List<CategoryDTO> resultTree = firstLevelList.stream().map(item -> {
            CategoryDTO first = new CategoryDTO();
            BeanUtils.copyProperties(item, first);
            first.setFullName(item.getTitle());
            speedUp.put(first.getId(),first);
            return first;
        }).collect(Collectors.toList());
 
        // 组装二级类目
        secondLevelList.stream().forEach(item ->{
            publicCodeAssembly(speedUp,item);
        });
 
        return resultTree;
    }
 
    /**
     * 上面生成类目树的公用代码提取
     */
    private void publicCodeAssembly(HashMap<Long, CategoryDTO> speedUp, CategoryDO item){
        CategoryDTO parentDTO = speedUp.get(item.getParentId());
        if(parentDTO != null){
            if(parentDTO.getChildrenList() == null){
                parentDTO.setChildrenList(new ArrayList<>());
            }
            CategoryDTO child = new CategoryDTO();
            BeanUtils.copyProperties(item,child);
            child.setFullName(parentDTO.getFullName() + "/" + item.getTitle());
            parentDTO.getChildrenList().add(child);
            speedUp.put(item.getId(),child);
        }
    }
 
    /**
     * 获得所有类目list,类中有调用,不能使用切面
     * TODO 切面缓存
     */
 
    public List<CategoryDTO> getCategoryList() throws ServiceException{
        List<CategoryDTO> categoryDTOListFormCache = cacheComponent.getObjList(CacheConst.CATEGORY_ALL_LIST, CategoryDTO.class);
        if (categoryDTOListFormCache != null) {
            return categoryDTOListFormCache;
        }
 
        List<CategoryDTO> categoryDTOS = categorySecondLevelTree();
        List<CategoryDTO> resultList = new LinkedList<>();
        categoryDTOS.forEach(first -> {
            resultList.add(first);
            if (!CollectionUtils.isEmpty(first.getChildrenList()))
                first.getChildrenList().forEach(second -> {
                    resultList.add(second);
                });
        });
 
        cacheComponent.putObj(CacheConst.CATEGORY_ALL_LIST, resultList, Const.CACHE_ONE_DAY);
        return resultList;
    }
 
    /**
     * 将 节点传入 获取其 父节点ID 和 父父节点ID 组成List
     *
     * @param categoryId 节点ID
     * @return
     * @throws ServiceException
     */
    public List<Long> getCategoryFamily(Long categoryId) {
        CategoryDO categoryDO = categoryMapper.selectById(categoryId);
        if (categoryDO.getLevel() == CategoryLevelType.TWO.getCode().intValue()) {
            return Arrays.asList(categoryDO.getFirstLevelId(), categoryDO.getId());
        } else {
            return Arrays.asList(categoryDO.getId());
        }
    }
 
    /**
     * 将一父节点传入,返回父节点本身与其子节点,及其孙节点ID列表
     *
     * @param categoryId
     * @return
     * @throws ServiceException
     */
    public List<Long> getCategorySelfAndChildren(Long categoryId) throws ServiceException {
        // 利用冗余数据实现
        List<CategoryDTO> categoryList = getCategoryList();
        CategoryDTO categoryDTO = new CategoryDTO();
        for (int i = 0; i < categoryList.size(); i++) {
            if(categoryList.get(i).getId().equals(categoryId)){
                categoryDTO = categoryList.get(i);
                break;
            }
        }
 
        LinkedList<CategoryDTO> queue = new LinkedList<CategoryDTO>();
        queue.add(categoryDTO);
        List<Long> ids = new LinkedList<>();
        while(!queue.isEmpty()){
            CategoryDTO pop = queue.pop();
            if(pop.getChildrenList() != null){
                queue.addAll(pop.getChildrenList());
            }
            ids.add(pop.getId());
        }
        return ids;
    }
}