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
package cn.autoform.excel.impl;
 
import cn.autoform.bean.CodeAndValueBean;
import cn.autoform.db.entity.FieldPropertyEntity;
import cn.autoform.db.entity.FormBaseEntity;
import cn.autoform.db.entity.FormFieldEntity;
import cn.autoform.db.exten.MasterValue;
import cn.autoform.excel.Excel;
import cn.autoform.factory.FormFactory;
import cn.autoform.factory.product.ProductMethod;
import cn.autoform.factory.product.ProductMethod.IOType;
import cn.autoform.fw.utility.ConstMap;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.JSONTool;
import cn.autoform.web.client.MasterClient;
import cn.autoform.web.controller.datamanagement.DataManagementController;
import cn.autoform.web.interceptor.spring.InitParamInterceptor;
import cn.autoform.web.mapper.datamanagement.DataManagementMapper;
import cn.autoform.web.mapper.formset.FormSetMapper;
import cn.autoform.web.service.datamanagement.DataManagementService;
import cn.autoform.web.service.formbase.FormBaseService;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import jxl.CellView;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Slf4j
@Service
@Scope("prototype")
public class ExcelImpl implements Excel {
    
    private final String YEAR = "20";
    
    private static final String TAGATTRIBUTE = "tag_Attribute"; 
    
    private static final String FIELDSET = "fieldset";
 
    @Resource
    private FormSetMapper formSetMapper;
 
    static final ExecutorService pool = Executors.newFixedThreadPool(10);
 
    @Autowired
    private DataManagementController dataManagementController = null;
    
    @Autowired
    private DataManagementService dataManagementService = null;
 
    @Autowired
    private FormFactory formFactory = null;
    
    @Resource
    DataManagementMapper dataManagementMapper;
    
    @Autowired
    private FormBaseService formBaseService = null;
    
    private String DATAROWNUM = "DATAROWNUM";
 
    /**
     * 主数据缓存
     */
    private Map<Integer,List<CodeAndValueBean>> masterCatch = new HashMap<Integer,List<CodeAndValueBean>>();
 
    @Autowired
    private MasterClient masterClient;
 
    /**
     * 导出模板
     * @param formID
     * @param tenantID
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public String expMod(String formID, String tenantID, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //导出模版
        //取出当前表单名
        String formName = formSetMapper.selectBuss(formID,tenantID)+new SimpleDateFormat("yyyyHHddhhmmss").format(new Date());
        System.out.println("当前表单:"+formName);
        OutputStream out = response.getOutputStream();// 取得输出流
        response.reset();// 清空输出流 
        response.setHeader("Access-Control-Allow-Origin", "*"); //跨域
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(formName+".xls", "UTF-8"));
        response.setContentType("application/octet-stream");// 指明response的返回对象是文件流,通知浏览器下载文件而不是打开
        WritableWorkbook workbook = Workbook.createWorkbook(out); //生成于response输出流
        //创建sheet页(工作表)
        WritableSheet sheet = workbook.createSheet("main", 0); //页名,页码
        WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 12);
        //定义样式,在该实例中可以定义字体,背景颜色,位置等
        WritableCellFormat format = new WritableCellFormat(wfont);
        //查询表单字段
        List<FormFieldEntity> rsList = dataManagementService.queryFormFields(formID, tenantID);
        DataMain subForm = new DataMain();
        rsList.stream().forEach(formField ->{
            if(formField.getFieldtype().equals("subform")){
                subForm.getSubList().put(Objects.toString(formField.getColumnOrderNum(),"-1")
                        ,new ArrayList<>());
            }
        });
        System.out.println(rsList);
        System.out.println("表单数据:"+ JSONTool.toJson(rsList.get(0)));
        getModelData(JSONTool.toObj(rsList.get(0).getFieldset(),Map.class) ,subForm);
        //首先生成main页面(主表单)
        Label label = null;
        Integer i = 0;
        for(String in :subForm.getMain()){
            label = new Label(i, 0,in);
            sheet.addCell(label);
            i++;
        };
        //生成子表单
        System.out.println("这是撒"+JSONTool.toJson(subForm));
        subForm.getSubList().keySet().stream()
                .forEach(o->{
                    Integer pa = 1;
                    WritableSheet subSheet = workbook.createSheet(o, pa); //页名,页码
                    WritableFont subWfont = new WritableFont(WritableFont.createFont("楷书"), 12);
                    //定义样式,在该实例中可以定义字体,背景颜色,位置等
                    WritableCellFormat subFormat = new WritableCellFormat(subWfont);
                    Label subLabel = null;
                    Integer s = 1;
                    try {
                        //单独添加一个行
                        subSheet.addCell(new Label(0, 0,"num"));
                        for(String in :subForm.getSubList().get(o)){
                            subLabel = new Label(s, 0,in);
                                subSheet.addCell(subLabel);
                            s++;
                        }
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }finally {
                        pa++;
                    }
 
                });
        workbook.write();
        workbook.close();
        out.flush();
        out.close();
        return null;
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public String  expExcel(String formID, String tenantID, IOType flag, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //新开线程
//        ImpExcelThread impExcelThread = new ImpExcelThread();
        //导出为true
        ImpExcelThread impExcelThread = SpringUtil.getBean(ImpExcelThread.class);
        impExcelThread.setFormId(formID);
        impExcelThread.setTenantID(tenantID);
        impExcelThread.setRequest(request);
        impExcelThread.setResponse(response);
        impExcelThread.setCookie(copyCookie());
//        pool.submit(new Thread(impExcelThread));
        impExcelThread.run();
        //导出表单
        //查询表单信息,获取表单名
        FormBaseEntity formBaseEntity = formBaseService.getFormBaseInfo(tenantID, formID,null);
        String formName = formBaseEntity.getFormName();
        OutputStream out = response.getOutputStream();// 取得输出流
        response.reset();// 清空输出流 
        response.setHeader("Access-Control-Allow-Origin", "*"); //跨域
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(formName + ".xls", "UTF-8"));
        response.setContentType("application/octet-stream");// 指明response的返回对象是文件流,通知浏览器下载文件而不是打开
        //创建一个工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(out);
        //创建sheet页(工作表)
        WritableSheet sheet = workbook.createSheet("sheet1", 0); //页名,页码
        //定义样式,在该实例中可以定义字体,背景颜色,位置等 
        WritableCellFormat format = new WritableCellFormat(); 
        CellView cellView = new CellView();
        cellView.setAutosize(true); //设置自动大小
        //查询表单字段
        List<FormFieldEntity> rsList = dataManagementService.queryFormFields(formID, tenantID);
        //获得子表单个数
        List<Integer> subForm = new ArrayList<>();
        rsList.stream().forEach(formField ->{
            if(formField.getFieldtype().equals("subform")){
                subForm.add(formField.getColumnOrderNum());
            }
        });
        Map<Integer, Integer> subField = new HashMap<>();
        if(subForm.size()> 0){
            subForm.stream().forEach(columnOrderNum ->{
                rsList.stream().filter(formField -> formField.getParentsubFormNum().equals(columnOrderNum) && !formField.getFieldKey().equals(getDATAROWNUM()))
                    .forEach(formField -> {
                        int i = 1;
                        subField.put(columnOrderNum,i);
                        i++;
                        }
                    );
            });
        }
        //字段名(主+子)
        List<Object> filedName = new ArrayList<>(); 
        StringBuilder subname = new StringBuilder(); //装子表单序号
        int mainfiled = 0; //主表单字段数
        List<Integer> subfiled = new ArrayList<>(); //子表单字段统计
        //统计主表单字段数
        for (int i = 0; i < rsList.size(); i++) {
            if (!"subform".equals(rsList.get(i).getFieldtype()) && rsList.get(i).getParentsubFormNum() ==0) {
                mainfiled++;
            }
        }
        //字段写入题头(第二行)
        Label label = null;
        int j = 0;
        int subc = 0;
        int begin = 0;
        String wToS = "main";//以子表单开头还是主表单开头
        List<Map<Integer, Integer>> why = new ArrayList<>();
        for (int i = 0; i < rsList.size(); i++) {
            if ("subform".equals(rsList.get(i).getFieldtype())) {
                if(i ==0){
                    wToS = "subForm";
                    begin = j;
                }
                Map<Integer, Integer> where = new HashMap<>();
                where.put(rsList.get(i).getColumnOrderNum(),j);
                why.add(where);
                label = new Label(j, 0, rsList.get(i).getFieldKey(), format);
                sheet.setColumnView(j, cellView);//根据内容自动设置列宽
                subname.append(rsList.get(i).getColumnOrderNum());
                subname.append(",");
                subfiled.add(subc);
                subc = 0;
            } else {
                if(rsList.get(i).getParentsubFormNum() == 0){
                        sheet.mergeCells(j,0,j,1);
                    label = new Label(j, 0, rsList.get(i).getFieldKey(), format);    //1.列 2.行 3.数据
                }else{
                    label = new Label(j, 1, rsList.get(i).getFieldKey(), format);
                    if(!(getDATAROWNUM()).equals(rsList.get(i).getFieldKey()))
                      subc++ ;
                    
                }
                j++;
                filedName.add(rsList.get(i).getFieldKey());
            }
            sheet.addCell(label);
        } 
        if(wToS.equals("main")){
            sheet.mergeCells(begin, 0, j-1, 0);//
        }else{
            if(why.size()>0){
                why.stream().forEach(wh ->{
                    Iterator<Integer> it = wh.keySet().iterator();
                    while(it.hasNext()){
                        int i = it.next();
                        try {
                            sheet.mergeCells(wh.get(i), 0,wh.get(i) + subField.get(i), 0);
                        } catch (WriteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
        subfiled.add(subc);
        subfiled.remove(0); //去掉第一位
        String[] subNameArr = subname.toString().split(","); //子表单序号数组
        //查询主表单数据,统计数据条数
        ProductMethod productMethod = this.formFactory.createFormMethod();
        List<Map<String, Object>> rsMap =null;
        //查询当前表单所有数据
        rsMap = productMethod.queryFormDataList(formID, tenantID, null);
        
        //取得数据行号datarowNum
        List<Integer> datarowNum = new ArrayList<>();
        for (int i = 0; i < rsMap.size(); i++) {
            Map<String, Object> map = rsMap.get(i);
            datarowNum.add(Integer.parseInt(map.get("DATAROWNUM").toString()));
        }
        //逐条查询表单数据
        int count = rsMap.size(); //表单数据条数
        int line = 2; //数据在表格中所在的行
        //循环查询单条数据,datarowNum:数据行号,formdataNum:子表单序号
        for (int i = 0; i < count; i++) {
            boolean subWhere = true;//判断子表单是不是第一个控件,默认为第一个
            Map<String, Object> single = productMethod.queryFormDataList(formID, tenantID, datarowNum.get(i), subname.toString());
            //主表单
            Iterator<String> whForm= single.keySet().iterator();
            String toMerge = null;//判断是否合并 默认为合并
            int filedNameNum = 0;
            int maxL = 0;//最大长度
            while(whForm.hasNext()){
                String formW = whForm.next().toString();
                if ("main".equals(formW)) {
                    String todo = null;
                    int realine = line;
                    if(toMerge != null){
                        realine = line-maxL;
                        todo = "do";
                    }
                    toMerge = "NO";
                    subWhere = false;
                    if(wToS.equals("main")){
//                        mainfiled = filedNameNum + mainfiled;
                        filedNameNum = 0;
                    }
                    Map<String, Object> mainData = (Map<String, Object>)single.get("main");
                    for (int x = filedNameNum; x < mainfiled; x ++) {
                        String value = mainData.get(filedName.get(x)).toString();
                        if (value.indexOf("★☆") != -1) { //复选数据替换为,间隔
                            label = new Label(x, realine, value.replaceAll("★☆", ","), format);
                        } else {
                            label = new Label(x, realine, value, format);    //1.列 2.行 3.数据
                        }
                        sheet.addCell(label);
                        if("do".equals(todo)){
                            sheet.mergeCells(x, realine, x, realine + maxL -1);
                        }
                    }
                }
                //子表单 
                if ("subs".equals(formW)) {
                    //全部子表单
                    Map<Integer, List<Map<String, Object>>> subs = (Map<Integer, List<Map<String, Object>>>)single.get("subs");
                    int maxC = 0;
                    for (int k = 0; k < subNameArr.length; k++) { //统计数据最多条数
                        List<Map<String, Object>> subList = (List<Map<String, Object>>)subs.get(subNameArr[k]);
                        if (subList!=null &&subList.size() > maxC) {
                            maxC = subList.size();
                        }
                    }
                    maxL = maxC;
                    if(!subWhere){
                        //合并主表单元格
                        for(int cul=0; cul<mainfiled; cul++){
                            sheet.mergeCells(cul, line, cul, line+maxC -1);
                        }
                    }else{
                        filedNameNum = this.dataManagementMapper.getsubFormFieldCount(formID, tenantID);
                    }
                    for (int c = 0; c < maxC; c++) {
                         
                        int n = (wToS.equals("subForm") ? 0 : mainfiled);
                        for (int k = 0; k < subNameArr.length; k++) {
                            //每个子表单
                            List<Map<String, Object>> subList = (List<Map<String, Object>>)subs.get(subNameArr[k]);
                            if(maxC>1){
                                toMerge = "YES";
                            }
                            for (int m = 0; m < subfiled.get(k); m++) {
                                if (subList.size() > c) {
                                    Map<String, Object> subData = subList.get(c);
                                    String value = subData.get(filedName.get(n+m)).toString();
                                    if (value.indexOf("★☆") != -1) { //复选数据替换为,间隔
                                        label = new Label(n+m, line, value.replaceAll("★☆", ","), format);
                                    } else {
                                        label = new Label(n+m, line, value, format); //1.列 2.行 3.数据
                                    }
                                    
                                    sheet.addCell(label);
                                }
                            }
                            n = n + subfiled.get(k);
                        }
                        line++;
                    }
                }
            }
//            line++;
        }
        workbook.write();
        workbook.close();
        
        out.flush();
        out.close();
        
        return null;
    }
 
    @Override
    public String impExcel(MultipartFile file, String formID, String tenantID, String userName, IOType flag, HttpServletRequest request , HttpServletResponse response) throws Exception {
        //新开线程
        ImpExcelThread impExcelThread = SpringUtil.getBean(ImpExcelThread.class);
        impExcelThread.setFile(file);
        //导入为true
        impExcelThread.setFlag(true);
        impExcelThread.setFormId(formID);
        impExcelThread.setTenantID(tenantID);
        impExcelThread.setUserName(userName);
        impExcelThread.setRequest(request);
        impExcelThread.setResponse(response);
        impExcelThread.setCookie(copyCookie());
        impExcelThread.run();
//        pool.submit(impExcelThread);
 
 
        return null;
    }
    
    // 验证数字格式
    public boolean isNumeric(String str){ 
        Pattern pattern = null;
        if(str.indexOf(".")>0){
            pattern = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$");
        }else{
            pattern = Pattern.compile("^[0-9]*$");
        }
        // +表示1个或多个,*表示0个或多个,?表示0个或1个
        // "[0-9]*" 正数
        // "^-?[0-9]+" 负数
        //"-?[0-9]+.?[0-9]+" 全部数字
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            System.out.println("数字格式错误:++++++++++++++"+str);
            return false; 
        } 
        return true; 
    }
    
    //验证日期格式
    public boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
           try {
               // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
               format.setLenient(false);
               format.parse(str);
           } catch (ParseException e) {
               // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
              System.out.println("日期格式错误:++++++++++++"+str);
               convertSuccess = false;
           }
           return convertSuccess;
    }
    
 
    /**
     * 导入子表单
     */
    @Override
    public List<List<String>> imgSonExcel(Integer rowNum,String formID, String tenantID, String userName,
            MultipartFile file, IOType flag,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        Workbook workbook = Workbook.getWorkbook(file.getInputStream());
        //取得工作页
        Sheet sheet = workbook.getSheet(0);
        
        //查询表单字段
        List<FormFieldEntity> formList = dataManagementService.queryFormFields(formID, tenantID);
        //获得当前子表单
        Map<Integer,String> types1 = new HashMap<>();
        for(FormFieldEntity form: formList){
            if(Objects.equals(form.getParentsubFormNum(), rowNum) && !form.getFieldKey().equals("DATAROWNUM")){
                types1.put(Integer.parseInt(Objects.toString(form.getColumnOrderNum(),"0")),form.getFieldtype());
            }
        }
        Set<Integer> types2 = types1.keySet();
        List<Integer> types = new ArrayList<>();
        types2.forEach(o -> types.add(o));
        List<List<String>> lists = new ArrayList<>();
 
        for(int c=0; c< sheet.getColumns(); c++){
            String title = sheet.getCell(c, 0).getContents();
            if("".equals(title)|| title == null){
                continue;
            }
            Integer itemId = this.getItemId(JSONTool.toObj(JSONTool.toJson(formList.get(0)),Map.class).get(FIELDSET),title);
            List<String> list = new ArrayList<>();
            row:
            {
                for (int r = 1; r < sheet.getRows(); r++) {
                    if (c > types.size() - 1) {
                        continue;
                    }
                    //验证是否必填
                    if (this.isMustCheck(formID, tenantID, itemId)) {
                        String v = sheet.getCell(c, r).getContents();
                        if ("".equals(v) || v == null) {
                            break row;
                        }
                    }
                    if (types1.get(itemId).equals("number")) {
                        if (!this.isNumeric(sheet.getCell(c, r).getContents())) {
                            break row;
                        }
                        list.add(sheet.getCell(c, r).getContents());
                    } else if (types1.get(itemId).equals("calendar")) {
                        if (!this.isValidDate(sheet.getCell(c, r).getContents())) {
                            break row;
                        }
                        list.add(YEAR + sheet.getCell(c, r).getContents());
                        //获取excel的下拉列表和树型的值
                    } else if (types1.get(itemId).equals("dropdownlist") || types1.get(itemId).equals("tree")) {
                        String value = sheet.getCell(c, r).getContents();
                        //调用获取Code 的方法
                        list.add(getCode(formList, value, itemId, types1.get(itemId), tenantID));
                    } else {
 
                        list.add(sheet.getCell(c, r).getContents());
                    }
                }
            }
            lists.add(list);
        }
        this.masterCatch.clear();
        return lists;
    }
 
    //验证控件属性是否必填 返回是否必填
    public boolean isMustCheck(String formID, String tenantID, Integer columnOrderNum){
        List<FieldPropertyEntity> fieldPropertys= dataManagementMapper.queryFormFieldProperty(formID, tenantID, columnOrderNum)
                .stream().
                    filter(formField ->"mustcheck".equals(formField.getProperty()))
                        .collect(Collectors.toList());
        if(Objects.isNull(fieldPropertys)){
            return true;
        }else{
            return "true".equals(fieldPropertys.get(0).getValue()) ? true : false;
        }
    }
 
    private String getDATAROWNUM() {
        return DATAROWNUM;
    }
 
    /**
     * 主数据value转code
     */
    @SuppressWarnings("unchecked")
    public String getCode(List<FormFieldEntity> formList,String value,Integer number,String fieldType,String tenantId) throws Exception{
        List<CodeAndValueBean> master = this.masterCatch.get(number);
        
        if(master!= null){
            return getValue(value,master);
        }
        //取出表单控件中的主数据
        String d = formList.get(0).getFieldset();
        Map<String,Object> map = JSONTool.toObj(d,Map.class);
        Map<String,Object> map1 = JSONTool.toObj(JSONTool.toJson(map.get("itemId"+"_"+number)),Map.class);
        
        Map<String,Object> map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class);
        String code = Objects.toString(map2.get("masterData"),null);
        //查询费用云主数据
        String datasourceType = Objects.toString(map2.get("datasourcetype"));
        if(Objects.equals(datasourceType,"masterdata")){
            System.out.println("进入主数据转换");
            JSONObject jsonObj = JSONObject.fromObject(code);
            String defindCode = null;
            String eleCode = null;
            if (jsonObj.has("defindCode")) {
                defindCode = jsonObj.getString("defindCode");
            }
            if (jsonObj.has("eleCode")) {
                eleCode = jsonObj.getString("eleCode");
            }
 
            if (defindCode != null && eleCode != null) {
                String eleName = null;
                if (jsonObj.has(ConstMap.ELENAME)) {
                    eleName = jsonObj.getString(ConstMap.ELENAME);
                }
                String parentCode = null;
 
                if (jsonObj.has("parentCode")) {
                    parentCode = jsonObj.getString("parentCode");
                }
 
                //筛选主数据
                MasterValue MasterValue = new MasterValue();
                MasterValue.setTenantID(tenantId);
                MasterValue.setDefineCode(defindCode);
                MasterValue.setELE_CODE(eleCode);
                MasterValue.setParentCode(parentCode);
                MasterValue.setEleName(eleName);
                System.out.println("主数据参数:" + JSONTool.toJson(MasterValue));
 
                List<CodeAndValueBean> result1 = this.masterClient.queryClientMasterValues(MasterValue);
                masterCatch.put(number, result1);
                System.out.println("Value值" + ":" + value);
                return getValue(value, result1);
            }
        }else if(Objects.equals(datasourceType,"custom")){
            System.out.println("进入自定义转换");
            List<Map> customMap = JSONTool.toList(JSONTool.toJson(map2.get("custom")),Map.class);
            List<CodeAndValueBean> res = new ArrayList<>();
            customMap.stream().forEach(o->res.add(new CodeAndValueBean(Objects.toString(o.get("code")),Objects.toString(o.get("itemtext")))));
            masterCatch.put(number, res);
            return getValue(value, res);
        }
 
        return null;
    }
 
 
 
    
    public String getValue(String name,List<CodeAndValueBean> result){
        for(CodeAndValueBean con : result){
            if(name.equals(con.getValue())){
                System.out.println("转换后code:"+con.getCode());
                return con.getCode();
            }
        }
        return "";
    }
    /**
     * 导入子表单模板
     * @return
     * @throws IOException 
     * @throws WriteException 
     */
    public String expSonExcel(String data,String formID, String tenantID,String number, HttpServletRequest request, HttpServletResponse response) throws IOException, WriteException{
        //最终表头字段
        Map<String,Object> map = new HashMap<>();
        OutputStream out = response.getOutputStream();// 取得输出流
        response.reset();// 清空输出流 
        response.setHeader("Access-Control-Allow-Origin", "*"); //跨域
        response.setHeader("content-disposition", "attachment;filename=" + new SimpleDateFormat("yyyyHHddhhmmss").format(new Date()) + ".xls");
        response.setContentType("application/octet-stream");// 指明response的返回对象是文件流,通知浏览器下载文件而不是打开
        WritableWorkbook workbook = Workbook.createWorkbook(out); //生成于response输出流
        //创建sheet页(工作表)
        WritableSheet sheet = workbook.createSheet("sheet1", 0); //页名,页码
        WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 12);
        //定义样式,在该实例中可以定义字体,背景颜色,位置等
        WritableCellFormat format = new WritableCellFormat(wfont);
        //查询表单字段
        List<String> list = JSONTool.toList(data, String.class);
        //单元格
        Label label = null;
        Integer i = 0;
        for(String in :list){
            label = new Label(i, 0,in);
            sheet.addCell(label);
                    i++;
                };
        workbook.write();
        workbook.close();
        
        out.flush();
        out.close();
        
        return null;
    }
 
    @Override
    public String newImpExcel(MultipartFile file, String formID, String tenantID, String userName, IOType flag, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return null;
    }
 
    /**
     * 通过title获取相应得 item_id
     * 
     */
    @SuppressWarnings("unchecked")
    public Integer getItemId(Object fieldSet,String title){
        Map<String,Object> map = JSONTool.toObj(fieldSet.toString(),Map.class);
        for(String o :map.keySet()){
            Map<String,Object> m = JSONTool.toObj(JSONTool.toJson(map.get(o)),Map.class);
            Map<String,String> m1 = JSONTool.toObj(JSONTool.toJson(m.get(TAGATTRIBUTE)),Map.class);
            if(title.equals(m1.get("title"))){
                return Integer.parseInt(Objects.toString(m.get("columnOrderNum"),"0"));
            }
        };
        return 0;
    }
 
 
 
    /**
     * 自定义属性code转value
     */
    public String getCoustCode(List<FormFieldEntity> formList,String value,Integer number,String fieldType,String tenantId) throws Exception{
        List<CodeAndValueBean> master = this.masterCatch.get(number);
 
        if(master!= null){
            return getValue(value,master);
        }
        //取出表单控件中的主数据
        String d = formList.get(0).getFieldset();
        Map<String,Object> map = JSONTool.toObj(d,Map.class);
        Map<String,Object> map1 = JSONTool.toObj(JSONTool.toJson(map.get("itemId"+"_"+number)),Map.class);
 
        Map<String,Object> map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class);
 
        return null;
    }
 
    Map<String,List<String>> subList = new HashMap<>();
    /**
     * 取出需要导出模板得字段
     * @return
     */
    public void getModelData(Map<String,Object> map ,DataMain dataMain){
        System.out.println("参数:"+ JSONTool.toJson(dataMain));
        List<String> mainList = new ArrayList<>();
        Map<String,String> subName = new HashMap<>();
        map.keySet().stream().forEach(o->{
            Map<String,Object> item = JSONTool.toObj(JSONTool.toJson(map.get(o)),Map.class);
            String tagType = Objects.toString(item.get("tag_Type"));
            Map<String,Object> attr = JSONTool.toObj(JSONTool.toJson(item.get(TAGATTRIBUTE)),Map.class);
            String title = Objects.toString(attr.get("title"));
            String parentsubFormNum = Objects.toString(item.get("parentsubFormNum"),"-1");
            if(!Objects.equals(tagType,"mytext")) {
                if (Objects.equals("0", parentsubFormNum)
                        && !Objects.equals(tagType, "subform")) {
                    System.out.println("-------->主表控件:" + title);
                    mainList.add(title);
                } else if (!Objects.equals("0", parentsubFormNum)) {
                    System.out.println("-------->子表控件:" + title);
                    List<String> li = dataMain.getSubList().get(parentsubFormNum);
                    li.add(title);
                    dataMain.getSubList().put(parentsubFormNum, li);
                } else if (Objects.equals(tagType,"subform")) {
                    System.out.println("-------->子表单:" + title);
                    String column = Objects.toString(item.get("columnOrderNum"), "-1");
                    subName.put(column, title);
                }
            }
        });
        dataMain.setMain(mainList);
        //转换子表单别名
        System.out.println("最后数据:"+ JSONTool.toJson(dataMain));
        Map<String,List<String>> subL = dataMain.getSubList();
        Map<String,List<String>> newSub = new HashMap<>();
        System.out.println("所有子表单:"+ JSONTool.toJson(subName));
        subL.keySet().stream()
                .forEach(o->
                    newSub.put(subName.get(o),dataMain.getSubList().get(o))
        );
        dataMain.setSubList(newSub);
    }
 
    //获取cookie
    public Map<String,Object> copyCookie(){
        Map<String, Object> cookie = new HashMap<>();
        InitParamInterceptor.COOKIE_KEY.entrySet().stream()
                .filter(cookieKey->{
                    return StringUtils.isNoneBlank(Objects.toString(ThreadData.get(cookieKey.getValue()), ""));
                })
                .forEach(cookieKey->{
                    log.debug("当前添加:{},{}", cookieKey.getKey(),ThreadData.get(cookieKey.getValue()));
                    cookie.put(cookieKey.getKey(), ThreadData.get(cookieKey.getValue()));
                });
        return cookie;
    }
 
 
 
    //导出模板字段
@Data
class DataMain{
    //主表字段
    private List<String> main = new ArrayList<>();
    //字表字段
    private Map<String,List<String>> subList = new HashMap<>();
}
}