yuxc
2025-01-09 f49622515192a11878628b8eecbc7315671ae562
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
package com.vci.web.service.impl;
 
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.vci.client.common.oq.OQTool;
import com.vci.common.qt.object.*;
import com.vci.constant.FrameWorkLangCodeConstant;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.atm.AttributeDef;
import com.vci.corba.omd.btm.BizType;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.omd.data.LinkObject;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.corba.omd.qtm.QTD;
import com.vci.corba.omd.qtm.QTInfo;
import com.vci.dto.*;
import com.vci.omd.constants.SystemAttribute;
import com.vci.omd.objects.OtherInfo;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.pagemodel.DataGrid;
import com.vci.starter.web.util.ControllerUtil;
import com.vci.starter.web.util.Lcm.DateUtil;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.web.other.BtmQTExportData;
import com.vci.web.other.LinkQTExportData;
import com.vci.web.service.OsQuereyTemplateServiceI;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import com.vci.web.utility.AttributeDataFetcher;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 查询模板服务
 * @author yuxc
 * @date 2024-8-1
 */
@Service
public class OsQueryTemplateImpl implements OsQuereyTemplateServiceI {
 
 
    /**
     * 平台的调用工具类
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    private final String EQUAL = "=";
    private final String UNEQUAL = "!=";
    private final String CONTAINS = "包含";
    private final String IN = "in";
    private final String NOTIN = "not in";
    private final String GT = ">";
    private final String GTE = ">=";
    private final String LT = "<";
    private final String LTE = "<=";
    private final String AND = "并且";
    private final String OR = "或者";
 
    /**
     * 查询模板的列表
     * @param btmName 类型
     * @param linkFlag 是否链接类型 :true 链接类型 ,false 业务类型
     * @return 查询模板的列表
     */
    @Override
    public BaseResult queryTemplateList(String btmName, Boolean linkFlag) throws PLException {
        QTD[] qtdArray = null;
        if(linkFlag){
            qtdArray = platformClientUtil.getQTDService().getLinkTypeQTDs(btmName);
        }else {
            qtdArray = platformClientUtil.getQTDService().getBizTypeQTDs(btmName);
        }
        List<QTD> qtdList = Arrays.asList(qtdArray).stream().sorted(Comparator.comparing(s -> s.name,String.CASE_INSENSITIVE_ORDER)).collect(Collectors.toList());
        return BaseResult.dataList(qtdList);
    }
 
    /**
     * 保存查询模板
     * @param qtd 查询模板实体类
     * @return 保存结果
     */
    @Override
    public BaseResult saveTemplate(QTD qtd) throws PLException {
        //校验参数
        checkName(qtd);
        //设置默认值
        qtd.creator = WebUtil.getCurrentUserId();
        qtd.createTime = System.currentTimeMillis();
        boolean addFlag = false;
        //进行保存
        addFlag = platformClientUtil.getQTDService().addQTD(qtd);
        if (addFlag) {
            return BaseResult.success("增加业务类型查询模板成功");
        } else {
            throw new PLException("500", new String[]{"增加业务类型查询模板失败"});
        }
    }
 
    /**
     * 修改查询模板
     * @param qtd 查询模板实体类
     * @return 保存结果
     */
    @Override
    public BaseResult updateTemplate(QTD qtd) throws PLException {
        //校验参数
        if (qtd.name.equals("")) {
            throw new PLException("500", new String[]{"请输入模板定义名"});
        }
        if (!qtd.name.matches("[a-z A-Z]*")) {
            throw new PLException("500", new String[]{"模板定义名只能为英文字母"});
        }
        qtd.createTime = System.currentTimeMillis();
        boolean updateFlag = false;
        //进行保存
        updateFlag = platformClientUtil.getQTDService().modifyQTD(qtd);
        if (updateFlag) {
            return BaseResult.success("修改业务类型查询模板成功");
        } else {
            throw new PLException("500", new String[]{"修改业务类型查询模板失败"});
        }
    }
    /**
     * 删除查询模板
     * @param name 查询模板名
     * @return 保存结果
     */
    @Override
    public BaseResult deleteTemplate(String name) throws PLException {
        boolean flag = false;
        flag = platformClientUtil.getQTDService().deleteQTD(name);
        if (flag) {
            return BaseResult.success("删除查询模板成功");
        } else {
            return BaseResult.fail("删除查询模板失败");
        }
    }
 
    /**
     * @param qtInfoDTO 传输的数据对象:
     *                linkTypeName 链接类型、
     *                rdPositive 方向,true正向,false反向
     *                btmName 业务类型名称
     *                combRelaType 业务类型选择值
     *                versionValue 版本班次值
     *                isQueryIsLeaf 是否选择下级
     *                level 子节点层数
     * @return 查询结果
     */
    @Override
    public BaseResult getCriteria(QTInfoDTO qtInfoDTO) throws PLException {
        QueryTemplate qt = getQT(qtInfoDTO,true);
        String checkInfo = OQTool.checkQT(qt);
        if(!checkInfo.equals("OK")){
            throw new PLException("500", new String[]{checkInfo});
        }
        qt.setId("qt1");
        LinkObject[] result = platformClientUtil.getQueryService().findLTObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML());
        return BaseResult.dataList(Arrays.asList(result));
    }
 
    /**
     * 查询模板列表
     * @param btName 类型名称
     * @return 查询结果
     */
    @Override
    public BaseResult getObjTypeQTs(String btName) throws PLException, DocumentException {
        VciBaseUtil.alertNotNull(btName,"类型名");
        QTInfo[] objTypeQTs = platformClientUtil.getQTDService().getObjTypeQTs(btName);
        if(Func.isEmpty(objTypeQTs)){
            return BaseResult.success();
        }
        List<QTInfoDTO> dtos = new ArrayList<>();
        Map<String, AttributeDef> allSysAttr = Arrays.stream(platformClientUtil.getBtmService().getSysAttributeDefs()).collect(Collectors.toMap(e -> e.name, e -> e));
        allSysAttr.putAll(Arrays.stream(platformClientUtil.getLinkTypeService().getSysAttributeDefs()).collect(Collectors.toMap(e -> e.name, e -> e)));
 
        for (QTInfo obj : objTypeQTs) {
            QTInfoDTO qtInfoDTO = new QTInfoDTO();
            qtInfoDTO.setCreator(obj.creator);
            qtInfoDTO.setBtmName(obj.btmName);
            qtInfoDTO.setQtName(obj.qtName);
            qtInfoDTO.setLevelFlag(obj.levelFlag);
            //qtInfoDTO.setQtText(obj.qtText);
            qtInfoDTO.setQtUIText(obj.qtUIText);
            if(StringUtils.isNotBlank(obj.qtUIText)){
                //将给定的String文本解析为XML文档并返回新创建的document
                qtInfoDTO.setTree(analysisXml(obj, allSysAttr));
            }
            QueryTemplate qtByDoc = OQTool.getQTByDoc(DocumentHelper.parseText(obj.qtText), obj.qtName);
            qtInfoDTO.setQueryTemplate(queryTemplateToDto(qtByDoc, allSysAttr));
            qtInfoDTO.setCreateTimeText(DateFormatUtils.format(new Date(obj.createTime), DateUtil.PATTERN_DATETIME));
            dtos.add(qtInfoDTO);
        }
        // TODO: 2024/12/2 Ludc 返回值排序
        dtos = dtos.stream().sorted(Comparator.comparing(QTInfoDTO::getQtName,String.CASE_INSENSITIVE_ORDER)).collect(Collectors.toList());
        return BaseResult.dataList(dtos);
    }
 
    /**
     * 对象转换
     * @param qtByDoc 模板对象
     * @param allSysAttr 所有系统属性
     * @return 模板传输对象
     * @throws PLException
     */
    private QueryTemplateDTO queryTemplateToDto(QueryTemplate qtByDoc, Map<String, AttributeDef> allSysAttr) throws PLException {
        QueryTemplateDTO dto = new QueryTemplateDTO();
        dto.setId(qtByDoc.getId());
        dto.setBtmType(qtByDoc.getBtmType());
        dto.setDirection(qtByDoc.getDirection());
        dto.setLevel(qtByDoc.getLevel());
        dto.setQueryChildrenFlag(qtByDoc.isQueryChildren());
        dto.setQueryISLeaf(qtByDoc.isQueryISLeaf());
        dto.setClauseList(qtByDoc.getClauseList());
        dto.setPageInfo(qtByDoc.getPageInfo());
        dto.setLinkType(qtByDoc.getLinkType());
        dto.setVersion(qtByDoc.getVersion());
        dto.setType(qtByDoc.getType());
        dto.setSecretFlag(qtByDoc.isSecretFlag());
        dto.setRightFlag(qtByDoc.isRightFlag());
        dto.setOrderInfoList(qtByDoc.getOrderInfoList());
        dto.setRecReturnMode(qtByDoc.getRecReturnMode());
        Condition con = qtByDoc.getCondition();
        if(con != null){
            ConditionDTO conDto = new ConditionDTO();
            conDto.setRootCIName(con.getRootCIName());
            Map<String, ConditionItem> ciMap = con.getCIMap();
            Map<String, ConditionItemDTO> ciMapDto = new HashMap<>();
            for (String key : ciMap.keySet()) {
                ConditionItem item = ciMap.get(key);
                LeafInfo leafInfo = item.getLeafInfo();
                ConditionItemDTO itemDTO = new ConditionItemDTO();
                itemDTO.setChildrenInfo(item.getChildrenInfo());
                itemDTO.setId(item.getId());
                itemDTO.setLeafFlag(item.isLeaf());
                ciMapDto.put(key, itemDTO);
                if(leafInfo == null){
                    continue;
                }
                LeafInfoDTO leafInfoDTO = new LeafInfoDTO();
                itemDTO.setLeafInfo(leafInfoDTO);
                //处理查询字段类型
                String column = leafInfo.getClause();
                if(column.contains(".")){
                    column = StringUtils.substringAfterLast(column, ".");
                }
                if(column.contains(" ")){
                    column = StringUtils.substringBefore(column, " ");
                }
                AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(column);
                if(att == null || "".equals(att.oid)){
                    att = allSysAttr.get(column.toLowerCase());
                }
                if(att == null){
                    throw new PLException("500", new String[]{"属性字段:" + column + "在属性池中未查询到,请确认!!"});
                }
                leafInfoDTO.setClause(leafInfo.getClause());
 
                leafInfoDTO.setOperator(leafInfo.getOperator());
                leafInfoDTO.setType(att.vtDataType);
                if(leafInfo.getValue() != null ){
                    LeafValueDto valueDto = new LeafValueDto();
                    valueDto.setOrdinaryValue(leafInfo.getValue().getOrdinaryValue());
                    valueDto.setQueryTemplate(leafInfo.getValue().getQueryTemplate() != null ? queryTemplateToDto(leafInfo.getValue().getQueryTemplate(),allSysAttr) : null);
                    leafInfoDTO.setValue(valueDto);
                }
 
            }
            conDto.setCIMap(ciMapDto);
            dto.setCondition(conDto);
        }
        return dto;
    }
 
    /**
     * 将客服端界面显示的xml转为hashMap
     * @param obj 界面对象
     * @return
     * @throws DocumentException
     */
    private HashMap<String,Object> analysisXml(QTInfo obj, Map<String, AttributeDef> allSysAttr) throws DocumentException, PLException {
        org.dom4j.Document document = DocumentHelper.parseText(obj.qtUIText);
        if(document == null){
            return null;
        }
        //获取根节点,在例子中就是responsedata节点
        Element root = document.getRootElement();
        HashMap<String,Object> treeMap = new HashMap<>();
        treeMap.put("connector",root.getText());
        List<Element> children = root.elements();
        List<Object> childList = new ArrayList<>();
        for(Iterator<Element> i = children.iterator(); i.hasNext();){
            Element child = i.next();
            if(AND.equals(child.getText()) || OR.equals(child.getText())){
                childList.add(addDefaultMutableTree(child, allSysAttr));
            }else {
                String column = StringUtils.substringBefore(child.getText().trim(), " ");
 
                if(column.contains(".")){
                    column = StringUtils.substringAfterLast(column, ".");
                }
                AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(column);
                if(att == null || "".equals(att.oid)){
                    att = allSysAttr.get(column.toLowerCase());
                }
                HashMap<String, String> data = new HashMap<>();
                data.put("column",child.getText().trim());
                data.put("type",att.vtDataType);
                childList.add(data);
            }
        }
        treeMap.put("child",childList);
        return treeMap;
    }
 
    /**
     * 将子节点转为map结构
     * @param element
     */
    public Map<String, Object> addDefaultMutableTree(Element element, Map<String, AttributeDef> allSysAttr) throws PLException {
        List<Object> childList = new ArrayList<>();
        List<Element> children = element.elements();
        HashMap<String,Object> treeMap = new HashMap<>();
        treeMap.put("connector",element.getText());
        for(Iterator<Element> i = children.iterator(); i.hasNext();){
            Element child = i.next();
            if(AND.equals(child.getText()) || OR.equals(child.getText())){
                childList.add(addDefaultMutableTree(child, allSysAttr));
            }else {
                String column = StringUtils.substringBefore(child.getText().trim(), " ");
 
                if(column.contains(".")){
                    column = StringUtils.substringAfterLast(column, ".");
                }
                AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(column);
                if(att == null || "".equals(att.oid)){
                    att = allSysAttr.get(column.toLowerCase());
                }
                HashMap<String, String> data = new HashMap<>();
                data.put("column",child.getText().trim());
                data.put("type",att.vtDataType);
                childList.add(data);
            }
        }
        treeMap.put("child",childList);
        return treeMap;
    }
 
    /**
     * 检查查询模板名字是否存在
     * @param name 查询模板名字
     * @return 查询结果
     */
    @Override
    public BaseResult isExistsQT(String name) throws PLException {
        VciBaseUtil.alertNotNull(name,"询模板名");
        return BaseResult.success(platformClientUtil.getQTDService().isExistsQT(name));
    }
 
    /**
     * 链接类型查询模板保存
     * @param qtInfoDTO 保存传输对象
     * @return 保存结果
     */
    @Override
    public BaseResult linkSave(QTInfoDTO qtInfoDTO) throws PLException {
        QueryTemplate qt = getQT(qtInfoDTO,true);
        String checkInfo = OQTool.checkQT(qt);
        if(!checkInfo.equals("OK")){
            throw new PLException("500", new String[]{checkInfo});
        }
        boolean saveFlag ;
        qt.setId(qtInfoDTO.getQtName());
        qt.setOrderInfoList(qtInfoDTO.getQueryTemplate().getOrderInfoList());
        QTInfo qtWrapper = new QTInfo();
        qtWrapper.qtName = qt.getId();
        qtWrapper.btmName = qt.getLinkType();
        qtWrapper.creator = WebUtil.getCurrentUserId();
        qtWrapper.createTime = System.currentTimeMillis();
        if(qtInfoDTO.getTree() == null){
            qtWrapper.qtUIText = "";
        }else{
            qtWrapper.qtUIText = getSeniorXML(qtInfoDTO);
        }
        qtWrapper.levelFlag = qtInfoDTO.getLevelFlag();
        qtWrapper.qtText = OQTool.qtTOXMl(qt).asXML();
        saveFlag = platformClientUtil.getQTDService().saveQT(qtWrapper);
        if(saveFlag){
            return BaseResult.success();
        }else{
            return BaseResult.fail("保存查询模板失败");
        }
    }
 
    /**
     * 链接类型查询模板树查询,用于界面的导出功能
     * @return 查询结果
     */
    @Override
    public BaseResult getLinkTree() throws PLException {
        LinkType[] linkTypes= null;
        QTInfo[] qts = null;
        HashMap<String,Object> tree = new HashMap();
        tree.put("text","业务类型");
        if (linkTypes == null) {
            List<LinkType> es = new ArrayList<LinkType>();
            linkTypes = platformClientUtil.getLinkTypeService().getLinkTypes();
            for (LinkType lt : linkTypes) {
                QTInfo[] qtWrappers = platformClientUtil.getQTDService().getObjTypeQTs(lt.name);
                if (qtWrappers.length!=0) {
                    es.add(lt);
                }
            }
            linkTypes = es.toArray(new LinkType[es.size()]);
            //TODO:需修正没有关联查询模板业务类型去掉
            qts = platformClientUtil.getQTDService().getAllQTs();//获取所有查询模板
        }
        List<HashMap<String,Object>> childList = new ArrayList<>();
        //添加业务类型根节点
        for (LinkType plAction : linkTypes) {
            HashMap<String,Object> childTree = new HashMap();
            childTree.put("text", plAction.label + "/" +plAction.name);
            childTree.put("oid", plAction.oid);
//            tree.put("children", childTree);
            addExportTreeNode(plAction, qts, childTree);
            childList.add(childTree);
        }
        tree.put("children", childList);
        return BaseResult.success(tree);
    }
 
    /**
     * 生成导出树选择以及导入树显示
     * @param linkType
     * @param qts
     * @param tree
     */
    private void addExportTreeNode(LinkType linkType/*业务类型*/,QTInfo[] qts/*查询模板对象*/,HashMap<String,Object> tree) {
        List<String> childList = new ArrayList<>();
        // 添加查询模板对象子节点
        for (QTInfo qtItem : qts) {
            //处理导入时无法进行类型判断
            int splitLength = linkType.name.indexOf("【");
            if(splitLength == -1){
                splitLength = linkType.name.length();
            }
            if (qtItem.btmName.equals(linkType.name.substring(0,
                    splitLength))) {
                childList.add(qtItem.qtName);
            }
        }
        tree.put("children",childList);
    }
 
    /**
     * 将高级条件数据转为xml
     * @param qtInfoDTO 查询模板列表传输对象
     * @return xml数据
     */
    private String getSeniorXML(QTInfoDTO qtInfoDTO){
        HashMap<String, Object> tree = qtInfoDTO.getTree();
        if(tree.isEmpty()){
            return "";
        }
        StringBuilder xmlStr = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>");
        xmlStr.append(tree.get("connector"));
        List<Object> childrens = (List<Object>) tree.get("child");
        Iterator<Object> children = childrens.iterator();
        while (children.hasNext()) {
            Object obj = children.next();
            if(obj instanceof JSONObject && (((JSONObject) obj).get("connector").equals(AND)
                    || ((JSONObject) obj).get("connector").equals(OR))){
                xmlStr.append(getSeniorChildXML((JSONObject) obj));
            }else{
                xmlStr.append("<child>").append(obj).append("</child>");
            }
        }
        return xmlStr.append("</root>").toString();
 
    }
 
    /**
     * 将高级条件数据转为xml
     * @param childs 高级条件数据
     * @return xml数据
     */
    private String getSeniorChildXML(JSONObject childs){
 
        StringBuilder xmlStr = new StringBuilder("<child>" + childs.get("connector"));
        List<Object> childrens = (List<Object>) childs.get("child");
        if(childrens == null){
            return "";
        }
        Iterator<Object> child = childrens.iterator();
        while (child.hasNext()) {
            Object obj = child.next();
            if(obj instanceof JSONObject && (((JSONObject) obj).get("connector").equals(AND)
                    || ((JSONObject) obj).get("connector").equals(OR))){
                xmlStr.append(getSeniorChildXML((JSONObject) obj));
            }else{
                xmlStr.append("<child>").append(obj).append("</child>");
            }
        }
        return xmlStr.append("</child>").toString();
    }
 
    /**
     * 组装查询模板
     * @return
     */
    public QueryTemplate getQT(QTInfoDTO qtInfoDTO,boolean isLinkType) throws PLException {
        QueryTemplate qt = new QueryTemplate();
 
        if(isLinkType){
            qt.setType(QTConstants.TYPE_LINK);
            qt.setLinkType(qtInfoDTO.getBtmName());
            qt.setDirection(qtInfoDTO.getQueryTemplate().getDirection());
            qt.setBtmType(qtInfoDTO.getQueryTemplate().getBtmType());
            qt.setVersion(qtInfoDTO.getQueryTemplate().getVersion());
            qt.setQueryISLeaf(qtInfoDTO.getQueryTemplate().isQueryISLeaf());
            qt.setLevel(qtInfoDTO.getQueryTemplate().getLevel());
        }else{
            qt.setType(QTConstants.TYPE_BTM);
            qt.setBtmType(qtInfoDTO.getBtmName());
        }
 
        List<String> clauseList = new ArrayList<String>();
        //TODO 查询列 显示列
        clauseList.add("*");
        qt.setClauseList(clauseList);
        Condition con = new Condition();
        qt.setCondition(con);
        HashMap<String, ConditionItem> ciMap = getCIMap(qtInfoDTO);
        con.setCIMap(ciMap);
        con.setRootCIName(con.getRootCINameByCIMap(ciMap));
        return qt;
    }
 
    /**
     * 获取查询条件
     * @return
     */
    private HashMap<String, ConditionItem> getCIMap(QTInfoDTO qtInfoDTO) throws PLException {
        HashMap<String, ConditionItem> ciMap = new HashMap<>();
        //普通查询条件
        if(qtInfoDTO.getLevelFlag() == 0){
            /**
             * 加入叶子节点
             */
            for(int i = 0; i < qtInfoDTO.getCondition().size(); i++){
                ConditionItem ci = new ConditionItem();
                String id = "ci" + (i + 1);
                ci.setId(id);
                ci.setLeafFlag(true);
                HashMap<String, String> condition = qtInfoDTO.getCondition().get(i);
                String clause = condition.get("clause");
                String operator = condition.get("operator");
                String ordinaryValue = condition.get("ordinaryValue");
                //add by zhangweiwei 2014/12/09  end  因增加选择查询模板按钮将每行组件数由4改成5
                LeafInfo leafInfo = new LeafInfo();
                if(clause.contains("T_OID.") || clause.contains("F_OID.")){
                    //去掉T_OID.或者F_OID.
                    String clause_ = clause.substring(6);
                    //属性为参照属性
                    if(clause_.contains(".")){
                        int fpIndex = clause_.indexOf(".");
                        String refAbName = clause.substring(0, fpIndex + 6);
                        clause = clause.substring(fpIndex + 6 + 1);
                        leafInfo.setClause(refAbName);
                        //去掉T_OID.或者F_OID.
                        refAbName = refAbName.substring(6);
                        leafInfo.setOperator(Operator.IN);
                        QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue);
                        LeafValue lValue = new LeafValue();
                        lValue.setQueryTemplate(qt);
                        leafInfo.setValue(lValue);
                        //属性为非参照属性
                    }else{
                        leafInfo.setClause(clause);
                        leafInfo.setOperator(operator);
                        LeafValue lValue = new LeafValue();
                        leafInfo.setValue(lValue);
                        //add by zhangweiwei 2014/12/09 start
                        //如果嵌套Link
                        if(ordinaryValue.contains(";")){
                            leafInfo.setOperator(Operator.IN);
                            String[] values = ordinaryValue.split(";");
                            String  QTname = values[0];
                            String  attr = values[1].substring(0, values[1].length());
                            try {
                                QTInfo qt = platformClientUtil.getQTDService().getQT(QTname);
                                String qtText = qt.qtText;
                                QueryTemplate qt_ = OQTool.getQTByQTText(qt.qtName, qtText);
                                List<String> clauseList = new ArrayList<String>();
                                clauseList.add(attr);
                                qt_.setClauseList(clauseList);
                                lValue.setQueryTemplate(qt_);
                                //lValue.setAttr(attr);
                            } catch (PLException e) {
                                e.printStackTrace();
                            }
                            catch (DocumentException e1) {
                                e1.printStackTrace();
                            }
                        }else{
                            lValue.setOrdinaryValue(ordinaryValue);
                        }
                        //add by zhangweiwei 2014/12/09 end
                    }
                }else{
                    //属性为参照属性
                    if(clause.contains(".")){
                        int fpIndex = clause.indexOf(".");
                        String refAbName = clause.substring(0, fpIndex);
                        clause = clause.substring(fpIndex + 1);
                        leafInfo.setClause(refAbName);
                        leafInfo.setOperator(Operator.IN);
                        QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue);
                        LeafValue lValue = new LeafValue();
                        lValue.setQueryTemplate(qt);
                        leafInfo.setValue(lValue);
                        //属性为非参照属性
                    }else{
                        leafInfo.setClause(clause);
                        leafInfo.setOperator(operator);
                        LeafValue lValue = new LeafValue();
                        leafInfo.setValue(lValue);
                        //add by zhangweiwei 2014/12/09 start
                        //如果嵌套Link
                        if(ordinaryValue.contains(";")){
                            leafInfo.setOperator(Operator.IN);
                            String[] values = ordinaryValue.split(";");
                            String  QTname = values[0];
                            String  attr = values[1].substring(0, values[1].length());
                            try {
                                QTInfo qt = platformClientUtil.getQTDService().getQT(QTname);
                                String qtText = qt.qtText;
                                QueryTemplate qt_ = OQTool.getQTByQTText(qt.qtName, qtText);
                                List<String> clauseList = new ArrayList<String>();
                                clauseList.add(attr);
                                qt_.setClauseList(clauseList);
                                lValue.setQueryTemplate(qt_);
                                //lValue.setAttr(attr);
                            } catch (PLException e) {
                                e.printStackTrace();
                            } catch (DocumentException e1) {
                                e1.printStackTrace();
                            }
 
                        }else{
                            lValue.setOrdinaryValue(ordinaryValue);
                        }
                        //add by zhangweiwei 2014/12/09 end
                    }
                }
 
                ci.setLeafInfo(leafInfo);
                ciMap.put(ci.getId(), ci);
            }
 
            Iterator<String> iterator = ciMap.keySet().iterator();
            ArrayList<ConditionItem> ciList = new ArrayList<ConditionItem>();
            String lCIId = null;
            while(iterator.hasNext()){
                lCIId = iterator.next();
                break;
            }
            int count = ciMap.size();
            while(iterator.hasNext()){
                String rCIId = iterator.next();
                ConditionItem pCI = new ConditionItem();
                String pId = "ci" + ++count;
                pCI.setId(pId);
                pCI.setLeafFlag(false);
                ChildrenInfo pChildrenInfo = new ChildrenInfo();
                pChildrenInfo.setLeftCIName(lCIId);
                pChildrenInfo.setConnector(Connector.AND);
                pChildrenInfo.setRightCIName(rCIId);
                pCI.setChildrenInfo(pChildrenInfo);
                ciList.add(pCI);
                lCIId = pId;
            }
            for(Iterator<ConditionItem> i = ciList.iterator(); i.hasNext();){
                ConditionItem ci = i.next();
                ciMap.put(ci.getId(), ci);
            }
            //高级查询条件
        }else if(qtInfoDTO.getLevelFlag() == 1){
            ciMap = getCIMapForSeniorTree(qtInfoDTO);
        }
        return ciMap;
    }
 
    /**
     * 获取高级查询的查询条件集合
     * @return
     */
    private HashMap<String, ConditionItem> getCIMapForSeniorTree(QTInfoDTO qtInfoDTO) throws PLException {
        HashMap<String, ConditionItem> ciMap = new HashMap<>();
        HashMap<String, Object> tree = qtInfoDTO.getTree();
        if (tree.isEmpty()) {
            // 无查询条件的查询
            return null;
        }
        ArrayList<ConditionItem> ciList = new ArrayList<ConditionItem>();
        String connector = String.valueOf(tree.get("connector"));
        List<Object> childrens = (List<Object>) tree.get("child");
        Iterator<Object> children = childrens.iterator();
        while (children.hasNext()) {
            Object obj = children.next();
            if(obj instanceof JSONObject && (((JSONObject) obj).get("connector").equals(AND)
                    || ((JSONObject) obj).get("connector").equals(OR))){
                List<ConditionItem> subCIList = getCIList((JSONObject) obj, ciList.size() + 1);
                if(subCIList == null){
                    continue;
                }
                if(ciList.size() > 0){
                    ConditionItem leftCI = ciList.get(ciList.size() - 1);
                    ConditionItem rightCI = subCIList.get(subCIList.size() - 1);
                    ciList.addAll(subCIList);
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + (ciList.size() + 1));
                    ciList.add(pCI);
                    pCI.setLeafFlag(false);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    pCI.setChildrenInfo(cInfo);
                    cInfo.setLeftCIName(leftCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(rightCI.getId());
                }else{
                    ciList.addAll(subCIList);
                }
            }else{
                String text = String.valueOf(obj);
                if(text == null || text.equals("")){
                    continue;
                }
                if(!text.contains(Operator.EQUAL) && !text.contains(Operator.UNEQUAL) && !text.contains(Operator.CONTAINS)
                        && !text.contains(Operator.IN) && !text.contains(Operator.NOTIN) &&!text.contains(Operator.GTE) && !text.contains(Operator.GT)
                        && !text.contains(Operator.LTE) && !text.contains(Operator.LT)){
                    continue;
                }
                if(ciList.size() > 0){
                    ConditionItem leftCI = ciList.get(ciList.size() - 1);
                    ConditionItem rightCI = getCIByNode(text, ciList.size() + 1);
                    ciList.add(rightCI);
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + (ciList.size() + 1));
                    ciList.add(pCI);
                    pCI.setLeafFlag(false);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    pCI.setChildrenInfo(cInfo);
                    cInfo.setLeftCIName(leftCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(rightCI.getId());
                }else{
                    ConditionItem ci = getCIByNode(text, ciList.size() + 1);
                    ciList.add(ci);
                }
            }
        }
 
        for(int i = 0; i < ciList.size(); i++){
            ConditionItem ci = ciList.get(i);
            ciMap.put(ci.getId(), ci);
        }
        return ciMap;
    }
 
    /**
     * 树化一个查询条件组节点
     * @param children
     * @param beginId
     * @return
     */
    private List<ConditionItem> getCIList(JSONObject children, int beginId) throws PLException {
        List<ConditionItem> ciList = new ArrayList<>();
        String connector = String.valueOf(children.get("connector"));
        List<Object> childrens = (List<Object>) children.get("child");
        if(childrens == null){
            return null;
        }
        Iterator<Object> child = childrens.iterator();
        while (child.hasNext()) {
            Object obj = child.next();
            if(obj instanceof JSONObject && (((JSONObject) obj).get("connector").equals(AND)
                    || ((JSONObject) obj).get("connector").equals(OR))){
                List<ConditionItem> subCIList = getCIList((JSONObject) obj, beginId);
                if(ciList.size() > 0){
                    ConditionItem leftCI = ciList.get(ciList.size() - 1);
                    ConditionItem rightCI = subCIList.get(subCIList.size() - 1);
                    ciList.addAll(subCIList);
                    beginId = beginId + subCIList.size();
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + beginId);
                    ciList.add(pCI);
                    beginId = beginId + 1;
                    pCI.setLeafFlag(false);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    pCI.setChildrenInfo(cInfo);
                    cInfo.setLeftCIName(leftCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(rightCI.getId());
                }else{
                    ciList.addAll(subCIList);
                    beginId = beginId + subCIList.size();
                }
            }else{
                String text = String.valueOf(obj);
                if(text == null || text.equals("")){
                    continue;
                }
                if(!text.contains(Operator.EQUAL) && !text.contains(Operator.UNEQUAL) && !text.contains(Operator.CONTAINS)
                        && !text.contains(Operator.IN) && !text.contains(Operator.NOTIN) &&!text.contains(Operator.GTE) && !text.contains(Operator.GT)
                        && !text.contains(Operator.LTE) && !text.contains(Operator.LT)){
                    continue;
                }
                if(ciList.size() > 0){
                    ConditionItem leftCI = ciList.get(ciList.size() - 1);
                    ConditionItem rightCI = getCIByNode(text, beginId);
                    ciList.add(rightCI);
                    beginId = beginId + 1;
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + beginId);
                    ciList.add(pCI);
                    beginId = beginId + 1;
                    pCI.setLeafFlag(false);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    pCI.setChildrenInfo(cInfo);
                    cInfo.setLeftCIName(leftCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(rightCI.getId());
                }else{
                    ConditionItem ci = getCIByNode(text, beginId);
                    ciList.add(ci);
                    beginId = beginId + 1;
                }
            }
        }
        return ciList;
    }
 
    /**
     * 高级查询
     * 把Node内容解析成ConditionItem
     * @param text
     * @param id
     * @return
     */
    private ConditionItem getCIByNode(String text, int id) throws PLException {
        ConditionItem ci = new ConditionItem();
        ci.setId("ci" + id);
        ci.setLeafFlag(true);
        LeafInfo leafInfo = new LeafInfo();
        ci.setLeafInfo(leafInfo);
        String operator = null;
        if(text.contains(Operator.EQUAL)){
            operator = Operator.EQUAL;
        }else if(text.contains(Operator.CONTAINS)){
            operator = Operator.CONTAINS;
        }else if(text.contains(Operator.UNEQUAL)){
            operator = Operator.UNEQUAL;
        }else if(text.contains(Operator.IN)){
            operator = Operator.IN;
        }else if(text.contains(Operator.NOTIN)){
            operator = Operator.NOTIN;
        }else if(text.contains(Operator.GTE)){
            operator = Operator.GTE;
        }else if(text.contains(Operator.GT)){
            operator = Operator.GT;
        }else if(text.contains(Operator.LTE)){
            operator = Operator.LTE;
        }else if(text.contains(Operator.LT)){
            operator = Operator.LT;
        }
        int operatorIndex = text.indexOf(operator);
        String clause = text.substring(0, operatorIndex).trim();
        String ordinaryValue = text.substring(operatorIndex + 2).trim();
        if(clause.contains("T_OID.") || clause.contains("F_OID.")){
            //去掉T_OID.或者F_OID.
            String clause_ = clause.substring(6);
            //属性为参照属性
            if(clause_.contains(".")){
                int fpIndex = clause_.indexOf(".");
                String refAbName = clause.substring(0, fpIndex + 6);
                clause = clause.substring(fpIndex + 6 + 1);
                leafInfo.setClause(refAbName);
                //去掉T_OID.或者F_OID.
                refAbName = refAbName.substring(6);
                leafInfo.setOperator(Operator.IN);
                QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue);
                LeafValue lValue = new LeafValue();
                lValue.setQueryTemplate(qt);
                leafInfo.setValue(lValue);
                //属性为非参照属性
            }else{
                leafInfo.setClause(clause);
                leafInfo.setOperator(operator);
                LeafValue lValue = new LeafValue();
                leafInfo.setValue(lValue);
                lValue.setOrdinaryValue(ordinaryValue);
            }
        }else{
            //属性为参照属性
            if(clause.contains(".")){
                int fpIndex = clause.indexOf(".");
                String refAbName = clause.substring(0, fpIndex);
                clause = clause.substring(fpIndex + 1);
                leafInfo.setClause(refAbName);
                leafInfo.setOperator(Operator.IN);
                QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue);
                LeafValue lValue = new LeafValue();
                lValue.setQueryTemplate(qt);
                leafInfo.setValue(lValue);
                //属性为非参照属性
            }else{
                leafInfo.setClause(clause);
                leafInfo.setOperator(operator);
                LeafValue lValue = new LeafValue();
                leafInfo.setValue(lValue);
                lValue.setOrdinaryValue(ordinaryValue);
            }
        }
 
        return ci;
    }
 
    /**
     * 获取参照的查询模板
     * @param refAbName: 参照属性名
     * @param clause: 属性参照的业务类型中的属性
     * @param operator
     * @param ordinaryValue
     * @return
     */
    private QueryTemplate getRefQT(String refAbName, String clause,
                                   String operator, String ordinaryValue) throws PLException {
        QueryTemplate qt = new QueryTemplate();
        List<String> clauseList = new ArrayList<String>();
        clauseList.add("OID");
        qt.setClauseList(clauseList);
        AttributeDef refAb = platformClientUtil.getAttributeService().getAttributeDefByName(refAbName);
        OtherInfo otherInfo = OtherInfo.getOtherInfoByText(refAb.other);
        int refFlag = otherInfo.getRefFlag();
        String type = otherInfo.getRefTypeName();
        if(refFlag == 0){
            qt.setType(QTConstants.TYPE_BTM);
            qt.setBtmType(type);
        }else if(refFlag == 1){
            qt.setType(QTConstants.TYPE_LINK);
            qt.setLinkType(type);
        }
        Condition condition = new Condition();
        qt.setCondition(condition);
        condition.setRootCIName("ci1");
        HashMap<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        condition.setCIMap(ciMap);
        ConditionItem ci = new ConditionItem();
        ci.setId("ci1");
        ciMap.put(ci.getId(), ci);
        ci.setLeafFlag(true);
        LeafInfo leafInfo = new LeafInfo();
        if(clause.contains(".")){
            int fpIndex = clause.indexOf(".");
            String refAbName_ = clause.substring(0, fpIndex);
            clause = clause.substring(fpIndex + 1);
            leafInfo.setClause(refAbName_);
            leafInfo.setOperator(Operator.IN);
            QueryTemplate qt_ = getRefQT(refAbName_, clause, operator, ordinaryValue);
            LeafValue lValue = new LeafValue();
            lValue.setQueryTemplate(qt_);
            leafInfo.setValue(lValue);
            qt.setId("qt_" + refAbName + "_" + refAbName_);
        }else{
            leafInfo.setClause(clause);
            leafInfo.setOperator(operator);
            LeafValue lValue = new LeafValue();
            lValue.setOrdinaryValue(ordinaryValue);
            leafInfo.setValue(lValue);
            qt.setId("qt_" + refAbName + "_" + clause);
        }
        ci.setLeafInfo(leafInfo);
        condition.setCIMap(ciMap);
        return qt;
    }
 
    /**
     * 返回查询的版本
     * @return
     */
    public int getVersion(String versionValue){
        int version = 0;
        if(versionValue.equals("当前版本当前版次")){
            version = 1;
        }else if(versionValue.equals("当前版本最新版次")){
            version = 2;
        }else if(versionValue.equals("最新版本最新版次")){
            version = 3;
        }else if(versionValue.equals("当前版次")){
            version = 4;
        }else if(versionValue.equals("当前版本")){
            version = 5;
        }else if(versionValue.equals("当前命名对象")){
            version = 6;
        }else if(versionValue.equals("已发布的最新版本")){
            version = 7;
        }
        return version;
    }
 
    /**
     * 校验参数
     * @param qtd
     * @return
     */
    private void checkName(QTD qtd) throws PLException {
        if (qtd.name.equals("")) {
            throw new PLException("500", new String[]{"请输入模板定义名"});
        }
        if (!qtd.name.matches("[a-z A-Z]*")) {
            throw new PLException("500", new String[]{"模板定义名只能为英文字母"});
        }
        if (platformClientUtil.getQTDService().isExistsQTD(qtd.name.toLowerCase())) {
            throw new PLException("500", new String[]{"该模板定义名已经被使用, 请更换"});
        }
    }
 
    /**
     * 导出链接类型查询模板
     * names 查询模板名
     * @return
     */
    @Override
    public void expLinkTemplate(String names, HttpServletResponse response) throws PLException, IOException {
        String defaultTempFolder = LocalFileUtil.getDefaultTempFolder();
        String vciqtmfFileName = defaultTempFolder + File.separator + "LinkTemplateExp" + new Date().getTime() + ".vciqtf";
        LinkQTExportData exportData = new LinkQTExportData();
        String[] splits = names.split(",");
        for (String name : splits) {
            QTInfo qt = platformClientUtil.getQTDService().getQT(name);
            exportData.getAllQTs().put(qt.qtName,qt);
            LinkType linkType = platformClientUtil.getLinkTypeService().getLinkType(qt.btmName);
            if(linkType != null && !linkType.oid.equals("")){
                exportData.getSelectedBtmItems().put(linkType.name, linkType);
                exportData.getSelectedBtmAttrs().put(linkType.name, linkType.attributes);
                exportData.getAllQTDs().put(linkType.name, platformClientUtil.getQTDService().getLinkTypeQTDs(linkType.name));
            }
        }
        ObjectOutputStream vciamfFileStream = null;
        try {
            File vciqtmfFile = new File(vciqtmfFileName);
            vciamfFileStream = new ObjectOutputStream(new FileOutputStream(vciqtmfFile));
            vciamfFileStream.writeObject(exportData);
        }finally {
            try {
                if (vciamfFileStream != null) {
                    vciamfFileStream.flush();
                    vciamfFileStream.close();
                }
            } catch (Exception e) {
                throw new PLException("500",new String[]{"导出流关闭异常!"});
            }
        }
        ControllerUtil.writeFileToResponse(response,vciqtmfFileName);
        FileUtil.del(defaultTempFolder + File.separator);
    }
 
    /**
     * 导入链接类型查询模板
     * @param file 上传的文件
     * @return 导入结果
     */
    @Override
    public BaseResult impLinkTemplate(MultipartFile file) throws IOException, ClassNotFoundException {
 
        if (file == null) {
            return BaseResult.fail(FrameWorkLangCodeConstant.IMPORT_FAIL, new String[]{"无导入的文件"});
        }
        if (!file.getOriginalFilename().endsWith(".vciqtf")) {
            throw new VciBaseException("仅能上传.vciqtf格式文件,请重新上传!");
        }
        ObjectInputStream obj = new ObjectInputStream(
                file.getInputStream());
        LinkQTExportData qtExportData = (LinkQTExportData) obj.readObject();
        List<LinkType> ltsNew = new ArrayList<>();
        List<QTInfo> qTWrapperNew = new ArrayList<>();
        Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> norepQTDs = new HashMap<String, QTD[]>();
        addImportData(qtExportData, ltsNew, qTWrapperNew, norepQTDs);
        QTInfo[] qts = qTWrapperNew
                .toArray(new QTInfo[qTWrapperNew.size()]);
        //处理树的返回
        HashMap<String,Object> tree = new HashMap();
        tree.put("text","链接类型");
        List<HashMap<String,Object>> childList = new ArrayList<>();
        //添加业务类型根节点
        for (LinkType plAction : ltsNew) {
            HashMap<String,Object> childTree = new HashMap();
            childTree.put("text", plAction.label + "/" +plAction.name);
            childTree.put("oid", plAction.oid);
            addExportTreeNode(plAction, qts, childTree);
            childList.add(childTree);
        }
        tree.put("children", childList);
        return BaseResult.success(tree);
    }
 
    // 数据过滤
    private void addImportData(LinkQTExportData qtExportData, List<LinkType> ltsNew, List<QTInfo> qTWrapperNew,
                               Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> norepQTDs) {
        Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> allQTDs = qtExportData
                .getAllQTDs();
        Map<String/* 类型名称 */, LinkType/* 类型对象 */> selectedBtmItems = qtExportData
                .getSelectedBtmItems();
        Map<String/* 类型名称 */, String[]/* 类型所属属性 */> selectedBtmAttrs = qtExportData
                .getSelectedBtmAttrs();
        Map<String/* 查询模板名称 */, QTInfo/* 查询模板对象 */> allSelectedQTs = qtExportData
                .getAllQTs();
        if (allQTDs.size() == 0 || allQTDs == null
                || selectedBtmItems.size() == 0 || selectedBtmItems == null
                || selectedBtmAttrs.size() == 0 || selectedBtmAttrs == null
                || allSelectedQTs.size() == 0 || allSelectedQTs == null) {
            throw new VciBaseException("导入的模板数据有误!!!");
        }
        Set<Map.Entry<String, QTD[]>> pLActions = allQTDs.entrySet();
        Set<Map.Entry<String, QTInfo>> pLQts = allSelectedQTs.entrySet();
        Set<Map.Entry<String, LinkType>> LinkpLQts = selectedBtmItems.entrySet();
        for (Map.Entry<String, QTD[]> entry : pLActions) {
            String string = entry.getKey();
            QTD[] qtds = entry.getValue();
            getPLQtIsInDB(string, qtds, norepQTDs);
        }
        for (Map.Entry<String, LinkType> entry : LinkpLQts) {
            String string = entry.getKey();
            LinkType LinkType = entry.getValue();
            getPLQtBtmItem(string, LinkType, ltsNew);
        }
        Map<String/*类型名称*/, QTD[]/*查询模板定义*/> newNorepQTDs = getnewNorepQTDs(norepQTDs,allSelectedQTs);
        for (Map.Entry<String, QTInfo> entry : pLQts) {
            String string = entry.getKey();
            QTInfo qtWrapper = entry.getValue();
            getPLQtWrapper(string, qtWrapper, ltsNew, qTWrapperNew, newNorepQTDs);
        }
 
    }
 
    /**
     * 查询模板定义的处理
     * @param string
     * @param qtds
     */
    private void getPLQtIsInDB(String string, QTD[] qtds, Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> norepQTDs) {
        // TODO Auto-generated method stub
        try {
            QTD[] qtds2 = platformClientUtil.getQTDService().getBizTypeQTDs(string);
            QTD[] qtdsNewQtds = null;
            List<QTD> es = new ArrayList<>();
            for (QTD qtd : qtds) {
                boolean isleat = true;
                for (int i = 0; i < qtds2.length; i++) {
                    if (qtd.name.equals(qtds2[i].name)) {
                        isleat = false;
                        break;
                    }
                }
                if (isleat) {
                    es.add(qtd);
                }
            }
            if (es.size() != 0) {
                qtdsNewQtds = es.toArray(new QTD[es.size()]);
                norepQTDs.put(string, qtdsNewQtds);
            }
        } catch (PLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    private void getPLQtBtmItem(String string, LinkType linkType, List<LinkType> ltsNew) {
        // TODO Auto-generated method stub
        try {
            LinkType queryLinkType = platformClientUtil.getLinkTypeService().getLinkType(string);
            LinkType[] btmArray = platformClientUtil.getLinkTypeService().getLinkTypes();
            String[] strings = queryLinkType.attributes;
            String[] stringsFrom = queryLinkType.btmItemsFrom;
            String[] stringsTo = queryLinkType.btmItemsTo;
            boolean b = true;
            for (int i = 0; i < btmArray.length; i++) {
                if (btmArray[i].name.equals(string)) {
                    b = false;
                    if (Arrays.equals(linkType.attributes, strings)
                            && Arrays
                            .equals(linkType.btmItemsFrom, stringsFrom)
                            && Arrays.equals(linkType.btmItemsTo, stringsTo)) {
                        btmArray[i].name += "【链接类型已存在】";
                        /* this.btmArray[i] = btmArray[i]; */
                        ltsNew.add(btmArray[i]);
                    } else {
                        btmArray[i].name += "【链接类型存在但属性不一致】";
                        ltsNew.add(btmArray[i]);
                    }
                }
            }
            if (b) {
                linkType.name += "【链接类型不存在】";
                ltsNew.add(linkType);
            }
        } catch (PLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    private void getPLQtBtmItem2(String string, BizType bizType, List<BizType> ltsNew) {
        // TODO Auto-generated method stub
        try {
            BizType queryBizType = platformClientUtil.getBtmService().getBizTypes(string)[0];
            BizType[] btmArray = platformClientUtil.getBtmService().getBizTypes("");
            String[] strings = queryBizType.apNameArray;
            boolean b = true;
            for (int i = 0; i < btmArray.length; i++) {
                if (btmArray[i].name.equals(string)) {
                    b = false;
                    if (Arrays.equals(bizType.apNameArray, strings)) {
                        btmArray[i].name += "【业务类型已存在】";
                        ltsNew.add(btmArray[i]);
                    } else {
                        btmArray[i].name += "【业务类型存在但属性不一致】";
                        ltsNew.add(btmArray[i]);
                    }
                }
            }
            if (b) {
                bizType.name += "【业务类型不存在】";
                ltsNew.add(bizType);
            }
        } catch (PLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    private void getPLQtWrapper(String string, QTInfo qtWrapper, List<LinkType> ltsNew, List<QTInfo> qTWrapperNew, Map<String/*类型名称*/, QTD[]/*查询模板定义*/> newNorepQTDs) {
        try {
            QTInfo[] qtws = platformClientUtil.getQTDService().getAllQTs();
            boolean islaet = true;
            for (int i = 0; i < qtws.length; i++) {
                if (string.equals(qtws[i].qtName)) {
                    islaet = false;
                    for (LinkType lt : ltsNew) {
                        if (qtWrapper.btmName.equals(lt.name.substring(0,
                                lt.name.indexOf("【")))) {
                            if (lt.name.endsWith("【链接类型已存在】")) {
                                qtWrapper.qtName += "【查询模板已存在,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }else if(lt.name.endsWith("【链接类型存在但属性不一致】")){
                                qtWrapper.qtName += "【查询模板所属链接类型属性不一致,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }else{
                                qtWrapper.qtName += "【查询模板所属链接类型不存在,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }
                        }
                    }
                }
            }
            if (islaet) {
                for (LinkType btmItem : ltsNew) {
                    if (qtWrapper.btmName.equals(btmItem.name.substring(0,
                            btmItem.name.indexOf("【")))) {
                        if (btmItem.name.endsWith("【链接类型已存在】")) {
                            //判断查询模板定义是否存在,进行保存
                            QTD[] qtds = newNorepQTDs.get(qtWrapper.btmName);
                            if(qtds != null){
                                for (QTD qtd : qtds) {
                                    QTD qtdByName = platformClientUtil.getQTDService().getQTDByName(qtd.name);
                                    if(!(qtdByName != null && StringUtils.isNotBlank(qtdByName.name))){
                                        boolean b = platformClientUtil.getQTDService().addQTD(qtd);
                                        if(!b){
                                            qtWrapper.qtName += qtWrapper.qtName+"导入查询模板定义【" + qtdByName.name + "】失败!";
                                        }
                                    }
                                }
                                newNorepQTDs.remove(qtWrapper.btmName);
                            }
                            boolean success = platformClientUtil.getQTDService().saveQT(qtWrapper);
                            if(!success){
                                qtWrapper.qtName += qtWrapper.qtName+"导入失败!";
                            }else {
                                qtWrapper.qtName += "【查询模板导入成功!】";
                            }
                            qTWrapperNew.add(qtWrapper);
                        }else if(btmItem.name.endsWith("【链接类型存在但属性不一致】")){
                            qtWrapper.qtName += "【查询模板所属链接类型属性不一致,不导入】";
                            qTWrapperNew.add(qtWrapper);
                        }else{
                            qtWrapper.qtName += "【查询模板所属链接类型不存在,不导入】";
                            qTWrapperNew.add(qtWrapper);
                        }
                    }
                }
            }
        } catch (PLException e) {
            e.printStackTrace();
        }
    }
 
    private void getPLBtmQtWrapper(String string, QTInfo qtWrapper, List<BizType> ltsNew, List<QTInfo> qTWrapperNew, Map<String/*类型名称*/, QTD[]/*查询模板定义*/> newNorepQTDs) {
        try {
            QTInfo[] qtws = platformClientUtil.getQTDService().getAllQTs();
            boolean islaet = true;
            for (int i = 0; i < qtws.length; i++) {
                if (string.equals(qtws[i].qtName)) {
                    islaet = false;
                    for (BizType lt : ltsNew) {
                        if (qtWrapper.btmName.equals(lt.name.substring(0,
                                lt.name.indexOf("【")))) {
                            if (lt.name.endsWith("【业务类型已存在】")) {
                                qtWrapper.qtName += "【查询模板已存在,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }else if(lt.name.endsWith("【业务类型存在但属性不一致】")){
                                qtWrapper.qtName += "【查询模板所属业务类型属性不一致,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }else{
                                qtWrapper.qtName += "【查询模板所属业务类型不存在,不导入】";
                                qTWrapperNew.add(qtWrapper);
                            }
                        }
                    }
                }
            }
            if (islaet) {
                for (BizType btmItem : ltsNew) {
                    if (qtWrapper.btmName.equals(btmItem.name.substring(0,
                            btmItem.name.indexOf("【")))) {
                        if (btmItem.name.endsWith("【业务类型已存在】")) {
                            //判断查询模板定义是否存在,进行保存
                            QTD[] qtds = newNorepQTDs.get(qtWrapper.btmName);
                            if(qtds != null){
                                for (QTD qtd : qtds) {
                                    QTD qtdByName = platformClientUtil.getQTDService().getQTDByName(qtd.name);
                                    if(!(qtdByName != null && StringUtils.isNotBlank(qtdByName.name))){
                                        boolean b = platformClientUtil.getQTDService().addQTD(qtd);
                                        if(!b){
                                            qtWrapper.qtName += qtWrapper.qtName+"导入查询模板定义【" + qtdByName.name + "】失败!";
                                        }
                                    }
                                }
                                newNorepQTDs.remove(qtWrapper.btmName);
                            }
                            boolean success = platformClientUtil.getQTDService().saveQT(qtWrapper);
                            if(!success){
                                qtWrapper.qtName += qtWrapper.qtName+"导入失败!";
                            }else {
                                qtWrapper.qtName += "【查询模板导入成功!】";
                            }
                            qTWrapperNew.add(qtWrapper);
                        }else if(btmItem.name.endsWith("【业务类型存在但属性不一致】")){
                            qtWrapper.qtName += "【查询模板所属业务类型属性不一致,不导入】";
                            qTWrapperNew.add(qtWrapper);
                        }else{
                            qtWrapper.qtName += "【查询模板所属业务类型不存在,不导入】";
                            qTWrapperNew.add(qtWrapper);
                        }
                    }
                }
            }
        } catch (PLException e) {
            e.printStackTrace();
        }
    }
 
    //根据选择的查询模板过滤模板定义
    private Map<String, QTD[]> getnewNorepQTDs(Map<String, QTD[]> norepQTDs,
                                               Map<String, QTInfo> allQTs) {
        // TODO Auto-generated method stub
        Map<String, QTD[]> map = new HashMap<String, QTD[]>();
        Set<Map.Entry<String, QTInfo>> pLQts = allQTs.entrySet();
        Set<Map.Entry<String, QTD[]>> pLActions = norepQTDs.entrySet();
        for (Map.Entry<String, QTD[]> entrys : pLActions) {
            for (Map.Entry<String, QTInfo> entry : pLQts) {
                if(entry.getValue().btmName.equals(entrys.getKey())){
                    map.put(entrys.getKey(), entrys.getValue());
                }
            }
        }
        return map;
    }
    /**
     * 查询方案删除
     * @param names 查询方案名
     * @return 操作结果
     */
    @Override
    public BaseResult deleteLinkTemplate(String names) throws PLException {
        if(StringUtils.isBlank(names)){
            return BaseResult.fail("请选择查询方案!");
        }
        String[] nameSplit = names.split(",");
        boolean b = platformClientUtil.getQTDService().deleteQTs(nameSplit);
        if(!b){
            return BaseResult.fail("删除失败!!!");
        }
        return BaseResult.success("查询模板删除成功!");
    }
 
    /**
     * @param qtInfoDTO 传输的数据对象:
     *                linkTypeName 链接类型、
     *                rdPositive 方向,true正向,false反向
     *                btmName 业务类型名称
     *                combRelaType 业务类型选择值
     *                versionValue 版本班次值
     *                isQueryIsLeaf 是否选择下级
     *                level 子节点层数
     * @return 查询结果
     */
    @Override
    public BaseResult getCriteriaBtm(QTInfoDTO qtInfoDTO) throws PLException {
        QueryTemplate qt = getQT(qtInfoDTO,false);
        String checkInfo = OQTool.checkQT(qt);
        if(!checkInfo.equals("OK")){
            throw new PLException("500", new String[]{checkInfo});
        }
        qt.setId("qt1");
        try {
            BusinessObject[] result = platformClientUtil.getQueryService().findBTMObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML());
            return BaseResult.dataList(Arrays.asList(result));
        }catch (Exception e){
            String errorLog = "查询时出现问题,请检查配置的条件是否存在问题,具体原因:"+VciBaseUtil.getExceptionMessage(e);
            throw new VciBaseException(errorLog);
        }
    }
 
    /**
     * 业务类型查询模板保存
     * @param qtInfoDTO 保存传输对象
     * @return 保存结果
     */
    @Override
    public BaseResult btmSave(QTInfoDTO qtInfoDTO) throws PLException {
        VciBaseUtil.alertNotNull(
                qtInfoDTO,"保存的业务类型模板对象",
                qtInfoDTO.getBtmName(),"请选择业务类型和查询模板定义来创建查询模板"
        );
 
        //add by caill start 遍历普通查询模板输入框中的值做校验
        //        ArrayList<String> list = cpm_centerPanel.getList();
        //        for(int i=0;i<list.size();i++){
        //            String text = (String) list.get(i);
        //            if(text.contains("'")){
        //                JOptionPane.showMessageDialog(cp_mainPanel, "条件值中包含了非法字符", "非法字符", JOptionPane.ERROR_MESSAGE);
        //                return;
        //            }
        //        }
        //保存之前带出已选择的qtName
        QueryTemplate qt = getQT(qtInfoDTO,false);
        //前端调用这个方法isExistsQT()
        /*if(isExists(qtName)){
            return BaseResult.success("","该查询模板名已经存在,是否覆盖?");
        }*/
        String checkInfo = OQTool.checkQT(qt);
        if(!checkInfo.equals("OK")){
            throw new PLException("500", new String[]{checkInfo});
        }
        qt.setId(qtInfoDTO.getQtName());
        //设置排序信息
        qt.setOrderInfoList(qtInfoDTO.getQueryTemplate().getOrderInfoList());
        QTInfo qtWrapper = new QTInfo();
        qtWrapper.qtName = qtInfoDTO.getQtName();
        qtWrapper.btmName = qt.getBtmType();
        qtWrapper.creator = WebUtil.getCurrentUserId();
        qtWrapper.createTime = System.currentTimeMillis();
        if(qtInfoDTO.getTree() == null){
            qtWrapper.qtUIText = "";
        }else{
            //当勾选了高级查询时需要处理高级查询的相关条件
            qtWrapper.qtUIText = getSeniorXML(qtInfoDTO);
        }
        qtWrapper.levelFlag = qtInfoDTO.getLevelFlag();
        qtWrapper.qtText = OQTool.qtTOXMl(qt).asXML();
        boolean saveFlag = platformClientUtil.getQTDService().saveQT(qtWrapper);
        if(saveFlag){
            return BaseResult.success("保存查询模板成功");
        }else{
            return BaseResult.fail("保存查询模板失败");
        }
    }
 
    /**
     * 更新数据库结构
     * @return 保存结果
     */
    @Override
    public BaseResult updateDBStructure() throws PLException {
        boolean f = platformClientUtil.getQTDService().updateQT();
        if(f){
            return BaseResult.success("更新成功");
        }else{
            return BaseResult.fail("更新失败");
        }
    }
 
    /**
     * 业务类型查询模板树查询,用于界面的导出功能
     * @return 查询结果
     */
    @Override
    public BaseResult getBtmQtTree() throws PLException {
        BizType[] bizTypes= null;
        QTInfo[] qts = null;
        HashMap<String,Object> tree = new HashMap();
        tree.put("text","业务类型");
        if (bizTypes == null) {
            List<BizType> es = new ArrayList<>();
            bizTypes = platformClientUtil.getBtmService().getBizTypes("");
            for (BizType lt : bizTypes) {
                QTInfo[] qtWrappers = platformClientUtil.getQTDService().getObjTypeQTs(lt.name);
                if (qtWrappers.length!=0) {
                    es.add(lt);
                }
            }
            bizTypes = es.toArray(new BizType[es.size()]);
            //TODO:需修正没有关联查询模板业务类型去掉
            qts = platformClientUtil.getQTDService().getAllQTs();//获取所有查询模板
        }
        List<HashMap<String,Object>> childList = new ArrayList<>();
        //添加业务类型根节点
        for (BizType plAction : bizTypes) {
            HashMap<String,Object> childTree = new HashMap();
            childTree.put("text", plAction.label + "/" +plAction.name);
            childTree.put("oid", plAction.oid);
            //tree.put("children", childTree);
            addExportTreeNode(plAction, qts, childTree);
            childList.add(childTree);
        }
        tree.put("children", childList);
        return BaseResult.success(tree);
    }
 
    /**
     * 导出业务类型查询模板
     * names 查询模板名
     * @return
     */
    @Override
    public String expBtmQTTemplate(String qtNames) throws PLException, IOException {
        String defaultTempFolder = LocalFileUtil.getDefaultTempFolder();
        String vciqtmfFileName = defaultTempFolder + File.separator + "BtmTemplateExp" + new Date().getTime() + ".vciqtf";
        String[] names = qtNames.split(",");
        BtmQTExportData exportData = new BtmQTExportData();
        for (String name : names) {
            QTInfo qt = platformClientUtil.getQTDService().getQT(name);
            exportData.getAllQTs().put(qt.qtName,qt);
            BizType[] bizTypes = platformClientUtil.getBtmService().getBizTypes(qt.btmName);
            for (int i = 0; i < bizTypes.length; i++) {
                BizType bizType = bizTypes[i];
                if(bizTypes != null && !bizType.oid.equals("")){
                    exportData.getSelectedBtmItems().put(bizType.name, bizType);
                    exportData.getSelectedBtmAttrs().put(bizType.name, bizType.apNameArray);
                    exportData.getAllQTDs().put(bizType.name, platformClientUtil.getQTDService().getBizTypeQTDs(bizType.name));
                }
            }
        }
        ObjectOutputStream vciamfFileStream = null;
        try {
            File vciqtmfFile = new File(vciqtmfFileName);
            vciamfFileStream = new ObjectOutputStream(new FileOutputStream(vciqtmfFile));
            vciamfFileStream.writeObject(exportData);
        }finally {
            try {
                if (vciamfFileStream != null) {
                    vciamfFileStream.flush();
                    vciamfFileStream.close();
                }
            } catch (Exception e) {
                throw new PLException("500",new String[]{"导出流关闭异常!"});
            }
        }
        //ControllerUtil.writeFileToResponse(response,vciqtmfFileName);
        //FileUtil.del(defaultTempFolder + File.separator);
        return vciqtmfFileName;
    }
 
    /**
     * 查询条件下的选择查询模板对话框(查询全部模板和所对应的业务类型或链接类型名)
     * @return
     */
    @Override
    public BaseResult getAllQTs() throws PLException {
        QTInfo[] allQTs = platformClientUtil.getQTDService().getAllQTs();
        List<QTInfo> allQTList = Arrays.stream(allQTs).sorted(Comparator.comparing(o -> o.qtName, String.CASE_INSENSITIVE_ORDER)).collect(Collectors.toList());
        DataGrid<QTInfo> dataGrid = new DataGrid<>();
        dataGrid.setData(allQTList);
        dataGrid.setTotal(allQTs.length);
        return BaseResult.dataGrid(dataGrid);
    }
 
    /**
     * 导入业务类型查询模板
     * @param file 上传的文件
     * @return 导入结果
     */
    @Override
    public BaseResult impBtmTemplate(MultipartFile file) throws IOException, ClassNotFoundException {
        if (file == null) {
            return BaseResult.fail(FrameWorkLangCodeConstant.IMPORT_FAIL, new String[]{"无导入的文件"});
        }
        if (!file.getOriginalFilename().endsWith(".vciqtf")) {
            throw new VciBaseException("仅能上传.vciqtf格式文件,请重新上传!");
        }
        ObjectInputStream obj = new ObjectInputStream(
                file.getInputStream());
        BtmQTExportData qtExportData = (BtmQTExportData) obj.readObject();
        List<BizType> ltsNew = new ArrayList<>();
        List<QTInfo> qTWrapperNew = new ArrayList<>();
        Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> norepQTDs = new HashMap<String, QTD[]>();
        addImportBtmData(qtExportData, ltsNew, qTWrapperNew, norepQTDs);
        QTInfo[] qts = qTWrapperNew
                .toArray(new QTInfo[qTWrapperNew.size()]);
        //处理树的返回
        HashMap<String,Object> tree = new HashMap();
        tree.put("text","业务类型");
        List<HashMap<String,Object>> childList = new ArrayList<>();
        //添加业务类型根节点
        for (BizType plAction : ltsNew) {
            HashMap<String,Object> childTree = new HashMap();
            childTree.put("text", plAction.label + "/" +plAction.name);
            childTree.put("oid", plAction.oid);
            addExportTreeNode(plAction, qts, childTree);
            childList.add(childTree);
        }
        tree.put("children", childList);
        return BaseResult.success(tree);
    }
 
    /**
     * 查询模板的列表添加了字段的相关属性
     * @param btmName 类型
     * @param linkFlag 是否链接类型 :true 链接类型 ,false 业务类型
     * @param direction 正反方向
     * @return 查询模板的列表
     */
    @Override
    public BaseResult queryTemplateListByAttr(String btmName, Boolean linkFlag, String direction) throws PLException {
        //返回的界面下拉框显示树
        List<QTDDTO> qtddtos = new ArrayList<>();
        //将链接类型与业务类型的所有默认字段查询出来放到map里面方便后续调用
        Map<String, AttributeDef> allSysAttr = Arrays.stream(platformClientUtil.getBtmService().getSysAttributeDefs()).collect(Collectors.toMap(e -> e.name, e -> e));
        allSysAttr.putAll(Arrays.stream(platformClientUtil.getLinkTypeService().getSysAttributeDefs()).collect(Collectors.toMap(e -> e.name, e -> e)));
        QTD[] qtdArray = null;
        //查询相关类型的查询模板进行处理
        if(linkFlag){
            qtdArray = platformClientUtil.getQTDService().getLinkTypeQTDs(btmName);
        }else {
            qtdArray = platformClientUtil.getQTDService().getBizTypeQTDs(btmName);
        }
        //获取属性的数据加载类
        AttributeDataFetcher attrDataFetcher = new AttributeDataFetcher();
        for (QTD qtd : qtdArray) {
            QTDDTO qtddto = new QTDDTO();
            qtddto.setName(qtd.name);
            qtddto.setCreateTime(qtd.createTime);
            qtddto.setLinkTypeName(qtd.linkTypeName);
            qtddto.setBtmName(qtd.btmName);
            qtddto.setCreator(qtd.creator);
            for (String abName : qtd.abNames) {
                //AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(abName);
                AttributeDef att = attrDataFetcher.getAttributeDef(abName);
                //如果在属性接口中没有查到相关字段应该就在默认字段中
                if(att == null || "".equals(att.oid)){
                    att = allSysAttr.get(abName.toLowerCase());
                }
                if(direction != null) {
                    if (direction.equals(QTConstants.DIRECTION_POSITIVE)) {
                        if(att == null ){
                            continue;
//                            throw new PLException("500",new String[]{"属性字段:"+ abName +"未查询到,请确认!"});
                        }
                        att.name = "T_OID." + abName;
                    } else if (direction.equals(QTConstants.DIRECTION_OPPOSITE)) {
                        if(att == null ){
                            continue;
//                            throw new PLException("500",new String[]{"属性字段:"+ abName +"未查询到,请确认!"});
                        }
                        att.name = "F_OID." + abName;
                    }
                }else{
                    att.name = abName;
                }
                AttributeDefDTO attrDto = new AttributeDefDTO();
                attrDto.setDescription(att.description);
                attrDto.setLabel(att.label);
                attrDto.setOid(att.oid);
                attrDto.setCreator(att.creator);
                attrDto.setName(att.name);
                attrDto.setDefValue(att.defValue);
                attrDto.setLayersNum(1);
                attrDto.setRage(att.rage);
                attrDto.setVtDataType(att.vtDataType);
                attrDto.setOther(att.other);
                qtddto.getAttrs().add(attrDto);
                addNode(attrDto, allSysAttr);
            }
            qtddtos.add(qtddto);
        }
        return BaseResult.dataList(qtddtos);
    }
 
    /**
     * 子节点处理
     * @param attributeDefByName 父节点的属性信息
     * @param allSysAttr 所有默认字段
     */
    private void addNode(AttributeDefDTO attributeDefByName, Map<String, AttributeDef> allSysAttr){
 
        if(attributeDefByName.getLayersNum() >= 3){
            return;
        }
        String abName = attributeDefByName.name;
        //取node上的最后一个属性名
        if(abName.contains(".")){
            abName = abName.substring(abName.lastIndexOf(".") + 1);
        }
        if(SystemAttribute.sysAttList().contains(abName.toUpperCase())){
            return;
        }
        AttributeDef abItem = null;
        try {
            abItem = platformClientUtil.getAttributeService().getAttributeDefByName(abName);
            String other = abItem.other;
//            String btmName = ApProvider.getInstance().getOtherValueByType(other, BTM);
            OtherInfo otherInfo = OtherInfo.getOtherInfoByText(other);
            int refFlag = otherInfo.getRefFlag();
            String refTypeName = otherInfo.getRefTypeName();
            if(refFlag != -1){
                //参照业务类型
                if(refFlag == 0){
                    //pName: 为参照属性名加上路径
                    String pName = attributeDefByName.name + ".";
                    String[] abNames = platformClientUtil.getBtmService().getAttributeNames(refTypeName);
                    for(int i = 0; i < abNames.length; i++){
                        String abName_ = abNames[i];
                        AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(abName_);
                        if(att == null || "".equals(att.oid)){
                            att = allSysAttr.get(abName_.toLowerCase());
                            if(att == null || "".equals(att.oid)){
                                continue;
                            }
                        }
                        att.name = pName + abName_ ;
                        AttributeDefDTO attrDto = new AttributeDefDTO();
                        attrDto.setDescription(att.description);
                        attrDto.setLabel(att.label);
                        attrDto.setOid(att.oid);
                        attrDto.setLayersNum(attributeDefByName.getLayersNum() + 1);
                        attrDto.setCreator(att.creator);
                        attrDto.setName(att.name);
                        attrDto.setDefValue(att.defValue);
                        attrDto.setRage(att.rage);
                        attrDto.setVtDataType(att.vtDataType);
                        attrDto.setOther(att.other);
                        attributeDefByName.getAttrs().add(attrDto);
                        addNode(attrDto, allSysAttr);
                    }
                    //系统属性ID,NAME,DESCRIPTION
                    for (int i = 0; i < SystemAttribute.bosysAttList().size(); i++) {
                        AttributeDef attributeDef = allSysAttr.get(SystemAttribute.bosysAttList().get(i).toLowerCase());
                        if(Func.isEmpty(attributeDef)){
                            break;
                        }
                        AttributeDefDTO attrDto = new AttributeDefDTO();
                        attrDto.setDescription(attributeDef.description);
                        attrDto.setLabel(attributeDef.label);
                        attrDto.setOid(attributeDef.oid);
                        attrDto.setLayersNum(attributeDefByName.getLayersNum() + 1);
                        attrDto.setCreator(attributeDef.creator);
                        attrDto.setName(pName + SystemAttribute.bosysAttList().get(i));
                        attrDto.setDefValue(attributeDef.defValue);
                        attrDto.setRage(attributeDef.rage);
                        attrDto.setVtDataType(attributeDef.vtDataType);
                        attrDto.setOther(attributeDef.other);
                        attributeDefByName.getAttrs().add(attrDto);
                        addNode(attrDto, allSysAttr);
                    }
                    //参照链接类型
                }else if(refFlag == 1){
                    //pName: 为参照属性名加上路径
                    String pName = attributeDefByName.name + ".";
                    LinkType link = null;
                    try {
                        link = platformClientUtil.getLinkTypeService().getLinkType(refTypeName);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if(link != null){
                        String[] abNames = link.attributes;
                        for(int i = 0; i < abNames.length; i++){
                            String abName_ = abNames[i];
                            AttributeDef att = platformClientUtil.getAttributeService().getAttributeDefByName(abName_);
                            if(att == null || att.oid.equals("")){
                                att = allSysAttr.get(abName_.toLowerCase());
                            }
                            AttributeDefDTO attrDto = new AttributeDefDTO();
                            attrDto.setDescription(att.description);
                            attrDto.setLabel(att.label);
                            attrDto.setOid(att.oid);
                            attrDto.setLayersNum(attributeDefByName.getLayersNum() + 1);
                            attrDto.setCreator(att.creator);
                            attrDto.setName(pName + abName_);
                            attrDto.setDefValue(att.defValue);
                            attrDto.setRage(att.rage);
                            attrDto.setVtDataType(att.vtDataType);
                            attrDto.setOther(att.other);
                            attributeDefByName.getAttrs().add(attrDto);
                            addNode(attrDto, allSysAttr);
                        }
                        //系统属性ID,NAME,DESCRIPTION
                        for (int i = 0; i < SystemAttribute.losysAttList().size(); i++) {
                            AttributeDef sysAttributeDefs = allSysAttr.get(SystemAttribute.losysAttList().get(i).toLowerCase());
                            AttributeDefDTO attrDto = new AttributeDefDTO();
                            attrDto.setDescription(sysAttributeDefs.description);
                            attrDto.setLabel(sysAttributeDefs.label);
                            attrDto.setOid(sysAttributeDefs.oid);
                            attrDto.setLayersNum(attributeDefByName.getLayersNum() + 1);
                            attrDto.setCreator(sysAttributeDefs.creator);
                            attrDto.setName(pName + SystemAttribute.losysAttList().get(i));
                            attrDto.setDefValue(sysAttributeDefs.defValue);
                            attrDto.setRage(sysAttributeDefs.rage);
                            attrDto.setVtDataType(sysAttributeDefs.vtDataType);
                            attrDto.setOther(sysAttributeDefs.other);
                            attributeDefByName.getAttrs().add(attrDto);
                            addNode(attrDto, allSysAttr);
                        }
                    }
                }
            }
        } catch (PLException e) {
            e.printStackTrace();
        }
    }
 
    // 数据过滤
    private void addImportBtmData(BtmQTExportData qtExportData, List<BizType> ltsNew, List<QTInfo> qTWrapperNew,
                               Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> norepQTDs) {
        Map<String/* 类型名称 */, QTD[]/* 查询模板定义 */> allQTDs = qtExportData
                .getAllQTDs();
        Map<String/* 类型名称 */, BizType/* 类型对象 */> selectedBtmItems = qtExportData
                .getSelectedBtmItems();
        Map<String/* 类型名称 */, String[]/* 类型所属属性 */> selectedBtmAttrs = qtExportData
                .getSelectedBtmAttrs();
        Map<String/* 查询模板名称 */, QTInfo/* 查询模板对象 */> allSelectedQTs = qtExportData
                .getAllQTs();
        if (allQTDs.size() == 0 || allQTDs == null
                || selectedBtmItems.size() == 0 || selectedBtmItems == null
                || selectedBtmAttrs.size() == 0 || selectedBtmAttrs == null
                || allSelectedQTs.size() == 0 || allSelectedQTs == null) {
            throw new VciBaseException("导入的模板数据有误!!!");
        }
        Set<Map.Entry<String, QTD[]>> pLActions = allQTDs.entrySet();
        Set<Map.Entry<String, QTInfo>> pLQts = allSelectedQTs.entrySet();
        Set<Map.Entry<String, BizType>> btmpLQts = selectedBtmItems.entrySet();
        for (Map.Entry<String, QTD[]> entry : pLActions) {
            String string = entry.getKey();
            QTD[] qtds = entry.getValue();
            getPLQtIsInDB(string, qtds, norepQTDs);
        }
        for (Map.Entry<String, BizType> entry : btmpLQts) {
            String string = entry.getKey();
            BizType bizType = entry.getValue();
            getPLQtBtmItem2(string, bizType, ltsNew);
        }
        Map<String/*类型名称*/, QTD[]/*查询模板定义*/> newNorepQTDs = getnewNorepQTDs(norepQTDs,allSelectedQTs);
        for (Map.Entry<String, QTInfo> entry : pLQts) {
            String string = entry.getKey();
            QTInfo qtWrapper = entry.getValue();
            getPLBtmQtWrapper(string, qtWrapper, ltsNew, qTWrapperNew, newNorepQTDs);
        }
 
    }
 
    /**
     * 生成导出树选择以及导入树显示
     * @param bizType
     * @param qts
     * @param tree
     */
    private void addExportTreeNode(BizType bizType/*业务类型*/,QTInfo[] qts/*查询模板对象*/,HashMap<String,Object> tree) {
        List<String> childList = new ArrayList<>();
        // 添加查询模板对象子节点
        for (QTInfo qtItem : qts) {
            //处理导入时无法进行类型判断
            int splitLength = bizType.name.indexOf("【");
            if(splitLength == -1){
                splitLength = bizType.name.length();
            }
            if (qtItem.btmName.equals(bizType.name.substring(0,
                    splitLength))) {
                childList.add(qtItem.qtName);
            }
        }
        tree.put("children",childList);
    }
 
}