jiangping
2025-04-27 41d9c70dfc6dbe060eaaf5bfc153885be8140c76
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
package com.doumee.service.business.impl;
 
import com.doumee.core.annotation.excel.ExcelImporter;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.CategoryMapper;
import com.doumee.dao.business.model.Category;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.business.vo.CategoryDcaProblemDto;
import com.doumee.dao.business.vo.CompanyTree;
import com.doumee.dao.web.vo.CategoryVO;
import com.doumee.dao.web.vo.CategoryVOTree;
import com.doumee.dao.business.vo.CategoryTree;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 分类信息表Service实现
 * @author 江蹄蹄
 * @since 2025/04/02 17:49
 */
@Service
public class CategoryServiceImpl implements CategoryService {
 
    @Resource(name="sessionRedisTemplate")
    private RedisTemplate<Object, Serializable> redisTemplate;
    @Autowired
    private CategoryMapper categoryMapper;
 
    @Override
    public Integer create(Category category) {
        if(StringUtils.isBlank(category.getName())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(category.getType()==null){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,组织类型不正确,请按要求填写~");
        }
  /*      //查询名称不能重复
        if(categoryMapper.selectCount(new QueryWrapper<Category>().lambda()
                .eq(Category::getName,category.getName())
                .eq(Category::getIsdeleted,Constants.ZERO)) >0){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,名称不能重复~");
        }*/
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        category.setIdPath(category.getId()+"/");//名称路径
        category.setNamePath(category.getName());//名称路径
        String idPath = "";
        if(category.getParentId() !=null){
            Category parent = findById(category.getParentId());
            if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级信息不存在~");
            }
            if(Constants.equalsInteger(parent.getType(),Constants.FOUR) && parent.getParentId()!=null){
                //如果是二级CDA主题,自己为观察项
                category.setType(Constants.SIX);
            }
            idPath = parent.getIdPath();
            category.setIdPath(parent.getIdPath()+category.getId()+"/");
            category.setNamePath(parent.getNamePath()+"/"+category.getName());
        }
        category.setCreateDate(new Date());
        category.setCreator(user.getId());
        category.setIsdeleted(Constants.ZERO);
        category.setStatus(Constants.ZERO);
        category.setEditDate(category.getCreateDate());
        category.setEditor(category.getCreator());
        categoryMapper.insert(category);
 
        Category com = new Category();
        com.setId(category.getId());
        com.setIdPath(idPath+"/"+category.getId()+"/");
        categoryMapper.updateById(com);
        return category.getId();
 
    }
 
    @Override
    public void deleteById(Integer id) {
        Category model = new Category();
        model.setId(id);
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        model.setEditor(user.getId());
        model.setEditDate(new Date());
        model.setIsdeleted(Constants.ONE);
        categoryMapper.updateById(model);
    }
 
    @Override
    public void delete(Category category) {
        UpdateWrapper<Category> deleteWrapper = new UpdateWrapper<>(category);
        categoryMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for(Integer id :ids){
            deleteById(id);
        }
    }
 
    @Override
    public void updateById(Category category) {
        if( category.getId() == null){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(Constants.equalsInteger(category.getId(),category.getParentId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,父级不能设置为自身!");
        }
        category.setType(null);//类型不支持修改
        Category model = categoryMapper.selectById(category.getId());
        if(model ==null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
      /*  //查询名称不能重复
        if(categoryMapper.selectCount(new QueryWrapper<Category>().lambda()
                .eq(Category::getName,category.getName())
                .eq(Category::getType,model.getType())
                .ne(Category::getId,category.getId())
                .eq(Category::getIsdeleted,Constants.ZERO)) >0){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,名称不能重复~");
        }*/
        String oldIdPath = model.getIdPath();
        String oldNamePath = model.getNamePath();
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        category.setIdPath(category.getId()+"/");//名称路径
        category.setNamePath(category.getName());//名称路径
        if(category.getParentId() !=null ){
            //如果父级发生改变
            Category parent = findById(category.getParentId());
            if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级信息不存在~");
            }
            category.setIdPath(parent.getIdPath()+category.getId()+"/");
            category.setNamePath(parent.getNamePath()+"/"+category.getName());
        }
        category.setEditDate(new Date());
        category.setEditor(user.getId());
        categoryMapper.update(null,new UpdateWrapper<Category>().lambda()
                        .eq(Category::getId,category.getId())
                        .set(Category::getEditDate,category.getEditDate())
                        .set(Category::getEditor,category.getEditor())
                        .set(Category::getName,category.getName())
                        .set(Category::getParentId,category.getParentId())
                        .set(Category::getSortnum,category.getSortnum())
                        .set(Category::getRemark,category.getRemark())
                        .set(Category::getNamePath,category.getNamePath())
                        .set(Category::getIdPath,category.getId())
        );
        //修改全部下级的编码和名称路径
        categoryMapper.update(null,new UpdateWrapper<Category>().lambda()
                .likeRight(Category::getIdPath,oldIdPath)
                .set(Category::getEditDate,category.getEditDate())
                .set(Category::getEditor,category.getEditor())
                .setSql("id_path=replace(id_path,'"+oldIdPath+"','"+category.getIdParentPath()+"')")
                .setSql("name_path=replace(name_path,'"+oldNamePath+"/','"+category.getName()+"/')")
        );
    }
 
    @Override
    public void updateByIdInBatch(List<Category> categorys) {
        if (CollectionUtils.isEmpty(categorys)) {
            return;
        }
        for (Category category: categorys) {
            this.updateById(category);
        }
    }
 
    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectById(id);
    }
 
    @Override
    public Category findOne(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category);
        return categoryMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Category> findList(Category category) {
        category.setIsdeleted(Constants.ZERO);
        MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Category.class);
        queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName);
        queryWrapper.select("t1.name",Category::getParentName);
        queryWrapper.select("t1.id_path",Category::getIdParentPath);
        queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor);
        queryWrapper.eq(category.getId() != null, Category::getId, category.getId())
                .eq(category.getCreator() != null, Category::getCreator, category.getCreator())
                .ge(category.getCreateDate() != null, Category::getCreateDate, Utils.Date.getStart(category.getCreateDate()))
                .le(category.getCreateDate() != null, Category::getCreateDate, Utils.Date.getEnd(category.getCreateDate()))
                .eq(category.getEditor() != null, Category::getEditor, category.getEditor())
                .ge(category.getEditDate() != null, Category::getEditDate, Utils.Date.getStart(category.getEditDate()))
                .le(category.getEditDate() != null, Category::getEditDate, Utils.Date.getEnd(category.getEditDate()))
                .eq(category.getIsdeleted() != null, Category::getIsdeleted, category.getIsdeleted())
                .like(category.getName() != null, Category::getName, category.getName())
                .eq(category.getRemark() != null, Category::getRemark, category.getRemark())
                .eq(category.getStatus() != null, Category::getStatus, category.getStatus())
                .eq(category.getSortnum() != null, Category::getSortnum, category.getSortnum())
                .eq(category.getImgurl() != null, Category::getImgurl, category.getImgurl())
                .eq(category.getType() != null, Category::getType, category.getType())
                .eq(category.getParentId() != null, Category::getParentId, category.getParentId())
                .eq(category.getNamePath() != null, Category::getNamePath, category.getNamePath())
                .isNull(category.getIsRoot() != null &&category.getIsRoot().equals(Constants.ONE), Category::getParentId)
                .eq(category.getIdPath() != null, Category::getIdPath, category.getIdPath())
                .orderByAsc( Category::getSortnum);
        return categoryMapper.selectJoinList(Category.class,queryWrapper);
    }
  
    @Override
    public PageData<Category> findPage(PageWrap<Category> pageWrap) {
        IPage<Category> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Utils.MP.blankToNull(pageWrap.getModel());
        pageWrap.getModel().setIsdeleted(Constants.ZERO);
        MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Category.class);
        queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName);
        queryWrapper.select("t1.name",Category::getParentName);
        queryWrapper.select("t1.id_path",Category::getIdParentPath);
        queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor);
        queryWrapper.eq(pageWrap.getModel().getId() != null, Category::getId, pageWrap.getModel().getId())
                .eq(pageWrap.getModel().getCreator() != null, Category::getCreator, pageWrap.getModel().getCreator())
                .ge(pageWrap.getModel().getCreateDate() != null, Category::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()))
                .le(pageWrap.getModel().getCreateDate() != null, Category::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()))
                .eq(pageWrap.getModel().getEditor() != null, Category::getEditor, pageWrap.getModel().getEditor())
                .ge(pageWrap.getModel().getEditDate() != null, Category::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()))
                .le(pageWrap.getModel().getEditDate() != null, Category::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()))
                .eq(pageWrap.getModel().getIsdeleted() != null, Category::getIsdeleted, pageWrap.getModel().getIsdeleted())
                .like(pageWrap.getModel().getName() != null, Category::getName, pageWrap.getModel().getName())
                .eq(pageWrap.getModel().getRemark() != null, Category::getRemark, pageWrap.getModel().getRemark())
                .eq(pageWrap.getModel().getStatus() != null, Category::getStatus, pageWrap.getModel().getStatus())
                .eq(pageWrap.getModel().getSortnum() != null, Category::getSortnum, pageWrap.getModel().getSortnum())
                .eq(pageWrap.getModel().getImgurl() != null, Category::getImgurl, pageWrap.getModel().getImgurl())
                .eq(pageWrap.getModel().getType() != null, Category::getType, pageWrap.getModel().getType())
                .eq(pageWrap.getModel().getParentId() != null, Category::getParentId, pageWrap.getModel().getParentId())
                .isNull(pageWrap.getModel().getIsRoot() != null && pageWrap.getModel().getIsRoot().equals(Constants.ONE), Category::getParentId)
                .eq(pageWrap.getModel().getNamePath() != null, Category::getNamePath, pageWrap.getModel().getNamePath())
                .eq(pageWrap.getModel().getIdPath() != null, Category::getIdPath, pageWrap.getModel().getIdPath())
                .orderByAsc( Category::getSortnum);
        return PageData.from(categoryMapper.selectJoinPage(page,Category.class, queryWrapper));
    }
 
    @Override
    public long count(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category);
        return categoryMapper.selectCount(wrapper);
    }
 
    @Override
    public List<Category> treeList(Category param ) {
        MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Category.class);
        queryWrapper.selectAs(SystemUser::getUsername,Category::getEditorName);
        queryWrapper.select("t1.name",Category::getParentName);
        queryWrapper.select("t1.id_path",Category::getIdParentPath);
        queryWrapper.leftJoin(Category.class,Category::getId,Category::getParentId);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Category::getEditor);
        queryWrapper.eq(Category::getIsdeleted, Constants.ZERO);
        queryWrapper.eq(Category::getType, param.getType());
        queryWrapper.orderByAsc( Category::getSortnum);
        List<Category> companyList = categoryMapper.selectJoinList(Category.class,queryWrapper);
        CategoryTree treeBuild = new CategoryTree(companyList);
        companyList = treeBuild.buildTree();
        return companyList;
    }
 
 
 
 
 
    @Override
    public List<CategoryVO>  getCategoryVOTree(String categoryType,Integer categoryId){
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                .eq(Category::getIsdeleted, Constants.ZERO)
                .in(Objects.nonNull(categoryType),Category::getType,categoryType.split(","))
                .apply(Objects.nonNull(categoryId)," find_in_set("+categoryId+", REPLACE(ID_PATH,'/',',')  )  ")
                .orderByAsc(Category::getSortnum,Category::getId)
        );
        List<CategoryVO>  categoryVOList = new ArrayList<>();
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){
            for (Category category:categoryList) {
                CategoryVO categoryVO = new CategoryVO();
                categoryVO.setId(category.getId());
                categoryVO.setName(category.getName());
                categoryVO.setParentId(category.getParentId());
                categoryVOList.add(categoryVO);
            }
            CategoryVOTree treeBuild = new CategoryVOTree(categoryVOList);
            categoryVOList = treeBuild.buildTree();
        }
        return categoryVOList;
    }
 
 
    @Override
    public List<CategoryVO>  getCategoryVOForGCXTree(Integer categoryId){
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                .eq(Category::getIsdeleted, Constants.ZERO)
                .eq(Category::getType, Constants.FOUR)
                .and( ms->ms.eq(Category::getId,categoryId).or().eq(Category::getParentId,categoryId))
                .orderByAsc(Category::getSortnum,Category::getId)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){
            categoryList.addAll(
                    categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                            .eq(Category::getIsdeleted, Constants.ZERO)
                            .eq(Category::getType, Constants.SIX)
                            .in(Category::getParentId,categoryList.stream().map(i->i.getId()).collect(Collectors.toList()))
                            .orderByAsc(Category::getSortnum,Category::getId))
            );
        }
        List<CategoryVO>  categoryVOList = new ArrayList<>();
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){
            for (Category category:categoryList) {
                CategoryVO categoryVO = new CategoryVO();
                categoryVO.setId(category.getId());
                categoryVO.setName(category.getName());
                categoryVO.setParentId(category.getParentId());
                categoryVOList.add(categoryVO);
            }
            CategoryVOTree treeBuild = new CategoryVOTree(categoryVOList);
            categoryVOList = treeBuild.buildTree();
        }
        return categoryVOList;
    }
 
 
 
 
    @Override
    public List<CategoryVO>  getCategoryVOList(Integer categoryType,Integer isRoot){
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                .eq(Category::getIsdeleted, Constants.ZERO)
                .eq(Objects.nonNull(categoryType),Category::getType,categoryType)
                .isNull(Constants.equalsInteger(isRoot,Constants.ONE),Category::getParentId)
                .orderByAsc(Category::getSortnum,Category::getId)
        );
        List<CategoryVO>  categoryVOList = new ArrayList<>();
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)){
            for (Category category:categoryList) {
                CategoryVO categoryVO = new CategoryVO();
                categoryVO.setId(category.getId());
                categoryVO.setName(category.getName());
                categoryVO.setParentId(category.getParentId());
                categoryVOList.add(categoryVO);
            }
        }
        return categoryVOList;
    }
    public   List<Category>  findAllListForDca() {
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                .eq(Category::getIsdeleted, Constants.ZERO)
                .in(Category::getType, Constants.FOUR, Constants.SIX)//主题和观察项
                .orderByAsc(Category::getSortnum)
        );
        return  categoryList;
    }
    public   List<Category>  findListForDcaTree(   List<Category> categoryList) {
        if (com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)) {
            long index = 1;
            List<Category> pList = new ArrayList<>();
            for (Category category : categoryList) {
                if (category.getParentId() == null) {
                    category.setChildList(new ArrayList<>());
                    for (Category cc : categoryList) {
                        if (Constants.equalsInteger(cc.getParentId(), category.getId())) {
                            cc.setChildList(new ArrayList<>());
                            for (Category ccc : categoryList) {
                                if (Constants.equalsInteger(ccc.getParentId(), cc.getId())) {
                                    cc.getChildList().add(ccc);
                                }
                            }
                            category.getChildList().add(cc);
                        }
                    }
                    pList.add(category);
                }
            }
            return pList;
        }
        return  null;
    }
    public   List<Category>  findImportTreeForDca (  List<CategoryDcaProblemDto> categoryList,LoginUserInfo user, Date date) {
        List<Category> pList = new ArrayList<>();
        if (com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(categoryList)) {
            Integer index = 1;
            for (CategoryDcaProblemDto categoryDcaProblemDto : categoryList) {
                if(StringUtils.isBlank(categoryDcaProblemDto.getProblem())
                        &&StringUtils.isBlank(categoryDcaProblemDto.getParentName())
                        &&StringUtils.isBlank(categoryDcaProblemDto.getTypeName()) ){
                    //去空行
                    continue;
                }
                if(StringUtils.isBlank(categoryDcaProblemDto.getParentName())){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,表格第【"+(index + 1)+"】行一级主题信息不能为空,请返回检查表格信息!");
                }
                categoryDcaProblemDto.setProblem(StringUtils.defaultString(categoryDcaProblemDto.getProblem(),"").trim());
                categoryDcaProblemDto.setTypeName(StringUtils.defaultString(categoryDcaProblemDto.getTypeName(),"").trim());
                categoryDcaProblemDto.setParentName(StringUtils.defaultString(categoryDcaProblemDto.getParentName(),"").trim());
                Category first = getNewCateFromListByName(categoryDcaProblemDto.getParentName(), pList);
                if(first == null){
                    first = new Category();
                    first.setIsdeleted(Constants.ZERO);
                    first.setCreateDate(date);
                    first.setCreator(user.getId());
                    first.setIsdeleted(Constants.ZERO);
                    first.setStatus(Constants.ZERO);
                    first.setEditDate(date);
                    first.setEditor(user.getId());
                    first.setName(categoryDcaProblemDto.getParentName());
                    first.setType(Constants.FOUR);//主题
                    first.setSortnum(index++);
                    first.setChildList(new ArrayList<>());
                    pList.add(first);
                    List<Category> childList = getSecondListFromImport(first,0,categoryList);
                    first.setChildList(childList);
                }
            }
        }
        return  pList;
    }
 
    private List<Category> getSecondListFromImport(Category parent,int type,   List<CategoryDcaProblemDto> categoryList) {
        List<Category> pList = new ArrayList<>();
        Integer index = 1;
        for (CategoryDcaProblemDto categoryDcaProblemDto : categoryList) {
            if(type ==0 && (StringUtils.isBlank(categoryDcaProblemDto.getTypeName()) || StringUtils.isBlank(categoryDcaProblemDto.getProblem()))){
                //去空行
                continue;
            }
            if(type ==1 &&(StringUtils.isBlank(categoryDcaProblemDto.getProblem()))){
                //去空行
                continue;
            }
            if(type ==0 && StringUtils.isBlank(categoryDcaProblemDto.getTypeName())){
                throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,表格第【"+(index + 1)+"】行二级主题信息不能为空,请返回检查表格信息!");
            }
            if(type == 0 && !StringUtils.equals(categoryDcaProblemDto.getParentName(),parent.getName())){
                //只处理子级
                continue;
            }
            if(type == 1 && !StringUtils.equals(categoryDcaProblemDto.getTypeName(),parent.getName())){
                //只处理子级
                continue;
            }
            Category model = getNewCateFromListByName(type == 0 ?categoryDcaProblemDto.getTypeName():categoryDcaProblemDto.getProblem(), pList);
            if(model == null){
                model = new Category();
                model.setIsdeleted(Constants.ZERO);
                model.setCreateDate(parent.getCreateDate());
                model.setCreator(parent.getCreator());
                model.setIsdeleted(Constants.ZERO);
                model.setStatus(Constants.ZERO);
                model.setEditDate(parent.getCreateDate());
                model.setEditor(parent.getCreator());
                model.setName(type == 0 ?categoryDcaProblemDto.getTypeName():categoryDcaProblemDto.getProblem());
                model.setType(type == 0 ? Constants.FOUR:Constants.SIX);//
                model.setSortnum(index++);
                model.setChildList(new ArrayList<>());
                pList.add(model);
            }
            if(type == 0){
                //如果是二级主题处理获取子集部门
                List<Category> childList = getSecondListFromImport(model,1,categoryList);
                model.setChildList(childList);
            }
        }
        return  pList;
    }
 
    private Category getNewCateFromListByName(String parentName, List<Category> pList) {
        if(pList ==null){
            return  null;
        }
        for(Category cate : pList){
            if(StringUtils.equals(parentName,cate.getName())){
                    return cate;
            }
        }
        return null;
    }
    private Category getNewCateFromListByNameAndParent(String parentName,int type,boolean isRoot, List<Category> pList) {
        for(Category cate : pList){
            if(StringUtils.equals(parentName,cate.getName()) && Constants.equalsInteger(cate.getType(),type)){
                if(isRoot && cate.getParentId()==null){
                    return cate;
                }else  if(isRoot && cate.getParentId()==null){
                    return cate;
                }
            }
        }
        return null;
    }
 
    @Override
    public   List<CategoryDcaProblemDto>  findListForDca(Category param){
        long index =1;
        List<CategoryDcaProblemDto>  categoryVOList = new ArrayList<>();
        List<Category> pList = findListForDcaTree( findAllListForDca());
        for (Category category:pList) {
            if(category.getChildList().size() ==0){
                CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto();
                categoryVO.setIndex(index);
                categoryVO.setParentName(category.getName());
                index++;
                categoryVOList.add(categoryVO);
            }else{
                for (Category cc:category.getChildList()) {
                    if(cc.getChildList().size() ==0){
                        CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto();
                        categoryVO.setIndex(index);
                        categoryVO.setParentName(category.getName());
                        categoryVO.setTypeName(cc.getName());
                        index++;
                        categoryVOList.add(categoryVO);
                    }else{
                        for (Category ccc:cc.getChildList()) {
                            CategoryDcaProblemDto categoryVO = new CategoryDcaProblemDto();
                            categoryVO.setIndex(index);
                            categoryVO.setParentName(category.getName());
                            categoryVO.setTypeName(cc.getName());
                            categoryVO.setProblem(ccc.getName());
                            index++;
                            categoryVOList.add(categoryVO);
 
                        }
                    }
                }
            }
        }
        return categoryVOList;
    }
 
 
    @Override
    @PostConstruct
    public String initRedis(){
        redisTemplate.delete(Constants.RedisKeys.IMPORTING_CATEGORY);
        return  "";
    }
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public String importDcaBatch(MultipartFile file){
        Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.IMPORTING_CATEGORY);
        if(importing!=null && importing){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,已存在员工导入任务正在执行中,请稍后再试!");
        }
        redisTemplate.opsForValue().set(Constants.RedisKeys.IMPORTING_CATEGORY,true);
        try {
            ExcelImporter ie = null;
            List<CategoryDcaProblemDto> dataList =null;
            try {
                ie = new ExcelImporter(file,0,0, CellType.STRING); // 确保单元格类型为字符串);
                dataList = ie.getDataList(CategoryDcaProblemDto.class,null);
            }  catch (Exception e) {
                e.printStackTrace();
            }
            if(dataList == null || dataList.size() ==0){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入数据为空!");
            }
            Date date = new Date();
            LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
            List<Category> treeImportList =   findImportTreeForDca(dataList,user,date);//查询现有的全部主题观察项数据
            if(treeImportList == null || treeImportList.size() ==0){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入的有效数据为空!");
            }
            //查询现有的全部主题观察项数据
            List<Category> list = categoryMapper.selectJoinList(Category.class,new MPJLambdaWrapper<Category>()
                             .selectAll(Category.class)
//                             .select("t1.name",Category::getParentName)
//                             .select("t2.name",Category::getRootName)
//                             .leftJoin(Category.class,Category::getId,Category::getParentId)
//                             .leftJoin( "category t2 on t1.parent_id=t2.id")
                            .eq(Category::getIsdeleted, Constants.ZERO)
                            .in(Category::getType, Constants.FOUR, Constants.SIX)//主题和观察项
                            .orderByAsc(Category::getSortnum)
            ) ;
            List<Category> treeList =   findListForDcaTree(list);//查询现有的全部主题观察项数据
            List<Integer> allUpdateIds = new ArrayList<>();
            dealImportNewOrUpdateBiz(treeList,treeImportList,allUpdateIds);
            dealDeletedList(list,allUpdateIds,user,date);//处理删除的数据信息
            dealTreePathInfo(treeImportList);
            return "导入成功";
        }catch (BusinessException e){
            throw e;
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"员工信息导入失败,请稍后重试");
        }finally {
            redisTemplate.delete(Constants.RedisKeys.IMPORTING_CATEGORY);
        }
    }
 
    private void dealImportNewOrUpdateBiz(List<Category> treeList, List<Category> treeImportList, List<Integer> allUpdateIds) {
        List<Category> newList = new ArrayList<>();
        List<Category> updateList = new ArrayList<>();
        for(Category tmodel :treeImportList){
 
            //处理一级主题增改逻辑
            //查询是否存在名称相同的一级主题,存在则更新,不存在则新增
            Category first = getNewCateFromListByName(tmodel.getName(),treeList);
            if(first!=null){
                tmodel.setId(first.getId());
                tmodel.setChildMatchList(first.getChildList());
                allUpdateIds.add(first.getId());
                updateList.add(tmodel);
            }else{
                newList.add(tmodel);
            }
        }
 
        //先批量处理一级主题的增改数据,刷新一级主题对象的编码
        if(newList.size() >0){
            categoryMapper.insert(newList);
        }
        if( updateList.size() >0){
            for(Category update :updateList){
                categoryMapper.updateById(update);
            }
        }
        dealSecChildList(newList,updateList,allUpdateIds,1);//处理二级数据
    }
 
    private void dealDeletedList(List<Category> list, List<Integer> allUpdateIds, LoginUserInfo user, Date date) {
        List<Integer> deleteList = new ArrayList<>();
        for(Category cate : list){
            for(Integer nowId : allUpdateIds){
                if(Constants.equalsInteger(cate.getId(),nowId)){
                    continue;
                }
                deleteList.add(cate.getId());
            }
        }
        if(deleteList.size() >0){
            //删除不存在的
            categoryMapper.update(null,new UpdateWrapper<Category>().lambda()
                    .set(Category::getIsdeleted,Constants.ONE)
                    .set(Category::getEditor,user.getId())
                    .set(Category::getEditDate,date)
                    .in(Category::getId,deleteList));
        }
    }
 
    private void dealSecChildList(List<Category> newList, List<Category> updateList,List<Integer> allUpdateIds,int level) {
        List<Category> childNewList = new ArrayList<>();
        List<Category> childUpdateList = new ArrayList<>();
        if(newList.size() >0 || updateList.size()>0) {
            for(Category c : newList){
                if(c.getChildList()!=null && c.getChildList().size()>0){
                    for(Category sec : c.getChildList()){
                        sec.setParentId(c.getId());
                    }
                    childNewList.addAll(c.getChildList());
                }
            }
            for(Category c : updateList){
                if(c.getChildList()!=null && c.getChildList().size()>0){
                    List<Category> tList = c.getChildMatchList();
                    for(Category sec : c.getChildList()){
                        sec.setParentId(c.getId());
                        Category mmodel = getNewCateFromListByName(sec.getName(),tList);
                        if(mmodel!=null){
                            sec.setId(mmodel.getId());
                            sec.setChildMatchList(mmodel.getChildList());
                            allUpdateIds.add(mmodel.getId());
                            childUpdateList.add(sec);
                        }else{
                            childNewList.add(sec);
                        }
                    }
                }
            }
        }
        if(childNewList.size() >0){
            categoryMapper.insert(childNewList);
        }
        if( childUpdateList.size() >0){
            for(Category update :childUpdateList){
                categoryMapper.updateById(update);
            }
        }
        if(level == 1){
            dealSecChildList(childNewList,childUpdateList,allUpdateIds,2);//处理三级级数据
        }
    }
    private void dealTreePathInfo(List<Category> tree) {
        if(tree!=null || tree.size()>0){
            for(Category t : tree){
                t.setIdPath(t.getId()+"/");
                t.setNamePath(t.getName());
                categoryMapper.updateById(t);
                dealChildParentId(t,t.getChildList());
            }
        }
    }
 
    private void dealChildParentId(Category t, List<Category> childList) {
        if(childList ==null || childList.size()==0){
            return;
        }
        for(Category c : childList){
            c.setParentId(t.getId());
            c.setIdPath(t.getIdPath()+c.getId()+"/");
            c.setNamePath(t.getNamePath()+"/"+c.getName());
            categoryMapper.updateById(c);
            if(t.getChildList()!=null || t.getChildList().size()==0){
                //递归处理子集
                dealChildParentId(c,c.getChildList());
            }
        }
    }
 
}