zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package com.changhong.epc.tenant.service.sunCloud.impl;
 
import com.changhong.epc.bean.admin.Account;
import com.changhong.epc.bean.admin.CorresElField;
import com.changhong.epc.bean.form.CheckBuget;
import com.changhong.epc.bean.form.FormDataEntity;
import com.changhong.epc.bean.parsing.BillInfo;
import com.changhong.epc.bean.tenant.bill.RuleCheckMsg;
import com.changhong.epc.bean.tenant.cost.CostType;
import com.changhong.epc.bean.tenant.cost.extend.CostTypeExtend;
import com.changhong.epc.bean.tenant.master.MasterDefine;
import com.changhong.epc.bean.tenant.master.MasterElement;
import com.changhong.epc.bean.tenant.master.MasterValue;
import com.changhong.epc.bean.tenant.sunCloud.*;
import com.changhong.epc.constter.admin.AdminUrlConst;
import com.changhong.epc.constter.base.Context;
import com.changhong.epc.constter.cache.master.MasterCache;
import com.changhong.epc.constter.form.utf.FormUrlConst;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.constter.system.run.EpcRestInterface;
import com.changhong.epc.constter.tenant.SunCloudConst;
import com.changhong.epc.rely.api.epc.admin.CorresElFieldApi;
import com.changhong.epc.rely.api.service.CscApiService;
import com.changhong.epc.rely.api.tool.VersionFilterApi;
import com.changhong.epc.tenant.controller.sunCloud.ValidateTool;
import com.changhong.epc.tenant.mapper.tenant.bill.BillInfoMapper;
import com.changhong.epc.tenant.mapper.tenant.bill.RuleCheckMsgMapper;
import com.changhong.epc.tenant.mapper.tenant.cost.CostTypeMapper;
import com.changhong.epc.tenant.mapper.tenant.norm.MasterDefineMapper;
import com.changhong.epc.tenant.mapper.tenant.norm.MasterElementMapper;
import com.changhong.epc.tenant.mapper.tenant.norm.MasterValueMapper;
import com.changhong.epc.tenant.service.sunCloud.SunCloudService;
import com.iemsoft.framework.cloud.core.base.ResMsg;
import com.iemsoft.framework.cloud.core.constant.BaseConst;
import com.iemsoft.framework.cloud.core.exception.IEMRestException;
import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.redis.annotation.CacheRm;
import com.iemsoft.framework.cloud.ribbon.RestInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
@Slf4j
@Service
public class SunCloudServiceImpl implements SunCloudService, SystemClients, SunCloudConst, BaseConst, MasterCache {
    
    @Resource
    private MasterValueMapper masterValueMapper;
    
    @Resource
    private MasterDefineMapper masterDefineMapper;
    
    @Resource
    private MasterElementMapper masterElementMapper;
    
    @Resource
    private CscApiService cscApiService;
    
    @Resource
    private CostTypeMapper costTypeMapper;
    
    
    @Resource
    private BillInfoMapper billInfoMapper;
    
    @Resource
    private RuleCheckMsgMapper ruleCheckMsgMapper;
    
 
    /*
     * 下标key
     */
    public static final String INDEX_KEY = "index";
    
    /*
     * 删除标记
     */
    public static final String DEL_FLAG = "delFlag";
    
    /*
     * 行号
     */
    public static final String ROW_NUM    = "rowNum";
    
    public static final String MAS_DATA_DEF_CODE = "masDataDefCode";
    
    private Integer tenantId;
    
    private boolean isNotNull;
    
    public static final String L = "0";
    
    public static final String DEFINE_CODE = "masDataDefcode";
    
    public static final String CODE = "code";
    
    
    public static final String VALUE = "value";
 
    //业务类型常量key
    private static final String BUSINESS_TYPE = "businessType";
     
    @Transactional
    @Override
    public int addCheckResult(String data) {
 
        CheckResult checkMsg =JSONTool.toObj(data, CheckResult.class);
        //参数
        RuleCheckMsg ruleCheckMsg = new RuleCheckMsg(checkMsg.getOrderCode(),data);
        ruleCheckMsg.setDataStart(INVALID_DATA_FLGE.intValue());
        ruleCheckMsgMapper.updateStrat(ruleCheckMsg.getOrderCode());
        ruleCheckMsg.initParam();
        int i = ruleCheckMsgMapper.insert(ruleCheckMsg);
//        if(i>0){
//            log.debug("********规则检查成功!!!");
//            log.debug("********修改票据状态!!!");
//            int b = billInfoMapper.updateBillState(ruleCheckMsg.getOrderCode());
//            if(b>0){
//                log.debug("********修改票据状态成功!!!");
//            }else{
//                log.debug("********票据为不可分配状态!!!");
//            }
//        }
        return i;
    }
 
    @Transactional
    @Override
    public int updateBillStatus(UpdateBillStatus updateBillStatus) {
        //参数
        /*OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderCode(updateBillStatus.getOrderCode());
        orderStatus.setOrderStatus(updateBillStatus.getOrderStatus());
        orderStatus.setTenantId(Integer.parseInt(updateBillStatus.getTenantId().toString()));*/
 
        //发请求
        RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class);
        Account acc=new Account();
        acc.setOrderCode(updateBillStatus.getOrderCode());
        Object data = restInterface.post(AdminUrlConst.ACCOUNT_SELECTORDERCODE,acc,ResMsg.class,EpcRestInterface.getEpcHeads()).getData();
        Account account=JSONTool.toObj(JSONTool.toJson(data),Account.class);
        acc.setVoucherCode(updateBillStatus.getVoucherCode());
        RestInterface restInterface2 = SpringUtil.getBean(SERVER_FORM.getBeanName(), RestInterface.class);
 
        if(ObjectUtil.empty(account)){
            BillInfo billInfo = new BillInfo();
            billInfo.setOrderCode(updateBillStatus.getOrderCode());
            billInfo = billInfoMapper.select(billInfo).stream().findFirst().orElse(null);
 
            if(ObjectUtil.empty(billInfo)){
                return -1;
            }
 
            Map<String, Object> param = new HashMap<>();
            param.put("formID", billInfo.getFormId());
            param.put("dataRowNum", billInfo.getFormDataId());
            param.put("status", updateBillStatus.getOrderStatus());
            param.put("tenantID", updateBillStatus.getTenantId());
 
            return Objects.isNull(
                    restInterface2.post(FormUrlConst.FORM_STATUS,param,ResMsg.class,EpcRestInterface.getEpcHeads()).getData())?-1:1;
        }else{
            ThreadData.set(com.changhong.epc.constter.base.BaseConst.COMPANY_ID,Integer.parseInt(account.getCompanyId()));
            ThreadData.set(com.changhong.epc.constter.base.BaseConst.TENANT_ID,account.getTenantId());
            ThreadData.set(com.changhong.epc.constter.base.BaseConst.OPEN_ID,account.getOpenId());
            if(Objects.isNull(account.getFormId())){
                acc.setFlag(false);
                Map<String,String> map=new HashedMap<>();
                map.put("status2",updateBillStatus.getOrderStatus());
                map.put("colId",account.getDataRowNum());
                map.put("voucherCode",updateBillStatus.getVoucherCode());
                restInterface.post(AdminUrlConst.ACCOUNT_UPDATEVOUCHERCODE,acc,ResMsg.class,
                        EpcRestInterface.getEpcHeads()).getData();
                return Objects.isNull(restInterface2.post(FormUrlConst.ASSET_UPDATESTATUS,map,ResMsg.class,EpcRestInterface.getEpcHeads()).getData())?-1:1;
            }else{
                acc.setFlag(true);
                CheckBuget checkBuget=new CheckBuget();
                checkBuget.setFormID(account.getFormId());
                checkBuget.setDataRowNum(account.getDataRowNum());
                checkBuget.setStatus(Integer.parseInt(updateBillStatus.getOrderStatus()));
                checkBuget.setTenantID(account.getTenantId());
                checkBuget.setVoucherCode(updateBillStatus.getVoucherCode());
                Object data2 = restInterface2.post(FormUrlConst.FORM_STATUS,checkBuget,ResMsg.class,
                        EpcRestInterface.getEpcHeads()).getData();
                restInterface.post(AdminUrlConst.ACCOUNT_UPDATEVOUCHERCODE,acc,ResMsg.class,
                        EpcRestInterface.getEpcHeads()).getData();
                return Objects.isNull(data2)?-1:1;
            }
        }
        //回写服务表
 
 
 
    }
 
    @SuppressWarnings("finally")
    @Transactional
    @Override
    public int pushOrderBill(PushOrderBill pushOrderBill) {
        //参数
        BillInfo billInfo = new BillInfo();
        billInfo.setOrderCode(pushOrderBill.getOrderCode());
        //暂时不调用规则检查
        //billInfo.setDataStart(0);
        billInfo.setDataStart(2);
        billInfo.setInfo(pushOrderBill.getBillDatas());
        
//        try{
//
//            log.debug("------------开始发送规则请求-------------");
//            Object respData = cscApiService.orderRuleCheck(pushOrderBill.getOrderCode());
//            log.debug(JSONTool.toJson(respData));
//        }catch(Exception e){
//            log.debug("------------规则请求出错-------------");
//            log.debug(e.toString());
//        }finally{//将票据信息添加到费用云
            //发请求
            log.debug("------------将票据信息添加到费用云-------------");
            //暂时放在tenant下
            int result = billInfoMapper.updateBill(billInfo);
            if(result == 0)
                result = billInfoMapper.addBill(billInfo);
            return result; 
//        }
        
        
        
    }
 
 
    @Transactional
    @Override
    @CacheRm(value = MASTER_VALUE, company = false)
    public void pushMasDataValue(PushMasDataValue pushMasDataValue) {
        List<Map<String, Object>> errData = new ArrayList<>();
        String type = pushMasDataValue.getType();
        this.tenantId = pushMasDataValue.getTenantId();
        if(StringUtils.isNotBlank(type)){
            List<Map<String, Object>> dataList = pushMasDataValue.getData();
            String masDataDefCode = pushMasDataValue.getMasDataDefCode();
            if(CollectionUtils.isNotEmpty(dataList)){
                switch (type) {
                /* 增加 */
                    case TYPE_INSERT: 
                        insert(masDataDefCode, dataList, errData);
                        break;
                /* 修改 */
                    case TYPE_UPDATE: 
                        update(masDataDefCode, dataList, errData);
                        break;
                /* 删除 */
                    case TYPE_DELETE:
                        beginDelete(masDataDefCode, dataList);
                        break;
                    default:
                        throw new IEMRestException("C0018");
                }
            }
        }
    }
    
    /**
     * 添加错误消息
     * @Title: setMsg
     
     * @param @param code
     * @param @param msg    设定文件
     * @return void    返回类型
     * @throws
     */
//    protected void setMsg(String code, Map<String, Object> msg) {
//        msg.put("rspCode", code);
//        msg.put("rspDesc", "E10000");
//    }
    
    private void insert(String mdCode, List<Map<String, Object>> dataList, List<Map<String, Object>> errData){
        if(ObjectUtil.empty(dataList) || ObjectUtil.empty(errData)){
            return;
        }
        if(dataList.size() == 1){
            if(beginInsert(mdCode, dataList.get(0)) == Integer.MIN_VALUE){
                errData.add(dataList.get(0));
            }
        }else{
            for (Map<String, Object> map : dataList) {
                if(beginInsert(mdCode, map) == Integer.MIN_VALUE){
                    errData.add(map);
                }
            }
        }
    }
 
    private void update(String mdCode, List<Map<String, Object>> dataList, List<Map<String, Object>> errData){
        if(ObjectUtil.empty(dataList) || ObjectUtil.empty(errData)){
            return;
        }
        for (Map<String, Object> map : dataList) {
            if(beginUpdate(mdCode, map) == Integer.MIN_VALUE){
                errData.add(map);
            }
        }
    }
    
    /**
     * 开始添加
     * @Title: insert
     
     * @param @param data
     * @param @param userInfo
     * @param @return    设定文件
     * @return int    返回类型
     * @throws
     */
    private int beginInsert(String mdCode, Map<String, Object> data){
        log.error(String.format("主数据同步ing...%s:%s", mdCode, data.toString()));
        
        Map<String, Object> config = new ModelMap(DEL_FLAG, data.get(DEL_FLAG))
                                            .addAttribute(ROW_NUM, data.get(ROW_NUM))
                                            .addAttribute(MAS_DATA_DEF_CODE, mdCode);
        data.remove(DEL_FLAG);
        data.remove(ROW_NUM);
 
        /**
         * 添加主数据
         */
//        Integer mdId = masterDefineMapper.selectIdByCode(mdCode);
        MasterValue precord = null;
        for (Map.Entry<String, Object> val : data.entrySet()) {
            
            /**
             * 验证是否存在
             */
            ValidateTool.assertException(
                    masterValueMapper.isEmpty(mdCode//define_code  --> master_value 表中对应字段
                                            ,val.getKey()//ele_code
                                            ,NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1)//row_no
                                            ,this.tenantId.toString()) > 0);//租户id
            
//            MasterElementExample mee = new MasterElementExample();
//            MasterElementExample.Criteria c =  mee.createCriteria();
//            c.andMeMasterIdEqualTo(mdId);
            precord = new MasterValue();
            precord.setDefineCode(mdCode);
            precord.setEleCode(val.getKey());
            //默认递增
            precord.setEleValue(Objects.toString(val.getValue(), ""));            
            precord.setRowNo(NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1));
            precord.setDelFlag(NumberUtils.toInt(Objects.toString(config.get(DEL_FLAG), ""), 0));
            precord.setDataStart(0);
            precord.setInsP("-1");
            precord.setUpdP("-1");
            // System.err.println(ThreadData.get(Keys.TENANT_ID));
            precord.setTenantId(this.tenantId.toString());
//            precord.setTenantId(ThreadData.get(Keys.TENANT_ID).toString());
            precord.setInsT(new Date());
            precord.setUpdT(new Date());
            masterValueMapper.insert(precord);
        }
        return 1;
        
    };
 
    /**
     * 开始修改
     * @Title: update
     * @return int    
     * @throws
     */
    private int beginUpdate(String mdCode, Map<String, Object> data){
 
        //验证
        ValidateTool.assertException(
                StringUtils.isBlank(Objects.toString(data.get(ROW_NUM), "")));
        
        Map<String, Object> config = new ModelMap(DEL_FLAG, data.get(DEL_FLAG))
                                        .addAttribute(ROW_NUM, data.get(ROW_NUM))
                                        .addAttribute(MAS_DATA_DEF_CODE, mdCode);
        data.remove(DEL_FLAG);
        data.remove(ROW_NUM);
        
        int count = masterValueMapper.deleteValue(config, data);
        
        /**
         * 添加主数据
         */
        MasterValue precord = null;
        for (Map.Entry<String, Object> val : data.entrySet()) {
            precord = new MasterValue();
            precord.setDefineCode(mdCode);
            precord.setEleCode(val.getKey());
            //默认递增
            precord.setEleValue(Objects.toString(val.getValue(), ""));            
            precord.setRowNo(NumberUtils.toInt(Objects.toString(config.get(ROW_NUM), ""), -1));
            precord.setDelFlag(NumberUtils.toInt(Objects.toString(config.get(DEL_FLAG), ""), 0));
            precord.setDataStart(0);
            precord.setInsP("-1");
            precord.setUpdP("-1");
            precord.setTenantId(this.tenantId.toString());
            precord.setInsT(new Date());
            precord.setUpdT(new Date());
            count += masterValueMapper.insert(precord);
        }
        return count;
    
        
    };
 
    /**
     * 开始删除
     * @Title: delete
     
     * @param @param data
     * @param @param userInfo
     * @param @return    设定文件
     * @return int    返回类型
     * @throws
     */
    private  int beginDelete(String mdCode, List<Map<String, Object>> dataList){
        int count =0;
        if(ObjectUtil.empty(dataList)){
            return count;
        }
        for (Map<String, Object> map : dataList) {
            
            Map<String, Object> config = new ModelMap(DEL_FLAG, map.get(DEL_FLAG))
                    .addAttribute(ROW_NUM, map.get(ROW_NUM))
                    .addAttribute(MAS_DATA_DEF_CODE, mdCode);
            //由之前 逻辑删除 --修改为现在 -- 物理删除
             count += masterValueMapper.deleteValue(config, map);
        }
        return count;
    };
    
    /**
     * 业务类型开始修改
     * @return 
     */
    private String updateBusType(UpdataBusType upDataBusType){
        CostType cost = this.getCost(upDataBusType);
        CostType result = costTypeMapper.selectParentId(cost);
        
        //如果修改的数据不存在,打印修改信息不存在
        if(result == null){
            log.debug("修改错误:" + EDIT_FAILURE);
            return EDIT_FAILURE;
        }
        
        if(result.getParentId() == 0){
            log.debug("-----------------开始修改父级数据----------------------");
            int pid = result.getId();
            costTypeMapper.updateBus(cost);
            log.debug("修改父级:" + EDIT_SUCCESS);
            CostType ct = new CostType();
            ct.setParentId(pid);
            ct.setType(cost.getNodeName());
            log.debug("--------------------开始修改子级数据-------------------------");
            List<CostType> resultList = costTypeMapper.selectChild(ct);
            if(resultList != null){
                costTypeMapper.updateType(ct);
                log.debug("修改子级:" + EDIT_SUCCESS);
            }
        }else {
            log.debug("--------------------开始修改单条数据-----------------------");
            costTypeMapper.updateBus(cost);
            log.debug("单独修改子级:" + EDIT_SUCCESS);
        }
        return EDIT_SUCCESS;
    }
 
 
    /**
     * 删除业务类型
     * @return 
     */
    private String deleteBusType(UpdataBusType upDataBusType){
        CostType cost = this.getCost(upDataBusType);
        CostType result = costTypeMapper.selectParentId(cost);
        
        //如果删除的数据不存在,打印删除信息不存在
        if(result == null){
            log.error("删除错误:" + DELETE_FAILURE);
            return DELETE_FAILURE;
        }
        
        if(result.getParentId() == 0){
            log.debug("---------------开始删除父级数据-----------------");
            int eid = result.getId();
            int rid = Integer.valueOf(result.getRecourseCode());
            costTypeMapper.deleteBusType(rid);
            log.debug("删除父级:" + DELETE_SUCCESS);
            CostType ct = new CostType();
            ct.setParentId(eid);
            log.debug("--------------------开始删除子级数据----------------------");
            List<CostType> resultList = costTypeMapper.selectChild(ct);
            if(resultList != null){
                Integer recourseCode = result.getId();
                costTypeMapper.deleteType(recourseCode);
                log.debug("删除子级:" + DELETE_SUCCESS);
            }            
        }else {
            log.debug("--------------------开始删除子级数据-----------------------");
            Integer recourseCode = Integer.valueOf(cost.getRecourseCode());
            costTypeMapper.deleteBusType(recourseCode);
            log.debug("删除子级:" + DELETE_SUCCESS);
        }
        return DELETE_SUCCESS;
    }
 
    /**
     * 指定公司业务类型增量同步接口
     * @throws Exception 
     */
    @Transactional
    @Override
    public int updateBusType(List<UpdataBusType> updataBusType) throws Exception {
        CostTypeExtend pId = new CostTypeExtend();
        pId.setId(0);
        this.getCostType(updataBusType,pId);
        return isNotNull ? 1 : 0;
    }
 
    /**
     * 递归插入
     * @param updataBusType
     * @throws Exception 
     */
    public void getCostType(List<UpdataBusType> updataBusType,CostTypeExtend peId) throws Exception{
         if(updataBusType==null){
             this.isNotNull = false;
             return;
         }
         for(UpdataBusType up : updataBusType){ 
            CostTypeExtend id = operationBusType(up,peId);
            if(up.getChildren()!=null && up.getChildren().size()>0){
                this.getCostType(up.getChildren(),id);
            }
         }
    } 
    
    /**
     * 插入数据
     * @param up
     * @throws Exception 
     */
    public CostTypeExtend insertCost(UpdataBusType up,CostTypeExtend id){
        log.debug("-----------------开始插入----------------");
        CostType cost = this.getCost(up);
        
        CostTypeExtend costExtend = new CostTypeExtend();    
        cost.setParentId(id.getId());
        cost.setType(id.getNodeName());
        costExtend.SetProperties(cost);
        costExtend.initParam();//插入时间
        // System.out.println(costExtend.getType());
        Object obj = null;
        try {
            obj = costTypeMapper.insertCost(costExtend);
        } catch (Exception e) {
            log.error("插入数据异常:",e);
        }
        log.debug("插入数据:" + INSERT_SUCCESS);
        // System.out.println(obj);
        costExtend.setType(cost.getNodeName());
        return costExtend;
    }
    
    /**
     * 转换格式
     */
    public CostType getCost(UpdataBusType up){
        CostType cost= new CostType();
        //对象转换
        cost.setOrderTypeCode(up.getOrderTypeCode());
        cost.setSortCode(up.getSortCode());
        cost.setType(up.getType());
        cost.setNodeName(up.getName());
        cost.setParentId(up.getParentId());
        cost.setRecourseCode(up.getId());
        return cost;
    }
 
    /**
     * 判断业务类型
     * @throws Exception 
     */
    public CostTypeExtend operationBusType(UpdataBusType up,CostTypeExtend id) throws Exception{
        String type = up.getType();
        CostType cost = this.getCost(up);
        CostType result = costTypeMapper.selectParentId(cost);//是否有该条数据
        
        //判断是否重复插入
        if(cost.getType().equals("1") && result != null){
            if((cost.getOrderTypeCode().equals(result.getOrderTypeCode()) 
                    && cost.getRecourseCode().equals(result.getRecourseCode()))){
                type = "3";
            }
        }         
        
        if(StringUtils.isNotBlank(type)){
                switch (type) {
                /* 增加 */
                    case TYPE_INSERT: 
                        CostTypeExtend costExtend = insertCost(up,id);
                        return costExtend;
                /* 修改 */
                    case TYPE_UPDATE: 
                        String resUpdate = updateBusType(up);
                        break; 
                /* 删除 */
                    case TYPE_DELETE:
                        String resDelete = deleteBusType(up);
                        break;
                    default:
                        throw new IEMRuntimeException("云上行返回业务类型状态错误");
                }
         }
        return id;
    }
 
    
    /**
     * 通用,租户主数据初始化接口
     */
    @Transactional
    @Override
    public boolean getMaster(Map<String, Object> map) {
        BigInteger tenantId = new BigInteger(L);
        //获取主数据定义
        map.put(PAGENUM, ONE);
        map.put(PAGESIZE, Integer.MAX_VALUE);
        String tenantID = Objects.toString(map.get("tenantId"),L);
        if(!tenantID.equals(L)){
            tenantId = new BigInteger(tenantID);
        }
        //定义主数据三个集合
        List<Map> define,element,value;
        define = element = value = Collections.EMPTY_LIST;
        try{
            if(!Objects.equals(tenantId.toString(), L)){
                //获取租户主数据
                define = cscApiService.getTenantDefine(map);
            }
            //获取通用主数据
            if(define == null){
                define = Collections.EMPTY_LIST;
            }
        // System.out.println("主数据定义:"+JSONTool.toJson(define));
        //清空主数据定义,元素,value
        masterDefineMapper.deleteAll();
        masterElementMapper.deleteAll();
        masterValueMapper.deleteAll();
         // -----------添加主数据定义----------------
        for(Map de : define){
            GetMasterDefine masterD = JSONTool.toObj(JSONTool.toJson(de),GetMasterDefine.class);
            MasterDefine master = new MasterDefine(masterD);
            if(!Objects.equals(tenantId.toString(), L)){
                master.setTenantId(tenantId);
            }
            masterDefineMapper.insert(master);
            //根据主数据定义添加元素
            map.put(CODE, master.getCode());
            if(!Objects.equals(tenantId.toString(), L)){
                //获取租户主数据
                element = cscApiService.getTenantEle(map);    
            }
            //获取通用主数据
            if(element == null) {
                element = Collections.EMPTY_LIST;
            }
            // -----------根据主数据定义添加主数据元素-----------
            for(Map ele : element){
                GetMasterEle getM = JSONTool.toObj(JSONTool.toJson(ele),GetMasterEle.class);
                MasterElement el = new MasterElement(getM,master.getCode());
                //区别于通用主数据和租户主数据
                if(!Objects.equals(tenantId.toString(), L)){
                    el.setTenantId(tenantId);
                }
                //通过定义判断元素的code
                if(masterD.getEleCodeCode().equals(el.getCode())){
                    el.setAlias(CODE);
                    el.setLen(masterD.getEleCodeDataLen());
                    el.setType(masterD.getEleCodeDataType());
                }
                //通过定义判断元素的name
                if(masterD.getEleNameCode().equals(el.getCode())){
                    el.setAlias(VALUE);
                    el.setLen(masterD.getEleNameDataLen());
                    el.setType(masterD.getEleNameDataType());
                }
                //添加主数据元素
                masterElementMapper.insert(el);
            }
                /**
                 * ----------根据主数据定义添加主数据value---------
                 * ----------------租户and通用----------------
                 */
                //更换查询主数据元素的参数
                map.remove(CODE);
                map.put(DEFINE_CODE, master.getCode() );
                if(!Objects.equals(tenantId.toString(), L)){
                    //获取租户主数据
                
                    value = cscApiService.getTenantValue(map);    
                }
                //获取通用主数据
                if(value == null) {
                    value = Collections.EMPTY_LIST;
                }
                if(ObjectUtil.empty(value) || value.stream().findFirst().orElse(null) == null){
                    continue;
                }
                //添加主数据value
                for(Map val: value){
                    GetMasterValue Gval = JSONTool.toObj(JSONTool.toJson(val),GetMasterValue.class);
                    for(Rows row:Gval.getRows()){
                        for(Columns col :row.getColumns()){
                            MasterValue mValue = new MasterValue(col,Gval.getMasDataDefCode());
                            if(!Objects.equals(tenantId, L)){
                                mValue.setTenantId(tenantId.toString());
                            }
                            masterValueMapper.insert(mValue);
                        }
                        }
                    }
                
            }
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return false;
        }
        return true;
    }
    /**
     * 同步增量公司业务类型
     * zz
     */
    @Override
    public int updateBusTypes(String compId, String tenantId, String type, List<UpdataBusType> updataBusType){
 
        String mdCode = SpringUtil.getBean(CorresElFieldApi.class).getVal(BUSINESS_TYPE, CorresElField::getMdCode);
        //修改主数据版本号
        VersionFilterApi.filter(mdCode, Context.getTenantIdStr(), Context.getCompanyIdStr());
 
        //通过公司id、租户id选择表
        List<UpdataBusType> u =new ArrayList<>();
        //将递归数据改为列表
        this.getUpdataBusTypeList(updataBusType,u);
        int i=0;
        switch(type){
        case INSERT_BUSTYPE:
            i = addBusType(u);
            break;
        case DELETE_BUSTYPE:
            i = delBusType(u,updataBusType);
            break;
        case UPDATE_BUSTYPE:
            i = updBusType(u);
            break;
            default:break;
        }
        return i;
    }
 
    @Override
    public Map<String, Object> backRule(Map<String, Object> billInfo) {
        Map<String,Object> m = cscApiService.backRule(new ModelMap("orderCode",billInfo.get("orderCode")));
        //清空现有表单数据
        BillInfo bi = new BillInfo();
        bi.setDataStart(2);
        bi.setInfo("");
        bi.setOrderCode(Objects.toString(billInfo.get("orderCode"),""));
        billInfoMapper.updateBill(bi);
        //修改表单单据状态
        FormDataEntity formData = new FormDataEntity(Objects.toString(billInfo.get("formId")),
                Integer.parseInt(Objects.toString(billInfo.get("dataRowNum"),"0")),ThreadData.get(Keys.TENANT_ID));
        formData.setMap(new ModelMap("orderStatus",10));
        RestInterface restInterface = SpringUtil.getBean(SERVER_FORM.getBeanName(), RestInterface.class);
        Object data = restInterface.post(FormUrlConst.FORM_DATA_UPDATFORM, formData, ResMsg.class, EpcRestInterface.getEpcHeads()).getData();
 
        return m;
    }
 
    private int addBusType(List<UpdataBusType> updataBusType){
        // System.out.println("//增加业务类型");
        int i = 0;
        if(ObjectUtil.empty(updataBusType)){
            return i;
        }
        for(UpdataBusType up:updataBusType){
            if(costTypeMapper.selectByName(up.getId()) == 0){
                // System.out.println("没有-插入");
                i += costTypeMapper.insertUpdataBusType(up);
            }
        }
        return i;
    }
    private int delBusType(List<UpdataBusType> updataBusType,List<UpdataBusType> updata){
        // System.out.println("//删除业务类型");
        int i = 0;
        if(ObjectUtil.notEmpty(updataBusType)) {
            for (UpdataBusType up : updataBusType) {
                if (up.getParentId() != 0) {
                    // System.out.println("删除zi");
                    i += costTypeMapper.delteByCodeId(up.getId());
                }
            }
        }
        if(ObjectUtil.notEmpty(updata)) {
            for (UpdataBusType up : updata) {
                if (up.getParentId() == 0) {
                    if (costTypeMapper.selectConstCounts(new Integer(up.getId())) == 0) {
                        // System.out.println("删除fu");
                        i += costTypeMapper.delteByCodeId(up.getId());
                    }
                }
            }
        }
        return i;
    }
    private int updBusType(List<UpdataBusType> updataBusType){
        // System.out.println("//更改业务类型");
        int i = 0;
        if(ObjectUtil.empty(updataBusType)){
            return i;
        }
        for(UpdataBusType up:updataBusType){
            costTypeMapper.updateByCodeId(up);
        }
        return i;
    }
    
    //递归变列表后的数据
    
    //递归变列表
    private void getUpdataBusTypeList(List<UpdataBusType> updataBusTypes,List<UpdataBusType> u){
        if(ObjectUtil.empty(updataBusTypes) || null == u){
            return;
        }
        for(UpdataBusType up:updataBusTypes){
            if(up != null){
                u.add(up);
                if(up.getChildren()!=null && up.getChildren().size()!=0){
                    getUpdataBusTypeList(up.getChildren(),u);
                }
            }
        }
    }
 
    
 
}