ludc
2023-03-30 1dc8a3226e9ccb8b35567a0a55495d53b6352703
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
package com.vci.ubcs.code.service.impl;
 
import com.alibaba.cloud.commons.io.FileUtils;
import com.vci.ubcs.code.constant.MdmDuckingConstant;
import com.vci.ubcs.code.constant.MdmEngineConstant;
import com.vci.ubcs.code.mapper.DockingPreApplyDataMapper;
import com.vci.ubcs.code.mapper.DockingPreApplyDataInfoMapper;
import com.vci.ubcs.code.lifecycle.CodeDefaultLC;
import com.vci.ubcs.code.entity.DockingPreApplyDataDO;
import com.vci.ubcs.code.entity.DockingPreApplyDataInfoDO;
import com.vci.ubcs.code.utils.AttributeMapConfig;
import com.vci.ubcs.code.utils.EnumVO;
import com.vci.starter.poi.bo.SheetDataSet;
import com.vci.starter.poi.bo.SheetRowData;
import com.vci.starter.poi.bo.WriteExcelData;
import com.vci.starter.poi.bo.WriteExcelOption;
import com.vci.starter.poi.util.ExcelUtil;
import com.vci.starter.revision.service.RevisionModelUtil;
import com.vci.starter.web.constant.QueryOptionConstant;
import com.vci.starter.web.enumpck.ResultCodeEnum;
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.pagemodel.PageHelper;
import com.vci.starter.web.util.*;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
import com.vci.ubcs.code.service.*;
import com.vci.ubcs.code.vo.pagemodel.*;
import com.vci.web.pageModel.BatchCBO;
import com.vci.web.pageModel.KeyValue;
import com.vci.web.pageModel.UITableFieldVO;
import com.vci.web.redis.RedisService;
import com.vci.web.service.WebBoServiceI;
import com.vci.web.util.WebUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vci.ubcs.code.dto.CodeOrderDTO;
import com.vci.ubcs.code.dto.DockingPreApplyDataDTO;
import com.vci.ubcs.code.dto.DockingPreApplyDataInfoDTO;
import com.vci.ubcs.code.dto.PreApplyCodeOrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import plm.bs.bom.clientobject.ClientBusinessObject;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
import static com.vci.frameworkcore.constant.FrameWorkBusLangCodeConstant.*;
 
/**
 * 记录工艺推送过来的数据信息服务
 * @author weidy
 * @date 2022-04-05
 */
@Service
public class DockingPreApplyDataServiceImpl implements DockingPreApplyDataServiceI {
 
    /**
    * 日志
    */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
    * 数据操作层
    */
    @Resource
    private DockingPreApplyDataMapper dockingPreApplyDataMapper;
 
    /**
     * 数据操作层
     */
    @Resource
    private DockingPreApplyDataInfoMapper dockingPreApplyDataInfoMapper;
 
    /**
    * 业务类型操作的服务
    */
    @Autowired
    @Lazy
    private WebBoServiceI boService;
 
    /**
     * 对象的操作
     */
    @Autowired
    private DockingPreApplyDataInfoServiceI dockingPreApplyDataInfoServiceI;
 
    /**
     * 模板的服务
     */
    @Autowired
    private CodeClassifyTemplateServiceI templateService;
    /**
     * 分类操作服务
     */
    @Autowired
    private CodeClassifyServiceImpl codeClassifyService;
 
    /**
    * 对象的操作
    */
    @Autowired
    private RevisionModelUtil revisionModelUtil;
 
    /**
     * 对象的操作
     */
    @Autowired
    private CodeClassifyServiceI codeClassifyServiceI;
 
    /**
     * 预申请数据,属性映射模板存放路径
     */
    @Value("${docking.templateDir:D:\\desktop}")
    public String preApplyTemplate;
 
    @Value("${docking.tuhao:tuhao}")
    public String tuhao;
    /**
     * 主数据引擎服务
     */
    @Autowired(required = false)
    @Lazy
    private MdmEngineServiceI engineService;
 
    /**
     * 主数据引擎服务
     */
    @Autowired(required = false)
    @Lazy
    private MdmIOServiceI mdmIOService;
 
    @Autowired
    private RedisService redisService;
 
    @Autowired
    private DockingPreAttrMappingSeviceI dockingPreAttrMappingSeviceI;
 
    /***
     * 获取MAP枚举值
     */
 
    @Autowired
    private AttributeMapConfig attributeMapConfig;
 
    /**
     * 批量数据对象转换为显示对象
     * @param dockingPreApplyDataDOs 数据对象列表
     * @return 显示对象
     * @throws VciBaseException 参数为空或者不存在的时候会抛出异常
     */
    public List<DockingPreApplyDataVO> dockingPreApplyDataDO2VOs(Collection<DockingPreApplyDataDO> dockingPreApplyDataDOs, boolean iscontainAttr) throws VciBaseException {
        List<DockingPreApplyDataVO> voList = new ArrayList<DockingPreApplyDataVO>();
        if(!CollectionUtils.isEmpty(dockingPreApplyDataDOs)){
            for(DockingPreApplyDataDO s: dockingPreApplyDataDOs){
                DockingPreApplyDataVO vo =  dockingPreApplyDataDO2VO(s,iscontainAttr);
                if(vo != null){
                    voList.add(vo);
                }
            }
        }
        return voList;
    }
 
    private  DockingPreApplyDataVO dockingPreApplyDataDO2VO(DockingPreApplyDataDO dockingPreApplyDataDO,boolean iscontainAttr) throws VciBaseException{
        DockingPreApplyDataVO vo = new DockingPreApplyDataVO();
        if(dockingPreApplyDataDO != null){
            BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataDO,vo);
            //如果有lcstatus的类的话
           if(iscontainAttr) {
               List<DockingPreApplyDataInfoDO> dockingPreApplyDataInfoDOS = redisService.getCacheList(vo.getOid() + "_attr");
               if (CollectionUtils.isEmpty(dockingPreApplyDataInfoDOS)) {
                   //查询数据的属性对应值信息
                   VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(DockingPreApplyDataInfoDO.class);
                   queryWrapperForDO.addQueryMap("dataoid",dockingPreApplyDataDO.getOid());
                   dockingPreApplyDataInfoDOS= boService.selectByQueryWrapper(queryWrapperForDO,DockingPreApplyDataInfoDO.class);
               }
               List<DockingPreApplyDataInfoVO> dockingPreApplyDataInfoVOS = dockingPreApplyDataInfoServiceI.dockingPreApplyDataInfoDO2VOs(dockingPreApplyDataInfoDOS);
               vo.setDockingPreApplyDataInfoVOs(dockingPreApplyDataInfoVOS);
           }
        }
        return vo;
    }
 
 
    @Override
    public List<DockingPreApplyDataVO> batchSetDataClassId(DockingPreApplyDataDTO dockingPreApplyDataDTO) throws VciBaseException {
        VciBaseUtil.alertNotNull(dockingPreApplyDataDTO,"数据对象",dockingPreApplyDataDTO.getOid(),"记录工艺推送过来的数据信息主键");
        List<String> oidList=  VciBaseUtil.str2List(dockingPreApplyDataDTO.getOid());
        String classOid=dockingPreApplyDataDTO.getClassifyoid();
        List<DockingPreApplyDataDO> dockingPreApplyDataDOS=dockingPreApplyDataMapper.selectByPrimaryKeyCollection(oidList);
        dockingPreApplyDataDOS.stream().forEach(dockingPreApplyDataDO -> {
            dockingPreApplyDataDO.setClassifyoid(classOid);
        });
        dockingPreApplyDataMapper.batchUpdate(dockingPreApplyDataDOS);
        List<DockingPreApplyDataVO> dockingPreApplyDataVOS= dockingPreApplyDataDO2VOs(dockingPreApplyDataDOS);
        return dockingPreApplyDataVOS;
    }
 
    /***
     * 返回信息
     * @param codeClassifyOid
     * @param templateOid
     * @param btmType
     * @param dataOids
     * @return
     * @throws VciBaseException
     */
    @Override
    public DataGrid<Map<String, String>> gridApplyData(String codeClassifyOid, String templateOid, String btmType, String dataOids) throws VciBaseException {
        VciBaseUtil.alertNotNull(codeClassifyOid,"数据对象",codeClassifyOid,"分类主键");
        VciBaseUtil.alertNotNull(templateOid,"数据对象",templateOid,"分类模板");
        VciBaseUtil.alertNotNull(dataOids,"数据对象",dataOids,"数据主键");
        CodeClassifyVO topClassifyVO = codeClassifyService.getTopClassifyVO(codeClassifyOid);
        String btmTypeId = topClassifyVO.getBtmtypeid();
        if (StringUtils.isBlank(btmTypeId)) {
            return new DataGrid<>("这个分类所属顶层分类没有添加业务类型");
        }
        boolean iccheckHasChild=codeClassifyService.checkHasChild(codeClassifyOid);
        if(iccheckHasChild){
            throw new VciBaseException("请选择叶子分类节点申请数据!");
        }
        CodeClassifyTemplateVO templateVO= engineService.getUsedTemplateByClassifyOid(codeClassifyOid);
        Map<String, String> classifyDevlevMap= engineService.previewClassify(codeClassifyOid,templateOid);
        List<CodeClassifyTemplateAttrVO> templateAttrVOS=templateVO.getAttributes();
        Map<String,CodeClassifyTemplateAttrVO> attrTemplateAttrMap=templateAttrVOS.stream().collect(Collectors.toMap(s -> s.getId(), t -> t, (o1, o2) -> o2));
        Map<String,String> condtionMappingMap=new HashMap<>();
        condtionMappingMap.put("jviewModeId", QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(VciBaseUtil.str2List(templateOid).toArray(new String[]{})) + ")");
        //condtionMappingMap.put("jSourceClsfId",codeClassifyOid);
        //获取属性映射关系
        List<DockingPreAttrMappingVO> dockingPreAttrMappingVOList=dockingPreAttrMappingSeviceI.selectAttrMappings(condtionMappingMap);
        if(CollectionUtils.isEmpty(dockingPreAttrMappingVOList)){
            return new DataGrid<>("未获取到相关属性映射配置");
        }
        List<DockingPreApplyDataDO> dockingPreApplyDataDOS=this.dockingPreApplyDataMapper.selectByPrimaryKeyCollection(VciBaseUtil.str2List(dataOids));
        List<DockingPreApplyDataVO> dockingPreApplyDataVOS= dockingPreApplyDataDO2VOs(dockingPreApplyDataDOS,true);
        List<Map> maps=new ArrayList<>();
 
        dockingPreApplyDataVOS.stream().forEach(dockingPreApplyDataVO -> {
            List<DockingPreApplyDataInfoVO> dockingPreApplyDataInfoVOS=  dockingPreApplyDataVO.getDockingPreApplyDataInfoVOs();
            Map<String/**oid**/, DockingPreApplyDataInfoVO/**对象**/> attrMappingMap=new HashMap<>();
            if(!CollectionUtils.isEmpty(dockingPreApplyDataInfoVOS)) {
               attrMappingMap = dockingPreApplyDataInfoVOS.stream().collect(Collectors.toMap(s -> s.getKey(), t -> t, (o1, o2) -> o2));
            }
            Map<String,String> attributMap=new HashMap<>();
            Map<String, DockingPreApplyDataInfoVO> finalAttrMappingMap = attrMappingMap;
            dockingPreAttrMappingVOList.stream().forEach(dockingPreAttrMappingVO -> {
                String innerName=dockingPreAttrMappingVO.getJsourceAttrKey();
                String jtargetAttrKey=dockingPreAttrMappingVO.getJtargetAttrKey().toLowerCase(Locale.ROOT);
                final String[] defaulValue = {dockingPreAttrMappingVO.getJdefaultValue()};
                CodeClassifyTemplateAttrVO templateAttr= attrTemplateAttrMap.get(innerName);
 
                if(innerName.equalsIgnoreCase(tuhao.toLowerCase(Locale.ROOT))){//属性映射中是图号的应当单独加
                    attributMap.put(tuhao,dockingPreApplyDataVO.getUniquecode());
                }else{
                    if (StringUtils.isNotBlank(jtargetAttrKey) && finalAttrMappingMap.containsKey(jtargetAttrKey)) {
                        DockingPreApplyDataInfoVO dataInfoVO = finalAttrMappingMap.get(jtargetAttrKey);
                        String value = dataInfoVO.getValue();
                        List<DockingPreAttrRangeVO> dockingPreAttrRangeVOList = dockingPreAttrMappingVO.getDockingPreAttrRangeVOS();
                        //Map<String/**集成属性取值范围的内部值**/, String/**主数据属性取值范围内部值**/> attrNameIdMap=new HashMap<>();
                        if (StringUtils.isNotBlank(value)) {
                            defaulValue[0] = value;
                        }
                        if (!CollectionUtils.isEmpty(dockingPreAttrRangeVOList)) {
                            // attrNameIdMap = dockingPreAttrRangeVOList.stream().collect(Collectors.toMap(s -> s.getJtargeNumTextValue(), t -> t.getJnumTextValue().toLowerCase(Locale.ROOT),(o1, o2)->o2));
                            dockingPreAttrRangeVOList.stream().forEach(dockingPreAttrRangeVO -> {
                                String jtargeNumTextValue = dockingPreAttrRangeVO.getJtargeNumTextValue();
                                List<String> valueList = VciBaseUtil.str2List(jtargeNumTextValue);
                                if (valueList.contains(value)) {
                                    defaulValue[0] = dockingPreAttrRangeVO.getJnumTextValue();
                                }
                            });
                        }
                    }
                    //判断模板属性设置规则默认值
                    if (StringUtils.isBlank(defaulValue[0]) && templateAttr != null && StringUtils.isNotBlank(templateAttr.getClassifyinvokelevel())) {
                        defaulValue[0] = classifyDevlevMap.get(innerName);
                    }
 
                    //判断模板属性默认值
                    if (StringUtils.isBlank(defaulValue[0]) && templateAttr != null && StringUtils.isNotBlank(templateAttr.getDefaultvalue())) {
                        defaulValue[0] = templateAttr.getDefaultvalue();
                    }
                    if (((StringUtils.isNotBlank(templateAttr.getEnumString())
                            && !"[]".equalsIgnoreCase(templateAttr.getEnumString())) ||
                            StringUtils.isNotBlank(templateAttr.getEnumid()))) {
                        UITableFieldVO fieldVO = engineService.templateAttr2TableField(templateAttr, false);
                        List<KeyValue> keyValueList = fieldVO.getData();
                        String text = "";
                        if (!CollectionUtils.isEmpty(keyValueList)) {
                            Map<String, String> enumMap = keyValueList.stream().collect(Collectors.toMap(s -> s.getKey(), t -> t.getValue().toLowerCase(Locale.ROOT), (o1, o2) -> o2));
                            if (enumMap.containsKey(defaulValue[0])) {
                                text = enumMap.get(defaulValue[0]);
                            }
                        }
                        attributMap.put(fieldVO.getField(), text);
                    }
                    attributMap.put(innerName, defaulValue[0]);
                }
            });
            maps.add(attributMap);
        });
        DataGrid<Map<String, String>> dataGrid = new DataGrid<>();
        List<Map<String, String>> dataList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(maps)) {
            maps.stream().forEach(map -> {
                Map<String, String> data = new HashMap<>();
                map.forEach((key, value) -> {
                    data.put((String) key, (String) value);
                });
                dataList.add(data);
            });
        }
        dataGrid.setData(dataList);
        return dataGrid;
    }
 
    /**
     * 批量数据对象转换为显示对象
     * @param dockingPreApplyDataDOs 数据对象列表
     * @return 显示对象
     * @throws VciBaseException 参数为空或者不存在的时候会抛出异常
     */
    @Override
    public List<DockingPreApplyDataVO> dockingPreApplyDataDO2VOs(Collection<DockingPreApplyDataDO>  dockingPreApplyDataDOs) throws VciBaseException{
        List<DockingPreApplyDataVO> voList = new ArrayList<DockingPreApplyDataVO>();
        if(!CollectionUtils.isEmpty(dockingPreApplyDataDOs)){
           for(DockingPreApplyDataDO s: dockingPreApplyDataDOs){
                DockingPreApplyDataVO vo =  dockingPreApplyDataDO2VO(s);
                if(vo != null){
                    voList.add(vo);
                }
            }
        }
        return voList;
    }
 
    /**
     * 数据对象转换为显示对象
     * @param  dockingPreApplyDataDO 数据对象
     * @return 显示对象
     * @throws VciBaseException 拷贝属性出错的时候会抛出异常
     */
    @Override
    public  DockingPreApplyDataVO dockingPreApplyDataDO2VO(DockingPreApplyDataDO dockingPreApplyDataDO) throws VciBaseException{
              DockingPreApplyDataVO vo = new DockingPreApplyDataVO();
        if(dockingPreApplyDataDO != null){
            BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataDO,vo);
            //如果有lcstatus的类的话
 
        }
        return vo;
    }
 
    /**
     * 增加记录工艺推送过来的数据信息
     * @param dockingPreApplyDataDTO 记录工艺推送过来的数据信息数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public DockingPreApplyDataVO addSave(DockingPreApplyDataDTO dockingPreApplyDataDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(dockingPreApplyDataDTO,"需要添加的数据对象");
        //将DTO转换为DO
        DockingPreApplyDataDO dockingPreApplyDataDO = new DockingPreApplyDataDO();
        BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataDTO,dockingPreApplyDataDO);
        dockingPreApplyDataMapper.insert(dockingPreApplyDataDO);
        return dockingPreApplyDataDO2VO(dockingPreApplyDataDO);
    }
 
    /**
     * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除
     * @param dockingPreApplyDataDTO 数据传输对象
     * @param dockingPreApplyDataDO 数据库中的数据对象
     * @return success为true为可以删除,false表示有数据引用,obj为true表示有下级
     */
    private BaseResult checkIsCanDeleteForDO(DockingPreApplyDataDTO dockingPreApplyDataDTO, DockingPreApplyDataDO dockingPreApplyDataDO) {
            DockingPreApplyDataDO tsDO = new DockingPreApplyDataDO();
            BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataDTO,tsDO);
        boService.checkTs(tsDO);
        if(!checkIsLinked(dockingPreApplyDataDO.getOid())) {
            return BaseResult.success();
        }else{
            return BaseResult.fail(DATA_LINKED_NOT_DELETE,new String[]{""});
        }
    }
 
    /**
    * 校验是否被引用
    * @param oid 主键
    * @throws VciBaseException 被引用的时候会抛出异常
    */
    private boolean checkIsLinked(String oid) throws VciBaseException{
        //TODO 添加需要校验引用的地方
        return false;
    }
 
    /**
     * 删除记录工艺推送过来的数据信息
     * @param dockingPreApplyDataDTO 记录工艺推送过来的数据信息数据传输对象,oid和ts需要传输
     * @return 删除结果反馈::success:成功,fail:失败
     * @throws VciBaseException 参数为空,被引用时抛出异常
     */
    @Override
    public BaseResult deleteDockingPreApplyData(DockingPreApplyDataDTO dockingPreApplyDataDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(dockingPreApplyDataDTO,"记录工艺推送过来的数据信息数据对象",dockingPreApplyDataDTO.getOid(),"记录工艺推送过来的数据信息的主键");
        DockingPreApplyDataDO dockingPreApplyDataDO = selectByOid(dockingPreApplyDataDTO.getOid());
        BaseResult baseResult = checkIsCanDeleteForDO(dockingPreApplyDataDTO,dockingPreApplyDataDO);
        if(baseResult.isSuccess()) {
                    }else{
            return baseResult;
        }
        //执行删除操作
        BatchCBO batchCBO = dockingPreApplyDataMapper.deleteByPrimaryKey(dockingPreApplyDataDO.getOid());
        return (batchCBO!=null && batchCBO.getDeleteCbos() !=null &&batchCBO.getDeleteCbos().size() > 0)?BaseResult.successMsg(DELETE_SUCCESS):BaseResult.fail(DELETE_FAIL);
    }
 
    /**
    * 主键获取记录工艺推送过来的数据信息
    * @param oid 主键
    * @return 记录工艺推送过来的数据信息显示对象
    * @throws VciBaseException 参数为空,数据不存在时会抛出异常
    */
    @Override
    public  DockingPreApplyDataVO getObjectByOid(String oid) throws VciBaseException{
        return dockingPreApplyDataDO2VO(selectByOid(oid));
    }
 
    /**
    * 主键查询数据对象
    * @param oid 主键
    * @return  数据对象
    * @throws VciBaseException 参数为空,并且数据不存在的时候会抛出异常
    */
    private DockingPreApplyDataDO selectByOid(String oid) throws VciBaseException{
        VciBaseUtil.alertNotNull(oid,"主键");
        DockingPreApplyDataDO dockingPreApplyDataDO = dockingPreApplyDataMapper.selectByPrimaryKey(oid.trim());
        if(dockingPreApplyDataDO == null || StringUtils.isBlank(dockingPreApplyDataDO.getOid())){
            throw new VciBaseException(DATA_OID_NOT_EXIST);
        }
        return dockingPreApplyDataDO;
    }
 
    /**
     * 主键批量获取记录工艺推送过来的数据信息
     * @param oidCollections 主键集合,但是受性能影响,建议一次查询不超过10000个
     * @return 记录工艺推送过来的数据信息显示对象
     * @throws VciBaseException 查询出现异常时会抛出
     */
    @Override
    public Collection<DockingPreApplyDataVO> listDockingPreApplyDataByOids(Collection<String> oidCollections) throws VciBaseException{
        VciBaseUtil.alertNotNull(oidCollections,"数据对象主键集合");
        List<DockingPreApplyDataDO> dockingPreApplyDataDOList = listDockingPreApplyDataDOByOidCollections(oidCollections);
        return dockingPreApplyDataDO2VOs(dockingPreApplyDataDOList);
    }
 
    /**
    * 使用主键集合查询数据对象
    * @param oidCollections 主键的集合
    * @return 数据对象列表
    */
    private List<DockingPreApplyDataDO> listDockingPreApplyDataDOByOidCollections(Collection<String> oidCollections){
        List<DockingPreApplyDataDO> dockingPreApplyDataDOList = new ArrayList<DockingPreApplyDataDO>();
        if(!CollectionUtils.isEmpty(oidCollections)){
            Collection<Collection<String>> oidCollectionsList = VciBaseUtil.switchCollectionForOracleIn(oidCollections);
            for(Collection<String> oids: oidCollectionsList){
                List<DockingPreApplyDataDO> tempDOList =  dockingPreApplyDataMapper.selectByPrimaryKeyCollection(oids);
                if(!CollectionUtils.isEmpty(tempDOList)){
                        dockingPreApplyDataDOList.addAll(tempDOList);
                }
            }
        }
        return  dockingPreApplyDataDOList;
    }
 
 
 
    /**
     * 参照记录工艺推送过来的数据信息列表
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 记录工艺推送过来的数据信息显示对象列表,生效的内容
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public DataGrid<DockingPreApplyDataVO> refDataGridDockingPreApplyData(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        if(conditionMap == null){
            conditionMap = new HashMap<String, String>();
        }
        return gridDockingPreApplyData(conditionMap,pageHelper);
    }
 
    //==================
 
    /**
     * 查询所有的记录工艺推送过来的数据信息
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 执行结果
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public DataGrid<DockingPreApplyDataVO> gridDockingPreApplyData(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        if (pageHelper == null) {
            pageHelper = new PageHelper(-1);
        }
        pageHelper.addDefaultDesc("createTime");
 
        //只查询启用的data,这样就不用去找最大版本号了再查询
        conditionMap.put("useddflag","true");
        List<DockingPreApplyDataDO> doList = dockingPreApplyDataMapper.selectByCondition(conditionMap,pageHelper);
        Map<String,List<EnumVO>> mpmEnumMap= this.attributeMapConfig.getMpmEnumMap();
        Map<String,List<String>> typeCodemap =new HashedMap();
        doList.stream().forEach(dockingPreApplyDataDO -> {
            String enumValue= dockingPreApplyDataDO.getParttype();
            String type=dockingPreApplyDataDO.getType();
            String code=dockingPreApplyDataDO.getNum();
            if(!CollectionUtils.isEmpty(mpmEnumMap)){
               if(mpmEnumMap.containsKey("partType")){
                   List<EnumVO>  partTypeEnumMapList= mpmEnumMap.get("partType");
                   Map<String, String> enumFieldValueMap = partTypeEnumMapList.stream().collect(Collectors.toMap(EnumVO::getEnumValue, EnumVO::getEnumText, (key1, key2) -> key2));
                   if(enumFieldValueMap.containsKey(enumValue)){
                      String enumText= enumFieldValueMap.get(enumValue);
                       dockingPreApplyDataDO.setParttype(enumText);
                   }
               }
            }
            if(StringUtils.isNotBlank(code)) {
                List<String> codeList = new ArrayList<>();
                codeList.add(code);
                if (typeCodemap.containsKey(type)) {
                    List<String> oldCodeList = typeCodemap.get(type);
                    codeList.addAll(oldCodeList);
                }
                typeCodemap.put(type, codeList);
            }
        });
        List<ClientBusinessObject> cbos=new ArrayList<>();
          if(!CollectionUtils.isEmpty(typeCodemap)) {
              typeCodemap.forEach((type,codes)->{
                  Map<String,String> contionMap=new HashedMap();
                  contionMap.put("id", QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(codes.toArray(new String[]{})) + ")");
                  List<ClientBusinessObject> newcbos=boService.queryCBO(type,contionMap);
                  cbos.addAll(newcbos);
              });
          }
        Map<String, ClientBusinessObject> codeCbosMap = cbos.stream().filter(systeDataObject -> systeDataObject != null && StringUtils.isNotBlank(systeDataObject.getId())).collect(Collectors.toList()).stream().collect(Collectors.toMap(s -> s.getId(), t -> t));
        doList.stream().forEach(s->{
            String code =s.getNum();
            if(codeCbosMap.containsKey(code)){
                ClientBusinessObject cbo=  codeCbosMap.get(code);
                if(!cbo.getLcStatus().equals(CodeDefaultLC.RELEASED.getValue())){//如果不是发布状态则隐藏编码
                    s.setNum("******");
                    s.setId("******");
                    s.setPreapplyoid("******");
                }
            }else{
 
            }
        });
 
        DataGrid<DockingPreApplyDataVO> dataGrid=new DataGrid<DockingPreApplyDataVO>();
        if (!CollectionUtils.isEmpty(doList)) {
            dataGrid.setData(dockingPreApplyDataDO2VOs(doList,false));
            dataGrid.setTotal(VciBaseUtil.getInt(String.valueOf(dockingPreApplyDataMapper.countByCondition(conditionMap))));
        }
        return dataGrid;
    }
 
    /**
     * 插入data和datainfo,会自动把data里的oid,unique赋值到datainfo里边去
     * @param dockingPreApplyDataDTO 记录工艺推送过来的数据信息数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public void addSaveDataAndInfo(DockingPreApplyDataDTO dockingPreApplyDataDTO, List<DockingPreApplyDataInfoDTO> dockingPreApplyDataInfoDTOList) throws VciBaseException{
        String unique = "";
        //将DTO转换为DO
        DockingPreApplyDataDO dockingPreApplyDataDO = new DockingPreApplyDataDO();
        BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataDTO,dockingPreApplyDataDO);
 
        String dataoid = dockingPreApplyDataDO.getOid();
        if(StringUtils.isEmpty(dockingPreApplyDataDO.getOid())) {
            dataoid = redisService.getUUIDEveryDay();
            dockingPreApplyDataDO.setOid(dataoid);
        }
 
        //查询以前的数据,准备修改usedflasg
        VciQueryWrapperForDO queryWrapperForDO = new VciQueryWrapperForDO(DockingPreApplyDataDO.class);
        queryWrapperForDO.addQueryMap("uniquecode",dockingPreApplyDataDO.getUniquecode());
        List<DockingPreApplyDataDO> dockingPreApplyDataDOList = boService.selectByQueryWrapper(queryWrapperForDO,DockingPreApplyDataDO.class);
 
        //执行数据保存操作
        WebUtil.setPersistence(false);//不执行保存
 
        BatchCBO batchUpdateData = new BatchCBO();
        //修改以前的数据useddflag=false
        if (dockingPreApplyDataDOList.size() != 0) {
            for (DockingPreApplyDataDO dockingPreApplyDataDOi : dockingPreApplyDataDOList) {
                dockingPreApplyDataDOi.setUseddflag(MdmDuckingConstant.PRE_APPLY_DATA_USER_FALSE);
            }
            batchUpdateData = dockingPreApplyDataMapper.batchUpdate(dockingPreApplyDataDOList);
        }
 
        //新增现在的data
        dockingPreApplyDataDO.setUseddflag(MdmDuckingConstant.PRE_APPLY_DATA_USER_TRUE);//是最新的
        BatchCBO batchInsertData = dockingPreApplyDataMapper.insert(dockingPreApplyDataDO);
        batchUpdateData.copyFromOther(batchInsertData);
 
        //新增dataInfo
        List<DockingPreApplyDataInfoDO> dockingPreApplyDataInfoDOList = new ArrayList<DockingPreApplyDataInfoDO>();
        for (DockingPreApplyDataInfoDTO dockingPreApplyDataInfoDTO : dockingPreApplyDataInfoDTOList) {
            DockingPreApplyDataInfoDO dockingPreApplyDataInfoDO = new DockingPreApplyDataInfoDO();
            BeanUtilForVCI.copyPropertiesIgnoreCase(dockingPreApplyDataInfoDTO, dockingPreApplyDataInfoDO);
            dockingPreApplyDataInfoDO.setDataoid(dataoid);
 
            if (StringUtils.isNotEmpty(dockingPreApplyDataDO.getUniquecode())) {
                dockingPreApplyDataInfoDO.setUniquecode(dockingPreApplyDataDO.getUniquecode());
            }
            if (StringUtils.isEmpty(dockingPreApplyDataInfoDO.getOid())) {
                String datainfooid = redisService.getUUIDEveryDay();
                dockingPreApplyDataInfoDO.setOid(datainfooid);
            }
 
            dockingPreApplyDataInfoDOList.add(dockingPreApplyDataInfoDO);
        }
 
        if(dockingPreApplyDataInfoDOList.size()!=0) {
            BatchCBO batchInsertDataInfo = dockingPreApplyDataInfoMapper.batchInsert(dockingPreApplyDataInfoDOList);
            batchUpdateData.copyFromOther(batchInsertDataInfo);
            redisService.setCacheList(dockingPreApplyDataDO.getOid(),dockingPreApplyDataInfoDOList);
        }
        WebUtil.setPersistence(true);//执行保存
        boService.persistenceBatch(batchUpdateData);//一起执行保存
    }
 
    /**
     * 修改记录工艺推送过来的数据信息
     * @param dockingPreApplyDataDTO 记录工艺推送过来的数据信息数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public BaseResult editSave(DockingPreApplyDataDTO dockingPreApplyDataDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(dockingPreApplyDataDTO,"数据对象",dockingPreApplyDataDTO.getOid(),"记录工艺推送过来的数据信息主键");
        //将DTO转换为DO
        DockingPreApplyDataDO dockingPreApplyDataDO = selectByOid(dockingPreApplyDataDTO.getOid());
        revisionModelUtil.copyFromDTOIgnore(dockingPreApplyDataDTO,dockingPreApplyDataDO);
        dockingPreApplyDataMapper.updateByPrimaryKey(dockingPreApplyDataDO);
        return BaseResult.success(dockingPreApplyDataDO2VO(dockingPreApplyDataDO));
    }
 
    /**
     * 根据data的oid查询data信息和datainfo信息,并且拼接datainfo为map
     * @param dockingPreApplyDataDTO
     * @return
     * @throws VciBaseException
     */
    @Override
    public BaseResult<Map<String,String>> getDataInfoMap(DockingPreApplyDataDTO dockingPreApplyDataDTO) throws VciBaseException{
 
        String dataoid = dockingPreApplyDataDTO.getOid();
 
        //查询data的type
        DockingPreApplyDataDO dockingPreApplyDataDO = dockingPreApplyDataMapper.selectByPrimaryKey(dataoid);
 
        if(dockingPreApplyDataDO == null){
            String msg = "预申请编码,通过oid查询DockingPreApplyDataDO未找到数据!";
            logger.error(msg);
            return BaseResult.fail(msg,null);
        }
 
        String type = dockingPreApplyDataDO.getType();
 
        //查询datainfo为map
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(DockingPreApplyDataInfoDO.class);
        queryWrapper.addQueryMap("dataoid",dataoid);
        List<DockingPreApplyDataInfoDO> dockingPreApplyDataInfoDOList = dockingPreApplyDataInfoMapper.selectByWrapper(queryWrapper);
        Map<String,String> dataInfoMap = new HashMap<String,String>();//所有的值
        for (DockingPreApplyDataInfoDO dockingPreApplyDataInfoDO :dockingPreApplyDataInfoDOList){
            dataInfoMap.put(dockingPreApplyDataInfoDO.getKey(),dockingPreApplyDataInfoDO.getValue());
        }
//把datainfomap根据配置的模板xml转化为最终存入的数据map
//        String systemid = dockingPreApplyDataDO.getSystemid();//系统编号
//
//        PreApplyDataTrans preApplyDataTrans = getPreApplyDataTransBySystemIdAndType(systemid,type);
//        if(preApplyDataTrans == null){
//            String msg = "读取预申请数据配置模板成功,但是没有匹配的模板属性,systemid: "+systemid+",type: "+type;
//            logger.error(msg);
//            preApplyDataTrans = new PreApplyDataTrans(systemid,type,new HashMap<String,String>());
//        }
//
//        Map<String, String> attrMap = preApplyDataTrans.getAttrMap();
//
//        Map<String, String> outMap = getShowMap(attrMap,dataInfoMap);
 
        return BaseResult.success(dataInfoMap,"查询成功");
    }
 
    /**
     * 根据systemid查询配置的属性模板
     * @param systemid  系统id
     * @param type  分类
     * @return
     */
    public PreApplyDataTrans getPreApplyDataTransBySystemIdAndType(String systemid,String type){
        String templateXml = null;
        String fileUrl = "";
        try {
            String templatename = systemid;
            fileUrl = preApplyTemplate+ File.separator+templatename+".xml";
            templateXml = FileUtils.readFileToString(new File(fileUrl),"utf-8");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("读取预申请属性映射模板失败,fileUrl: "+fileUrl);
            return null;
        }
 
        /**
         * 通过配置的模板文件,转换为前端读懂和mdmEngineController/getDataByOid接口类似的数据
         */
        List<PreApplyDataTrans>  preApplyDataTransList = null;
        try {
            preApplyDataTransList = readAttrTransfTemplate(templateXml);
        }catch (Exception e){
            logger.error("读取预申请数据配置模板失败,");
            return null;
        }
 
        PreApplyDataTrans preApplyDataTrans = null;
        for(PreApplyDataTrans preApplyDataTransi:preApplyDataTransList){
            String preApplyDataTrans_type = preApplyDataTransi.getType();
            if(type.equals(preApplyDataTrans_type)){
                preApplyDataTrans = preApplyDataTransi;
                break;
            }
        }
        return preApplyDataTrans;
    }
 
    /**
     * 把预申请的属性根据模板xml转化为最终的属性map
     * @param attrMap
     * @param dataInfoMap
     * @return
     */
    public Map<String, String> getShowMap(Map<String, String> attrMap,Map<String, String> dataInfoMap){
        Map<String, String> outMap = new HashMap<String, String>();//转换的key后的map
        for (String systemkey:attrMap.keySet()){
            String mdmKey = attrMap.getOrDefault(systemkey,"");
            String value = dataInfoMap.getOrDefault(mdmKey,"");
            if(StringUtils.isNotEmpty(value)){
                outMap.put(systemkey,value);
            }
        }
        return outMap;
    }
    /**
     * 批量处理预申请编码
     * 必须要有分类oid和data
     */
    @Override
    public BaseResult applyData(List<PreApplyCodeOrderDTO> preApplyCodeOrderDTOList) {
 
        List<Map> retMapList = new ArrayList<Map>();//保存返回的错误oid和信息
        for (PreApplyCodeOrderDTO preApplyCodeOrderDTO : preApplyCodeOrderDTOList) {
 
            CodeOrderDTO codeOrderDTO = new CodeOrderDTO();
            try {
                //根据分类oid查询模板oid,编码规则oid
                String codeClassfyOid = preApplyCodeOrderDTO.getCodeClassifyOid();
 
                //查询data的type
                String dataoid = preApplyCodeOrderDTO.getDataoid();
                DockingPreApplyDataDO dockingPreApplyDataDO = dockingPreApplyDataMapper.selectByPrimaryKey(dataoid);
 
                //查询编码规则oid
                String ruleOid = null;
                try {
                    CodeRuleVO codeRuleVO = engineService.getCodeRuleByClassifyOid(codeClassfyOid);
                    ruleOid = codeRuleVO.getOid();
                }catch (Exception e){
                    Map retMap = new HashMap<String,String>();
                    retMap.put("oid",codeOrderDTO.getOid());
                    retMap.put("msg","选择的分类以及上级分类没有配置编码规则");
                    retMapList.add(retMap);
                    logger.error("批量处理预申请数据失败,数据: "+preApplyCodeOrderDTO.toString()+",msg: 根据分类oid查询编码规则失败,分类oid: "+codeClassfyOid);
                    continue;
                }
 
                //查询使用的模板oid
                String templateOid = null;
                try {
                    CodeClassifyTemplateVO codeClassifyTemplateVO = engineService.getUsedTemplateByClassifyOid(codeClassfyOid);
                    templateOid = codeClassifyTemplateVO.getOid();
                }catch (Exception e){
                    Map retMap = new HashMap<String,String>();
                    retMap.put("oid",codeOrderDTO.getOid());
                    retMap.put("msg","选择的分类以及上级分类没有配置配置模板");
                    retMapList.add(retMap);
                    logger.error("批量处理预申请数据失败,数据: "+preApplyCodeOrderDTO.toString()+",msg: 根据分类oid查询使用的模板失败,分类oid: "+codeClassfyOid);
                    continue;
                }
 
                //根据dataoid查询dataInfoMap
                Map dataInfoMap = null;
 
                DockingPreApplyDataDTO dockingPreApplyDataDTO = new DockingPreApplyDataDTO();
                dockingPreApplyDataDTO.setOid(preApplyCodeOrderDTO.getDataoid());
                BaseResult baseResult_dataMap = getDataInfoMap(dockingPreApplyDataDTO);
                if(ResultCodeEnum.SUCCESS.code==baseResult_dataMap.getCode()){
                    Object datao = baseResult_dataMap.getObj();
                    if(datao!=null){
                        dataInfoMap = (Map<String, String>)datao;
                    }
                }
                if(dataInfoMap==null){
                    Map retMap = new HashMap<String,String>();
                    retMap.put("oid",codeOrderDTO.getOid());
                    retMap.put("msg","查询到接收的预申请数据为空");
                    retMapList.add(retMap);
                    logger.error("批量处理预申请数据失败,数据: "+preApplyCodeOrderDTO.toString()+",msg: 根据DockingPreApplyData_oid查询数据接收的预申请数据失败,DockingPreApplyData_oid: "+preApplyCodeOrderDTO.getDataoid());
                    continue;
                }
 
                //把datainfomap根据配置的模板xml转化为最终存入的数据map
                String systemid = dockingPreApplyDataDO.getSystemid();//系统编号
                String type = dockingPreApplyDataDO.getType();//type
 
                PreApplyDataTrans preApplyDataTrans = getPreApplyDataTransBySystemIdAndType(systemid,type);
                if(preApplyDataTrans == null){
                    String msg = "读取预申请数据配置模板成功,但是没有匹配的模板属性,systemid: "+systemid+",type: "+type;
                    logger.error(msg);
                    preApplyDataTrans = new PreApplyDataTrans(systemid,type,new HashMap<String,String>());
                }
 
                Map<String, String> attrMap = preApplyDataTrans.getAttrMap();
 
                Map<String, String> data = getShowMap(attrMap,dataInfoMap);
                if(data.size()==0){
                    Map retMap = new HashMap<String,String>();
                    retMap.put("oid",codeOrderDTO.getOid());
                    retMap.put("msg","接收的预申请数据根据配置的模板xml转化属性失败");
                    retMapList.add(retMap);
                    logger.error("批量处理预申请数据失败,数据: "+preApplyCodeOrderDTO.toString()+",msg: 接收的预申请数据根据配置的模板xml转化属性失败!");
                    continue;
                }
 
                String name = data.getOrDefault("name","");
 
                //执行数据保存
                codeOrderDTO.setCodeClassifyOid(codeClassfyOid);
                codeOrderDTO.setTemplateOid(templateOid);
                codeOrderDTO.setCodeRuleOid(ruleOid);
                codeOrderDTO.setSecDTOList(preApplyCodeOrderDTO.getSecDTOList());//码段
                codeOrderDTO.setEditInProcess(preApplyCodeOrderDTO.isEditInProcess());
                codeOrderDTO.setUpVersion(preApplyCodeOrderDTO.isUpVersion());
                codeOrderDTO.setName(name);
                codeOrderDTO.setData(data);
                engineService.addSaveCode(codeOrderDTO);
 
                //再修改data里边的classifyoid和classifuname
                String classifyoid = preApplyCodeOrderDTO.getCodeClassifyOid();
                //查询分类
                CodeClassifyVO codeClassifyVO = codeClassifyServiceI.getObjectByOid(classifyoid);
                //设置分类oid,name,flag
                dockingPreApplyDataDO.setClassifyoid(codeClassifyVO.getOid());
                dockingPreApplyDataDO.setClassifyname(codeClassifyVO.getName());
                dockingPreApplyDataMapper.updateByPrimaryKey(dockingPreApplyDataDO);
            } catch (Exception e) {
                Map retMap = new HashMap<String,String>();
                retMap.put("oid",preApplyCodeOrderDTO.getDataoid());
                retMap.put("msg","处理失败!");
                retMapList.add(retMap);
                logger.error("批量处理预申请数据失败,数据: "+preApplyCodeOrderDTO.toString()+",msg: "+e.getMessage());
            }
        }
        BaseResult result = BaseResult.success(retMapList);
        return result;
    }
 
    @Override
    public BaseResult batchApplyDatas(List<CodeOrderDTO> codeOrderDTOs) {
       if(!CollectionUtils.isEmpty(codeOrderDTOs)){
           CodeOrderDTO orderDTO=codeOrderDTOs.get(0);
           Map<String,String> rowsOidMap=new HashMap<>();
           SheetDataSet dataSet=new SheetDataSet();
           List<SheetRowData> sheetRowDatas=new ArrayList<>();
           CodeClassifyTemplateVO codeClassifyTemplateVO = engineService.getUsedTemplateByClassifyOid(orderDTO.getCodeClassifyOid());
           List<String> colName=new ArrayList<>();
           List<CodeClassifyTemplateAttrVO> templateAttrVOS=codeClassifyTemplateVO.getAttributes();
           List<CodeClassifyTemplateAttrVO> attrVOS = templateAttrVOS.stream().filter(s -> !MdmEngineConstant.DEFAULT_SYNC_ATTR_LIST.contains(s.getId()) && VciBaseUtil.getBoolean(s.getFormdisplayflag())
           ).collect(Collectors.toList());
           Map<String/**英文名称**/, String/**中文名称**/> attrNameIdMap = attrVOS.stream().collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t.getName(),(o1, o2)->o2));
           for (int i=0;i<codeOrderDTOs.size();i++) {
               CodeOrderDTO codeOrderDTO=codeOrderDTOs.get(i);
               String dataoid = codeOrderDTO.getOid();
               rowsOidMap.put(i+"",dataoid);//存储数据oid,一遍设置
 
               SheetRowData sheetRowData=new SheetRowData();
               Map<String, String> dataMap= codeOrderDTO.getData();
               sheetRowData.setRowIndex(i+"");
               Map<Integer,String> indexValueMap=new HashMap<>();
               int index=0;
               colName=new ArrayList<>();
               for (String field: attrNameIdMap.keySet()) {
                   String outAttrName=attrNameIdMap.get(field);
                   colName.add(outAttrName);
                   String value="";
                  if(dataMap.containsKey(field)){
                      value=StringUtils.isBlank(dataMap.get(field))?"":dataMap.get(field);
                  }else{
                      value="";
                  }
                   indexValueMap.put(index++,value);
               }
               sheetRowData.setData(indexValueMap);
               sheetRowDatas.add(sheetRowData);
           }
           dataSet.setRowData(sheetRowDatas);
           dataSet.setColName(colName);
           Map<String,String> errorMap=new HashMap<>();
           List<String> codeList=mdmIOService.batchImportCode(orderDTO,codeClassifyTemplateVO,dataSet,errorMap,false);
           List<SheetRowData> needSaveCboList = dataSet.getRowData().stream().filter(cbo -> {
               String rowIndex =cbo.getRowIndex();
               return !errorMap.containsKey(rowIndex);
           }).collect(Collectors.toList());
           LinkedList<String> needOIdList=new LinkedList<>();
           Map<String,String> codeIdMap=new HashMap<>();
           for(int i=0;i<needSaveCboList.size();i++) {
               SheetRowData cbo = needSaveCboList.get(i);
               String rouIndex = cbo.getRowIndex();
               if (rowsOidMap.containsKey(rouIndex)) {
                   String dataOid = rowsOidMap.get(rouIndex);
                   needOIdList.add(dataOid);
                   codeIdMap.put(dataOid,codeList.get(i));
               }
           }
           if(!CollectionUtils.isEmpty(needOIdList)) {
               CodeClassifyVO codeClassifyVO = codeClassifyServiceI.getObjectByOid(orderDTO.getCodeClassifyOid());
               List<DockingPreApplyDataDO> dockingPreApplyDataDOS = dockingPreApplyDataMapper.selectByPrimaryKeys(VciBaseUtil.array2String(needOIdList.toArray(new String[]{})));
               dockingPreApplyDataDOS.stream().forEach(dockingPreApplyDataDO -> {
                   //设置分类oid,name,flag
                   String dataOid=dockingPreApplyDataDO.getOid();
                   dockingPreApplyDataDO.setClassifyoid(codeClassifyVO.getOid());
                   dockingPreApplyDataDO.setClassifyname(codeClassifyVO.getName());
                   if(codeIdMap.containsKey(dataOid)) {//存入数据编码
                       String code= codeIdMap.get(dataOid);
                       dockingPreApplyDataDO.setId(code);
                       dockingPreApplyDataDO.setPreapplyoid(code);
                       dockingPreApplyDataDO.setNum(code);
                   }
 
                   dockingPreApplyDataDO.setDatamsg("赋码成功");
                   dockingPreApplyDataDO.setUseddflag("true");
               });
               dockingPreApplyDataMapper.batchUpdate(dockingPreApplyDataDOS);
           }
           //如果有错误则导出execl
           if(errorMap.size()>0){
               List<String> needRowIndexList = new ArrayList<>();
              String errorFile=returnErrorToExcel(dataSet.getRowData(),errorMap, needRowIndexList,dataSet.getColName());
               if(StringUtils.isNotBlank(errorFile)){
                   //放到map里
                   BaseResult result = BaseResult.fail("有部分数据申请失败,请核对");
                   result.setObj(ControllerUtil.putErrorFile(errorFile));
                   return result;
               }else {
                   return BaseResult.success("申请成功!");
               }
 
           }
 
       }
 
        return BaseResult.success("申请成功!");
    }
 
    /**
     * 错误信息返回excel
     * @param rowDataList 所有的导入数据
     * @param errorMap 错误的信息
     * @param needRowIndexList 需要写入的数据的行号
     * @param titleRowData 标题行
     *
     * @return 错误的excel文件,没有错误会返回空
     */
    private String returnErrorToExcel(Collection<SheetRowData> rowDataList,
                                      Map<String,String> errorMap,
                                      List<String> needRowIndexList,List<String> titleRowData){
        if(CollectionUtils.isEmpty(errorMap)){
            return "";
        }
        Map<String, SheetRowData> rowIndexDataMap = rowDataList.stream().filter(s -> !needRowIndexList.contains(s.getRowIndex())).collect(Collectors.toMap(s -> s.getRowIndex(), t -> t));
        LinkedList<WriteExcelData> errorDataList = new LinkedList<>();
        errorDataList.add(new WriteExcelData(0,0,"错误信息"));
        for (int i = 0; i < titleRowData.size(); i++) {
            //错误信息在最后
            errorDataList.add(new WriteExcelData(0,i+1,titleRowData.get(i)));
        }
        Integer[] newRowIndex = new Integer[]{1};
        errorMap.forEach((index,error)->{
            //错误信息全部组合到一起
            SheetRowData rowData = rowIndexDataMap.getOrDefault(index, null);
            if(rowData!=null){
                errorDataList.add(new WriteExcelData(newRowIndex[0],0,error));
                rowData.getData().forEach((colIndex,value)->{
                    errorDataList.add(new WriteExcelData(newRowIndex[0],colIndex+1,value));
                });
                newRowIndex[0]++;
            }
        });
        String excelFileName = LocalFileUtil.getDefaultTempFolder() + File.separator + "错误信息.xls";
        WriteExcelOption eo = new WriteExcelOption(errorDataList);
        try {
            new File(excelFileName).createNewFile();
        } catch (IOException e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e));
        }
        ExcelUtil.writeDataToFile(excelFileName,eo);
        return excelFileName;
    }
 
    /**
     * 解析预申请数据和分类的属性转换模板ss
     */
    public List<PreApplyDataTrans> readAttrTransfTemplate(String templateXml) throws Exception{
        templateXml = StringUtils.isEmpty(templateXml)?"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<datas systemid=\"oa0001\">\n" +
                "\t<data type=\"wupin\">\n" +
                "\t\t<attr systemkey=\"code01\" mdmkey=\"key01\"/>\n" +
                "\t\t<attr systemkey=\"code02\" mdmkey=\"key02\"/>\n" +
                "\t</data>\n" +
                "</datas>":templateXml;
 
        List<PreApplyDataTrans> preApplyDataTransList = new ArrayList<PreApplyDataTrans>();
        Document document = DocumentHelper.parseText(templateXml);
        Element datas_element = document.getRootElement();
        List<Element> data_elements = datas_element.elements(MdmDuckingConstant.XML_DATA);
        for (Element data_element:data_elements){
            Attribute systemid_attr = data_element.attribute(MdmDuckingConstant.XML_SYSTEMID);
            String systemid = systemid_attr==null?"":systemid_attr.getValue();//systemid
 
            Attribute type_attr = data_element.attribute(MdmDuckingConstant.XML_TYPE);
            String type = type_attr==null?"":type_attr.getValue();//type
 
            List<Element> attr_elements = data_element.elements(MdmDuckingConstant.XML_ATTR);
            Map<String,String> attrMap = new HashMap<String,String>();
            for (Element attr_element:attr_elements){
                Map<String,Object> dataMap = new HashMap<String,Object>();
 
                Attribute systemkey_attr = attr_element.attribute(MdmDuckingConstant.XML_SYSTEMKEY);
                String systemkey = systemkey_attr==null?"":systemkey_attr.getValue();//systemkey
 
                Attribute mdmkey_attr = attr_element.attribute(MdmDuckingConstant.XML_MDMKEY);
                String mdmkey = mdmkey_attr==null?"":mdmkey_attr.getValue();//mdmkey
 
                attrMap.put(systemkey,mdmkey);
                PreApplyDataTrans p = new PreApplyDataTrans(systemid,type,attrMap);
                preApplyDataTransList.add(p);
            }
        }
        return preApplyDataTransList;
    }
 
 
    class PreApplyDataTrans{
        private String systemid;
        private String type;
        private Map<String,String> attrMap;
 
        public PreApplyDataTrans(String systemid, String type, Map<String, String> attrMap) {
            this.systemid = systemid;
            this.type = type;
            this.attrMap = attrMap;
        }
 
        public String getSystemid() {
            return systemid;
        }
 
        public void setSystemid(String systemid) {
            this.systemid = systemid;
        }
 
        public String getType() {
            return type;
        }
 
        public void setType(String type) {
            this.type = type;
        }
 
        public Map<String, String> getAttrMap() {
            return attrMap;
        }
 
        public void setAttrMap(Map<String, String> attrMap) {
            this.attrMap = attrMap;
        }
    }
}