zhangherong
2025-06-12 6abc1a2fb78003bb1ea6283d813b8ccc0bcd9b2b
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
package org.jeecg.modules.base.controller;
 
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.SelectTreeModel;
import org.jeecg.modules.base.entity.Enterprise;
import org.jeecg.modules.base.service.IEnterpriseService;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
 
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.system.entity.Administrator;
import org.jeecg.common.system.base.entity.DataVersion;
import org.jeecg.modules.system.service.IAdministratorService;
import org.jeecg.modules.system.service.IDataVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
 
 /**
 * @Description: 企业架构
 * @Author: jeecg-boot
 * @Date:   2022-11-21
 * @Version: V1.0
 */
@Api(tags="企业架构")
@RestController
@RequestMapping("/base/enterprise")
@Slf4j
public class EnterpriseController extends JeecgController<Enterprise, IEnterpriseService> {
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private IDataVersionService dataVersionService;
    @Autowired
    private IQuartzJobService quartzJobService;
    @Autowired
    private IAdministratorService administratorService;
     /**
      *
      * 获取集团架构树
      *
      */
    @GetMapping("/enterpriseTree")
    public Result<?> getEnterpriseTree(@RequestParam(value = "version", required = false) Integer version){
        SelectTreeModel selectTreeModel = new SelectTreeModel();
        selectTreeModel.setKey("0");
        selectTreeModel.setTitle("集团架构");
        List<SelectTreeModel> finallyList = new ArrayList<>();
        selectTreeModel.setChildren(enterpriseService.getEnterpriseVersionTree("0",version));
        finallyList.add(selectTreeModel);
        return Result.OK(finallyList);
    }
    /**
     * 分页列表查询
     *
     * @param map
     * @param pageNo
     * @param pageSize
     * @param parentId
     * @return
     */
    //@AutoLog(value = "企业架构-分页列表查询")
    @ApiOperation(value="企业架构-分页列表查询", notes="企业架构-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<Map<String,Object>>> queryPageList(Map map,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            @RequestParam(name = "parentId",required = false) String parentId,
                                            @RequestParam(name="version",required = false) Integer version
                                            ) {
        Page<Map<String,Object>> page = new Page(pageNo, pageSize);
        IPage<Map<String,Object>> pageList = enterpriseService.getEnterpriseListByPid(page,version,parentId);
        return Result.OK(pageList);
    }
     /**
      *
      * 查询当前生效版本列表
      *
      */
     @GetMapping("getUsableList")
    public Result<?> getUsableList(){
        return Result.OK(enterpriseService.getUsableList());
    }
 
    /**
     *
     * 查所有企业列表
     *
     */
    @GetMapping("/mapList")
    public Result<?> getEnterpriseList(){
        return Result.OK(enterpriseService.getEnterprise());
    }
 
    /**
     *   添加需要一次性完成
     *
     * @param map
     * @return
     */
    @AutoLog(value = "企业架构-添加")
    @ApiOperation(value="企业架构-添加", notes="企业架构-添加")
    @PostMapping(value = "/add")
    @Transactional
    public Result<String> add(@RequestBody Map<String,Object> map) {
        Enterprise enterprise = new Enterprise();
        //(设置保存的工作中心数据)
        enterprise.setCode(String.valueOf(map.get("code")))
                .setName(String.valueOf(map.get("name")))
                .setRemark(String.valueOf(map.get("remark")))
                .setVersion(Integer.parseInt(String.valueOf(map.get("version"))))
                .setStatus(String.valueOf(map.get("status")))
                .setParentId(String.valueOf(map.get("parentId")))
                .setDelFlag(0);
        enterpriseService.save(enterprise);
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<Enterprise>()
                .eq("code",enterprise.getCode()).eq("version",enterprise.getVersion()).eq("del_flag",0);
        Enterprise selectEnterprise = enterpriseService.getOne(queryWrapper,true);
        DataVersion dataVersion = new DataVersion();
        dataVersion.setBusinessId(selectEnterprise.getId())
                .setBusinessType("集团架构")
                .setVersion(selectEnterprise.getVersion())
                .setVersionStatus(String.valueOf(map.get("versionStatus")))
                .setDelFlag(0)
                .setEffectiveType("0")
                .setIsLastUsable("0");
        dataVersionService.save(dataVersion);
        QueryWrapper<DataVersion> queryWrapper1 = new QueryWrapper<DataVersion>()
                .eq("business_id",selectEnterprise.getId()).eq("del_flag",0);
        DataVersion selectDataVersion = dataVersionService.getOne(queryWrapper1,true);
        selectEnterprise.setDataVersionId(selectDataVersion.getId());
        enterpriseService.saveOrUpdate(selectEnterprise);
        return Result.OK("添加成功!");
    }
     /**
      * 升版
      * 需要一次完成
      * @param map
      * @param
      * @return
      */
     @PostMapping("/updateVersion")
     @Transactional
     public  Result<?> updateVersion(@RequestBody Map<String,Object> map){
         Integer version = Integer.parseInt((String)map.get("version"));
         Integer newVersion = enterpriseService.getAllVersion().stream().findFirst().get()+1;
         //该方法在api区
         recurrenceUpdata("0","0",version,newVersion);
         return Result.OK("升版成功",newVersion);
     }
     /**
      * 立即生效
      * 需要一次完成
      * @param map
      * @param
      * @return
      */
     @PostMapping("/updateVersionStatusToUsable")
     @Transactional
     public Result<?> updateVersionStatusToUsable(@RequestBody Map<String,Object> map){
         //如果有定时生生效,则将定时生效任务关闭
         QuartzJob quartzJob = quartzJobService.getOne(new QueryWrapper<QuartzJob>()
                 .eq("job_class_name","org.jeecg.modules.quartz.job.EnterpriseSetUsableJob"),true);
         if(ObjectUtils.isNotNull(quartzJob)){
                 quartzJobService.deleteAndStopJob(quartzJob);
         }
         //获取版本号
         Integer version = Integer.parseInt((String)map.get("version"));
         //获取上次生效版本并将其是否上次生效位置置为否
         List<Enterprise> lastUsableList = enterpriseService.getLastUsableList();
         if(CollectionUtils.isNotEmpty(lastUsableList)){
         List<DataVersion> lastUsableVersionList = dataVersionService.list( new QueryWrapper<DataVersion>()
                 .in("business_id",lastUsableList.stream()
                         .map(Enterprise::getId)
                         .collect(Collectors.toList())).eq("del_flag",0));
         lastUsableVersionList.stream().forEach(dataVersion -> {
             dataVersion.setIsLastUsable("0");
         });
         dataVersionService.updateBatchById(lastUsableVersionList);
         }
         //获取生效版本及版本信息并将其置为失效
         List<Enterprise> usableList = enterpriseService.getUsableList();
         if(CollectionUtils.isNotEmpty(usableList)){
             List<DataVersion> usableDataVersionList = dataVersionService.list(new QueryWrapper<DataVersion>()
                     .in("business_id",usableList.stream()
                             .map(Enterprise::getId)
                             .collect(Collectors.toList())));
             usableDataVersionList.stream().forEach(dataVersion -> {
                 dataVersion
                         .setVersionStatus("3")
                         .setIsLastUsable("1")
                         .setExpiredTime(new Date(System.currentTimeMillis()));
             });
             dataVersionService.updateBatchById(usableDataVersionList);
         }
         //获取待生效版本及版本信息并将其置为生效
         List<DataVersion> dataVersionList = dataVersionService.list(new QueryWrapper<DataVersion>()
                 .in("business_id",enterpriseService
                         .list(
                                 new QueryWrapper<Enterprise>()
                                         .eq("version",version)
                                         .eq("del_flag",0)
                         )
                         .stream().map(Enterprise::getId)
                         .collect(Collectors.toList())));
         dataVersionList.stream().forEach(dataVersion -> {
             dataVersion.setVersionStatus("2")
                     .setEffectiveType("2")
                     .setEffectiveTime(new Date(System.currentTimeMillis()))
                     .setIsLastUsable("0");
         });
         //存入数据库
         dataVersionService.updateBatchById(dataVersionList);
         return  Result.OK("生效成功");
     }
     /**
      * 定时生效
      * 需要一次性完成
      * @param quartzJob
      * @param
      * @return
      */
     @PostMapping("/updateVersionStatusToUsableBySetTime")
     @Transactional
     public Result<?> updateVersionStatusToUsableBySetTime(@RequestBody QuartzJob quartzJob) throws ParseException {
         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy");
         String jobClassName = quartzJob.getJobClassName();
         String time = quartzJob.getCronExpression();
         System.out.println(time);
         LocalDateTime localDateTime = LocalDateTime.parse(time,dateTimeFormatter);
         String cronStr=dateTimeFormatter1.format(localDateTime);
         quartzJob.setCronExpression(cronStr);
         QueryWrapper<QuartzJob> queryWrapper = new QueryWrapper();
         queryWrapper.eq("JOB_CLASS_NAME",jobClassName);
         List<QuartzJob> quartzJobs = quartzJobService.list(queryWrapper);
         if(CollectionUtils.isEmpty(quartzJobs)){
             quartzJobService.saveAndScheduleJob(quartzJob);
             quartzJobs = quartzJobService.list(queryWrapper);
             quartzJob.setId(quartzJobs.get(0).getId());
             quartzJobService.resumeJob(quartzJob);
             return  Result.OK(quartzJob);
         }else{
             quartzJob.setId(quartzJobs.get(0).getId());
             quartzJobService.resumeJob(quartzJob);
             return Result.OK(quartzJob);
         }
     }
    /**
     *  编辑
     *
     * @param enterprise
     * @return
     */
    @AutoLog(value = "企业架构-编辑")
    @ApiOperation(value="企业架构-编辑", notes="企业架构-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody Enterprise enterprise) {
        enterpriseService.updateById(enterprise);
        return Result.OK("编辑成功!");
    }
 
    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "企业架构-通过id删除")
    @ApiOperation(value="企业架构-通过id删除", notes="企业架构-通过id删除")
    @DeleteMapping(value = "/delete")
    @Transactional
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        dataVersionService.update(new UpdateWrapper<DataVersion>()
                .set("del_flag",1)
                .eq("business_id",id));
        enterpriseService.update(new UpdateWrapper<Enterprise>().
                set("del_flag",1)
                .eq("id",id));
        return Result.OK("删除成功!");
    }
 
 
    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "企业架构-批量删除")
    @ApiOperation(value="企业架构-批量删除", notes="企业架构-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @Transactional
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        dataVersionService.update(new UpdateWrapper<DataVersion>()
                .set("del_flag",1)
                .in("business_id",Arrays.asList(ids.split(","))));
        enterpriseService.
                update(new UpdateWrapper<Enterprise>()
                        .set("del_flag",1)
                        .in("id",Arrays.asList(ids.split(","))));
        return Result.OK("删除成功");
    }
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="企业架构-通过id查询", notes="企业架构-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Enterprise> queryById(@RequestParam(name="id",required=true) String id) {
        Enterprise enterprise = enterpriseService.getById(id);
        if(enterprise==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(enterprise);
    }
 
    /**
    * 导出excel
    *
    * @param request
    * @param enterprise
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Enterprise enterprise) {
        return super.exportXls(request, enterprise, Enterprise.class, "企业架构");
    }
 
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Enterprise.class);
    }
     /**
      *   表单验证
      *
      *
      */
     @GetMapping("/check")
     public Result<?> check(@RequestParam(name="name",required = false) String name,
                            @RequestParam(name="version",required = false) Integer version,
                            @RequestParam(name="parentId",required = true) String parentId
     ){
         QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
         if(!StringUtils.isBlank(name)){
             queryWrapper.eq("name",name);
         }
         if(version!=null){
             queryWrapper.eq("version",version);
         }
         if(!StringUtils.isBlank(parentId)){
             queryWrapper.eq("parent_id",parentId);
         }
         queryWrapper.eq("del_flag","0");
         return Result.OK(enterpriseService.list(queryWrapper));
     }
     /**
      * 获取初始版本号
      *
      *
      */
     @GetMapping("/getInitVersion")
     public Result<?> getUsableVersion(){
       return Result.OK(enterpriseService.getInitVersion());
     }
     /**
      * 获取所有版本号
      *
      *
      */
     @GetMapping("/getVersionList")
     public Result<?> getAllVersion(){
         return Result.OK(enterpriseService.getAllVersion());
     }
     /**
      * 获取当前生效版本和上一次生效版本的版本号
      *
      * @return
      */
     @GetMapping("/getNowAndLastUsableVersion")
     public Result<?> getNowAndLastUsableVersion(){
         return Result.OK(enterpriseService.getNowAndLastUsableVersion());
     }
     /**
      * 删除消息提醒
      *
      * @param delIndexList
      * @return
      */
     @PostMapping("/checkDel")
     @Transactional
     public Result<?> checkDel(@RequestBody List<String> delIndexList){
         List<Administrator> administrators  = administratorService
                 .list(new QueryWrapper<Administrator>()
                         .in("enterprise_id",delIndexList)
                         .eq("del_flag",0)
                 );
         List<String> list1 = administrators.stream().map(Administrator::getEnterpriseId).collect(Collectors.toList());
         Set<String> set  = new TreeSet<>();
         set.addAll(list1);
         if(CollectionUtils.isNotEmpty(set)){
             List<Enterprise> enterpriseList = enterpriseService.listByIds(set);
             final String[] listMessage = {""};
             if(CollectionUtils.isNotEmpty(enterpriseList)){
                 enterpriseList.stream().forEach(enterprise -> {
                     listMessage[0] = listMessage[0] +enterprise.getCode()+",";
                 });
                 String trueMessage = "编号为"+listMessage[0]
                         .substring(0, listMessage[0].length()-1)+"的数据被引用,确认删除吗?";
                 return Result.OK(trueMessage);
             }
         }
         return Result.OK("选中数据没有被引用,可以安全删除,确认删除吗?");
     }
     /**
      * 拖拽节点
      *
      * @param map
      * @return
      */
     @PostMapping("/dropAndChangeNode")
     @Transactional
     public Result<?> dropAndChangeNode(@RequestBody Map<String,String> map){
         String dropedKey=map.get("dropedKey");
         String changeKey=map.get("changeKey");
         String dropedParentId = map.get("dropedParentId");
         String changeParentId = map.get("changeParentId");
         Enterprise dropedEnterprise = enterpriseService.getById(dropedKey);
         Enterprise changeEnterprise = enterpriseService.getById(changeKey);
         String dropedCode = dropedEnterprise.getCode();
         String changeCode = changeEnterprise.getCode();
         String status = map.get("status");
         if("1".equals(status)||"3".equals(status)){
             dropedEnterprise.setId(changeKey).setParentId(changeParentId).setCode(changeCode);
             changeEnterprise.setId(dropedKey).setParentId(dropedParentId).setCode(dropedCode);
             List<Enterprise> list = new ArrayList<>();
             list.add(dropedEnterprise);
             list.add(changeEnterprise);
             enterpriseService.updateBatchById(list);
         }else if("4".equals(status)){
             //互换位置
             String finalCode="";
             List<Enterprise> changeChildList = enterpriseService
                     .list(new QueryWrapper<Enterprise>()
                             .eq("parent_id",changeKey)
                             .eq("del_flag",0));
             if(CollectionUtils.isNotEmpty(changeChildList)){
                 TreeSet<Integer> childIntCodeSet = new TreeSet<>(Comparator.reverseOrder());
                 changeChildList.stream().forEach(sysDepart -> {
                     String orgCode = sysDepart.getCode();
                     Integer subCode = Integer.parseInt(orgCode.substring(orgCode.lastIndexOf(".")+1));
                     childIntCodeSet.add(subCode);
                 });
                 Integer finalIntSubfix=childIntCodeSet.first()+1;
                 finalCode = changeCode+".0"+finalIntSubfix;
             }
             else {
                 finalCode = changeCode+".01";
             }
             dropedEnterprise.setParentId(changeKey).setCode(finalCode);
             enterpriseService.updateById(dropedEnterprise);
             //重新计算调整位置后各孩子的code
             changeEveryChildCode(dropedKey,finalCode);
         }else if("2".equals(status)){
             String prefix = changeCode.substring(0,changeCode.lastIndexOf("."));
             Integer changeSubfix = Integer.parseInt(changeCode.substring(changeCode.lastIndexOf(".")+1));
             List<Enterprise> changeChildList = enterpriseService.list(new QueryWrapper<Enterprise>()
                    .eq("parent_id",changeParentId)
                    .eq("del_flag",0));
             changeChildList.stream().forEach(enterprise -> {
                 String code = enterprise.getCode();
                 String id = enterprise.getId();
                 Integer numSubfix = Integer.parseInt(code.substring(code.lastIndexOf(".")+1));
                 if(numSubfix>changeSubfix){
                     numSubfix = numSubfix+1;
                     String finalCode = prefix+".0"+numSubfix;
                     enterprise.setCode(finalCode);
                     changeEveryChildCode(id,finalCode);
                }
            });
             enterpriseService.saveOrUpdateBatch(changeChildList);
             Integer numDropedSubfix = changeSubfix+1;
             String finalDropedCode = prefix+".0"+numDropedSubfix;
             dropedEnterprise.setCode(finalDropedCode).setParentId(changeParentId);
             changeEveryChildCode(dropedKey,finalDropedCode);
             enterpriseService.saveOrUpdate(dropedEnterprise);
         }else if("5".equals(status)){
             String prefix = changeCode.substring(0,changeCode.lastIndexOf("."));
             Integer changeSubfix = Integer.parseInt(changeCode.substring(changeCode.lastIndexOf(".")+1));
             List<Enterprise> changeChildList = enterpriseService.list(new QueryWrapper<Enterprise>()
                     .eq("parent_id",changeParentId)
                     .eq("del_flag",0));
             changeChildList.stream().forEach(enterprise -> {
                 String code = enterprise.getCode();
                 String id = enterprise.getId();
                 Integer numSubfix = Integer.parseInt(code.substring(code.lastIndexOf(".")+1));
                 if(numSubfix>=changeSubfix){
                     numSubfix = numSubfix+1;
                     String finalCode = prefix+".0"+numSubfix;
                     enterprise.setCode(finalCode);
                     changeEveryChildCode(id,finalCode);
                 }
             });
             enterpriseService.saveOrUpdateBatch(changeChildList);
             Integer numDropedSubfix = changeSubfix;
             String finalDropedCode = prefix+".0"+numDropedSubfix;
             dropedEnterprise.setCode(finalDropedCode).setParentId(changeParentId);
             changeEveryChildCode(dropedKey,finalDropedCode);
             enterpriseService.saveOrUpdate(dropedEnterprise);
         }
 
         return Result.OK("树结构调整成功");
 
     }
     /**
      * 判断是否有亲子关系
      *
      * @param map
      * @return
      */
     @PostMapping("/checkPositionInfo")
     public Result<?> checkPositionInfo(@RequestBody Map<String,String> map){
         String dropedKey=map.get("dropedKey");
         String changeKey=map.get("changeKey");
         return Result.OK(isChildren(changeKey,dropedKey)||isChildren(dropedKey,changeKey));
     }
 
     /*-----------------------------------------------以下为提供方便的api-------------------------------*/
     /**
      * 升版单个树节点并返回升版后新节点Id
      * @param sourceEnterprise
      * @param newParentId
      * @param newVersion
      * @return newEnterPriseId
      *
      */
     public String updataOneNode
     (
             Enterprise sourceEnterprise,
            String newParentId,
            Integer newVersion
     ){
         //1.取出旧版本信息;2.创建将新版本集团架构及其版本号并存入数据库
         // 1.
         DataVersion oldDataVersion = dataVersionService
                 .getOne(new QueryWrapper<DataVersion>()
                         .eq("business_id",sourceEnterprise.getId())
                         .eq("del_flag",0),true);
            sourceEnterprise.setId(null)
                    .setParentId(newParentId)
                    .setVersion(newVersion);
         //2.
         enterpriseService.save(sourceEnterprise);
         Enterprise newEnterprise = enterpriseService
                .getOne(new QueryWrapper<Enterprise>()
                        .eq("code",sourceEnterprise.getCode())
                        .eq("version",newVersion)
                        .eq("del_flag",0),true);
         DataVersion newDataVersion = new DataVersion();
         newDataVersion
                .setVersion(newVersion)
                .setBusinessType("集团架构")
                .setBusinessId(newEnterprise.getId())
                .setIsLastUsable("0")
                .setVersionStatus("1")
                .setDelFlag(0)
                .setSourceVersionId(oldDataVersion.getId());
         dataVersionService.save(newDataVersion);
         //将版本号存入关联的集团架构
         String newDataVersionId = dataVersionService
                .getOne(new QueryWrapper<DataVersion>()
                        .eq("business_id",newDataVersion.getBusinessId())
                         .eq("del_flag",0)).getId();
         enterpriseService.update(new UpdateWrapper<Enterprise>()
                .set("data_version_id",newDataVersionId)
                .eq("id",newEnterprise.getId()));
         return newEnterprise.getId();
     }
     /**
      * 递归升版多个节点
      * @param parentId
      * @param newParentId
      * @param version
      * @param newVersion
      * @return
      */
     public void recurrenceUpdata(String parentId,String newParentId,Integer version,Integer newVersion){
         CopyOnWriteArrayList<Enterprise> childList = new CopyOnWriteArrayList<>();
         childList.addAll(enterpriseService.list(new QueryWrapper<Enterprise>()
                .eq("parent_id",parentId)
                .eq("version",version)
                .eq("del_flag",0)));
         if(CollectionUtils.isNotEmpty(childList)){
             childList.stream().forEach(childEnterprise ->{
                 //把child的Id存下来,因为在流里边操作对象容易扯着蛋
                 String childNewId= childEnterprise.getId();
                 //将数据存入数据库
                 String childNewParentId=updataOneNode(childEnterprise,newParentId,newVersion);
                 //递归
                 recurrenceUpdata(childNewId,childNewParentId,version,newVersion);
            } );
        }
 
     }
     /**
      *  拖拽交换两个树的位置
      * @param dropedEnterprise
      * @param changeEnterprise
      * @return
      */
    public void changePosition(Enterprise dropedEnterprise,Enterprise changeEnterprise){
        String dropedId = dropedEnterprise.getId();
        String changeId = changeEnterprise.getId();
        String dropedParentId = dropedEnterprise.getParentId();
        String changeParentId = changeEnterprise.getParentId();
        String dropedCode = dropedEnterprise.getCode();
        String changeCode = changeEnterprise.getCode();
        //存在父换子
        if(dropedId.equals(changeParentId)){
            dropedEnterprise.setParentId(changeId);
            changeEnterprise.setParentId(dropedParentId).setCode(dropedCode);
            enterpriseService.saveOrUpdate(changeEnterprise);
            enterpriseService.saveOrUpdate(dropedEnterprise);
            changeEveryChildCode(changeId,dropedCode);
        }
        //存在子换父
        else if(changeId.equals(dropedParentId)){
            dropedEnterprise.setParentId(changeParentId).setCode(changeCode);
            changeEnterprise.setParentId(dropedId);
            enterpriseService.saveOrUpdate(dropedEnterprise);
            enterpriseService.saveOrUpdate(changeEnterprise);
            changeEveryChildCode(dropedId,changeCode);
        }
        else {
            dropedEnterprise.setParentId(changeParentId).setCode(changeCode);
            changeEnterprise.setParentId(dropedParentId).setCode(dropedCode);
            enterpriseService.saveOrUpdate(dropedEnterprise);
            enterpriseService.saveOrUpdate(changeEnterprise);
            changeEveryChildCode(changeId,dropedCode);
            changeEveryChildCode(dropedId,changeCode);
        }
    }
     /**
      *  递归调整树节点的code
      * @param parentId
      * @param prefix
      * @return
      */
     public void changeEveryChildCode(String parentId,String prefix){
         List<Enterprise> children = enterpriseService.list(new QueryWrapper<Enterprise>()
                        .eq("parent_id",parentId)
                        .eq("del_flag",0));
         if(CollectionUtils.isNotEmpty(children)){
            for(Enterprise enterprise:children){
                String id = enterprise.getId();
                String oldCode = enterprise.getCode();
                String subfix = oldCode.substring(oldCode.lastIndexOf(".")+1);
                String realCode = prefix+"."+subfix;
                enterpriseService.update(new UpdateWrapper<Enterprise>().set("code",realCode).eq("id",id));
                changeEveryChildCode(id,realCode);
            }
        }
     }
     /**
      *  递归判断是否为后辈公司
      * @param parentId
      * @param id
      * @return
      */
     public boolean isChildren(String parentId,String id){
         boolean tag = false;
         List<String> idList = enterpriseService.list(new QueryWrapper<Enterprise>()
                .eq("parent_id",parentId)
                .eq("del_flag",0))
                .stream()
                .map(Enterprise::getId)
                .collect(Collectors.toList());
         if(CollectionUtils.isNotEmpty(idList)){
             for(String strId:idList){
                 if(strId.equals(id)){
                     return true;
                }
                 else if(!tag){
                     tag = isChildren(strId,id);
                }
            }
        }
         return tag;
     }
 
}