yuxc
2023-05-06 a6e6575bc3d668e14009ed0e931a376f1a4d86ff
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
package com.vci.ubcs.code.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.corba.se.impl.orbutil.ObjectUtility;
import com.vci.ubcs.code.bo.CodeClassifyFullInfoBO;
import com.vci.ubcs.code.dto.CodeOrderDTO;
import com.vci.ubcs.code.dto.datapush.BaseModelDTO;
import com.vci.ubcs.code.entity.CodeAllCode;
import com.vci.ubcs.code.entity.CodeOsbtmtypeEntity;
import com.vci.ubcs.code.entity.CodeRule;
import com.vci.ubcs.code.entity.CodeWupinEntity;
import com.vci.ubcs.code.enumpack.CodeDefaultLC;
import com.vci.ubcs.code.enumpack.CodeLevelTypeEnum;
import com.vci.ubcs.code.enumpack.CodeSecTypeEnum;
import com.vci.ubcs.code.mapper.CodeAllCodeMapper;
import com.vci.ubcs.code.mapper.CodeOsbtmtypeMapper;
import com.vci.ubcs.code.mapper.CodeRuleMapper;
import com.vci.ubcs.code.service.*;
import com.vci.ubcs.code.vo.CodeClstempattrVO;
import com.vci.ubcs.code.vo.CodeClstemplateVO;
import com.vci.ubcs.code.vo.CodeKeyattrrepeatVO;
import com.vci.ubcs.code.vo.pagemodel.CodeBasicSecVO;
import com.vci.ubcs.code.vo.pagemodel.CodeClassifyVO;
import com.vci.ubcs.code.vo.pagemodel.CodeRuleVO;
import com.vci.ubcs.omd.entity.Dict;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.revision.model.BaseModel;
import com.vci.ubcs.starter.web.enumpck.UserSecretEnum;
import com.vci.ubcs.starter.web.pagemodel.KeyValue;
import com.vci.ubcs.starter.web.toolmodel.DateConverter;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import com.vci.ubcs.starter.web.util.VciDateUtil;
import com.vci.ubcs.starter.web.util.VciQueryWrapperForDO;
import com.vci.ubcs.starter.web.util.WebUtil;
import com.vci.ubcs.system.entity.DictBiz;
import com.vci.ubcs.system.feign.IDictBizClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
 
/**
 * 主数据引擎服务
 *
 * @author weidy
 * @date 2022-2-22
 */
@Service
public class MdmEngineServiceImpl extends ServiceImpl<CodeAllCodeMapper, CodeAllCode> implements MdmEngineService {
 
 
    /**
     * 模板的服务
     */
    @Resource
    private CodeClstemplateServiceImpl templateService;
    /**
     * 模板的服务
     */
    @Resource
    private CodeOsbtmtypeMapper codeOsbtmtypeMapper;
    /**
     * 生成编码的服务
     */
    @Resource
    private MdmProductCodeService productCodeService;
    /**
     * 字典的服务
     */
    @Resource
    IDictBizClient iDictBizClient;
 
    /**
     * 编码规则的服务
     */
    @Autowired
    private ICodeRuleService ruleService;
 
    /**
     * 分类的服务
     */
    @Resource
    private ICodeClassifyService classifyService;
 
    /**
     * 关键属性的配置
     */
    @Autowired
    private ICodeKeyattrrepeatService keyRuleService;
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
//    @Autowired
//    private CodeOsbtmtypeMapper codeOsbtmtypeMapper;
 
 
 
    /**
     * 使用分类的主键获取可以使用的模板对象
     *
     * @param codeClassifyOid 分类的主键
     * @return 模板的显示对象
     */
    @Override
    public CodeClstemplateVO getUsedTemplateByClassifyOid(String codeClassifyOid) {
        return getUsedTemplateByClassifyOid(codeClassifyOid, true);
    }
 
    /**
     * 使用分类的主键获取可以使用的模板对象
     *
     * @param codeClassifyOid 分类的主键
     * @param hasAttr         包含属性
     * @return 模板的显示对象
     */
    @Override
    public CodeClstemplateVO getUsedTemplateByClassifyOid(String codeClassifyOid, boolean hasAttr) {
        List<CodeClstemplateVO> templateVOs = templateService.listReleaseTemplateByClassifyOid(codeClassifyOid, hasAttr);
        return templateVOs.get(templateVOs.size() - 1);
    }
 
    /**
     * 获取枚举的下拉选项
     *
     * @param attrVO 模板属性的对象
     * @return 下拉选项
     */
    @Override
    public List<KeyValue> listComboboxItems(CodeClstempattrVO attrVO) {
        List<KeyValue> comboboxKVs = null;
        if (StringUtils.isNotBlank(attrVO.getEnumstring())) {
            comboboxKVs = JSONObject.parseArray(attrVO.getEnumstring(), KeyValue.class);
        } else {
//            comboboxKVs = enumService.getEnum(attrVO.getEnumid());
//             Dict dict = new Dict();
//            dict.setParentId(Long.valueOf(attrVO.getEnumid()));
            R<List<DictBiz>> list = iDictBizClient.getList(attrVO.getEnumid());
            if(list.isSuccess()){
                for (DictBiz datum : list.getData()) {
                    KeyValue keyValue = new KeyValue();
                    keyValue.setKey(datum.getDictKey());
                    keyValue.setValue(datum.getDictValue());
                    comboboxKVs.add(keyValue);
                }
            }
        }
        return comboboxKVs;
    }
    /**
     * 修改状态
     *
     * @param baseModelDTO 数据传输对象
     */
    @Override
    public void changeStatus(BaseModelDTO baseModelDTO) {
        VciBaseUtil.alertNotNull(baseModelDTO, "数据信息", baseModelDTO.getOid(), "主键", baseModelDTO.getBtmname(), "业务类型", baseModelDTO.getLcStatus(), "目标状态");
        List<String> oids = VciBaseUtil.str2List(baseModelDTO.getOid());
//        List<ClientBusinessObject> cboList = boService.selectCBOByOidCollection(oids, baseModelDTO.getBtmname());
        //插个点 业务类型完成后需要修改
        QueryWrapper<CodeOsbtmtypeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("BTMNAME",baseModelDTO.getBtmname());
        wrapper.in("OID",oids);
        List<CodeOsbtmtypeEntity> cboList = codeOsbtmtypeMapper.selectList(wrapper);
        //还需要修改allCode的生命周期
//        Map<String, String> conditionMap = new HashMap<>();
        QueryWrapper<CodeAllCode> allCodeWrapper = new QueryWrapper<>();
        allCodeWrapper.eq("createcodebtm",baseModelDTO.getBtmname());
        allCodeWrapper.in("createcodeoid",oids);
//        conditionMap.put("createcodeoid", QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(oids.toArray(new String[0])) + ")");
//        conditionMap.put("createcodebtm", baseModelDTO.getBtmname());
        List<CodeAllCode> codeCbos = baseMapper.selectList(allCodeWrapper);
//        List<ClientBusinessObject> codeCbos = boService.queryCBO(MdmBtmTypeConstant.CODE_ALL_CODE, conditionMap);
        // 回收需要业务数据删除
//        if (CodeDefaultLC.TASK_BACK.getValue().equals(baseModelDTO.getLcStatus())) {
////            BatchCBO batchCBO = new BatchCBO();
////            batchCBO.getDeleteCbos().addAll(cboList);
//            codeOsbtmtypeMapper.deleteBatchIds(cboList);
////            boService.persistenceBatch(batchCBO);
//        } else {
//            lifeCycleService.transCboStatus(cboList, baseModelDTO.getLcStatus());
//        }
//        lifeCycleService.transCboStatus(codeCbos, baseModelDTO.getLcStatus());
    }
 
 
    /**
     * 申请单一编码
     *
     * @param orderDTO 申请的信息,需要包含属性的内容和码段相关的内容
     * @return 返回编码的内容
     */
    @Override
    public String addSaveCode(CodeOrderDTO orderDTO) {
        VciBaseUtil.alertNotNull(orderDTO, "编码申请相关的属性和码段的内容都为空", orderDTO.getCodeClassifyOid(), "主题库分类的主键",
            orderDTO.getTemplateOid(), "模板的主键", orderDTO.getCodeRuleOid(), "编码规则的主键");
        CodeClassifyFullInfoBO classifyFullInfo = classifyService.getClassifyFullInfo(orderDTO.getCodeClassifyOid());
        CodeClstemplateVO templateVO = templateService.getObjectHasAttrByOid(orderDTO.getTemplateOid());
        CodeRuleVO ruleVO = ruleService.getObjectHasSecByOid(orderDTO.getCodeRuleOid());
        //1.判断规则中除了流水码段,是否有其他码段
        checkSecValueOnOrder(ruleVO, orderDTO);
        //2.判断必输项
        checkRequiredAttrOnOrder(templateVO, orderDTO);
        //3.先注入,再组合,最后校验
        switchClassifyLevelOnOrder(templateVO, classifyFullInfo, orderDTO);
        //4.处理组合规则。组合规则不能使用编码的属性,因为编码的生成可能是需要属性的
        switchComponentAttrOnOrder(templateVO, orderDTO);
        //5.校验规则
        checkVerifyOnOrder(templateVO, orderDTO);
        //6.关键属性
        checkKeyAttrOnOrder(classifyFullInfo, templateVO, orderDTO);
        //7.枚举转换
        checkEnumOnOrder(templateVO, orderDTO);
        //8.处理时间格式,在数据库里面不论是字符串还是日期格式,都使用相同的格式存储
        switchDateAttrOnOrder(templateVO, orderDTO);
        //9.生成编码的信息
//        ClientBusinessObject cbo = boService.createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
//        CodeWupinEntity cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
//        //默认的属性都不用从前端拷贝
//        //设置编码需要的默认属性的内容
//        copyValueToCBO(classifyFullInfo, cbo, orderDTO, templateVO, false);
//        //TODO:因为默认的属性都不拷贝,目前集团码叫name,并没有从DTO拷贝到cbo里。增加一个单独处理,以后再看要不要调整
//        cbo.setName(orderDTO.getName() == null ? "" : orderDTO.getName());
//        //end -- modify by lihang @20220407
//        List<CodeWupinEntity> cboList = new ArrayList<>();
//
//        //备注
//        cbo.setDescription(orderDTO.getDescription());
//
//        cboList.add(cbo);
//        List<String> codeList = productCodeService.productCodeAndSaveData(classifyFullInfo, templateVO, ruleVO, orderDTO.getSecDTOList(),cboList);
////        batchSaveSelectChar(templateVO, cboList);
//        return codeList.size() > 0 ? codeList.get(0) : "";
        return null;
    }
 
    /**
     * 处理分类注入的信息
     *
     * @param templateVO         模板的显示对象,必须要后模板的属性
     * @param classifyFullInfoBO 分类的全路径
     * @param orderDTO           编码申请的信息
     */
    private void switchClassifyLevelOnOrder(CodeClstemplateVO templateVO, CodeClassifyFullInfoBO classifyFullInfoBO, CodeOrderDTO orderDTO) {
        Map<String,CodeClstempattrVO> classifyAttrVOMap = templateVO.getAttributes().stream().filter(
            s -> StringUtils.isNotBlank(s.getClassifyinvokeattr()) && StringUtils.isNotBlank(s.getClassifyinvokelevel())
        ).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (classifyFullInfoBO.getTopClassifyVO() == null) {
            //需要重新查询一下,因为这个是指定的分类进来的
 
        }
        if (!CollectionUtils.isEmpty(classifyAttrVOMap)) {
            classifyAttrVOMap.forEach((attrId, attrVO) -> {
                //分类注入的编号或者名称,
                //层级包含指定层和最小层
                CodeClassifyVO classifyVO = null;
                if (!CodeLevelTypeEnum.MIN.getValue().equalsIgnoreCase(attrVO.getClassifyinvokelevel()) && !"min".equalsIgnoreCase(attrVO.getClassifyinvokelevel())) {
                    //指定了层级的
                    //注意,因为查询上级分类出来的层级是倒序的,即顶层节点是最大的值
                    List<CodeClassifyVO> classifyVOS = classifyFullInfoBO.getParentClassifyVOs().stream().sorted(((o1, o2) -> o2.getDataLevel().compareTo(o1.getDataLevel()))).collect(Collectors.toList());
                    int level = VciBaseUtil.getInt(attrVO.getClassifyinvokelevel());
                    if (classifyVOS.size() >= level && level > 0) {
                        classifyVO = classifyVOS.get(level - 1);
                    }
                } else {
                    //当前的分类
                    classifyVO = classifyFullInfoBO.getCurrentClassifyVO();
                }
                if (classifyVO == null) {
                    //说明层级有误
                    orderDTO.getData().put(attrId, "分类树上没有层级[" + attrVO.getClassifyinvokelevel() + "]");
                    // classifyVO = classifyFullInfoBO.getCurrentClassifyVO();
                } else {
                    Map<String, String> classifyDataMap = VciBaseUtil.objectToMapString(classifyVO);
                    String value = classifyDataMap.getOrDefault(attrVO.getClassifyinvokeattr(), "");
                    orderDTO.getData().put(attrId, value);
                }
            });
        }
    }
 
 
    /**
     * 判断编码的码段是否输入或者选择了码值
     *
     * @param ruleVO   规则的显示对象
     * @param orderDTO 编码申请的内容
     */
    @Override
    public void checkSecValueOnOrder(CodeRuleVO ruleVO, CodeOrderDTO orderDTO) {
        List<String> unSerialSecOidList = ruleVO.getSecVOList().stream().filter(
            s -> !(CodeSecTypeEnum.CODE_SERIAL_SEC.getValue().equalsIgnoreCase(s.getSecType())
                || CodeSecTypeEnum.CODE_ATTR_SEC.getValue().equalsIgnoreCase(s.getSecType())
                || CodeSecTypeEnum.CODE_DATE_SEC.getValue().equalsIgnoreCase(s.getSecType())
                || CodeSecTypeEnum.CODE_LEVEL_SEC.getValue().equalsIgnoreCase(s.getSecType())
                || VciBaseUtil.getBoolean(s.getNullableFlag()))
        ).map(CodeBasicSecVO::getOid).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(unSerialSecOidList)) {
            if (CollectionUtils.isEmpty(orderDTO.getSecDTOList())) {
                throw new VciBaseException("非流水码段(或者必输码段)必须要输入(或选择)码值");
            }
            if (orderDTO.getSecDTOList().stream().anyMatch(s -> !unSerialSecOidList.contains(s.getSecOid())
                && StringUtils.isBlank(s.getSecValue()))) {
                throw new VciBaseException("非流水码段(或者必输码段)必须要输入(或选择)码值");
            }
        }
    }
 
    /**
     * 校验属性是否为必输
     *
     * @param templateVO 模板的显示对象,需要包含模板属性
     * @param orderDTO   编码申请的信息
     */
    private void checkRequiredAttrOnOrder(CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        Map<String, CodeClstempattrVO> requiredAttrMap = templateVO.getAttributes().stream().filter(
                s -> VciBaseUtil.getBoolean(s.getRequireflag()) && StringUtils.isBlank(s.getComponentrule())
                    && StringUtils.isBlank(s.getClassifyinvokeattr()))
            .collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (!CollectionUtils.isEmpty(requiredAttrMap)) {
            requiredAttrMap.forEach((attrId, attrVO) -> {
                //只有企业编码,状态,备注,模板主键,分类主键这几个是固定的,其余都是自行配置的
                if (StringUtils.isBlank(getValueFromOrderDTO(orderDTO, attrId))) {
                    throw new VciBaseException("属性【{0}】必须要输入(选择)内容", new String[]{attrVO.getName()});
                }
            });
        }
    }
 
    /**
     * 从编码申请信息对象上获取某个属性的值
     *
     * @param orderDTO 编码申请对象
     * @param attrId   属性的编号
     * @return 值
     */
    private String getValueFromOrderDTO(CodeOrderDTO orderDTO, String attrId) {
        attrId = attrId.toLowerCase(Locale.ROOT);
        String value = null;
        if (VciQueryWrapperForDO.BASIC_FIELD_MAP.containsKey(attrId)) {
            value = WebUtil.getStringValueFromObject(WebUtil.getValueFromField(WebUtil.getFieldForObject(attrId, orderDTO.getClass()).getName(), orderDTO));
        } else {
            //说明是自行配置的
            //前端必须要传递小写的属性
            value = orderDTO.getData().getOrDefault(attrId, "");
        }
        return value;
    }
 
    /**
     * 转换组合规则的值
     *
     * @param templateVO 模板的显示对象,需要包含模板属性
     * @param orderDTO   编码申请的信息
     */
    private void switchComponentAttrOnOrder(CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        Map<String, CodeClstempattrVO> compAttrVOMap = templateVO.getAttributes().stream().filter(s -> StringUtils.isNotBlank(s.getComponentrule())).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (!CollectionUtils.isEmpty(compAttrVOMap)) {
            Map<String, String> dataMap = WebUtil.objectToMapString(orderDTO);
 
            Map<String, String> dataLowMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(dataMap)) {
                dataMap.forEach((key, value) -> {
                    dataLowMap.put(key.toLowerCase(Locale.ROOT), value);
                });
            }
            dataLowMap.putAll(orderDTO.getData());
            compAttrVOMap.forEach((attrId, attrVO) -> {
//                dataLowMap.put(attrId, formulaService.getValueByFormula(dataLowMap, attrVO.getComponentrule()));
            });
            dataLowMap.forEach((key, value) -> {
                setValueToOrderDTO(orderDTO, key, value);
            });
        }
    }
 
 
    /**
     * 设置新的值到申请对象上
     *
     * @param orderDTO 编码申请对象
     * @param attrId   属性的编号
     * @param value    值
     */
    private void setValueToOrderDTO(CodeOrderDTO orderDTO, String attrId, String value) {
        attrId = attrId.toLowerCase(Locale.ROOT);
        if (VciQueryWrapperForDO.BASIC_FIELD_MAP.containsKey(attrId)) {
            WebUtil.setValueToField(WebUtil.getFieldForObject(attrId, orderDTO.getClass()).getName(), orderDTO, value);
        } else {
            orderDTO.getData().put(attrId, value);
        }
    }
 
    /**
     * 校验正则表达式是否正确
     *
     * @param templateVO 模板的信息,必须包含属性的内容
     * @param orderDTO   编码申请的相关的信息
     */
    private void checkVerifyOnOrder(CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        Map<String, CodeClstempattrVO> verifyAttrVOMap = templateVO.getAttributes().stream().filter(s -> StringUtils.isNotBlank(s.getVerifyrule())).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (!CollectionUtils.isEmpty(verifyAttrVOMap)) {
            verifyAttrVOMap.forEach((attrId, attrVO) -> {
                String value = getValueFromOrderDTO(orderDTO, attrId);
                if (StringUtils.isNotBlank(value) && !value.matches(attrVO.getVerifyrule())) {
                    //校验正则表达式
                    throw new VciBaseException("属性[{0}]的值不符合校验规则的要求", new String[]{attrVO.getName()});
                }
            });
        }
    }
 
    /**
     * 校验关键属性
     *
     * @param classifyFullInfo 分类的全部信息
     * @param templateVO       模板的内容,必须包含模板属性
     * @param orderDTO         编码申请的相关的信息
     */
    private void checkKeyAttrOnOrder(CodeClassifyFullInfoBO classifyFullInfo, CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        //先获取关键属性的规则,也利用继承的方式
        CodeKeyattrrepeatVO keyRuleVO = keyRuleService.getRuleByClassifyFullInfo(classifyFullInfo);
        //注意的是keyRuleVO可能为空,表示不使用规则控制
        //获取所有的关键属性
        Map<String, CodeClstempattrVO> ketAttrMap = templateVO.getAttributes().stream().filter(s -> VciBaseUtil.getBoolean(s.getKeyattrflag())).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        Map<String, String> conditionMap = new HashMap<>();
        boolean trimAll = keyRuleVO == null ? false : VciBaseUtil.getBoolean(keyRuleVO.getIgnoreallspaceflag());
        //全部去空的优先级大于去空
        boolean trim = keyRuleVO == null ? false : VciBaseUtil.getBoolean(keyRuleVO.getIgnorespaceflag());
        boolean ignoreCase = keyRuleVO == null ? false : VciBaseUtil.getBoolean(keyRuleVO.getIgnorecaseflag());
        boolean ignoreWidth = keyRuleVO == null ? false : VciBaseUtil.getBoolean(keyRuleVO.getIgnorewidthflag());
        ketAttrMap.forEach((attrId, attrVO) -> {
            String value = getValueFromOrderDTO(orderDTO, attrId);
            if (value == null) {
                value = "";
            }
//            wrapperKeyAttrConditionMap(value, keyRuleVO, attrId, trim, ignoreCase, ignoreWidth, trimAll, conditionMap);
        });
 
        //没有限制分类,但是一个模板只可能在一个业务类型里面,所以直接查询这个业务类型即可
 
//        if (!CollectionUtils.isEmpty(conditionMap)) {
//            final String[] sql = {"select count(*) from " + VciBaseUtil.getTableName(classifyFullInfo.getTopClassifyVO().getBtmtypeid()) + " t where 1 = 1 "};
//            conditionMap.forEach((key, value) -> {
//                sql[0] += " and " + key + " = " + value;
//            });
//            if (StringUtils.isNotBlank(orderDTO.getOid())) {
//                //修改的时候,需要排除自己
//                sql[0] += " and oid != '" + orderDTO.getOid() + "'";
//            } else if (StringUtils.isNotBlank(orderDTO.getCopyFromVersion())) {
//                sql[0] += " and oid != '" + orderDTO.getCopyFromVersion() + "'";
//            }
//            sql[0] += " and islastR = '1' and islastV = '1' ";
//            if (boService.queryCountBySql(sql[0], new HashMap<>()) > 0) {
//                String ruleInfoMsg = keyRuleVO == null ? "" : "查询规则:去除空格--{0},忽略大小写--{1},忽略全半角--{2},忽略全部空格--{3}";
//                String[] objs = new String[]{trim ? "是" : "否", ignoreCase ? "是" : "否", ignoreWidth ? "是" : "否", trimAll ? "是" : "否"};
//                throw new VciBaseException("根据您填写的关键属性的内容,结合关键属性查询规则,发现这个数据已经在系统中存在了。请修正!。" + ruleInfoMsg, objs);
//            }
//        }
    }
 
    /**
     * 封装关键属性的查询语句
     *
     * @param value        当前的值
     * @param keyRuleVO    关键属性的控制规则,可以为空
     * @param attrId       属性的编号
     * @param trim         是否去除空格
     * @param ignoreCase   是否不区分大小写
     * @param ignoreWidth  是否忽略全半角
     * @param trimAll      是否忽略全部空格
     * @param conditionMap 查询条件
     */
//    @Override
//    public void wrapperKeyAttrConditionMap(String value, CodeKeyattrrepeatVO keyRuleVO, String attrId,
//                                           boolean trim, boolean ignoreCase, boolean ignoreWidth,
//                                           boolean trimAll, Map<String, String> conditionMap) {
//        boolean ignoreSpace = trim || trimAll;
//        if (StringUtils.isBlank(value)) {
//            //为空的时候,不能用QueryOperation.ISNULL,平台不知道啥时候不处理这种了
//            conditionMap.put("t."+attrId, "null");
//        } else {
//            if (keyRuleVO != null) {
//                String queryKey = "";
//                String queryValue = "";
//
//                String temp = "";
//                if (ignoreCase && ignoreSpace && ignoreWidth) {
//                    //忽略大小写,且去空,忽略全半角
//                    temp = (trimAll ? "REPLACE" : "TRIM") + "(UPPER(to_single_byte(%s)) " + (trimAll ? ",' ','')" : ")");
//                } else if (ignoreCase && ignoreSpace && !ignoreWidth) {
//                    //忽略大小写、去空、不忽略全半角
//                    temp = (trimAll ? "REPLACE" : "TRIM") + "(UPPER(%s) " + (trimAll ? ",' ','')" : ")");
//                } else if (ignoreCase && !ignoreSpace && ignoreWidth) {
//                    //忽略大小写、不去空、忽略全半角
//                    temp = "UPPER(to_single_byte(%s))";
//                } else if (!ignoreCase && ignoreSpace && ignoreWidth) {
//                    //不忽略大小写、去空、忽略全半角
//                    temp = (trimAll ? "REPLACE" : "TRIM") + "(to_single_byte(%s) " + (trimAll ? ",' ','')" : ")");
//                } else if (ignoreCase && !ignoreSpace && !ignoreWidth) {
//                    //忽略大小写、不去空、不忽略全半角
//                    temp = "UPPER(%s)";
//                } else if (!ignoreCase && !ignoreCase && ignoreWidth) {
//                    //不忽略大小写、不去空、忽略全半角
//                    temp = "to_single_byte(%s)";
//                } else if (!ignoreCase && ignoreSpace && !ignoreWidth) {
//                    //不忽略大小写、去空、不忽略全半角
//                    temp = (trimAll ? "REPLACE" : "TRIM") + "(%s " + (trimAll ? ",' ','')" : ")");
//                } else if (!ignoreCase && !ignoreSpace && !ignoreWidth) {
//                    //不忽略大小写、不去空、不忽略全半角
//                    temp = "%s";
//                }
//                queryKey = String.format(temp, "t."+attrId);
//                queryValue = String.format(temp, "'" + (trim ? value.trim() : value) + "'");
//                conditionMap.put(queryKey, queryValue);
//            } else {
//                //为空的时候不代表不校验,只是不去除相关的信息
//                conditionMap.put("t."+attrId, value);
//            }
//        }
//    }
 
    /**
     * 校验枚举的内容
     *
     * @param templateVO 模板的显示对象,需要包含属性
     * @param orderDTO   编码申请的信息
     */
    private void checkEnumOnOrder(CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        //如果枚举可以修改,则不需要校验是否符合枚举的选项
 
        Map<String, CodeClstempattrVO> enumAttrVOMap = templateVO.getAttributes().stream().filter(s -> (StringUtils.isNotBlank(s.getEnumstring()) || StringUtils.isNotBlank(s.getEnumid())) && !VciBaseUtil.getBoolean(s.getEnumeditflag())).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (!CollectionUtils.isEmpty(enumAttrVOMap)) {
            enumAttrVOMap.forEach((attrId, attrVO) -> {
                String value = getValueFromOrderDTO(orderDTO, attrId);
                if (StringUtils.isNotBlank(value)) {
                    //有值才能校验
                    List<KeyValue> comboboxKVs = listComboboxItems(attrVO);
                    if (!comboboxKVs.stream().anyMatch(s -> value.equalsIgnoreCase(s.getKey()))) {
                        throw new VciBaseException("属性【{0}】的值不符合枚举的要求", new String[]{attrVO.getName()});
                    }
                }
            });
        }
    }
 
    /**
     * 转换时间的格式
     *
     * @param templateVO 模板的显示对象,需要包含属性
     * @param orderDTO   编码申请的信息
     */
    private void switchDateAttrOnOrder(CodeClstemplateVO templateVO, CodeOrderDTO orderDTO) {
        Map<String, CodeClstempattrVO> dateAttrVOMap = templateVO.getAttributes().stream().filter(s -> StringUtils.isNotBlank(s.getCodedateformat())).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
        if (!CollectionUtils.isEmpty(dateAttrVOMap)) {
            dateAttrVOMap.forEach((attrId, attrVO) -> {
                String value = getValueFromOrderDTO(orderDTO, attrId);
                if (StringUtils.isNotBlank(value)) {
                    DateConverter dateConverter = new DateConverter();
                    dateConverter.setAsText(value);
                    value = VciDateUtil.date2Str(dateConverter.getValue(), VciDateUtil.DateTimeMillFormat);
                    setValueToOrderDTO(orderDTO, attrId, value);
                }
            });
        }
    }
 
    /**
     * 拷贝数据到cbo对象上
     *
     * @param classifyFullInfo 分类的全部信息
     * @param cbo              业务数据
     * @param orderDTO         编码申请的信息
     * @param templateVO       模板的显示对象
     * @param edit             是否为修改
     */
    private void copyValueToCBO(CodeClassifyFullInfoBO classifyFullInfo, CodeWupinEntity cbo,
                                CodeOrderDTO orderDTO, CodeClstemplateVO templateVO,
                                boolean edit) {
        String fullPath = "";
        if (!CollectionUtils.isEmpty(classifyFullInfo.getParentClassifyVOs())) {
            fullPath = classifyFullInfo.getParentClassifyVOs().stream().sorted(((o1, o2) -> o2.getDataLevel().compareTo(o1.getDataLevel())))
                .map(CodeClassifyVO::getOid).collect(Collectors.joining("##"));
        } else {
            fullPath = classifyFullInfo.getCurrentClassifyVO().getOid();
        }
 
//        BeanUtils.
        BeanUtils.copyProperties(orderDTO.getData(),cbo);
        cbo.setMaterialtype(Short.valueOf("1001"));
//        orderDTO.getData().forEach((key, value) -> {
//            if (!edit || (!checkUnAttrUnEdit(key) &&
//                !VciQueryWrapperForDO.LC_STATUS_FIELD.equalsIgnoreCase(key))) {
//                try {
//                    cbo.setAttributeValue(key, value);
//                } catch (Exception e) {
//                    logger.error("设置属性的值错误", e);
//                }
//            }
//        });
        try {
 
            cbo.setCodeclsfid(classifyFullInfo.getCurrentClassifyVO().getOid());
            cbo.setCodetemplateoid(templateVO.getOid());
            cbo.setCodeclsfpath(fullPath);
//            cbo.setMaterialclassify("model_type");
//            cbo.setMaterialname(orderDTO.getData().get("materialname"));
//            cbo.setShifoupihaoguanli("true");
//            cbo.setKucunwl("true");
//            cbo.setXiaoshouwl("false");
            if (!edit && StringUtils.isBlank(orderDTO.getLcStatus())) {
                //找生命周期的起始状态,插个点,看生命周期是否需要创建
                if (StringUtils.isNotBlank(cbo.getLctid())) {
//                    OsLifeCycleVO lifeCycleVO = lifeCycleService.getLifeCycleById(cbo.getLctid());
//                    if (lifeCycleVO != null) {
//                        cbo.setLcStatus("Editing");
////                        cbo.setLcStatus(lifeCycleVO.getStartStatus());
//                    } else {
                        cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
//                    }
                } else {
                    cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
                }
 
            }
 
            int secret = VciBaseUtil.getInt(cbo.getSecretGrade().toString());
            //插个点,后续看密级服务是否可用
//            if (secret == 0 || !secretService.checkDataSecret(secret)) {
            if (secret == 0 ) {
                Integer userSecret = VciBaseUtil.getCurrentUserSecret();
//                cbo.setAttributeValue(SECRET_FIELD, String.valueOf((userSecret == null || userSecret == 0) ? UserSecretEnum.NONE.getValue() : userSecret));
                cbo.setSecretGrade(userSecret == null || userSecret == 0 ? UserSecretEnum.NONE.getValue() : userSecret);
            }
        } catch (Throwable e) {
            logger.error("设置默认的属性的值错误", e);
        }
    }
 
 
    /**
     * 初始化业务类型
     * --创建人默认为当前用户,如果需要修改,可以在获取后自行处理
     * @param btmName 业务类型的名称,会自动变成小写
     * @return CodeWupinEntity
     * @throws VciBaseException 初始化出错的是会抛出异常
     */
//    @Override
//    public CodeWupinEntity createCBOByBtmName(String btmName)
//        throws VciBaseException {
//        if(btmName!=null){
//            btmName = btmName.trim().toLowerCase();
//        }
//        String userid = AuthUtil.getUser().getUserName();
////        if(!hasCreatedCbos.containsKey(btmName)){
////            if(StringUtils.isEmpty(userid)){
////                throw new VciBaseException(msgCodePrefix +"noHasUserid");
////            }
////            try {
////                hasCreatedCbos.put(btmName, createBusinessObject(btmName));
////            } catch (Exception e) {
////                logger.error("创建业务类型对象",e);
////                throw new VciBaseException(msgCodePrefix + "initBoError",new String[]{btmName});
////            }
////        }
////        ClientBusinessObject cbo = cloneClientBusinessObject(hasCreatedCbos.get(btmName));
//
//        QueryWrapper<CodeOsbtmtypeEntity> btmWrapper = new QueryWrapper<>();
//        btmWrapper.eq("ID",btmName);
//        CodeOsbtmtypeEntity btmTypeVO = codeOsbtmtypeMapper.selectOne(btmWrapper);
////        OsBtmTypeVO btmTypeVO = btmService.getBtmById(boName);
//        String userName = AuthUtil.getUser().getUserName();
//        CodeWupinEntity wupinEntity = new CodeWupinEntity();
//        wupinEntity.setOid(null);
////        bo.setRevisionid((new ObjectUtility()).getNewObjectID36());
////        bo.setNameoid((new ObjectUtility()).getNewObjectID36());
//        wupinEntity.setBtmname(btmName);
//        wupinEntity.setLastR(String.valueOf(1));
//        wupinEntity.setFirstR(String.valueOf(1));
//        wupinEntity.setFirstV(String.valueOf(1));
//        wupinEntity.setLastV(String.valueOf(1));
//        wupinEntity.setCreator(userName);
//        wupinEntity.setCreateTime(new Date());
//        wupinEntity.setLastModifier(userName);
//        wupinEntity.setLastModifyTime(new Date());
//        wupinEntity.setRevisionRule(btmTypeVO.getRevisionruleid());
//        wupinEntity.setVersionRule(String.valueOf(btmTypeVO.getVersionRule()));
//        if(StringUtils.isNotBlank(btmTypeVO.getRevisionruleid())){
//            //插个点,需要问勇哥版本问题,展示默认为1
////            OsRevisionRuleVO revisionRuleVO = revisionRuleService.getRevisionRuleById(btmTypeVO.getRevisionruleid());
//            wupinEntity.setRevisionValue("1");
//        }
//
//        wupinEntity.setRevisionSeq(1);
//        wupinEntity.setVersionSeq(1);
//        //插个点,需要问勇哥版本问题,展示默认为1
//        wupinEntity.setVersionValue("1");
//        wupinEntity.setLctid("wupinLC");
//        wupinEntity.setLcStatus("Editing");
//        wupinEntity.setId("");
//        wupinEntity.setName("");
//        wupinEntity.setDescription("");
//        wupinEntity.setOwner(userName);
//        wupinEntity.setCheckinby(userName);
//        wupinEntity.setCopyFromVersion("");
//        wupinEntity.setMaterialtype((short) 1001);
//        wupinEntity.setCaigouwl("true");
//        wupinEntity.setShifoupihaoguanli("true");
//        wupinEntity.setKucunwl("true");
//        wupinEntity.setXiaoshouwl("false");
//        wupinEntity.setPassing("true");
//
////        this.initTypeAttributeValue(wupinEntity,btmTypeVO);
//        return wupinEntity;
//
//
////        return cbo;
//    }
 
    /**
     * 是否为修改忽略的属性
     * @param attrName 属性的名字
     * @return true 表示应该忽略
     */
    boolean checkUnAttrUnEdit(String attrName){
        return  (VciQueryWrapperForDO.OID_FIELD.equalsIgnoreCase(attrName)
            ||"ts".equalsIgnoreCase(attrName)
            || "lastmodifier".equalsIgnoreCase(attrName)
            || "lastmodifytime".equalsIgnoreCase(attrName)
            || "createtime".equalsIgnoreCase(attrName)
            || "checkintime".equalsIgnoreCase(attrName)
            ||"checkouttime".equalsIgnoreCase(attrName));
    }
 
    /**
     * 保存可输可选的信息
     *
     * @param templateVO 模板的对象
     * @param cboList    数据的内容
     */
//    @Override
//    public void batchSaveSelectChar(CodeClstemplateVO templateVO, List<ClientBusinessObject> cboList) {
//        if (templateVO != null && !CollectionUtils.isEmpty(cboList)) {
//            //是异步的,所以直接循环
//            List<CodeClstempattrVO> selectAttrVOs = templateVO.getAttributes().stream().filter(s -> StringUtils.isNotBlank(s.getLibraryidentification())).collect(Collectors.toList());
//
//            if (!CollectionUtils.isEmpty(selectAttrVOs)) {
////                SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
//                selectAttrVOs.parallelStream().forEach(attrVO -> {
//                    List<String> valuesList = new CopyOnWriteArrayList<>();
//                    cboList.parallelStream().forEach(cbo -> {
//                        String value = cbo.getAttributeValue(attrVO.getId());
//                        if (StringUtils.isNotBlank(value)) {
//                            valuesList.add(value);
//                        }
//                    });
//                    if (!CollectionUtils.isEmpty(valuesList)) {
//                        charService.saveBySameNamespaceAndFlag(templateVO.getBtmTypeId(), attrVO.getLibraryIdentification(), valuesList, sessionInfo);
//                    }
//                });
//            }
//        }
//    }
}