ludc
2024-08-14 24fa2e63f7c155c87457980e6c656f3891132a47
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
package com.vci.web.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.client.common.oq.OQTool;
import com.vci.common.qt.object.*;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.atm.AttributeDef;
import com.vci.corba.omd.data.LinkObject;
import com.vci.corba.omd.qtm.QTD;
import com.vci.corba.omd.qtm.QTInfo;
import com.vci.dto.QTInfoDTO;
import com.vci.omd.objects.OtherInfo;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.*;
import com.vci.web.service.*;
import com.vci.web.util.DateUtil;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
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 java.util.*;
 
/**
 * 查询模板服务
 * @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);
        }
        return BaseResult.dataList(Arrays.asList(qtdArray));
    }
 
    /**
     * 查询模板
     * @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 dataMap 传输的数据对象:
     *                linkTypeName 链接类型、
     *                rdPositive 方向,true正向,false反向
     *                btmName 业务类型名称
     *                combRelaType 业务类型选择值
     *                versionValue 版本班次值
     *                isQueryIsLeaf 是否选择下级
     *                level 子节点层数
     * @return 查询结果
     */
    @Override
    public BaseResult getCriteria(HashMap<String,Object> dataMap) throws PLException {
        QueryTemplate qt = getQT(dataMap);
        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);
        List<QTInfoDTO> dtos = new ArrayList<>();
        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));
            }
            qtInfoDTO.setQueryTemplate(OQTool.getQTByDoc(DocumentHelper.parseText(obj.qtText), obj.qtName));
            qtInfoDTO.setCreateTimeText(DateFormatUtils.format(new Date(obj.createTime), DateUtil.PATTERN_DATETIME));
            dtos.add(qtInfoDTO);
        }
        return BaseResult.dataList(dtos);
    }
 
    /**
     * 将客服端界面显示的xml转为hashMap
     * @param obj 界面对象
     * @return
     * @throws DocumentException
     */
    private HashMap<String,Object> analysisXml(QTInfo obj) throws DocumentException {
        org.dom4j.Document document = DocumentHelper.parseText(obj.qtUIText);
        if(document == null){
            return null;
        }
        //获取根节点,在例子中就是responsedata节点
        Element root = document.getRootElement();
        List<HashMap<String,Object>> treeList = new ArrayList<>();
        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));
            }else {
                childList.add(child.getText().trim());
            }
        }
        treeMap.put("child",childList);
        return treeMap;
    }
 
    /**
     * 将子节点转为map结构
     * @param element
     */
    public Map<String, Object> addDefaultMutableTree(Element element){
        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));
            }else {
                childList.add(child.getText().trim());
            }
        }
        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);
        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("保存查询模板失败");
        }
    }
 
    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();
 
    }
    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) throws PLException {
        QueryTemplate qt = new QueryTemplate();
        qt.setType(QTConstants.TYPE_LINK);
        //TODO String qtId =
        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());
        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);
 
//            OQTool.parseTreeToDoc(seniorTree);
        }
        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 QueryTemplate getQT(HashMap<String,Object> dataMap){
        QueryTemplate qt = new QueryTemplate();
        qt.setType(QTConstants.TYPE_LINK);
        //TODO String qtId =
        qt.setLinkType((String) dataMap.get("linkTypeName"));
        qt.setDirection( (Boolean)dataMap.get("rdPositive") ? QTConstants.DIRECTION_POSITIVE : QTConstants.DIRECTION_OPPOSITE);
        qt.setBtmType((String) dataMap.get("btmName"));
        if("所有类型".equals(dataMap.get("combRelaType"))){
            qt.setBtmType("*");
        }
        qt.setVersion(getVersion((String) dataMap.get("versionValue")));
        qt.setQueryISLeaf((Boolean) dataMap.get("isQueryIsLeaf"));
        qt.setLevel(StringUtils.isBlank((CharSequence) dataMap.get("level")) ? 1 : Integer.valueOf(String.valueOf(dataMap.get("level"))));
        List<String> clauseList = new ArrayList<String>();
        //TODO 查询列 显示列
        clauseList.add("*");
        qt.setClauseList(clauseList);
//        Condition con = new Condition();
//        qt.setCondition(con);
//        HashMap<String, ConditionItem> ciMap = getCIMap();
//        con.setCIMap(ciMap);
//        con.setRootCIName(con.getRootCINameByCIMap(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[]{"该模板定义名已经被使用, 请更换"});
        }
    }
}