zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
package org.jeecg.modules.doc.controller;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.IPUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.doc.api.JeecgSystemClient;
import org.jeecg.modules.doc.component.DocFileDealComp;
import org.jeecg.modules.doc.component.PathFileUtil;
import org.jeecg.modules.doc.component.PathPermissionComp;
import org.jeecg.modules.doc.component.UfopConfig;
import org.jeecg.modules.doc.constant.Constant;
import org.jeecg.modules.doc.dto.*;
import org.jeecg.modules.doc.entity.*;
import org.jeecg.modules.doc.service.*;
import org.jeecg.modules.doc.vo.CompareFileVo;
import org.jeecg.modules.doc.vo.DocTree;
import org.jeecg.modules.doc.exception.QiwenException;
import org.jeecg.modules.doc.vo.QiwenFile;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
 
import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
 
@RestController
@RequestMapping("/document")
@Api("文档管理")
@ApiOperation("文件管理")
@Slf4j
public class LbDocController {
 
    @Autowired
    private UfopConfig ufopConfig;
    @Autowired
    private ILbDocService lbDocService;
    @Autowired
    private DocFileDealComp fileDealComp;
    @Autowired
    private IDocFilePathService filePathService;
    @Autowired
    private IDocFileService docFileService;
    @Autowired
    private PathPermissionComp pathPermissionComp;
    @Autowired
    private IUserStorageFlowService userStorageFlowService;
    @Autowired
    private JeecgSystemClient jeecgSystemClient;
 
    @Autowired
    private IDocOperationHisService operationHisService;
 
    @Autowired
    private RedisUtil redisUtil;
 
 
    @ApiOperation( "本地目录树")
    @GetMapping(value = "/docTree")
    public List<DocTree> queryPageList(DocTree docTree) {
        System.out.println("开始:: " + System.currentTimeMillis());
        if (docTree.getPath() == null || "".equals(docTree.getPath())){
            docTree.setPath(ufopConfig.getReleasePath());
            docTree.setKey("0-0");
        }
        docTree = lbDocService.genDocTree( docTree );
        System.out.println("结束::" + System.currentTimeMillis());
        return docTree.getChildren();
    }
 
    @AutoLog(value = "初始化同步本地目录")
    @ApiOperation("初始化同步本地目录")
    @GetMapping("/init")
    public void initDoc() {
        System.out.println("开始初始化:::" + System.currentTimeMillis());
        List<DocFilePath> pathList = filePathService.list(
                new LambdaQueryWrapper<DocFilePath>()
 
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/01 公共区域")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/02 3D视觉事业部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/03 研发二部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/04 研发三部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/05 开发一部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/06 开发二部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/07 开发三部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/08 工业设计部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/09 研发管理部")
//                        .likeRight(DocFilePath::getFilePath,"/工作文件区/10 兰浦公司")
                        .likeRight(DocFilePath::getFilePath,"/档案资料区")
 
 
        );
        Map<String, String> paths = new HashMap<>();
        pathList.stream().forEach(item -> {
//            if (item.getFileName().contains("20171201技术体系文件标准化管理制度V1")) {
//                System.out.println("["+item.getFilePath()+item.getFileName()+(item.getExtendName().isEmpty()?"":item.getExtendName())+item.getIsDir()+"]");
//            }
            paths.put(item.getFilePath()+item.getFileName()+(item.getExtendName().isEmpty()?"":item.getExtendName())+item.getIsDir(), item.getPathId());
        });
        System.out.println("当前已有:" + paths.size());
        String releasePath = ufopConfig.getReleasePath();
        //filePathService.initDocFilePath();
        lbDocService.initDoc(paths, releasePath, null);
        System.out.println("初始化完成:::" + System.currentTimeMillis());
    }
 
 
 
    @AutoLog(value = "初始化同步本地目录结构")
    @ApiOperation("初始化同步本地目录结构")
    @GetMapping("/initDir")
    public void initDir() {
        System.out.println("开始初始化:::" + System.currentTimeMillis());
        List<DocFilePath> pathList = filePathService.list(new LambdaQueryWrapper<DocFilePath>().eq(DocFilePath::getIsDir, 1));
//        String releasePath = ufopConfig.getReleasePath();
//
//        Map<String, String> paths = new HashMap<>();
//        pathList.stream().forEach(item -> {
//            paths.put(item.getFilePath()+item.getFileName()+(item.getExtendName().isEmpty()?"":item.getExtendName())+item.getIsDir(), item.getPathId());
//        });
//
//                System.out.println("当前已有:" + paths.size());
//        List<DocFilePath> filePaths = new ArrayList<>();
//
//        lbDocService.initDir(paths,filePaths,releasePath, null);
//        System.out.println("文件夹有:" + filePaths.size());
//        filePathService.saveOrUpdateBatch(filePaths);
//        System.out.println("初始化完成:::" + System.currentTimeMillis());
//
 
 
 
 
//        filePaths = null;
//        pathList = filePathService.list();
 
        Map<String, String> dirMap = new HashMap<>();
 
        pathList.stream().forEach(item -> {
            if (item.isDirectory()) {
                if (item.getFilePath().endsWith("/")) {
                    dirMap.put(item.getFilePath()+item.getFileName(), item.getPathId());
                } else {
                    dirMap.put(item.getFilePath()+"/"+item.getFileName(), item.getPathId());
                }
 
            }
 
        });
        System.out.println("文件夹map准备完成::" + System.currentTimeMillis());
        List<DocFilePath> newList = new ArrayList<>();
        pathList.stream().forEach(item -> {
            if ( item.getParentId() == null || !item.getParentId().equals(dirMap.get(item.getFilePath()))) {
                item.setParentId(dirMap.get(item.getFilePath()));
                newList.add(item);
            }
 
        });
        System.out.println("文件夹排序完成::"  + System.currentTimeMillis());
        System.out.println("发现有问题目录:" + newList.size() +"个");
        filePathService.saveOrUpdateBatch(newList);
        System.out.println("更新完成::" + System.currentTimeMillis());
 
    }
//    @AutoLog(value = "检测文件是否损坏")
//    @ApiOperation("检测文件是否损坏")
//    @GetMapping("/checkFile")
//    public void checkFile() {
//        String filePath ="//192.168.0.9/研发技术文档";
//
//        lbDocService.checkFile(filePath);
//    }
//
//
//
//    @AutoLog(value = "检测文件是否损坏")
//    @ApiOperation("检测文件是否损坏")
//    @GetMapping("/checkFileIsExist")
//    public void checkFileIsExist() {
//        List<DocFile> list = docFileService.list();
//        String dir = "//192.168.0.9/研发技术文档";
//        List<DocFile> upids = new ArrayList<>();
//        list.forEach(file -> {
//            String fileurl = file.getFileUrl().replace("upload", dir);
//            File ff = new File(fileurl);
//
//            if(!ff.exists()) {
//                file.setFileStatus(0);
//                upids.add(file);
//
//                System.out.println(file.getFileId());
//            }
//        });
//        System.out.println("待更新个数:"+upids.size());
//        docFileService.updateBatchById(upids);
//        System.out.println("完成++++");
//
//    }
 
 
    @AutoLog(value = "获取同步目标文件个数")
    @ApiOperation("获取同步目标文件个数")
    @GetMapping("/initDocSize")
    public void initDocSize() {
 
        Map<String, Long> map = new HashedMap();
        map.put("dsize", 0L);
        map.put("fsize", 0L);
        String releasePath = ufopConfig.getReleasePath();
        lbDocService.initDocSize(map,releasePath,null);
        System.out.println("文件总数:" + map.get("fsize"));
        System.out.println("文件夹总数:" + map.get("dsize"));
        System.out.println("总个数:" +(map.get("fsize")+map.get("dsize")));
    }
 
 
    /**
     * 获取完整目录树
     * @return
     */
    @ApiOperation(value = "获取目录结构树", notes = "左侧目录树")
    @GetMapping("/getFileTree")
    public Map<String,Object> getFileTree() {
        System.out.println("获取目录树::" + LocalDateTime.now().toString());
 
        // 获取访问集合
        Set<String> visits = pathPermissionComp.getVisible();
        Map<String, Object> map = new HashMap<>();
        // 查询有访问权限的文件列表
        List<DocFilePath> pathList = filePathService.listDirTree(null, visits);
        // 查询有访问权限的文件下的子文件
        LambdaQueryWrapper<DocFilePath> subQueryWrapper = new LambdaQueryWrapper<>();
        subQueryWrapper.eq(DocFilePath::getIsDir, Constant.IS_DIR)
                .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE)
                .and(i -> {
                    i.apply("{0} = {1}",1,2);  // 无具体意义,方便后边拼接
 
                    pathList.stream().forEach(path -> {
                        i.or().likeRight(DocFilePath::getFilePath,new QiwenFile(path.getFilePath(),path.getFileName(),true).getPath());
                    });
                });
 
        List<DocFilePath> subList = filePathService.list(subQueryWrapper);
        System.out.println("查询完成::" + LocalDateTime.now());
        pathList.addAll(subList);
        DocTree resultTreeNode = getResultTreeNode(pathList);
        map.put("success", true);
        map.put("result", resultTreeNode);
 
        System.out.println("获取完成:::" + LocalDateTime.now());
        return map;
    }
 
 
    /**
     * 异步获取目录树
     * @param parent 父级目录
     * @return
     */
    @ApiOperation(value = "异步获取目录结构树", notes = "异步获取左侧目录树")
    @GetMapping("/getAsyncTree")
    public Map<String, Object> getAsyncTree(DocTreeDTO parent ){
        Map<String, Object> map = new HashMap<>();
        // 访问、下载、管理权限包
        Map<String, Map<String, DocFilePath>> pathPermission = pathPermissionComp.getPathPermission();
        // 拥有访问权限的文件id集合
        Map<String, DocFilePath> visits =  pathPermission.get(Constant.VISIT);
        //DocTree parent = new DocTree();
        DocTree tree = getDocTree(parent, visits, map);
        map.put("success", true);
        map.put("result", tree);
        return map;
    }
 
    /**
     * 根据传过来的上级方件组织异步树的下级文件列表
     * @param parent 上级文件
     * @param visits 可访问文件列表
     * @param map 返回前端对象
     * @return
     */
    @NotNull
    private DocTree getDocTree(DocTreeDTO parent, Map<String, DocFilePath> visits, Map<String, Object> map) {
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
 
 
        if (parent.getPathId() == null) {
            queryWrapper.isNull(DocFilePath::getParentId);
            parent.setTitle("全部文件");
            parent.setPath(QiwenFile.separator);
            parent.setId(0L);
            parent.setKey("0");
            parent.setPathId("1");
            parent.setTargetId("1");
        } else if ("1".equals(parent.getPathId())) {
            queryWrapper.isNull(DocFilePath::getParentId);
 
        } else {
            queryWrapper.eq(DocFilePath::getParentId, parent.getPathId());
        }
        queryWrapper.eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE)
                .eq(DocFilePath::getIsDir, Constant.IS_DIR);  // 注释掉后同时查询文件夹和文件
        queryWrapper.orderByAsc(DocFilePath::getFileName);
        List<DocTree> trees = new ArrayList<>();
        List<DocFilePath> list = filePathService.list(queryWrapper);
        // 当前查询结果中可访问的文件集合
        list = list.stream().filter(
                i -> visits.get(i.getPathId())!=null || fileDealComp.isParentsOrChild(visits,i, true)
        ).collect(Collectors.toList());
        String[] fileIds = new String[list.size()];
        for (int i=0; i< list.size(); i++) {
            fileIds[i] = list.get(i).getPathId();
            trees.add(new DocTree(list.get(i), parent.getKey()+ "-" + i));
        }
        setIsLeaf(trees, fileIds);
        DocTree tree = new DocTree(parent);
        tree.getChildren().addAll(trees);
        // 点击收藏列表递归查询,直到找到目标文件
        if (!tree.getPathId().equals(parent.getTargetId()) ) {
 
            for (DocTree i: tree.getChildren() ) {
                if (i.getPathId().equals(parent.getTargetId())||parent.getPath().startsWith(i.getPath()+QiwenFile.separator)) {
                    DocTreeDTO docTreeDTO = new DocTreeDTO(i);
                    docTreeDTO.setTargetId(parent.getTargetId());
                    docTreeDTO.setPath(parent.getPath());
                    DocTree docTree = this.getDocTree(docTreeDTO, visits, map);
                    i.setChildren(docTree.getChildren());
                }
            }
        }  else {
            map.put("target", tree);
        }
 
        return tree;
    }
 
    /**
     * 判断是否子节点
     * @param trees
     * @param fileIds
     */
    private void setIsLeaf(List<DocTree> trees, String[] fileIds) {
        List<Map<String,Object>> subCount = fileIds.length>0? filePathService.querySubCount(fileIds): new ArrayList<>();
 
        Map<String,Integer> subMap = subCount.stream().collect(Collectors.toMap(i->i.get("parentId").toString(), i-> new Integer( i.get("num").toString())));
        trees.stream().forEach(t -> {
            if (subMap.get(t.getPathId())!=null &&subMap.get(t.getPathId())>0) {
                t.setLeaf(false);
            } else {
                t.setLeaf(true);
            }
        });
    }
 
    @NotNull
    private DocTree getResultTreeNode(List<DocFilePath> pathList) {
        DocTree resultTreeNode = new DocTree();
        resultTreeNode.setTitle("全部文件");
        resultTreeNode.setPath(QiwenFile.separator);
        resultTreeNode.setId(0L);
        resultTreeNode.setKey("0");
        resultTreeNode.setPathId("1");
        long id = 1;
        for (int i = 0; i < pathList.size(); i++){
            DocFilePath userFile = pathList.get(i);
            QiwenFile qiwenFile = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), false);
            String filePath = qiwenFile.getPath();
            Queue<String> queue = new LinkedList<>();
            String[] strArr = filePath.split(QiwenFile.separator);
            for (int j = 0; j < strArr.length; j++){
                if (!"".equals(strArr[j]) && strArr[j] != null){
                    queue.add(strArr[j]);
                }
            }
            if (queue.size() == 0){
                continue;
            }
            resultTreeNode = fileDealComp.insertTreeNode(resultTreeNode, id++, QiwenFile.separator, queue, userFile.getPathId());
        }
        System.out.println("组织树完成::" + LocalDateTime.now());
        fileDealComp.sortByKey(resultTreeNode);
        fileDealComp.checkLeaf(resultTreeNode);
        return resultTreeNode;
    }
 
 
    @ApiOperation(value = "获取目录结构树列表", notes = "用于分配文件夹权限")
    @GetMapping("/queryTreeList")
    public Result<Map<String,Object>> queryTreeList(DocFilePath docFilePath) {
        Result<Map<String,Object>> result = new Result<>();
        List<DocFilePath> pathList = filePathService.listDirTree(docFilePath,null);
        List<String> ids = pathList.stream().map(DocFilePath::getPathId).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        DocTree resultTreeNode = getResultTreeNode(pathList);
        map.put("treeList", resultTreeNode.getChildren());
        map.put("ids",ids);
        result.setSuccess(true);
        result.setResult(map);
        return result;
    }
 
 
 
    @ApiOperation(value = "获取异步目录结构树列表", notes = "用于分配文件夹权限")
    @GetMapping("/queryAsyncTreeList")
    public Result<Map<String,Object>> queryAsyncTreeList(DocTreeDTO parent) {
        Result<Map<String,Object>> result = new Result<>();
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
 
        if (parent.getFileName()!=null && parent.getFileName().trim().length()>0) {
            queryWrapper.like(DocFilePath::getFileName, parent.getTitle());
        } else {
            if (parent.getPathId() == null) {
                queryWrapper.isNull(DocFilePath::getParentId);
                parent.setTitle("全部文件");
                parent.setPath(QiwenFile.separator);
                parent.setId(0L);
                parent.setKey("0");
                parent.setPathId("1");
            } else {
                queryWrapper.eq(DocFilePath::getParentId, parent.getPathId());
            }
        }
        queryWrapper.eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE)
                .eq(DocFilePath::getIsDir, Constant.IS_DIR);
        queryWrapper.orderByDesc(DocFilePath::getIsDir).orderByAsc(DocFilePath::getFileName);
 
 
        List<DocTree> trees = new ArrayList<>();
        List<DocFilePath> list = filePathService.list(queryWrapper);
        String[] fileIds = new String[list.size()];
        for (int i=0; i< list.size(); i++) {
            fileIds[i] = list.get(i).getPathId();
            trees.add(new DocTree(list.get(i), parent.getKey()+ "-" + i));
        }
 
 
        setIsLeaf(trees,fileIds);
        List<String> ids = list.stream().map(DocFilePath::getPathId).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        //DocTree resultTreeNode = getResultTreeNode(pathList);
        map.put("treeList", trees);
        map.put("ids",ids);
        result.setSuccess(true);
        result.setResult(map);
        return result;
    }
 
 
    /**
     * 点击目录树节点,查询前端主界面文件列表内容
     * @param path
     * @return
     */
    @ApiOperation("根据路径查询文件列表")
    @GetMapping("/listFile")
    public Map<String, Object> queryFileList(String path, String pathId) {
 
        QueryWrapper<DocFilePath> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DocFilePath::getFilePath,path)
                .eq(DocFilePath::getDeleteFlag,Constant.DEL_FALSE)
                .orderByDesc(DocFilePath::getIsDir);
 
//                .orderBy(DocFilePath::getFileName + 0)
                queryWrapper.orderByAsc("((file_name + 0) > 0) desc, (file_name + 0) asc,file_name");
                //.orderByAsc(DocFilePath::getFileName);
 
 
//        boolean create = filePathService.selectPermissionByPath(pathId);
        Map<String, Object> listFileResult = getListFileResult(queryWrapper, pathId);
//        listFileResult.put("create", create);
        return listFileResult;
    }
 
    /**
     * 根据传递过来的sql条件,查询文件夹下的子目录 或 文件搜索
     * TODO 待返回当前点击节点的权限,用于判断新建文件夹和上传文件及上传文件夹权限
     * TODO 需要查询子文件没有权限但下级子文件有权限的文件夹显示问题。全局文件搜索不需要此操作。
     * @param queryWrapper
     * @return
     */
    @NotNull
    private Map<String, Object> getListFileResult(QueryWrapper<DocFilePath> queryWrapper, String pathId) {
        Map<String, Object> result = new HashMap<>();
        List<DocFilePath> filePaths = filePathService.list(queryWrapper);
 
        // 访问、下载、管理权限包
        Map<String, Map<String, DocFilePath>> pathPermission = pathPermissionComp.getPathPermission();
        // 拥有访问权限的文件id集合
        Map<String, DocFilePath> visits =  pathPermission.get(Constant.VISIT);
        // 拥有下载权限的文件id集合
        Map<String, DocFilePath> downloads =  pathPermission.get(Constant.DOWNLOAD);
        // 拥有管理权限的文件id集合
        Map<String, DocFilePath> manages =  pathPermission.get(Constant.MANAGE);
 
 
 
        // 当前查询结果中可访问的文件集合
        filePaths = filePaths.stream().filter(
                i -> visits.get(i.getPathId())!=null || fileDealComp.isParentsOrChild(visits,i, true)
                ).collect(Collectors.toList());
 
        Set<String> downloadSet = filePaths.stream().filter(
                i -> downloads.get(i.getPathId()) != null || fileDealComp.isParentsOrChild(downloads,i, false)
        ).map(DocFilePath::getPathId).collect(Collectors.toSet());
        Set<String> manageSet = filePaths.stream().filter(
                i -> manages.get(i.getPathId()) != null || fileDealComp.isParentsOrChild(manages,i, false)
        ).map(DocFilePath::getPathId).collect(Collectors.toSet());
 
        // 是文件需要查询文件大小
        Set<String> fileIds = filePaths.stream()
                .filter(docFilePath -> docFilePath.getFileId()!=null&&!"".equals(docFilePath.getFileId()))
                .map(docFilePath -> docFilePath.getFileId())
                .collect(Collectors.toSet());
        if (fileIds.size()>0) {
            // 查询文件大小
            List<DocFile> files = docFileService.list(new LambdaQueryWrapper<DocFile>().in(DocFile::getFileId,fileIds));
            Map<String, Long> sizeMap = files.stream().collect(Collectors.toMap(DocFile::getFileId,DocFile::getFileSize));
            filePaths.stream().forEach(p -> p.setFileSize(sizeMap.get(p.getFileId())));
        }
 
        result.put("create", filePathService.selectPermissionByPath(pathId, manages));
        result.put("upload", filePathService.selectPermissionByPath(pathId, downloads));
        result.put("success", true);
        result.put("result", filePaths);
        result.put("downloadPrem", downloadSet);
        result.put("managePrem", manageSet);
        return result;
    }
 
 
 
    @ApiOperation("根据名称模糊搜索文件列表")
    @GetMapping("/searchFile")
    public Map<String, Object> searchFileList(String name) {
 
        QueryWrapper<DocFilePath> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(DocFilePath::getFileName,name)
                .eq(DocFilePath::getDeleteFlag,Constant.DEL_FALSE)
                .orderByDesc(DocFilePath::getIsDir);
                //.orderByAsc(DocFilePath::getFileName);
        queryWrapper.orderByAsc("((file_name + 0) > 0) desc, (file_name + 0) asc,file_name");
        return getListFileResult(queryWrapper, null);
    }
 
    @ApiOperation("创建文件")
    @PostMapping("createFile")
    public Result<Object> createFile(@RequestBody DocFilePath docFilePath, HttpServletRequest request) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        boolean isDirExist = fileDealComp.isDirExist(docFilePath.getFileName(), docFilePath.getFilePath(), docFilePath.getIsDir());
 
        if (isDirExist) {
            return Result.error("同名文件已存在");
        }
        DocFilePath parent = fileDealComp.getParent(docFilePath.getFilePath());
        DocFilePath filePath = PathFileUtil.getDir(docFilePath.getFilePath(), docFilePath.getFileName(), parent);
        filePathService.save(filePath);
 
        operationHisService.saveHis(loginUser,docFilePath,Constant.OPERATION_CREATE);
        return Result.OK();
    }
 
    @ApiOperation("创建文件")
    @PostMapping("test")
    public Result<Object> test(@RequestBody DocFilePath docFilePath, HttpServletRequest request) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
 
//        boolean isDirExist = fileDealComp.isDirExist(docFilePath.getFileName(), docFilePath.getFilePath(), docFilePath.getIsDir());
//
//        if (isDirExist) {
//            return Result.error("同名文件已存在");
//        }
//        DocFilePath parent = fileDealComp.getParent(docFilePath.getFilePath());
//        DocFilePath filePath = PathFileUtil.getDir(docFilePath.getFilePath(), docFilePath.getFileName(), parent);
//        filePathService.save(filePath);
//
//        operationHisService.saveHis(loginUser,docFilePath,Constant.OPERATION_CREATE);
        return Result.OK();
    }
 
 
 
 
    @ApiOperation(value = "修改文件", notes = "支持普通文本类文件的修改", tags = {"file"})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<String> updateFile(@RequestBody UpdateFileDTO updateFileDTO) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
        // 获取路径文件
        DocFilePath userFile = filePathService.getById(updateFileDTO.getUserFileId());
        // 获取目标文件
        DocFile fileBean = docFileService.getById(userFile.getFileId());
        // 目标文件被引用数
        Long pointCount = docFileService.getFilePointCount(userFile.getFileId());
        String fileUrl = fileBean.getFileUrl();
        if (pointCount > 1) {
            fileUrl = fileDealComp.copyFile(fileBean, userFile);
        }
        String content = updateFileDTO.getFileContent();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
        try {
            long fileSize = byteArrayInputStream.available();
            fileDealComp.saveFileInputStream( fileUrl, byteArrayInputStream);
            String md5Str = fileDealComp.getIdentifierByFile(fileUrl, 0);
            docFileService.updateFileDetail(userFile.getPathId(), md5Str, fileSize, loginUser.getId());
        } catch (Exception e) {
            throw new QiwenException(999999, "修改文件异常");
        } finally {
            try {
                byteArrayInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_UPDATE);
        return Result.OK("修改文件成功");
    }
 
 
    @ApiOperation(value = "批量删除文件", notes = "批量删除文件", tags = {"file"})
    @RequestMapping(value = "/batchdeletefile", method = RequestMethod.POST)
    public Result<String> deleteImageByIds(@RequestBody BatchDeleteFileDTO batchDeleteFileDto) {
        System.out.println("批量删除文件:::" + LocalDateTime.now().toString());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<DocFilePath> userFiles = JSON.parseArray(batchDeleteFileDto.getFiles(), DocFilePath.class);
        DigestUtils.md5Hex("data");
        for (DocFilePath userFile : userFiles) {
 
            filePathService.deleteUserFile(userFile.getPathId(),loginUser.getId());
            operationHisService.saveHis(loginUser,userFile,Constant.OPERATION_DELETE);
            //fileDealComp.deleteESByUserFileId(userFile.getPathId());
        }
        // 删除reids中缓存的 CacheConstant.DOC_FILE_PATH_CACHE 下的所有key
        redisUtil.removeAll(CacheConstant.DOC_FILE_PATH_CACHE);
 
        return Result.OK("批量删除文件成功");
    }
 
    @ApiOperation(value = "删除文件", notes = "可以删除文件或者目录", tags = {"file"})
    @RequestMapping(value = "/deletefile", method = RequestMethod.POST)
    public Result deleteFile(@RequestBody DeleteFileDTO deleteFileDto) {
        System.out.println("删除文件:::" + LocalDateTime.now().toString());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DocFilePath byId = filePathService.getById(deleteFileDto.getUserFileId());
        filePathService.deleteUserFile(deleteFileDto.getUserFileId(), loginUser.getId());
        //fileDealComp.deleteESByUserFileId(deleteFileDto.getUserFileId());
        System.out.println("删除完成:::" + LocalDateTime.now().toString());
        operationHisService.saveHis(loginUser,byId,Constant.OPERATION_DELETE);
        // 删除reids中缓存的 CacheConstant.DOC_FILE_PATH_CACHE 下的所有key
        redisUtil.removeAll(CacheConstant.DOC_FILE_PATH_CACHE);
        return Result.OK();
 
    }
 
 
    @ApiOperation(value = "文件重命名", notes = "文件重命名", tags = {"file"})
    @RequestMapping(value = "/renamefile", method = RequestMethod.POST)
    public Result<String> renameFile(@RequestBody RenameFileDTO renameFileDto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
        DocFilePath userFile = filePathService.getById(renameFileDto.getUserFileId());
 
        List<DocFilePath> userFiles = filePathService.selectUserFileByNameAndPath(renameFileDto.getFileName(), userFile.getFilePath(), loginUser.getId());
        if (userFiles != null && !userFiles.isEmpty()) {
            return Result.error("同名文件已存在", null);
        }
 
        LambdaUpdateWrapper<DocFilePath> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(DocFilePath::getFileName, renameFileDto.getFileName())
                .set(DocFilePath::getUpdateTime, DateUtils.getDate())
                .eq(DocFilePath::getPathId, renameFileDto.getUserFileId());
        filePathService.update(lambdaUpdateWrapper);
        if (Constant.IS_DIR == userFile.getIsDir()) {
            List<DocFilePath> list = filePathService.selectUserFileByLikeRightFilePath(new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true).getPath());
 
            for (DocFilePath newUserFile : list) {
                newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(new QiwenFile(userFile.getFilePath(), userFile.getFileName(), userFile.getIsDir() == Constant.IS_DIR).getPath().replace("(","\\(").replace(")","\\)"),
                        new QiwenFile(userFile.getFilePath(), renameFileDto.getFileName(), userFile.getIsDir() == Constant.IS_DIR).getPath()));
                filePathService.updateById(newUserFile);
            }
        }
    //    fileDealComp.uploadESByUserFileId(renameFileDto.getUserFileId());
        operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_RENAME);
        return Result.OK();
    }
 
 
    @ApiOperation(value = "解压文件", notes = "解压文件。", tags = {"file"})
    @RequestMapping(value = "/unzipfile", method = RequestMethod.POST)
    public Result<String> unzipFile(@RequestBody UnzipFileDTO unzipFileDto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        try {
            DocFilePath byId = filePathService.getById(unzipFileDto.getUserFileId());
            docFileService.unzipFile(unzipFileDto.getUserFileId(), unzipFileDto.getUnzipMode(), unzipFileDto.getFilePath());
            operationHisService.saveHis(loginUser, byId, Constant.OPERATION_UNZIP);
        } catch (QiwenException e) {
            return Result.error(e.getMessage(),null);
        }
 
        return Result.OK();
 
    }
 
    @ApiOperation(value = "文件复制", notes = "可以复制文件或者目录", tags = {"file"})
    @RequestMapping(value = "/copyfile", method = RequestMethod.POST)
    public Result<String> copyFile(@RequestBody CopyFileDTO copyFileDTO) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
 
        String userFileId = copyFileDTO.getUserFileId();
        DocFilePath userFile = filePathService.getById(userFileId);
        String oldfilePath = userFile.getFilePath();
        String newfilePath = copyFileDTO.getFilePath();
        String fileName = userFile.getFileName();
        if (userFile.getIsDir() == Constant.IS_DIR) {
            QiwenFile qiwenFile = new QiwenFile(oldfilePath, fileName, true);
            if (newfilePath.startsWith(qiwenFile.getPath() + QiwenFile.separator) || newfilePath.equals(qiwenFile.getPath())) {
                return Result.error("原路径与目标路径冲突,不能复制",null);
            }
        }
 
        filePathService.userFileCopy(userFileId, newfilePath);
        fileDealComp.deleteRepeatSubDirFile(newfilePath);
        operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_COPY);
        return Result.OK();
 
    }
 
    @ApiOperation(value = "批量复制文件", notes = "可以同时选择复制多个文件或者目录", tags = {"file"})
    @RequestMapping(value = "/batchcopyfile", method = RequestMethod.POST)
    public Result<String> batchCopyFile(@RequestBody BatchMoveFileDTO batchMoveFileDto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
 
        String files = batchMoveFileDto.getFiles();
        String newfilePath = batchMoveFileDto.getFilePath();
 
        List<DocFilePath> fileList = JSON.parseArray(files, DocFilePath.class);
 
        for (DocFilePath userFile : fileList) {
 
            if (StringUtil.isEmpty(userFile.getExtendName())) {
                QiwenFile qiwenFile = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true);
                if (newfilePath.startsWith(qiwenFile.getPath() + QiwenFile.separator) || newfilePath.equals(qiwenFile.getPath())) {
                    return Result.error("原路径与目标路径冲突,不能复制",null);
                }
            }
            filePathService.userFileCopy(userFile.getPathId(), newfilePath);
            operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_COPY);
        }
 
        return Result.OK("批量复制文件成功");
 
    }
 
 
 
    @ApiOperation(value = "文件移动", notes = "可以移动文件或者目录", tags = {"file"})
    @RequestMapping(value = "/movefile", method = RequestMethod.POST)
    public Result<String> moveFile(@RequestBody MoveFileDTO moveFileDto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
        DocFilePath userFile = filePathService.getById(moveFileDto.getUserFileId());
        String oldfilePath = userFile.getFilePath();
        String newfilePath = moveFileDto.getFilePath();
        String fileName = userFile.getFileName();
        String extendName = userFile.getExtendName();
        if (StringUtil.isEmpty(extendName)) {
            QiwenFile qiwenFile = new QiwenFile(oldfilePath, fileName, true);
            if (newfilePath.startsWith(qiwenFile.getPath() + QiwenFile.separator) || newfilePath.equals(qiwenFile.getPath())) {
                return Result.error("原路径与目标路径冲突,不能移动",null);
            }
        }
 
        try {
            filePathService.updateFilepathByUserFileId(moveFileDto.getUserFileId(), newfilePath);
 
            fileDealComp.deleteRepeatSubDirFile(newfilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_MOVE);
        return Result.OK();
 
    }
 
    @ApiOperation(value = "批量移动文件", notes = "可以同时选择移动多个文件或者目录", tags = {"file"})
    @RequestMapping(value = "/batchmovefile", method = RequestMethod.POST)
    public Result<String> batchMoveFile(@RequestBody BatchMoveFileDTO batchMoveFileDto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //JwtUser sessionUserBean =  SessionUtil.getSession();
 
        String files = batchMoveFileDto.getFiles();
        String newfilePath = batchMoveFileDto.getFilePath();
 
        List<DocFilePath> fileList = JSON.parseArray(files, DocFilePath.class);
 
        for (DocFilePath userFile : fileList) {
 
            if (StringUtil.isEmpty(userFile.getExtendName())) {
                QiwenFile qiwenFile = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true);
                if (newfilePath.startsWith(qiwenFile.getPath() + QiwenFile.separator) || newfilePath.equals(qiwenFile.getPath())) {
                    return Result.error("原路径与目标路径冲突,不能移动",null);
                }
            }
            filePathService.updateFilepathByUserFileId(userFile.getPathId(), newfilePath);
            operationHisService.saveHis(loginUser, userFile, Constant.OPERATION_MOVE);
        }
 
        return Result.OK("批量移动文件成功");
 
    }
 
    @ApiOperation(value = "查询限额", notes = "获取个人下载限额及使用情况", tags = {"file"})
    @GetMapping("/loadFlow")
    public  Result<UserStorageFlow> loadFlow() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
 
 
        LambdaQueryWrapper<UserStorageFlow> qw = new LambdaQueryWrapper<>();
        qw.eq(UserStorageFlow::getUserId, loginUser.getId());
        UserStorageFlow userStorageFlow = userStorageFlowService.getOne(qw);
        if (userStorageFlow != null && userStorageFlow.getFlow() != null) { // 用户单独设置过限额
 
        } else { // 未单独设置限额的取所有角色限额最大值
            Result<List<String>> roles = jeecgSystemClient.queryUserRole(loginUser.getId());
            if (roles.getResult() == null || roles.getResult().size()<=0) {return Result.error("未设置限额", null);}
            qw = new LambdaQueryWrapper<>();
            qw.in(UserStorageFlow::getRoleId, roles.getResult());
            qw.isNotNull(UserStorageFlow::getFlow);
            qw.orderByDesc(UserStorageFlow::getFlow).last("limit 1");
            UserStorageFlow one = userStorageFlowService.getOne(qw);
            if (userStorageFlow == null) {
                userStorageFlow = new UserStorageFlow();
                userStorageFlow.setUserId(loginUser.getId());
                userStorageFlow.setSurplusFlow(0L);
                userStorageFlowService.save(userStorageFlow);
            }
            if (one != null) {
                userStorageFlow.setFlow(one.getFlow());
            }
 
        }
        if (userStorageFlow.getSurplusFlow() == null) {
            userStorageFlow.setSurplusFlow(0L);
        }
        return Result.OK(userStorageFlow);
    }
 
 
    @ApiOperation(value = "文件夹内文件总大小", notes = "查询文件夹内文件总大小", tags = {"file"})
    @GetMapping("/getSubFileSize")
    public Result<BigDecimal> querySubFileSize(DocFilePath docFilePath) {
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
        QiwenFile qiwenFile = new QiwenFile(docFilePath.getFilePath(),docFilePath.getFileName(),docFilePath.isDirectory());
        queryWrapper.likeRight(DocFilePath::getFilePath,qiwenFile.getPath());
        queryWrapper.isNotNull(DocFilePath::getFileId);
        List<DocFilePath> docFilePaths = filePathService.list(queryWrapper);
        BigDecimal total = queryTotalSize(docFilePaths);
        return Result.OK(total);
    }
 
    /**
     * 查询传入文件列表的总大小
     * @param docFilePaths
     * @return
     */
    private BigDecimal queryTotalSize(List<DocFilePath> docFilePaths) {
        // 所有文件id集合
        List<String> fileIds = docFilePaths.stream().map(DocFilePath::getFileId).collect(Collectors.toList());
        // 查询出文件大小之和
        QueryWrapper<DocFile> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.select("IFNULL(sum(file_size),0) as totalSize");
        fileQueryWrapper.lambda().in(DocFile::getFileId, fileIds);
        Map<String, Object> map = docFileService.getMap(fileQueryWrapper);
        BigDecimal total = (BigDecimal) map.get("totalSize");
        return total;
    }
 
 
    @ApiOperation(value = "多选文件总大小", notes = "多选文件大小总和", tags = {"file"})
    @GetMapping("/getBatchFileSize")
    public Result<BigDecimal> queryBatchFileSize(BatchDownloadFileDTO batchDownloadFileDTO) {
        // 查询出所有选择的文件
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
        String[] ids = batchDownloadFileDTO.getUserFileIds().split(",");
        queryWrapper.in(DocFilePath::getPathId,ids);
        List<DocFilePath> batchFiles = filePathService.list(queryWrapper);
 
        // 查询出选择的文件夹内的子文件
        LambdaQueryWrapper<DocFilePath> subQueryWrapper = new LambdaQueryWrapper<>();
        subQueryWrapper.isNotNull(DocFilePath::getFileId);
        subQueryWrapper.and( i -> {
            i.apply("{0} = {1}", 1,2);
            for (DocFilePath d: batchFiles) {
                if (d.isDirectory()) {
                    QiwenFile qiwenFile = new QiwenFile(d.getFilePath(),d.getFileName(),d.isDirectory());
                    i.or().likeRight(DocFilePath::getFilePath,qiwenFile.getPath());
                }
            }
        });
        List<DocFilePath> docFilePaths = filePathService.list(subQueryWrapper);
        docFilePaths.addAll(batchFiles);
        // 所有文件id集合
        BigDecimal total = queryTotalSize(docFilePaths);
        return Result.OK(total);
    }
 
//    @ApiOperation(value = "查询文件详情", notes = "查询文件详情", tags = {"file"})
//    @RequestMapping(value = "/detail", method = RequestMethod.GET)
//    @ResponseBody
//    public RestResult<FileDetailVO> queryFileDetail(
//            @Parameter(description = "用户文件Id", required = true) String userFileId){
//        FileDetailVO vo = fileService.getFileDetail(userFileId);
//        return RestResult.success().data(vo);
//    }
 
    @GetMapping("/heapSize")
    public Result<?> getHeap() {
        double maxHeapSize = Runtime.getRuntime().maxMemory();
        String sizeInReadableForm;
        double kbSize = maxHeapSize / 1024;
        double mbSize = kbSize / 1024;
        double gbSize = mbSize / 1024;
        if (gbSize > 0) {
            sizeInReadableForm = gbSize + " GB";
        } else if (mbSize > 0) {
            sizeInReadableForm = mbSize + " MB";
        } else {
            sizeInReadableForm = kbSize + " KB";
        }
        return Result.OK(maxHeapSize);
    }
 
    @ApiOperation( value = "文件对比", notes = "文件对比")
    @PostMapping("/compareFile")
    public Result<CompareFileVo> compareFile( UploadFileDTO uploadFileDto) {
        CompareFileVo compareFileVo = lbDocService.compareFile( uploadFileDto);
 
        return Result.OK(compareFileVo);
    }
 
}