WangHan
2025-01-03 24c4699005da96f45562d7057d80c103b8e428a2
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package com.consum.base.controller;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.consum.base.pojo.*;
import com.consum.base.pojo.excel.ImportClassificationTemplate;
import com.consum.model.po.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.service.BaseCategoryService;
import com.consum.base.service.BaseGoodsTemplateService;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * @Description 物品分类
 * @Author 卢庆阳
 * @Date 2023/10/23
 */
@Api(value = "物品分类", tags = "物品分类")
@RestController
@RequestMapping("/pc/base/category")
public class BaseCategoryController extends BaseController {
 
    @Autowired
    private BaseCategoryService baseCategoryService;
 
    @Autowired
    private BaseGoodsTemplateService baseGoodsTemplateService;
 
    /**
     * @Description 获取分类树
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    @GetMapping("/select/tree")
    public ResponseValue trees() {
        List<ProjectTreeResult> tree = this.baseCategoryService.tree();
        return ResponseValue.success(tree);
    }
 
    /**
     * @Description 新增分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    @PostMapping("/add")
    public ResponseValue add() {
        BaseCategoryParam param = CommonUtil.getObjFromReqBody(BaseCategoryParam.class);
        BaseCategoryParam param2 = new BaseCategoryParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        if (StringUtils.isEmpty(param.getCategoryName())) {
            return ResponseValue.error("分类名称为空");
        }
        if (param.getOrderNumber() == null) {
            return ResponseValue.error("顺序号为空");
        }
        // 判断同一父类id下分类名称是否重复
        BaseCategory category = this.baseCategoryService.getByCategoryNameAndFatherCategoryId(param.getCategoryName(),
                param.getFatherCategoryId());
        if (category != null) {
            return ResponseValue.error("分类名称已存在");
        }
        // 判断添加的父级不能是3
        if (param2.getFatherCategoryId() != null) {
            BaseCategory baseCategory = new BaseCategory();
            baseCategory.setId(param2.getFatherCategoryId());
            BaseCategory baseCategory1 = this.baseCategoryService.get(baseCategory);
            if (baseCategory1 != null && baseCategory1.getLevels() >= 3) {
                return ResponseValue.error("分类最多支持三级");
            }
        }
        int result = this.baseCategoryService.add(param, this.getCurrentUser());
        if (result > 0) {
            return ResponseValue.success(1);
        }
        return ResponseValue.error("新增失败!");
    }
 
    /**
     * @Description 物品分类列表查询
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    @GetMapping("/list")
    public ResponseValue queryBaseCategoryList() {
        BaseCategoryParam param = CommonUtil.getObjFromReq(BaseCategoryParam.class);
        BaseCategoryParam param2 = new BaseCategoryParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
 
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        GenericPager<BaseCategory> pager = this.baseCategoryService.queryBaseCategoryList(param);
        return ResponseValue.success(pager);
    }
 
    /**
     * @Description 编辑
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    @PostMapping("/edit")
    public ResponseValue edit() {
        BaseCategory baseCategory = CommonUtil.getObjFromReqBody(BaseCategory.class);
        BaseCategory param2 = new BaseCategory();
        CommonUtil.copyProperties(baseCategory, param2);
        baseCategory = param2;
 
        Long id = baseCategory.getId();
        if (id == null || id.longValue() <= 0) {
            return ResponseValue.error("编辑的物品分类不存在");
        }
        if (StringUtils.isEmpty(baseCategory.getCategoryName())) {
            return ResponseValue.error("分类名称为空");
        }
        if (baseCategory.getOrderNumber() == null) {
            return ResponseValue.error("顺序号为空");
        }
 
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        List<BaseCategory> list = this.baseGoodsTemplateService.getByCategoryId(baseCategory.getId());
        if (!CollectionUtils.isEmpty(list) && param2.getStates() != 1) {
            return ResponseValue.error("分类下有商品在用,不允许禁用");
        }
        // 查询分类下是否有子级分类
        BaseCategory baseCategory1 = new BaseCategory();
        baseCategory1.setFatherCategoryId(param2.getId());
        List<BaseCategory> select = this.baseCategoryService.select(baseCategory1);
        if (!CollectionUtils.isEmpty(select) && param2.getStates() != 1) {
            return ResponseValue.error("分类下有子级分类在用,不允许禁用");
        }
        int num = this.baseCategoryService.updateBaseCategory(baseCategory);
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("编辑失败!");
    }
 
    /**
     * 修改状态
     *
     * @author 卢庆阳
     * @date 2023/10/23
     */
    // 分类下有正常状态的物品时,不允许禁用
    @PostMapping("/updStatus")
    public ResponseValue updateStatus() {
        BaseCategory baseCategory = CommonUtil.getObjFromReqBody(BaseCategory.class);
        BaseCategory param2 = new BaseCategory();
        CommonUtil.copyProperties(baseCategory, param2);
        baseCategory = param2;
 
        if (baseCategory.getId() == null || baseCategory.getStates() == null) {
            return ResponseValue.error("参数错误");
        }
        // 根据分类id和状态查询物品模版
        List<BaseCategory> list = this.baseGoodsTemplateService.getByCategoryId(baseCategory.getId());
        if (!CollectionUtils.isEmpty(list)) {
            return ResponseValue.error("分类下有商品在用,不允许禁用");
        }
        // 查询分类下是否有子级分类
        BaseCategory baseCategory1 = new BaseCategory();
        baseCategory1.setFatherCategoryId(param2.getId());
        List<BaseCategory> select = this.baseCategoryService.select(baseCategory1);
        if (!CollectionUtils.isEmpty(select)) {
            return ResponseValue.error("分类下有子级分类在用,不允许禁用");
        }
        int num = this.baseCategoryService.updateStatus(baseCategory);
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("修改失败!");
    }
 
    /**
     * @Description 根据id删除物品分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    @DeleteMapping("/del")
    public ResponseValue updateById() {
        BaseCategory baseCategory = CommonUtil.getObjFromReqBody(BaseCategory.class);
        BaseCategory param2 = new BaseCategory();
        CommonUtil.copyProperties(baseCategory, param2);
        baseCategory = param2;
        if (baseCategory.getId() == null) {
            return ResponseValue.error("分类id为空");
        }
        // 判断分类是否有商品在用
        List<BaseCategory> byCategoryId = this.baseGoodsTemplateService.getByCategoryId(baseCategory.getId());
        if (!CollectionUtils.isEmpty(byCategoryId)) {
            return ResponseValue.error("分类下有商品在用,不允许删除");
        }
        // 查询分类下是否有子级分类
        List<BaseCategory> select = this.baseCategoryService.selectByFatherId(param2.getId());
        if (!CollectionUtils.isEmpty(select)) {
            return ResponseValue.error("分类下有子级分类在用,不允许删除");
        }
        int num = this.baseCategoryService.updateById(baseCategory, this.getCurrentUser());
 
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("删除失败!");
    }
 
    /**
     * 根据物品id查询节点详情
     *
     * @author 卢庆阳
     * @Date 2023/10/23
     */
    @GetMapping("/detail")
    public ResponseValue getById(Long id) {
        if (id == null) {
            return ResponseValue.error("分类id为空");
        }
        BaseCategory baseCategory = this.baseCategoryService.getById(id);
        if (baseCategory == null) {
            return ResponseValue.error("查询失败!");
        }
        return ResponseValue.success("查询成功!", baseCategory);
    }
 
    /**
     * @Description 三级分类列表查询
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    @GetMapping("/select/lv3_tree")
    public ResponseValue tree() {
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        List<BaseCategory> baseCategories = this.baseCategoryService.queryForLv3Tree();
        return ResponseValue.success(baseCategories);
    }
 
    /**
     * 导入物品分类
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    public ResponseValue importData(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
//        if (!".xls".endsWith(originalFilename)) {
//            return ResponseValue.error("文件格式有误!");
//        }
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("当前登录用户为空");
        }
        // 当前登录用户
        S_user_core currentUser = this.getCurrentUser();
        try {
            EasyExcelFactory.read(file.getInputStream(), ImportClassificationTemplate.class,
                    new AnalysisEventListener<ImportClassificationTemplate>() {
 
                        /**
                         * 每条存储的list
                         */
                        final List<ImportClassificationTemplate> importData = Lists.newArrayList();
 
                        /**
                         * 读取的行号
                         */
                        Integer rowIndex = 0;
 
                        /**
                         * 表头信息
                         * @param headMap
                         * @param context
                         */
                        @Override
                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
                            // 验证表头数量
                            logger.info("解析分发单的表头长度: {}", headMap.size());
                            if (headMap.size() != 4) {
                                throw new ExcelAnalysisException("上传的文件不符!");
                            }
                        }
 
                        /**
                         * 数据处理
                         * @param data
                         * @param analysisContext
                         */
                        @Override
                        public void invoke(ImportClassificationTemplate data, AnalysisContext analysisContext) {
                            rowIndex++;
                            // 跳过
                            if (data.getCategoryOne().contains("说明:")) {
                                return;
                            }
                            if (StrUtil.isEmpty(data.getCategoryOne())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,一级分类不能为空:" + data.getCategoryOne());
                            }
                            if (StrUtil.isEmpty(data.getCategoryTwo())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,二级分类不能为空:" + data.getCategoryTwo());
                            }
                            if (StrUtil.isEmpty(data.getCategoryThree())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,品类名称不能为空:" + data.getCategoryThree());
                            }
                            if (StrUtil.isEmpty(data.getType())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,管理分类不能为空:" + data.getType());
                            }
                            importData.add(data);
                        }
 
                        /**
                         * 数据处理导入主方法
                         * @param analysisContext
                         */
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            for (ImportClassificationTemplate data : importData) {
                                String h1 = data.getCategoryOne();
                                String h2 = data.getCategoryTwo();
                                String h3 = data.getCategoryThree();
                                String type = data.getType();
                                BaseCategoryParam baseCategoryParam1 = new BaseCategoryParam();
                                baseCategoryParam1.setFatherCategoryId(0L);
                                baseCategoryParam1.setCategoryName(h1);
                                GenericPager<BaseCategory> baseCategoryGenericPager = baseCategoryService.queryBaseCategoryList2(baseCategoryParam1);
                                List<BaseCategory> datas = baseCategoryGenericPager.getDatas();
 
                                // 一级分类
                                BaseCategory D1 = null;
                                if (!CollectionUtil.isEmpty(datas)) {
                                    D1 = datas.get(0);
                                } else {
                                    // 新增父级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h1);
                                    param.setStates(1);
                                    int getIndex = baseCategoryService.selIndexByPid(0L);
                                    param.setOrderNumber(getIndex + 1);
                                    baseCategoryService.add(param, currentUser);
                                    GenericPager<BaseCategory> baseCategoryGenericPager2 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam1);
                                    List<BaseCategory> datas2 = baseCategoryGenericPager2.getDatas();
                                    D1 = datas2.get(0);
                                }
 
                                // 2、二级分类 查询第二层数据
                                BaseCategoryParam baseCategoryParam2 = new BaseCategoryParam();
                                baseCategoryParam2.setCategoryName(h2);
                                baseCategoryParam2.setFatherCategoryId(D1.getId());
                                GenericPager<BaseCategory> baseCategoryGenericPager2 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam2);
                                List<BaseCategory> datas2 = baseCategoryGenericPager2.getDatas();
                                BaseCategory D2 = null;
                                if (!CollectionUtil.isEmpty(datas2)) {
                                    D2 = datas2.get(0);
                                } else {
                                    // 新增父2级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h2);
                                    param.setStates(1);
                                    param.setFatherCategoryId(D1.getId());
                                    int getIndex = baseCategoryService.selIndexByPid(D1.getId());
                                    param.setOrderNumber(getIndex + 1);
                                    baseCategoryService.add(param, currentUser);
                                    GenericPager<BaseCategory> baseCategoryGenericPager4 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam2);
                                    List<BaseCategory> datas4 = baseCategoryGenericPager4.getDatas();
                                    D2 = datas4.get(0);
                                }
 
                                // 3、三级分类(如果已经存在,那么不处理,不存在新增)
                                BaseCategoryParam baseCategoryParam3 = new BaseCategoryParam();
                                baseCategoryParam3.setCategoryName(h3);
                                baseCategoryParam3.setClassification(type);
                                baseCategoryParam3.setFatherCategoryId(D2.getId());
                                GenericPager<BaseCategory> baseCategoryGenericPager3 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam3);
                                List<BaseCategory> datas3 = baseCategoryGenericPager3.getDatas();
                                if (!CollectionUtil.isEmpty(datas3)) {
                                } else {
                                    // 新增父2级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h3);
                                    param.setStates(1);
                                    param.setFatherCategoryId(D2.getId());
                                    int getIndex = baseCategoryService.selIndexByPid(D2.getId());
                                    param.setOrderNumber(getIndex + 1);
                                    param.setClassification(type);
                                    baseCategoryService.add(param, currentUser);
                                    //GenericPager<BaseCategory> baseCategoryGenericPager4 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam3);
                                    //List<BaseCategory> datas4 = baseCategoryGenericPager4.getDatas();
                                    //D3 = datas4.get(0);
                                }
                            }
                        }
 
                        @Override
                        public void onException(Exception exception, AnalysisContext analysisContext) throws Exception {
                            if (exception instanceof ExcelDataConvertException) {
                                ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
                                Integer row = excelDataConvertException.getRowIndex() + 1;
                                Integer column = excelDataConvertException.getColumnIndex() + 1;
                                throw new ExcelAnalysisException("第" + row + "行,第" + column + "列解析异常,请正确填写");
                            } else {
                                throw new ExcelAnalysisException(exception.getMessage());
                            }
                        }
                    }).sheet(0).doRead();
        } catch (ExcelAnalysisException e) {
            return ResponseValue.error(e.getMessage());
        }catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        }
        return ResponseValue.success("导入成功!");
    }
 
}