许鹏程
2025-02-17 419714dc96e2c880f48ba3eed9ec9b3a8fa0cf32
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
package com.product.file.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.product.common.collect.ListUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.core.util.CodeUtil;
import com.product.file.config.CmnConst;
import com.product.file.config.DocumentCode;
import com.product.file.config.FileCode;
import com.product.file.service.ide.IDocumentDirectoryService;
import com.product.module.sys.entity.SystemUser;
import com.product.quartz.service.impl.SysJobService;
import com.product.tool.table.enums.FieldType;
import com.product.util.BaseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
/**
 * Copyright lx
 *
 * @Title: ClaimExpenseController
 * @Project: lx-SERVER
 * @Date: 2020-11-18 11:30:48
 * @Author: luoxin
 * @Description: 文件夹管理server
 */
@Component
public class DocumentDirectoryService extends AbstractBaseService implements IDocumentDirectoryService {
 
    @Autowired
    public BaseDao baseDao;
    @Autowired
    public DocumentDirectoryService documentDirectoryService;
    @Autowired
    SysJobService sysJobService;
 
    /**
     * 单位文件夹树
     *
     * @return
     */
    public DataTableEntity allDirectoryTree() {
        DataTableEntity dtTree = baseDao.listTable(CmnConst.PRODUCT_OA_DIRECTORY, "directory_type = 1");
        if (!BaseUtil.dataTableIsEmpty(dtTree)) {
            return BaseUtil.dataTableToTreeTable(dtTree, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null);
        }
        return dtTree;
    }
 
    /**
     * 文档目录保存
     *
     * @param fse
     * @return
     */
    @Transactional
    public boolean saveDocumentDireactory(FieldSetEntity fse) {
        BaseUtil.createCreatorAndCreationTime(fse);
        String tricode = null;
        String tricodeParent = BaseUtil.strIsNull(fse.getString(CmnConst.TRICODE_PARENT)) ? "" : fse.getString(CmnConst.TRICODE_PARENT);
        if (BaseUtil.strIsNull(fse.getUUID())) {
            tricode = CodeUtil.getNewCodeByTemp(CmnConst.PRODUCT_OA_DIRECTORY, CmnConst.TRICODE, tricodeParent);
        } else {
            FieldSetEntity fseOrigin = baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_DIRECTORY, fse.getUUID(), false);
            if (fseOrigin == null) {
                fse.remove(CoreConst.UUID);
            }
            String originParent = BaseUtil.strIsNull(fseOrigin.getString(CmnConst.TRICODE_PARENT)) ? "" : fseOrigin.getString(CmnConst.TRICODE_PARENT);
            if (!originParent.equals(tricodeParent)) {
                String originTricode = BaseUtil.strIsNull(fseOrigin.getString(CmnConst.TRICODE)) ? "" : fseOrigin.getString(CmnConst.TRICODE);
                tricode = CodeUtil.getNewCodeByTemp(CmnConst.PRODUCT_OA_DIRECTORY, CmnConst.TRICODE, tricodeParent);
                // 刷新code
                StringBuilder sql = new StringBuilder();
                sql.append("update product_oa_directory ");
                sql.append("set tricode=concat(replace(substring(tricode,1,locate(?,tricode)+length(?)),?,?),substring(tricode,locate(?,tricode)+length(?)+1)), ");
                sql.append("tricode_parent=concat(replace(substring(tricode_parent,1,locate(?,tricode_parent)+length(?)),?,?),substring(tricode_parent,locate(?,tricode_parent)+length(?)+1)) ");
                sql.append("where tricode like ? ");
                baseDao.executeUpdate(sql.toString(), new Object[]{originTricode, originTricode, originTricode, tricode, originTricode, originTricode,
                        originTricode, originTricode, originTricode, tricode, originTricode, originTricode, originTricode + "-%"});
                // 刷新层级
                StringBuilder sb = new StringBuilder();
                sb.append("UPDATE product_oa_directory \n");
                sb.append("SET directory_tier = ( \n");
                sb.append("    (LENGTH(tricode) - LENGTH(REPLACE(tricode, '-', '')) + 1) \n");
                sb.append(") \n");
                sb.append("WHERE tricode LIKE ? \n");
                baseDao.executeUpdate(sb.toString(), new Object[]{tricode + "%"});
            } else {
                tricode = fseOrigin.getString(CmnConst.TRICODE);
            }
        }
        fse.setValue(CmnConst.TRICODE, tricode);
        fse.setValue(CmnConst.DIRECTORY_TIER, tricode.split("-").length);
        return baseDao.saveFieldSetEntity(fse);
    }
 
    @Transactional
    @Override
    public boolean saveFileConfig(FieldSetEntity fse) {
        //获取功能表名
        FieldSetEntity functionUuid = baseDao.getFieldSet(CmnConst.PRODUCT_SYS_FUNCTIONS, fse.getString("function_uuid"), false);
 
        DataTableEntity field = baseDao.listTable(CmnConst.PRODUCT_SYS_DATAMODEL_FIELD, "table_uuid=? and field_type='archive-sign'", new String[]{functionUuid.getString("table_uuid")});
        if (field.getRows() == 0) {
            String fieldName = "archive_flag";
            FieldSetEntity fieldInfo = null;
            int i = 0;
            do {
                if (i > 0) {
                    fieldName = "archive_flag_" + i;
                }
                fieldInfo = BaseUtil.getSingleInfoByCache("表字段信息", new String[]{functionUuid.getString("table_uuid"), fieldName});
                i++;
            } while (FieldSetEntity.isEmpty(fieldInfo));
            FieldSetEntity fs = new FieldSetEntity();
            fs.setTableName(CmnConst.PRODUCT_SYS_DATAMODEL_FIELD);
            fs.setValue("table_uuid", functionUuid.getString("table_uuid"));
            fs.setValue("field_name", fieldName);
            fs.setValue("field_type", FieldType.ARCHIVE_SIGN.getDictValue());
            fs.setValue("field_show_name", "归档标识");
            fs.setValue("field_description", "归档标识");
            fs.setValue("field_length", 4);
            fs.setValue("field_unit", 0);
            fs.setValue("is_required", 0);
            fs.setValue("is_unique", 0);
            fs.setValue("is_filter", 0);
            fs.setValue("field_sequence", 0);
            fs.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
            fs.setValue("created_utc_datetime", new Date());
            baseDao.saveFieldSetEntity(fs);
            FieldSetEntity table = baseDao.getFieldSet(CmnConst.PRODUCT_SYS_DATAMODEL_TABLE, functionUuid.getString("table_uuid"), false);
            baseDao.executeUpdate("ALTER TABLE " + table.getString("table_name") + " ADD COLUMN " + fieldName + " varchar(10) COMMENT '归档标识';");
        }
 
 
        FieldSetEntity timedTask = baseDao.getFieldSetByFilter("product_sys_timed_task", "job_name=? and invoke_target like concat('%',?,'%') ", new String[]{fse.getString("filing_name") + "定时", fse.getString("uuid")}, false);
        String timeTaskUuid = null;
        if (!FieldSetEntity.isEmpty(timedTask)) {
            timeTaskUuid = timedTask.getUUID();
        }
        boolean re = baseDao.saveFieldSetEntity(fse);
        createdTimeTask(fse.getString("filing_name"), fse.getString("cron"), fse.getUUID(), timeTaskUuid);
        return re;
    }
 
    private FieldSetEntity createdTimeTask(String target_table, String execute_time, String task_uuid, String timeTaskUuid) throws BaseException {
        //
        FieldSetEntity fse = new FieldSetEntity();
        try {
            fse.setTableName("product_sys_timed_task");
            fse.setValue("uuid", timeTaskUuid);
            fse.setValue("job_name", target_table + "定时");//任务名称
            fse.setValue("job_group", "system");//分组
            fse.setValue("invoke_target", "documentDirectoryService.fileTime('" + task_uuid + "')");//调用目标字符串
            fse.setValue("cron_expression", execute_time);//cron表达式
            fse.setValue("misfire_policy", "3");//错误执行策略  只执行一次
            fse.setValue("concurrent", 0);//不允许并发执行
            fse.setValue("remark", target_table + "定时");
            fse.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
            fse.setValue("created_utc_datetime", new Date());
            fse.setValue("status", 1);
            fse.setValue("is_conceal", 1);
            if (!com.product.common.lang.StringUtils.isEmpty(timeTaskUuid)) {
                sysJobService.updateJob(fse);
            } else {
                sysJobService.insertJob(fse);
            }
            return fse;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(DocumentCode.CRATED_TIMED_TASK_FAIL);
        }
//
    }
 
    /**
     * 归档定时任务
     */
    @Transactional
    public void fileTime(String uuid) {
        FieldSetEntity fs = baseDao.getFieldSet(CmnConst.PRODUCT_AO_FILING_CONFIG, uuid, false);
        FieldSetEntity function = BaseUtil.getSingleInfoByCache("所有功能", new String[]{fs.getString("function_uuid"), fs.getString(CmnConst.FUNCTION_UUID)});
 
        DataTableEntity tableFields = DataPoolCacheImpl.getInstance().getCacheData("所有字段信息并按表分组", new String[]{function.getString("table_uuid")});
        if (DataTableEntity.isEmpty(tableFields)) {
            return;
        }
        String flowSignField = null;
        String archiveFlagField = null;
 
 
        Set<String> fileField = new HashSet<>();
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < tableFields.getRows(); i++) {
            String fieldType = tableFields.getString(i, "field_type");
            if (FieldType.FILE.getDictValue().equals(fieldType)) {
                fileField.add(tableFields.getString(i, "field_name"));
            } else if (FieldType.FLOWSIGN.getDictValue().equals(fieldType)) {
                flowSignField = tableFields.getString(i, "field_name");
            } else if (FieldType.ARCHIVE_SIGN.getDictValue().equals(fieldType)) {
                archiveFlagField = tableFields.getString(i, "field_name");
            }
            map.put(tableFields.getString(i, "field_name"), "");
        }
        if (StringUtils.isEmpty(flowSignField) || StringUtils.isEmpty(archiveFlagField) || fileField.isEmpty()) {
            return;
        }
        FieldSetEntity tableInfo = BaseUtil.getSingleInfoByCache("所有表信息(uuid)", new String[]{function.getString("table_uuid")});
        if (FieldSetEntity.isEmpty(tableInfo)) {
            return;
        }
        StringBuilder sql = new StringBuilder();
        int pageSize = 1000;
        String sub_folder = fs.getString("sub_folder");
        List<String> businessValueField = Lists.newArrayList();
        if (!map.containsKey(fs.getString("field_title"))) {
            throw new BaseException(FileCode.ARCHIVE_FILE_TITLE_FIELD_NO_EXIST);
        }
        businessValueField.add(fs.getString("field_title"));
        if (!StringUtils.isEmpty(sub_folder)) {
            String[] split = sub_folder.split("/");
            for (int i = 0; i < split.length; i++) {
                String name = split[i];
                if (StringUtils.startsWith(name, "{#") && StringUtils.endsWith(name, "#}")) {
                    name = split[i].replace("{#", "").replace("#}", "");
                    if (map.containsKey(name)) {
                        businessValueField.add(name);
                    }
                }
            }
        } else {
            sub_folder = "{#" + fs.getString("field_title") + "#}";
        }
        //查询业务数据
        sql.append("SELECT\n");
//        sql.append("\tT1.").append(fs.getString("field_title")).append(" as title");
        for (int i = 0; i < businessValueField.size(); i++) {
            sql.append("\tT1." + businessValueField.get(i) + " as val_").append(i).append("\n,");
        }
        sql.append("\tT2.uuid,T2.attachment_size,T2.file_name,T3.dict_label file_type\n");
        sql.append("FROM\n");
        sql.append("\t").append(tableInfo.getString(CoreConst.FIELD_TABLE_NAME)).append(" T1\n");
        sql.append("\tJOIN product_sys_flow_detail T4 ON T1.uuid=T4.record_uuid\n");
        sql.append("\tJOIN product_sys_attachments T2 ON T1.attachment = T2.uuid or T1.attachment_meeting=T2.uuid\n");
        sql.append("\tJOIN product_sys_dict T3 ON T2.attachment_type_uuid=T3.uuid\n");
        sql.append("WHERE\n");
        sql.append("\tT1.flow_flag =2\n");
        sql.append("\tAND T1." + archiveFlagField + " =1\n");
        sql.append("\tAND T4.table_name='").append(tableInfo.getString(CoreConst.FIELD_TABLE_NAME)).append("'\n");
        sql.append("\tGROUP BY 1,2,3,4 limit ").append(pageSize);
 
        DataTableEntity files = null;
        String filing_catalog = fs.getString("filing_catalog");
        FieldSetEntity parentDir = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_OA_DIRECTORY, "tricode=?", new String[]{filing_catalog}, false);
        if (FieldSetEntity.isEmpty(parentDir)) {
            throw new BaseException(FileCode.ARCHIVE_FILE_PARENT_DIRECTORY_NO_EXIST);
        }
        do {
 
            files = baseDao.listTable(sql.toString(), new Object[]{});
            if (DataTableEntity.isEmpty(files)) {
                break;
            }
            for (int i = 0; i < files.getRows(); i++) {
                FieldSetEntity file = files.getFieldSetEntity(i);
                for (int i1 = 0; i1 < businessValueField.size(); i1++) {
                    String value = file.getString("val_" + i1);
                    if (StringUtils.isEmpty(value)) {
                        continue;
                    }
                    sub_folder = sub_folder.replace("{#" + businessValueField.get(i1) + "#}", value);
                }
                String[] dirs = sub_folder.split("/");
                //递归查询父级目录
                FieldSetEntity parent = parentDir;
                for (int j = 0; j < dirs.length; j++) {
                    String dir = dirs[j];
                    FieldSetEntity temp = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_OA_DIRECTORY, "tricode_parent=? and directory_name=", new String[]{parent.getString("tricode"), dir}, false);
                    if (FieldSetEntity.isEmpty(temp)) {
                        //创建文件夹
                        parent = addDirectory(dir, BaseUtil.createCode(CmnConst.PRODUCT_OA_DIRECTORY,
                                CmnConst.TRICODE,
                                parent.getString("tricode")), parent.getString(CmnConst.TRICODE), parent.getInteger("directory_tier") + 1, fs.getUUID(), tableInfo.getString(CoreConst.FIELD_TABLE_NAME));
                    }
                }
                FieldSetEntity document = new FieldSetEntity();
                document.setTableName(CmnConst.PRODUCT_OA_DOCUMENT);
                document.setValue("directory_uuid", parent.getUUID());
                document.setValue("file_name", file.getString("file_name"));
                document.setValue("attachments_uuid", file.getUUID());
                document.setValue("file_type", file.getString("file_type"));
                document.setValue("created_by", 1);
                document.setValue("created_utc_datetime", new Date());
                document.setValue("file_size", file.getString("attachment_size"));
                baseDao.saveFieldSetEntity(document);
            }
        } while (!DataTableEntity.isEmpty(files));
 
 
    }
 
    /**
     *
     * @param sql
     * @param field_title 标题字段
     * @param filing_catalog 父级归档目录
     */
    private void fieldLimit(String sql, String nodeSal, String field_title, String sub_folder, String filing_catalog, String turn_button, String table) {
 
        //获取父类文档信息
        FieldSetEntity parentDirectory = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_OA_DIRECTORY, "tricode=?", new String[]{filing_catalog}, false);
        DataTableEntity fieldDt = baseDao.listTable(sql, new String[]{table});
        //获取流程附件并加入到file里
        DataTableEntity nodeDt = baseDao.listTable(nodeSal, new String[]{});
        for (int i = 0; i < nodeDt.getRows(); i++) {
            FieldSetEntity fieldfs = fieldDt.getFieldSetEntity(nodeDt.getString(i, "uuid")).get(0);
            if (!FieldSetEntity.isEmpty(fieldfs)) {
                if (StringUtils.isEmpty(fieldfs.getString("file"))) {
                    fieldfs.setValue("file", nodeDt.getString(i, "flow_attachment"));
                } else {
                    fieldfs.setValue("file", fieldfs.getString("file") + "," + nodeDt.getString(i, "flow_attachment"));
                }
            }
        }
 
        ArrayList<String> fileList = new ArrayList<>();
        for (int i = 0; i < fieldDt.getRows(); i++) {
            String file = fieldDt.getString(i, "file");
            if (!StringUtils.isEmpty(file)) {
                for (int j = 0; j < file.split(",").length; j++) {
                    fileList.add(file.split(",")[j]);
                }
            }
        }
        DataTableEntity attachments = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS, BaseUtil.buildQuestionMarkFilter("uuid", fileList.size(), true), fileList.toArray(new String[0]));
        if (fieldDt.getRows() > 0) {
            for (int i = 0; i < fieldDt.getRows(); i++) {
                FieldSetEntity fs = fieldDt.getFieldSetEntity(i);
                String directoryUUID = null;
                if (StringUtils.isEmpty(fs.getString("directory_uuid"))) {
                    //根据上级目录获取目录编号
                    String tricode = CodeUtil.getNewCodeByTemp(CmnConst.PRODUCT_OA_DIRECTORY, CmnConst.TRICODE, filing_catalog);
                    //获取目录名称
                    String title = fs.getString(field_title);
                    directoryUUID = addDirectory(title, tricode, filing_catalog, parentDirectory.getInteger("directory_tier") + 1, fs.getUUID(), table).getUUID();
 
                } else {
                    directoryUUID = fs.getString("directory_uuid");
                }
                String file = fs.getString("file");
                if (!StringUtils.isEmpty(file)) {
                    for (int j = 0; j < file.split(",").length; j++) {
                        //创建文件
                        String f = file.split(",")[j];
                        if (StringUtils.isEmpty(file.split(",")[j]) || DataTableEntity.isEmpty(attachments)) {
                            continue;
                        }
                        FieldSetEntity attachmentsFs = attachments.getFieldSetEntity(f).get(0);
                        FieldSetEntity document = new FieldSetEntity();
                        document.setTableName(CmnConst.PRODUCT_OA_DOCUMENT);
                        document.setValue("directory_uuid", directoryUUID);
                        document.setValue("file_name", attachmentsFs.getString("file_name"));
                        document.setValue("attachments_uuid", f);
                        String[] type = f.split("\\.");
                        document.setValue("file_type", type[type.length - 1]);
                        document.setValue("created_by", 1);
                        document.setValue("created_utc_datetime", new Date());
                        document.setValue("file_size", attachmentsFs.getString("attachment_size"));
                        baseDao.saveFieldSetEntity(document);
                    }
                }
                FieldSetEntity document = new FieldSetEntity();
                document.setTableName(CmnConst.PRODUCT_OA_DOCUMENT);
                document.setValue("directory_uuid", directoryUUID);
                document.setValue("file_name", fs.getString(field_title) + " " + fs.getString("user_name") + " " + fs.getString("send_time"));
                document.setValue("created_by", 1);
                document.setValue("created_utc_datetime", new Date());
                document.setValue("turn_button", turn_button);
                document.setValue("file_size", 0);
                document.setValue("file_type", "虚拟文档");
 
                baseDao.saveFieldSetEntity(document);
                baseDao.executeUpdate("UPDATE " + table + " set archive_flag =1 where uuid=?", new String[]{fs.getUUID()});
            }
            fieldLimit(sql, nodeSal, field_title, sub_folder, filing_catalog, turn_button, table);
        }
    }
 
    /**
     *
     * @param field_title 目录标题
     * @param tricode  编号
     * @param tricode_parent 父级编号
     * @param directory_tier 目录层级
     */
 
    private FieldSetEntity addDirectory(String field_title, String tricode, String tricode_parent, Integer directory_tier, String filing_config_field_uuid, String table) {
        //新建目录
        FieldSetEntity fsDirectory = new FieldSetEntity();
        fsDirectory.setTableName(CmnConst.PRODUCT_OA_DIRECTORY);
        fsDirectory.setValue("created_by", 1);
        fsDirectory.setValue("created_utc_datetime", new Date());
        fsDirectory.setValue("org_level_uuid", "00000000-0000-0000-0000-000000000000");
        fsDirectory.setValue("directory_name", field_title);
        fsDirectory.setValue("directory_tier", directory_tier);
        fsDirectory.setValue("directory_type", 1);
        fsDirectory.setValue("tricode", tricode);
        fsDirectory.setValue("tricode_parent", tricode_parent);
        fsDirectory.setValue("filing_config_field_uuid", filing_config_field_uuid);
        fsDirectory.setValue("filing_config_table", table);
 
        baseDao.saveFieldSetEntity(fsDirectory);
        return fsDirectory;
    }
 
 
    @Override
    @Transactional
    public boolean delDocumentDirectory(FieldSetEntity fieldSetEntity) throws BaseException {
        String uuid = fieldSetEntity.getString(CmnConst.UUID);
        String directory_type = fieldSetEntity.getString(CmnConst.DIRECTORY_TYPE);
        String filter = "";
        if ("2".equals(directory_type)) {
            filter = " AND " + "created_by = " + SpringMVCContextHolder.getCurrentUserId();
        }
        DataTableEntity dataTableEntity = baseDao.listTable(
                "SELECT * FROM PRODUCT_OA_directory WHERE directory_type = ? AND tricode_parent = (SELECT tricode FROM PRODUCT_OA_directory where uuid = ?)"
                        + filter,
                new String[]{directory_type, uuid});
        if (BaseUtil.dataTableIsEmpty(dataTableEntity)) {
            baseDao.delete(CmnConst.PRODUCT_OA_DOCUMENT, "directory_uuid = ?", new String[]{uuid});
            return baseDao.delete(CmnConst.PRODUCT_OA_DIRECTORY, new String[]{uuid});
        } else {
            throw new BaseException(DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getValue(),
                    DocumentCode.DOCUMENT_CATALOG_DELETE_FAIL.getText(), this.getClass(),
                    "public boolean delDocumentDirectory(FieldSetEntity fieldSetEntity) throws BaseException");
        }
    }
 
    /**
     * 获取当前文件路径
     *
     * @param fieldSetEntity
     * @return
     * @throws BaseException
     */
    @Override
    public DataTableEntity getDirectoryStructure(FieldSetEntity fieldSetEntity) throws BaseException {
        String uuid = fieldSetEntity.getString(CmnConst.DIRECTORY_UUID);
        fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_DIRECTORY, uuid, false);
        String triCode = fieldSetEntity.getString(CmnConst.TRICODE);
        String type = fieldSetEntity.getString(CmnConst.DIRECTORY_TYPE);
        List<String> codeList = ListUtils.newArrayList();
        // 拆分为上级目录
        if (triCode.indexOf('-') != -1) {
            do {
                codeList.add(0, triCode);
                triCode = triCode.substring(0, triCode.lastIndexOf('-'));
            } while (triCode.indexOf('-') != -1);
            codeList.add(0, triCode);
        } else {
            codeList.add(triCode);
        }
        SystemUser user = SpringMVCContextHolder.getCurrentUser();
        DataTableEntity dataTableEntity;
        // 单位文件夹
        if ("1".equals(type)) {
            codeList.add(0, user.getOrg_level_uuid());
            dataTableEntity = baseDao.listTable(CmnConst.PRODUCT_OA_DIRECTORY,
                    "directory_type = 1 AND org_level_uuid = ? AND "
                            + BaseUtil.buildQuestionMarkFilter(CmnConst.TRICODE, codeList.size() - 1, true),
                    codeList.toArray(), "tricode");
            // 个人文件夹
        } else {
            codeList.add(0, String.valueOf(user.getUser_id()));
            dataTableEntity = baseDao.listTable(CmnConst.PRODUCT_OA_DIRECTORY,
                    "directory_type = 2 AND created_by = ? AND "
                            + BaseUtil.buildQuestionMarkFilter(CmnConst.TRICODE, codeList.size() - 1, true),
                    codeList.toArray(), "tricode");
        }
        baseDao.listInternationDataTable(dataTableEntity, null);
        return dataTableEntity;
    }
 
    @Override
    public JSONArray treeListAll(FieldSetEntity fieldSetEntity) throws BaseException {
        // 关联公司uuid
        String directory_type = fieldSetEntity.getString(CmnConst.DIRECTORY_TYPE);
        // 公司文件夹
        DataTableEntity data;
        SystemUser user = SpringMVCContextHolder.getCurrentUser();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT a.*,ifnull(b.number,0) number FROM PRODUCT_OA_directory a LEFT JOIN ");
        sql.append(
                "(SELECT COUNT(*) number,directory_uuid FROM PRODUCT_OA_document GROUP BY directory_uuid)  b on a.uuid=b.directory_uuid ");
 
        if ("1".equals(directory_type)) {
            String org_level_uuid = user.getOrg_level_uuid();
            sql.append("where directory_type = 1 AND org_level_uuid = ? ");
            data = baseDao.listTable(sql.toString(), new Object[]{org_level_uuid});
//      data = baseDao.listTable(CmnConst.PRODUCT_OA_DIRECTORY, " directory_type = 1 AND org_level_uuid = ?", new String[]{org_level_uuid}, "tricode");
            // 个人文件夹
        } else {
            Integer userId = user.getUser_id();
            sql.append("where directory_type = 2 AND created_by = ? ");
            data = baseDao.listTable(sql.toString(), new Object[]{userId});
 
//      data = baseDao.listTable(CmnConst.PRODUCT_OA_DIRECTORY, " directory_type = 2 AND created_by = ?", new Object[]{userId}, "tricode");
        }
 
 
        baseDao.listInternationDataTable(data, null);
        JSONArray menus = this.encapsulationTree(data);
        // 获取当前公司
        JSONObject orgLevelInfo = new JSONObject();
        String orgLevelName = user.getOrg_level_name();
 
        orgLevelInfo.put("directory_name", orgLevelName);
        orgLevelInfo.put("tricode", "");
        orgLevelInfo.put("org_level_uuid", user.getOrg_level_uuid());
        orgLevelInfo.put("children", menus);
        JSONArray array = new JSONArray();
        if (!"1".equals(directory_type)) {
//        查询数量
            sql.setLength(0);
            sql.append(" SELECT\n");
            sql.append("        ( SELECT count(*) FROM product_oa_document_collection WHERE document_collector = ? ) collection_count,\n");
            sql.append("        ( SELECT count(distinct sub_uuid ) FROM product_oa_document_borrow_sub WHERE borrow_uuid IN ( SELECT uuid FROM product_oa_document_borrow WHERE borrower = ? and flow_flag=2) ) borrow_count\n");
            sql.append("        FROM\n");
            sql.append("DUAL\n");
            FieldSetEntity fieldSetBySQL = baseDao.getFieldSetBySQL(sql.toString(), new Object[]{user.getUser_id(), user.getUser_id()}, false);
            int collectionCount = 0;
            int borrowCount = 0;
            if (fieldSetBySQL != null) {
                collectionCount = BaseUtil.ifNull(fieldSetBySQL.getInteger("collection_count"), 0);
                borrowCount = BaseUtil.ifNull(fieldSetBySQL.getInteger("borrow_count"), 0);
            }
            //添加收藏夹和借阅文件夹
            JSONObject favorite = new JSONObject();
            favorite.put("directory_name", "收藏夹");
            favorite.put("uuid", "!favorite");
            favorite.put("number", collectionCount);
            menus.add(0, favorite);
            JSONObject borrow = new JSONObject();
            borrow.put("directory_name", "借阅文件");
            borrow.put("uuid", "!borrow");
            borrow.put("number", borrowCount);
            menus.add(1, borrow);
        }
        array.add(orgLevelInfo);
        return array;
    }
 
    /**
     * 封装树方法
     *
     * @param
     * @return
     * @throws BaseException
     */
    @Override
    public JSONArray encapsulationTree(DataTableEntity data) throws BaseException {
        JSONArray menus = new JSONArray();
        JSONObject parent_menu = new JSONObject();
        if (!BaseUtil.dataTableIsEmpty(data)) {
            for (int i = 0; i < data.getRows(); i++) {
                FieldSetEntity fs = data.getFieldSetEntity(i);
                JSONObject menu = BaseUtil.fieldSetEntityToJson(fs);
                String tricode = fs.getString(CmnConst.TRICODE_PARENT);
                if (StringUtils.isEmpty(tricode) || parent_menu.getString(tricode) == null) {
                    menus.add(menu);
                } else {// 上级目录
                    JSONObject pm = parent_menu.getJSONObject(tricode);
                    JSONArray subs = pm.getJSONArray(CmnConst.CHILDREN);
                    JSONArray submenus = null;
                    if (subs == null) {
                        submenus = new JSONArray();
                        pm.put(CmnConst.CHILDREN, submenus);
                    } else {
                        submenus = subs;
                    }
                    submenus.add(menu);
                }
                parent_menu.put(menu.getString(CmnConst.TRICODE), menu);
            }
        }
        return menus;
    }
 
    @Override
    public FieldSetEntity documentDirectoryInfo(FieldSetEntity fieldSetEntity) throws BaseException {
        String uuid = fieldSetEntity.getString(CmnConst.UUID);
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_DIRECTORY, uuid, true);
    }
 
    /**
     * 移动到目录列表(根据当前用户过滤展示目录列表) 获取用户有新增(上传)权限的列表
     *
     * @param
     * @return
     * @throws BaseException
     */
    @Override
    public DataTableEntity moveToList(FieldSetEntity fse) throws BaseException {
        String type = fse.getString(CmnConst.DIRECTORY_TYPE);
        StringBuffer sql = new StringBuffer();
        Integer userId = SpringMVCContextHolder.getCurrentUser().getUser_id();
        StringBuffer param = new StringBuffer();
        // 1为单位文件夹
        if ("1".equals(type)) {
            param.append("%").append(userId).append("%");
            sql.append("SELECT a.* FROM " + CmnConst.PRODUCT_OA_DIRECTORY + " a LEFT JOIN ")
                    .append(" PRODUCT_OA_directory_rights b ON a.uuid = b.directory_uuid ")
                    .append(" LEFT JOIN product_sys_organizational_structure_storage c on b.storage_uuid = c.uuid ")
                    .append(" WHERE a.directory_type = 1 AND b.rigths like '%3%' AND c.staff_ids like ? GROUP BY a.tricode ");
        } else {
            param.append(userId);
            sql.append("SELECT * FROM PRODUCT_OA_directory ").append(" WHERE directory_type = 2 AND created_by = ? ");
        }
        DataTableEntity dataTableEntity = baseDao.listTable(sql.toString(), new String[]{param.toString()});
        baseDao.listInternationDataTable(dataTableEntity, null);
        return dataTableEntity;
    }
 
    @Override
    @Transactional
    public boolean upDocumentDirectoryName(FieldSetEntity fse) throws BaseException {
        String uuid = fse.getString(CmnConst.UUID);
        String directoryName = fse.getString(CmnConst.DIRECTORY_NAME);
        FieldSetEntity docField = baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_DIRECTORY, uuid, false);
        docField.setValue(CmnConst.DIRECTORY_NAME, directoryName);
        return baseDao.update(docField);
    }
}