futian.liu
2023-12-12 b4c27cf4af545af1a240f56d34702d38a783062d
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.consum.base.service.impl;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
 
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import com.consum.base.Constants;
import com.consum.base.core.utils.IdUtil;
import com.consum.base.pojo.BaseCategoryParam;
import com.consum.base.pojo.ProjectTreeResult;
import com.consum.base.service.BaseCategoryService;
import com.consum.model.po.BaseCategory;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
 
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
 
/**
 * @Description 物品分类
 * @Author 卢庆阳
 * @Date 2023/10/23
 */
@Service
public class BaseCategoryServiceImpl extends BaseServiceImpl implements BaseCategoryService {
 
    private static final String QUERY_TREE_ALL =
        "select * from base_category where states = 1  order by FATHER_CATEGORY_ID, LEVELS ASC";
 
    /**
     * @Description 新增分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public int add(BaseCategoryParam param, S_user_core currentUser) {
        BaseCategory baseCategory = new BaseCategory();
        BeanUtils.copyProperties(param, baseCategory);
        baseCategory.setId(IdUtil.generateId());
        // 层级
        if (baseCategory.getFatherCategoryId() == null) {
            // 一级分类
            baseCategory.setFatherCategoryId(0L);
            baseCategory.setLevels(Constants.LEVELS_ONE);
        } else {
            // 根据父类id查询上级分类信息
            BaseCategory category = this.get(new BaseCategory(baseCategory.getFatherCategoryId()));
            if (category.getFatherCategoryId() == 0L) { // 二级分类
                baseCategory.setLevels(Constants.LEVELS_TWO);
            } else { // 三级分类
                baseCategory.setLevels(Constants.LEVELS_THREE);
            }
        }
        // 创建人id和创建人姓名
        baseCategory.setCreateUserId(currentUser.getId());
        baseCategory.setCreateUserName(currentUser.getUser_name());
        // 创建时间
        baseCategory.setCreateTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
 
        return this.insert(baseCategory);
    }
 
    /**
     * @param categoryName
     * @param fatherCategoryId
     * @Description 根据分类名称和父类id查询分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public BaseCategory getByCategoryNameAndFatherCategoryId(String categoryName, Long fatherCategoryId) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
 
        // 分类名称
        sql.append("and category_name =:category_name ");
        paramts.put("category_name", categoryName);
        // 父类id
        sql.append("and father_category_id =:father_category_id ");
        paramts.put("father_category_id", fatherCategoryId);
        sql.append("and states = 1 ");
        return this.get(sql.toString(), paramts, new BaseCategory());
    }
 
    /**
     * @Description 物品分类列表查询
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public GenericPager<BaseCategory> queryBaseCategoryList(BaseCategoryParam param) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        // 分类名称
        if (param.getFatherCategoryId() != null) {
            sql.append("and father_category_id =:fatherCategoryId ");
            paramts.put("fatherCategoryId", param.getFatherCategoryId());
        } else {
            sql.append("and levels =1 ");
        }
        // 分类名称
        if (!StringUtils.isEmpty(param.getCategoryName())) {
            sql.append(" and category_name like:category_name ");
            paramts.put("category_name", StringUtils.CHAR_PERCENT + param.getCategoryName() + StringUtils.CHAR_PERCENT);
        }
        // 类别
        if (!StringUtils.isEmpty(param.getClassification())) {
            sql.append(" and classification =:classification ");
            paramts.put("classification", param.getClassification());
        }
        // 状态
        if (param.getStates() != null) {
            sql.append(" and states =:states ");
            paramts.put("states", param.getStates());
        } else {
            sql.append(" and states !=3 ");
        }
        sql.append(" ORDER BY ORDER_NUMBER,CREATE_TIME DESC ");
        return selectSplit(sql.toString(), paramts, new BaseCategory());
    }
 
    /**
     * @Description 编辑
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public int updateBaseCategory(BaseCategory baseCategory) {
        return this.update(baseCategory);
    }
 
    /**
     * 修改状态
     *
     * @author 卢庆阳
     * @date 2023/9/27
     */
    public int updateStatus(BaseCategory baseCategory) {
        return this.update(baseCategory);
    }
 
    /**
     * @Description 根据id删除物品分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public int updateById(BaseCategory baseCategory, S_user_core currentUser) {
        baseCategory.setStates(Constants.STATES_DELETED);
        // 删除时间
        baseCategory.setDTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        // 删除人id和删除人姓名
        baseCategory.setDUserId(currentUser.getId());
        baseCategory.setDUserName(currentUser.getUser_name());
        return this.update(baseCategory);
    }
 
    /**
     * 根据节点id查询节点详情
     *
     * @author 卢庆阳
     * @Date 2023/10/23
     */
    public BaseCategory getById(Long id) {
        return this.get(new BaseCategory(id));
    }
 
    /**
     * @Description
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    public List<BaseCategory> queryForTree() {
        // 展示全部节点
        return this.select(QUERY_TREE_ALL, new Object[] {}, new BaseCategory());
    }
 
    public List<ProjectTreeResult> tree() {
        BaseCategory categoryParam = new BaseCategory();
        categoryParam.setStates(1);
        // 查出所有分类
        List<BaseCategory> all = select(categoryParam);
        // 组装成父子树形结构
        // 1级分类
        List<ProjectTreeResult> menus = all.stream().filter(entity -> entity.getLevels() == 1).map(entity -> {
            ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
            if (getChildren(projectTreeResult, all).isEmpty()) {
                projectTreeResult.setChildren(null);
            } else {
                projectTreeResult.setChildren(getChildren(projectTreeResult, all));
            }
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
            .collect(Collectors.toList());
        return menus;
 
    }
 
    /**
     * 递归查找所有菜单的子菜单
     */
    private List<ProjectTreeResult> getChildren(ProjectTreeResult root, List<BaseCategory> all) {
        List<ProjectTreeResult> children =
            all.stream().filter(entity -> entity.getFatherCategoryId().equals(root.getId())).map(entity -> {
 
                ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
                // 通过递归找到子分类
                if (getChildren(projectTreeResult, all).isEmpty()) {
                    projectTreeResult.setChildren(null);
                } else {
                    projectTreeResult.setChildren(getChildren(projectTreeResult, all));
                }
                return projectTreeResult;
            }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
        return children;
    }
 
    /**
     * @return
     * @Description 三级分类列表查询
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    public List<BaseCategory> queryForLv3Tree() {
        StringBuilder sql = new StringBuilder(
            "SELECT * FROM base_category WHERE states = 1 and levels = 3 order by ORDER_NUMBER,CREATE_TIME desc");
        return this.select(sql.toString(), new Object[] {}, new BaseCategory());
    }
 
    @Override
    public BaseCategory getByCategoryByName(String categoryName) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        // 分类名称
        sql.append(" and category_name =:category_name ");
        paramts.put("category_name", categoryName);
        BaseCategory baseCategory = this.get(sql.toString(), paramts, new BaseCategory());
        return baseCategory;
    }
}