许鹏程
2023-07-05 5c0d2ecb9476515cab0f1cc52ba49b02a5149a67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
package com.product.module.data.service;
 
import com.google.common.collect.Sets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.product.admin.service.UpdateLoginUserInfoService;
import com.product.common.lang.StringUtils;
import com.product.common.utils.IdcardUtils;
import com.product.core.config.Global;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringBeanUtil;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.data.config.CmnCode;
import com.product.module.data.config.CmnConst;
import com.product.module.data.service.idel.IOrganizationImportService;
import com.product.module.sys.entity.SystemUser;
import com.product.org.admin.service.StaffManagerService;
import com.product.util.BaseUtil;
import com.product.util.CallBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * @ClassName OrganizationImportService
 * @Description 组织机构导入数据处理
 * @Author cheng
 * @Date 2021/12/2 16:14
 */
@Service
public class OrganizationImportService extends AbstractBaseService implements IOrganizationImportService {
 
 
    @Autowired
    StaffManagerService staffManagerService;
 
    private final String companyLevelSeparator = ">";
 
    /**
     * 导入公司 之前 数据处理
     *
     * @param dt
     * @throws BaseException
     */
    @Override
    public DataTableEntity importCompanyBefore(DataTableEntity dt) throws BaseException {
        ImportErrorEntity errorEntity = new ImportErrorEntity();
        try {
 
            SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
            //管理员权限验证
            managerPermissionValidation(errorEntity, currentUser);
            FieldSetEntity managerFieldSet = currentUser.getCurrentManager();
            String managerCompany = managerFieldSet.getString(CmnConst.ORG_LEVEL_UUID);
            // 当前管理员管理的公司
            String managerCompanyUuids = currentUser.getCurrentCompany().getString(CmnConst.ORG_LEVEL_UUID);
            if (!BaseUtil.dataTableIsEmpty(dt)) {
                //数据验证结果
                Map<String, String> fieldType = Maps.newHashMap();
                fieldType.put(CmnConst.SEQUENCE, "int");
                errorEntity = fieldRequiredValidation(dt, new String[]{CmnConst.FIELD_ORG_LEVEL_NAME, CmnConst.FIELD_ORG_LEVEL_ALL, CmnConst.SEQUENCE}, null, f -> {
                    if (f != null && f[0] != null) {
                        FieldSetEntity fse = f[0];
                        //公司名称
                        String companyName = fse.getString(CmnConst.FIELD_ORG_LEVEL_NAME);
                        //公司全称
                        String companyAllName = fse.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
                        //上级公司全称
                        String parentAllName = companyAllName.replace(companyLevelSeparator + companyName, "");
                        fse.setValue("parentAllName", parentAllName);
                    }
                });
                DataTableEntity systemData = getBaseDao().listTable(CmnConst.TABLE_PRODUCT_SYS_ORG_LEVELS, " org_level_type=0 AND "
                        + BaseUtil.buildQuestionMarkFilter(CmnConst.ORG_LEVEL_UUID, managerCompanyUuids.split(","), true), new Object[]{}, "org_level_code");
                // 验证重复 & 合并错误信息
                errorEntity.mergeError(fieldRepeatValidation(dt, systemData, CmnConst.FIELD_ORG_LEVEL_ALL));
                if (errorEntity.isError) {
                    errorEntity.newThrowBaseException();
                }
                List<FieldSetEntity> data = dt.getData();
                data.sort((a, b) -> {
                    String Aorg_level_all = a.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
                    String Borg_level_all = b.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
                    int lengthA = Aorg_level_all != null ? Aorg_level_all.length() : -1;
                    int lengthB = Borg_level_all != null ? Borg_level_all.length() : -1;
                    return lengthA - lengthB;
                });
                // 获取当前管理管理公司以及子公司
                DataTableEntity allCompany = getBaseDao().listTable(CmnConst.TABLE_PRODUCT_SYS_ORG_LEVELS, " org_level_type=0 AND "
                        + BaseUtil.buildQuestionMarkFilter(CmnConst.ORG_LEVEL_UUID, managerCompanyUuids.split(","), true), new Object[]{}, "org_level_code");
                Map<String, String> levelCode = Maps.newHashMap();
                if (!BaseUtil.dataTableIsEmpty(allCompany)) {
                    for (int i = 0; i < allCompany.getRows(); i++) {
                        String org_level_all = allCompany.getString(i, CmnConst.FIELD_ORG_LEVEL_ALL);
                        String org_level_code = allCompany.getString(i, CmnConst.FIELD_ORG_LEVEL_CODE);
                        // 将公司编码用全称存储
                        levelCode.put(org_level_all, org_level_code);
                    }
                }
                for (FieldSetEntity fse : data) {
                    //当前行
                    Integer rowIndex = fse.getInteger("~row~");
                    //公司全称
                    String companyAllName = fse.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
                    //上级组织机构全称
                    String parentAllName = fse.getString("parentAllName");
                    //上级机构编码
                    String code = levelCode.get(parentAllName);
                    if (StringUtils.isEmpty(code)) {
                        errorEntity.addError(rowIndex, " 获取上级机构编码错误,请检查公司名称、全称是否正确,错误行号:").append(rowIndex + 1);
                        continue;
                    }
                    //初始化公司数据
                    initOrganizationFieldSet(fse, currentUser.getClient_uuid(), code, 0);
                    // 将当前公司生成的org_level_code 放入map中
                    levelCode.put(companyAllName, fse.getString(CmnConst.FIELD_ORG_LEVEL_CODE));
                    String uuid = null;
                    try {
                        // 保存出错直接跳过该条数据
                        getBaseDao().add(fse);
                    } catch (Exception e) {
                        continue;
                    }
                    if (null != uuid) {
                        // 导入时创建该公司的数据权限
                        FieldSetEntity fs = new FieldSetEntity();
                        fs.setTableName(CmnConst.TABLE_PRODUCT_SYS_DATA_STRATEGY_MASTER);
//                        fs.setValue(CmnConst.ORG_LEVEL_UUID, uuid);
                        fs.setValue("property_uuid", "001");
                        fs.setValue("is_used", "1");
                        fs.setValue("effective_start_date", new Date());
                        fs.setValue(CmnConst.CREATED_BY, currentUser.getUser_id());
                        fs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
                        getBaseDao().add(fs);
                    }
                }
                // 回写到隐藏管理员所管理的公司 todo group_concat 太多会报错
                getBaseDao().executeUpdate("UPDATE product_sys_org_manager SET org_level_uuid =(SELECT group_concat(UUID) FROM product_sys_org_levels WHERE CLIENT_UUID =? AND ORG_LEVEL_TYPE =0) WHERE CLIENTS_UUID=? AND MANAGER_TYPE=? "
                        , new Object[]{currentUser.getClient_uuid(), currentUser.getClient_uuid(), 3});
                // 回写当前管理员管理的公司
                if (currentUser.getUserType() == 2 && dt.getUuids() != null) {
                    if (!StringUtils.isEmpty(managerCompany)) {
                        managerCompany += ",";
                    } else {
                        managerCompany = "";
                    }
                    managerCompany += dt.getUuidsToString();
                    String manager_uuid = managerFieldSet.getUUID();
                    String sql = "update product_sys_org_manager set org_level_uuid=? where uuid=?";
                    getBaseDao().executeUpdate(sql, new Object[]{managerCompany, manager_uuid});
                }
                //抛出错误
                if (errorEntity.isError) {
                    errorEntity.newThrowBaseException();
                }
                UpdateLoginUserInfoService loginUserInfoService = SpringBeanUtil.getBean("updateLoginUserInfoService", UpdateLoginUserInfoService.class);
                loginUserInfoService.updateUserInfoByUserId(new Object[]{currentUser.getUser_id()}, false);
                return dt;
            } else {
                errorEntity.newThrowBaseException("导入公司失败,未提取到数据");
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            errorEntity.addError(0, "导入公司失败,未知错误 ," + e.getMessage());
            errorEntity.newThrowBaseException();
        }
        return null;
    }
 
    /**
     * 字段重复验证
     *
     * @param errorEntity
     * @param importData  导入数据
     * @param systemData  系统数据 (可选)
     * @param fieldNames  字段名称
     * @return
     * @throws BaseException
     */
    protected ImportErrorEntity fieldRepeatValidation(ImportErrorEntity errorEntity, DataTableEntity importData, DataTableEntity systemData, String[] fieldNames) throws BaseException {
        if (fieldNames == null || fieldNames.length <= 0) {
            return errorEntity;
        }
        List<FieldSetEntity> dataList = importData.clones().getData();
        Map<String, Object> valueMap = Maps.newHashMap();
        if (!BaseUtil.dataTableIsEmpty(systemData)) {
            for (int i = 0; i < systemData.getRows(); i++) {
                for (String field : fieldNames) {
                    String value = systemData.getString(i, field);
                    if (!StringUtils.isEmpty(value)) {
                        valueMap.put(field + this.companyLevelSeparator + value, Boolean.FALSE);
                    }
                }
            }
        }
        fieldRepeatValidation(errorEntity, dataList, valueMap, fieldNames);
        return errorEntity;
    }
 
    /**
     * 字段重复验证
     *
     * @param dataList   导入数据
     * @param valueMap
     * @param fieldNames 字段名称
     * @throws BaseException
     */
    protected void fieldRepeatValidation(ImportErrorEntity errorEntity, List<FieldSetEntity> dataList, Map<String, Object> valueMap, String[] fieldNames) throws BaseException {
 
        for (FieldSetEntity fse : dataList) {
            DataLocation dataLocation = getDataLocation(fse);
            for (String fieldName : fieldNames) {
                String value = fse.getString(fieldName);
                if (!StringUtils.isEmpty(value)) {
                    if (valueMap.get(fieldName + this.companyLevelSeparator + value) != null) {
                        Object val = valueMap.get(fieldName + this.companyLevelSeparator + value);
                        if (val != null) {
                            String error;
                            if (val instanceof Boolean) {
                                // 系统中存在
                                // 第 N 行,第 N 列,'表头名称' ('数据值') 数据重复,在系统中已存在
                                error = "第 %s 行,第 %s 列 '%s', 数据重复,在系统中已存在";
                                error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(fieldName), dataLocation.getColName(fieldName), value);
                            } else {
                                FieldSetEntity fs = (FieldSetEntity) val;
                                DataLocation historyLocation = getDataLocation(fs);
                                // 第N行,第 N列,'表头名称' ('数据值') 数据和第 %s 行,第N行,第 N列,'表头名称' ('数据值') 数据重复
                                error = "第 %s 行,第 %s 列 '%s', 数据和第 %s 行,第 %s 列 '%s' ,数据重复";
                                error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(fieldName), dataLocation.getColName(fieldName), value,
                                        historyLocation.getRowIndex(), historyLocation.getColIndex(fieldName), historyLocation.getColName(fieldName), value);
                            }
                            errorEntity.addError(dataLocation.getRowIndex(), error);
                        }
                    } else {
                        valueMap.put(fieldName + this.companyLevelSeparator + value, fse);
                    }
                }
            }
        }
    }
 
    /**
     * 字段重复验证
     *
     * @param importData 导入数据
     * @param systemData 系统数据 (可选)
     * @param fieldName  字段名称
     * @return
     * @throws BaseException
     */
    protected ImportErrorEntity fieldRepeatValidation(DataTableEntity importData, DataTableEntity
            systemData, String fieldName) throws BaseException {
        return fieldRepeatValidation(new ImportErrorEntity(), importData, systemData, new String[]{fieldName});
    }
 
    /**
     * 字段必填验证
     *
     * @param dt
     * @return
     * @throws BaseException
     */
    protected ImportErrorEntity fieldRequiredValidation(DataTableEntity dt, String[]
            fieldNames, Map<String, String> fieldType, CallBack<FieldSetEntity> callBack) throws BaseException {
        ImportErrorEntity errorEntity = new ImportErrorEntity();
        for (int i = 0; i < dt.getRows(); i++) {
            FieldSetEntity fieldSetEntity = dt.getFieldSetEntity(i);
            if (callBack != null) {
                callBack.method(fieldSetEntity);
            }
            fieldRequiredValidation(errorEntity, fieldSetEntity, fieldNames, fieldType);
        }
 
        return errorEntity;
    }
 
    /**
     * 字段类型验证
     * 目前支持 int  double dateTime
     *
     * @param errorEntity
     * @param fse
     * @param fieldTypeMap
     * @return
     * @throws BaseException
     */
    protected boolean fieldTypeValidation(ImportErrorEntity errorEntity, FieldSetEntity
            fse, Map<String, String> fieldTypeMap) throws BaseException {
        boolean flag = true;
        if (fieldTypeMap != null && fieldTypeMap.size() > 0 && fse != null) {
            for (Map.Entry<String, String> v : fieldTypeMap.entrySet()) {
                String fieldType = v.getValue();
                String key = v.getKey();
                Object value = fse.getObject(key);
                if (value == null) {
                    continue;
                }
                String type = null;
                String msg = "";
                try {
                    switch (fieldType) {
                        case "int":
                            type = "整数";
                            fse.getInteger(key);
                            continue;
                        case "dateTime":
                            type = "日期";
                            Date date = fse.getDate(key);
                            fse.setValue(key, date);
                            continue;
                        case "double":
                            type = "小数";
                            fse.getDouble(key);
                            continue;
                        case "mobile_phone":
                            type = "移动电话";
                            String phone = fse.getString(key);
                            // 验证是否为中国大陆 手机号
                            if (!BaseUtil.isChinaPhoneLegal(phone)) {
                                throw new BaseException("", "");
                            }
                            continue;
                        case "id_card":
                            type = "身份证号码";
                            String idCard = fse.getString(key);
                            // 验证身份证号码是否合法
                            if (IdcardUtils.validateCard(idCard)) {
                                msg = BaseUtil.idCardVerification(idCard);
                                throw new BaseException("", "");
                            }
                            continue;
                        case "email":
                            type = "邮箱";
                            String email = fse.getString(key);
                            // 邮箱验证
                            if (!BaseUtil.isEmail(email)) {
                                throw new BaseException("", "");
                            }
                            continue;
                        case "sex":
                            type = "性别";
                            String sex = fse.getString(key);
                            // 邮箱验证
                            if (!"男".equals(sex) && !"女".equals(sex)) {
                                throw new BaseException("", "");
                            }
                            continue;
                        default:
                    }
                } catch (BaseException e) {
                    DataLocation dataLocation = getDataLocation(fse);
                    String error = "第 %s 行,第 %s 列,数据类型错误" + (type != null ? ",请输入正确的 '%s'值" : "");
                    if (type != null) {
                        error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(key), type);
                    } else {
                        error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(key));
                    }
                    if (!StringUtils.isEmpty(msg)) {
                        error += "," + msg;
                    }
                    errorEntity.addError(dataLocation.getRowIndex(), error);
                    flag = false;
                }
            }
        }
 
        return flag;
    }
 
 
    /**
     * 字段必填验证
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    private boolean fieldRequiredValidation(ImportErrorEntity errorEntity, FieldSetEntity fse, String[]
            fieldNames, Map<String, String> fieldType) throws BaseException {
        if (fse == null || fieldNames == null || fieldNames.length <= 0) {
            return true;
        }
        DataLocation dataLocation = getDataLocation(fse);
        boolean flag = true;
        for (String fieldName : fieldNames) {
            if (!StringUtils.isEmpty(fieldName)) {
                String value = fse.getString(fieldName);
                if (StringUtils.isEmpty(value)) {
                    String error = "第 %s 行,第 %s 列 '%s', 不能为空";
                    Integer rowIndex = dataLocation.getRowIndex();
                    String colIndex = dataLocation.getColIndex(fieldName);
                    String colName = dataLocation.getColName(fieldName);
                    error = String.format(error, rowIndex, colIndex, colName);
                    errorEntity.addError(rowIndex, error);
                    flag = false;
                }
            }
        }
        if (flag) {
            return fieldTypeValidation(errorEntity, fse, fieldType);
        } else {
            fieldTypeValidation(errorEntity, fse, fieldType);
            return false;
        }
    }
 
    /**
     * 导入部门 之前 数据处理
     *
     * @param dt
     * @throws BaseException
     */
    @Override
    public DataTableEntity importDeptBefore(DataTableEntity dt) throws BaseException {
        ImportErrorEntity errorEntity = new ImportErrorEntity();
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            try {
                SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
                //管理员权限验证
                managerPermissionValidation(errorEntity, currentUser);
 
                Map<String, String> fieldType = Maps.newHashMap();
                fieldType.put(CmnConst.SEQUENCE, "int");
                //数据必填验证结果
                errorEntity = fieldRequiredValidation(dt, new String[]{CmnConst.FIELD_ORG_LEVEL_NAME, CmnConst.FIELD_ORG_LEVEL_ALL, CmnConst.SEQUENCE, CmnConst.ORG_LEVEL_UUID}, fieldType, null);
                if (errorEntity.isError) {
                    errorEntity.newThrowBaseException();
                }
                FieldSetEntity currentManager = currentUser.getCurrentManager();
                String managerCompanyUuids = currentManager.getString(CmnConst.ORG_LEVEL_UUID);
                String uuidFilter = BaseUtil.buildQuestionMarkFilter("uuid", managerCompanyUuids.split(","), true);
                StringBuilder sql = new StringBuilder(8);
                sql.append(" select uuid,org_level_name,org_level_code, ");
                sql.append(" org_level_code_parent,org_level_all,org_level_type ");
                sql.append(" FROM product_sys_org_levels where client_uuid=?  and ( ");
                sql.append(" ( ");
                sql.append("  ").append(uuidFilter);
                sql.append(" AND org_level_type=0 ) ");
                sql.append(" or ( ");
                sql.append("  ").append(uuidFilter.replace("uuid", "org_level_uuid"));
                sql.append(" AND org_level_type=1 ) ");
                sql.append(" )  order by org_level_code,org_level_type");
                // 系统数据
                DataTableEntity systemData = getBaseDao().listTable(sql.toString(), new Object[]{currentUser.getClient_uuid()});
                importDeptBefore(dt, systemData, currentUser, errorEntity);
                return dt;
            } catch (BaseException e) {
                e.printStackTrace();
                throw e;
            } catch (Exception e) {
                e.printStackTrace();
                errorEntity.addError(0, "导入部门失败,未知错误 ," + e.getMessage());
                errorEntity.newThrowBaseException();
            }
        }
        errorEntity.newThrowBaseException("导入公司失败,未提取到数据");
        return null;
    }
 
    private void importDeptBefore(DataTableEntity dt, DataTableEntity systemData, SystemUser
            user, ImportErrorEntity errorEntity) throws BaseException {
        List<FieldSetEntity> data = dt.getData();
        // 机构全称排序
        data.sort(Comparator.comparing(a -> a.getString(CmnConst.FIELD_ORG_LEVEL_ALL)));
        if (BaseUtil.dataTableIsEmpty(systemData)) {
            errorEntity.newThrowBaseException("获取系统机构信息失败,请联系管理员 ");
        }
        List<FieldSetEntity> systemDataList = systemData.getData();
        // 公司
        Map<String, FieldSetEntity> companyMap = Maps.newHashMap();
        // 部门
        Map<String, FieldSetEntity> deptMap = Maps.newHashMap();
        for (FieldSetEntity fse : systemDataList) {
            // 机构类型
            String orgLevelType = fse.getString(CmnConst.FIELD_ORG_LEVEL_TYPE);
            // 机构全称
            String org_level_all = fse.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
            Map<String, FieldSetEntity> map;
            if ("0".equals(orgLevelType)) {
                map = companyMap;
            } else if ("1".equals(org_level_all)) {
                map = deptMap;
            } else {
                continue;
            }
            map.put(org_level_all, fse);
        }
        for (int i = 0; i < dt.getRows(); i++) {
            FieldSetEntity fse = dt.getFieldSetEntity(i);
            // 公司全称
            String companyAllName = fse.getString(CmnConst.ORG_LEVEL_UUID);
            // 部门全称
            String deptAllName = fse.getString(CmnConst.FIELD_ORG_LEVEL_ALL);
            // 部门名称
            String deptName = fse.getString(CmnConst.FIELD_ORG_LEVEL_NAME);
 
            DataLocation dataLocation = getDataLocation(fse);
            FieldSetEntity companyFse = companyMap.get(companyAllName);
            if (companyFse == null || StringUtils.isEmpty(companyFse.getUUID())) {
                String error = "第 %s 行,上级机构获取失败,所属公司不存在,请检查' %s '是否正确:' %s '";
                error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColName(CmnConst.ORG_LEVEL_UUID), companyAllName);
                errorEntity.addError(dataLocation.getRowIndex(), error);
                continue;
            }
            String error = "第 %s 行,上级机构获取失败,上级部门不存在,请检查' %s '是否正确:' %s '";
            // 部门上级全称
            String parentDeptAllName = getParentAllName(deptAllName, deptName);
            if (StringUtils.isEmpty(parentDeptAllName)) {
                error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColName(CmnConst.FIELD_ORG_LEVEL_ALL), deptAllName);
                errorEntity.addError(dataLocation.getRowIndex(), error);
                continue;
            }
            // 上级编码
            String code;
            // 上级机构的uuid
            String parentUuid;
            if (!parentDeptAllName.equals(companyAllName)) {
                FieldSetEntity deptFse = deptMap.get(parentDeptAllName);
                if (deptFse == null || StringUtils.isEmpty(deptFse.getUUID())) {
                    error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColName(CmnConst.FIELD_ORG_LEVEL_ALL), deptAllName);
                    errorEntity.addError(dataLocation.getRowIndex(), error);
                    continue;
                }
                // 取出上级部门的code作为上级编码
                code = deptFse.getString(CmnConst.FIELD_ORG_LEVEL_CODE);
 
                parentUuid = deptFse.getString(CmnConst.ORG_LEVEL_UUID);
            } else {
                // 取出上级公司的code作为上级编码
                code = companyFse.getString(CmnConst.FIELD_ORG_LEVEL_CODE);
                parentUuid = companyFse.getUUID();
            }
            initOrganizationFieldSet(fse, user.getClient_uuid(), code, 1);
            fse.setValue(CmnConst.ORG_LEVEL_UUID, parentUuid);
            getBaseDao().saveFieldSetEntity(fse);
            deptMap.put(deptAllName, fse);
        }
        if (errorEntity.isError) {
            errorEntity.newThrowBaseException();
        }
    }
 
    private String getParentAllName(String allName, String name) {
        if (!StringUtils.isEmpty(allName) && !StringUtils.isEmpty(name)) {
            int index = allName.indexOf(this.companyLevelSeparator + name);
            if (index > -1) {
                return allName.substring(0, index);
            }
        }
        return "";
    }
 
    /**
     * 导入岗位等级 之前 数据处理
     *
     * @param dt
     * @throws BaseException
     */
    @Override
    public DataTableEntity importPostLevelBefore(DataTableEntity dt) throws BaseException {
        ImportErrorEntity errorEntity = new ImportErrorEntity();
        try {
            if (!BaseUtil.dataTableIsEmpty(dt)) {
                SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
                //管理员权限验证
                managerPermissionValidation(errorEntity, currentUser);
                // 当前管理员管理的公司
                String managerCompanyUuids = currentUser.getCurrentManager().getString(CmnConst.ORG_LEVEL_UUID);
                Map<String, List<FieldSetEntity>> groupCompany = Maps.newHashMap();
                Map<String, String> fieldType = Maps.newHashMap();
                fieldType.put(CmnConst.SEQUENCE, "int");
                fieldType.put(CmnConst.FIELD_JOB_GRADE_CLASS, "int");
                StringBuilder sql = new StringBuilder();
                sql.append(" select concat( ");
                sql.append(" org_level_uuid,'>',job_grade_name) uuid ");
                sql.append("  FROM product_sys_job_post_grades where ");
                sql.append(BaseUtil.buildQuestionMarkFilter("org_level_uuid", managerCompanyUuids.split(","), true));
                DataTableEntity systemData = getBaseDao().listTable(sql.toString(), new Object[]{});
                Set<Object> systemDataSet = Sets.newHashSet();
                if (!BaseUtil.dataTableIsEmpty(systemData)) {
                    systemDataSet.addAll(Arrays.asList(systemData.getUuidsToString().split(",")));
                }
                for (int i = 0; i < dt.getRows(); i++) {
                    FieldSetEntity fse = dt.getFieldSetEntity(i);
                    // 字段必填验证
                    boolean validation = fieldRequiredValidation(errorEntity, fse, new String[]{CmnConst.ORG_LEVEL_UUID, CmnConst.FIELD_JOB_GRADE_NAME, CmnConst.SEQUENCE, CmnConst.FIELD_JOB_GRADE_CLASS}, fieldType);
                    if (!validation) {
                        // 验证未通过 跳过该条数据
                        continue;
                    }
                    // 公司全称
                    String companyAllName = fse.getString(CmnConst.ORG_LEVEL_UUID);
 
                    List<FieldSetEntity> groupPostGradeList = groupCompany.get(companyAllName);
                    if (groupPostGradeList == null) {
                        groupPostGradeList = Lists.newArrayList();
                        groupCompany.put(companyAllName, groupPostGradeList);
                    }
                    groupPostGradeList.add(fse);
                }
                if (errorEntity.isError) {
                    errorEntity.newThrowBaseException();
                }
                Set<String> companyAllNames = groupCompany.keySet();
                sql = new StringBuilder();
                sql.append(" select uuid,org_level_all ");
                sql.append(" FROM product_sys_org_levels ");
                sql.append(" where ");
                sql.append(BaseUtil.buildQuestionMarkFilter("uuid", managerCompanyUuids.split(","), true));
                sql.append(" AND ");
                sql.append(BaseUtil.buildQuestionMarkFilter("org_level_all", companyAllNames.toArray(), true));
                // 根据用户上传的公司全称 & 该管理员管理的所有公司 进行查询公司的uuid
                DataTableEntity companyInfo = getBaseDao().listTable(sql.toString(), new Object[]{});
                // 定义没有找到公司的数据 写入错误方法
                CallBack<FieldSetEntity> callBack = (value) -> {
                    for (FieldSetEntity fs : value) {
                        DataLocation dataLocation = getDataLocation(fs);
                        String error = "第 %s 行,第 %s 数据错误,所属公司不存在,请检查列 '%s'";
                        errorEntity.addError(dataLocation.getRowIndex(), String.format(error,
                                dataLocation.getRowIndex(),
                                dataLocation.getColIndex(CmnConst.ORG_LEVEL_UUID),
                                dataLocation.getColName(CmnConst.ORG_LEVEL_UUID)));
                    }
                };
                // 没有查询到所有公司信息
                if (BaseUtil.dataTableIsEmpty(companyInfo)) {
                    for (List<FieldSetEntity> value : groupCompany.values()) {
                        // 循环写入错误
                        callBack.method(value.toArray(new FieldSetEntity[]{}));
                    }
                    errorEntity.newThrowBaseException();
                } else {
                    // copy 公司全称
                    Set<String> companyNames = Sets.newHashSet();
                    companyNames.addAll(companyAllNames);
                    // 循环查询的公司结果
                    for (int i = 0; i < companyInfo.getRows(); i++) {
                        String uuid = companyInfo.getString(i, CmnConst.UUID);
                        String org_level_all = companyInfo.getString(i, CmnConst.FIELD_ORG_LEVEL_ALL);
                        if (companyNames.contains(org_level_all)) {
                            companyNames.remove(org_level_all);
                        }
                        List<FieldSetEntity> postGradeList = groupCompany.get(org_level_all);
                        if (postGradeList != null) {
                            for (FieldSetEntity fs : postGradeList) {
                                // 将公司uuid写入每条数据
                                BaseUtil.createCreatorAndCreationTime(currentUser, fs);
                                fs.setValue(CmnConst.ORG_LEVEL_UUID, uuid);
                                fs.setValue(CmnConst.IS_USED, 1);
                                String job_grade_name = fs.getString(CmnConst.FIELD_JOB_GRADE_NAME);
//                            String group_name = fs.getString(CmnConst.FIELD_GROUP_NAME);
                                String managerValue = uuid + this.companyLevelSeparator + job_grade_name;
 
                                if (systemDataSet.contains(managerValue)) {
 
                                    DataLocation dataLocation = getDataLocation(fs);
                                    String error = "第 %s 行,第 %s 列,数据重复";
                                    error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_JOB_GRADE_NAME));
                                    errorEntity.addError(dataLocation.getRowIndex(), error);
                                    continue;
                                }
                                systemDataSet.add(managerValue);
                            }
                            if (errorEntity.isError) {
                                errorEntity.newThrowBaseException();
                            }
                        }
                    }
                    // 判断有是否有公司没查询到
                    if (companyNames != null && companyNames.size() > 0) {
                        List<FieldSetEntity> postGradeList = Lists.newArrayList();
                        for (String companyName : companyNames) {
                            //将没查询到的公司所属岗位等级合并
                            postGradeList.addAll(groupCompany.get(companyName));
                        }
                        if (postGradeList.size() > 0) {
                            //调用错误组装回调
                            callBack.method(postGradeList.toArray(new FieldSetEntity[]{}));
                            // 抛出错误
                            errorEntity.newThrowBaseException();
                        }
                    }
                    // 保存所有数据
                    getBaseDao().add(dt);
                    return dt;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
 
            errorEntity.newThrowBaseException("导入岗位等级失败", e);
        }
        errorEntity.newThrowBaseException("导入岗位等级失败,未提取到数据");
        return null;
    }
 
    /**
     * 导入岗位 之前 数据处理
     *
     * @param dt
     * @throws BaseException
     */
    @Override
    public DataTableEntity importPostBefore(DataTableEntity dt) throws BaseException {
        ImportErrorEntity errorEntity = new ImportErrorEntity();
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
            //管理员权限验证
            managerPermissionValidation(errorEntity, currentUser);
            Map<String, String> fieldType = Maps.newHashMap();
            // 当前管理员管理的公司
            String managerCompanyUuids = currentUser.getCurrentManager().getString(CmnConst.ORG_LEVEL_UUID);
            fieldType.put(CmnConst.SEQUENCE, "int");
            String companyFilter = BaseUtil.buildQuestionMarkFilter(CmnConst.ORG_LEVEL_UUID, managerCompanyUuids.split(","), true);
            StringBuilder sql = new StringBuilder(8);
            sql.append(" SELECT org_level_all uuid, uuid dept_uuid,org_level_uuid FROM product_sys_org_levels where org_level_type=1 and length(org_level_uuid)>0 ");
            sql.append(" and ");
            sql.append(companyFilter);
            DataTableEntity deptData = getBaseDao().listTable(sql.toString(), new Object[]{});
            if (BaseUtil.dataTableIsEmpty(deptData)) {
                errorEntity.newThrowBaseException("您所管理的公司没有可供选择的部门,请核对部门数据");
            }
            sql = new StringBuilder(8);
            sql.append(" SELECT job_grade_name uuid,org_level_uuid,uuid grade_uuid ");
            sql.append(" FROM product_sys_job_post_grades WHERE ");
            sql.append(companyFilter);
            // 查询当前管理管理公司所有的岗位等级
            DataTableEntity postGradeData = getBaseDao().listTable(sql.toString(), new Object[]{});
            if (BaseUtil.dataTableIsEmpty(postGradeData)) {
//                errorEntity.newThrowBaseException("您所管理的公司没有可供选择的岗位等级,请核对岗位等级数据");
            }
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fse = dt.getFieldSetEntity(i);
                // 字段必填 & 类型验证
                boolean validationResult = fieldRequiredValidation(errorEntity, fse, new String[]{CmnConst.FIELD_DEPT_UUID, CmnConst.FIELD_JOB_POST_NAME, CmnConst.SEQUENCE}, fieldType);
                if (!validationResult) {
                    // 验证未通过跳过
                    continue;
                }
                // 部门全称
                String deptAllName = fse.getString(CmnConst.FIELD_DEPT_UUID);
                // 岗位等级名称
                String[] gradeNames = BaseUtil.ifNull(fse.getString(CmnConst.FIELD_JOB_POST_GRADE), "").split(",");
                List<FieldSetEntity> fieldSetEntity = deptData.getFieldSetEntity(deptAllName);
                // 部门所在的公司
                String companyUuid = null;
                // 错误标识
                boolean errorFlag = false;
                DataLocation dataLocation = null;
                if (fieldSetEntity == null || fieldSetEntity.size() < 1) {
                    dataLocation = getDataLocation(fse);
                    String error = "第 %s 行,第 %s 列,部门不存在,请核对 '%s' 数据在系统中是否存在";
                    error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_DEPT_UUID), dataLocation.getColName(CmnConst.FIELD_DEPT_UUID));
                    errorEntity.addError(dataLocation.getRowIndex(), error);
                    errorFlag = true;
                } else {
                    FieldSetEntity fs = fieldSetEntity.get(0);
                    companyUuid = fs.getString(CmnConst.ORG_LEVEL_UUID);
                    fse.setValue(CmnConst.FIELD_DEPT_UUID, fs.getString(CmnConst.FIELD_DEPT_UUID));
                }
                // 根据岗位等级名称取出相关数据
                List<FieldSetEntity> postGradeList = new ArrayList<>();
                for (String gradeName : gradeNames) {
                    if (!BaseUtil.dataTableIsEmpty(postGradeData) && postGradeData.getFieldSetEntity(gradeName) != null) {
                        postGradeList.addAll(postGradeData.getFieldSetEntity(gradeName));
                    }
                }
                String error = "第 %s 行,第 %s 列,岗位等级不存在,请核对 '%s' 数据在系统中是否存在";
                // 判断岗位等级是否存在 默认不抛出错误
                if (postGradeList.size() < 1 && false) {
                    if (dataLocation == null) {
                        dataLocation = getDataLocation(fse);
                    }
                    error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_JOB_POST_GRADE), dataLocation.getColName(CmnConst.FIELD_JOB_POST_GRADE));
                    errorEntity.addError(dataLocation.getRowIndex(), error);
                    errorFlag = true;
                } else if (postGradeList.size() > 0) {
                    String gradeUuid = "";
                    // 循环岗位等级
                    for (FieldSetEntity fs : postGradeList) {
                        // 查找岗位等级与公司是否匹配
                        if (companyUuid != null && companyUuid.equals(fs.getString(CmnConst.ORG_LEVEL_UUID))) {
                            if (!StringUtils.isEmpty(gradeUuid)) {
                                gradeUuid += ",";
                            }
                            gradeUuid += fs.getString("grade_uuid");
                        }
                    }
                    // 没有匹配到该公司下有相应的岗位等级
                    if (StringUtils.isEmpty(gradeUuid)) {
                        if (dataLocation == null) {
                            dataLocation = getDataLocation(fse);
                        }
                        error = "第 %s 行,第 %s 列,岗位等级在不存在,请核对 '%s' 数据在所属部门的公司中是否存在";
                        error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_JOB_POST_GRADE), dataLocation.getColName(CmnConst.FIELD_JOB_POST_GRADE));
                        errorEntity.addError(dataLocation.getRowIndex(), error);
                        errorFlag = true;
                    } else {
                        String[] gradeUuids = gradeUuid.split(",");
                        // 创建岗位与岗位等级的映射表
                        DataTableEntity subGradeMapping = new DataTableEntity();
                        for (String uuid : gradeUuids) {
                            FieldSetEntity gradeMappingFieldSet = new FieldSetEntity();
                            gradeMappingFieldSet.setTableName(CmnConst.TABLE_PRODUCT_SYS_JOB_POSTS_GRADES_MAPPING);
                            gradeMappingFieldSet.setValue("job_grade_uuid", uuid);
                            BaseUtil.createCreatorAndCreationTime(gradeMappingFieldSet);
                            subGradeMapping.addFieldSetEntity(gradeMappingFieldSet);
                        }
                        fse.addSubDataTable(subGradeMapping);
                        fse.setValue(CmnConst.FIELD_JOB_POST_GRADE, null);
                    }
                }
                if (errorFlag) {
                    continue;
                }
                // 导入数据默认启用
                fse.setValue(CmnConst.IS_USED, 1);
            }
            if (errorEntity.isError) {
                errorEntity.newThrowBaseException();
            } else {
                return dt;
            }
        }
        errorEntity.newThrowBaseException("导入岗位等级失败,未提取到数据");
        return null;
    }
 
    /**
     * 导入员工 之前 数据处理
     *
     * @param fse
     * @throws BaseException
     */
    @Override
    public DataTableEntity importStaffBefore(DataTableEntity fse) throws BaseException {
        ImportStaffDataVo staffDataVo = new ImportStaffDataVo(fse);
        staffDataVo.startImporting();
        return fse;
    }
 
 
    /**
     * 员工导入操作类
     */
    private class ImportStaffDataVo {
        //导入的所有数据
        private DataTableEntity dt;
        /**
         * 错误处理类
         */
        private ImportErrorEntity errorEntity;
        /**
         * 系统中上级领导数据
         */
        DataTableEntity directLeaderData;
        /**
         * 部门数据
         */
        DataTableEntity deptData;
        /**
         * 角色数据
         */
        DataTableEntity roleData;
        /**
         * 岗位 & 岗位等级数据
         */
        DataTableEntity postData;
        /**
         * 员工表历史数据
         */
        DataTableEntity systemData;
        /**
         * 导入数据的所有领导(员工编码)
         */
        Set<String> direct_leader_codes;
        /**
         * 导入的数据按部门分组
         */
        Map<String, List<FieldSetEntity>> deptGroupMap;
        /**
         * 当前用户
         */
        SystemUser currentUser;
        /**
         * 当前客户
         */
        String client_uuid;
        /**
         * 导入的数据以员工编码存放(员工编码是唯一的)
         */
        Map<String, FieldSetEntity> staffCodeMap;
        /**
         * 导入数据类型验证
         */
        Map<String, String> fieldType;
        /**
         * 公司过滤条件
         */
        String companyFilter;
        /**
         * 分隔符
         */
        String separator = ">";
 
        StringBuilder sql;
        /**
         * 初始密码
         * 来源于 properties 中的初始密码 如不存在默认为 '123'
         */
        String init_pwd;
        /**
         * 部门全称存放的部门详情数据
         */
        private Map<String, FieldSetEntity> deptMap;
        /**
         * 员工管理service
         * 定义该操作类中避免没必要的注入 进入构造时会初始化对应bean
         */
        private StaffManagerService staffManagerService;
 
        /**
         * 构造方法 用于clone 非必要不要自行调用此方法
         *
         * @param currentData
         * @param currentDeptData
         */
        ImportStaffDataVo(FieldSetEntity currentData, FieldSetEntity currentDeptData) throws BaseException {
            // 该构造用于 clone 方法
            // 非clone 方式调用会出现未知错误
            this.currentData = currentData;
            this.currentDeptData = currentDeptData;
            if (currentData == null || currentDeptData == null) {
                try {
                    throw new RuntimeException("Constructor arguments cannot be null");
                } catch (RuntimeException e) {
                    throw new BaseException(e);
                }
            }
        }
 
        /**
         * 无参构造 初始必要数据
         *
         * @param dt
         */
        public ImportStaffDataVo(DataTableEntity dt) throws BaseException {
            errorEntity = new ImportErrorEntity();
            if (BaseUtil.dataTableIsEmpty(dt)) {
                errorEntity.newThrowBaseException("导入员工失败,未提取到数据");
            }
            this.staffManagerService = SpringBeanUtil.getBean("staffManagerService", StaffManagerService.class);
            direct_leader_codes = Sets.newHashSet();
            fieldType = Maps.newHashMap();
            currentUser = SpringMVCContextHolder.getCurrentUser();
            client_uuid = currentUser.getClient_uuid();
            staffCodeMap = Maps.newHashMap();
            deptGroupMap = Maps.newHashMap();
            init_pwd = Global.getSystemConfig("initial.pwd", "123");
            this.dt = dt;
            //管理员权限验证
            managerPermissionValidation(errorEntity, currentUser);
            // 公司过滤条件
            companyFilter = BaseUtil.buildQuestionMarkFilter(CmnConst.ORG_LEVEL_UUID, currentUser.getCurrentManager().getString(CmnConst.ORG_LEVEL_UUID).split(","), true);
            fieldType.put("id_number", "id_card");
            fieldType.put("birth_date", "dateTime");
            fieldType.put("entry_datetime", "dateTime");
            fieldType.put("mobile_phone", "mobile_phone");
            fieldType.put("staff_email", "email");
            fieldType.put("age", "int");
        }
 
        /**
         * 导入数据入口
         * 开始导入
         */
        public void startImporting() throws BaseException {
            // 数据有效性验证 数据必填验证 & 数据有效性验证 & 数据分组
            this.requiredValidation();
            // 查询系统中已存在的员工
            findExistStaff();
            // 数据重复验证
            this.repeatValidation();
            this.throwError();
            // 初始化所需数据
            this.initializeSystemData();
            // 开始处理导入数据
            this.handleDataTable();
        }
 
        /**
         * handleDataTable 遍历时的数据
         * 可能是通过 clone 对象赋值的 没有通过 handleDataTable 方法
         */
        private FieldSetEntity currentData;
        /**
         * handleDataTable 遍历数据对应的部门
         * 可能是通过 clone 对象赋值的 没有通过 handleDataTable 方法
         */
        private FieldSetEntity currentDeptData;
 
        /**
         * 上级领导未找到
         */
        private void superStaffNotExist() throws BaseException {
            DataLocation dataLocation = getDataLocation(this.currentData);
            String error = "第 %s 行,第 %s 列,上级领导获取失败,请检查 %s 填写值是否正确,该列值应填写 %s 列对应的值";
            error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_DIRECT_LEADER_CODE), dataLocation.getColName(CmnConst.FIELD_DIRECT_LEADER_CODE), dataLocation.getColName(CmnConst.FIELD_STAFF_CODE));
            errorEntity.addError(dataLocation.getRowIndex(), error);
        }
 
        /**
         * 处理所有记录
         */
        private void handleDataTable() throws BaseException {
            for (Map.Entry<String, List<FieldSetEntity>> fieldSets : this.deptGroupMap.entrySet()) {
                String deptAllName = fieldSets.getKey();
                currentDeptData = this.getStaffDept(deptAllName);
                List<FieldSetEntity> values = fieldSets.getValue();
                if (currentDeptData == null) {
                    for (int i = 0; i < values.size(); i++) {
                        this.currentData = values.get(i);
                        DataLocation dataLocation = getDataLocation(this.currentData);
                        String error = "第 %s 行,第 %s 列,部门在系统或管理的公司中不存在,请检查列 '%s' 的值 '%s' 是否正确";
                        error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex(CmnConst.FIELD_DEPT_UUID),
                                dataLocation.getColName(CmnConst.FIELD_DEPT_UUID), deptAllName);
                        this.errorEntity.addError(dataLocation.getRowIndex(), error);
                    }
                    continue;
                }
                for (FieldSetEntity value : values) {
                    if (value.getBoolean("~processed~")) {
                        continue;
                    }
                    this.currentData = value;
                    this.handleFieldSet();
                }
            }
            throwError();
        }
 
        /**
         * 处理单条记录
         */
        private void handleFieldSet() throws BaseException {
            if (this.currentData.getBoolean("~processed~")) {
                return;
            }
            String dept_uuid = this.currentDeptData.getString(CmnConst.UUID);
            String org_level_uuid = this.currentDeptData.getString(CmnConst.ORG_LEVEL_UUID);
            this.currentData.setValue("~processed~", 1);
            this.currentData.setValue("~dept_uuid~", dept_uuid);
            this.currentData.setValue(CmnConst.FIELD_STAFF_CODE, this.currentData.getString("~temp_staff_code~"));
            this.currentData.setValue(CmnConst.FIELD_DEPT_UUID, currentDeptData.getString(CmnConst.FIELD_DEPT_UUID));
            this.currentData.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid);
            this.currentData.setValue("status", 1);
 
            //取出配置中账户初始密码
            this.currentData.setValue(CmnConst.USER_PWD, this.init_pwd);
            String sex = this.currentData.getString("sex");
            if (!StringUtils.isEmpty(sex)) {
                // 性别不为空时 转换参照数据 性别验证已在上个方法中验证过
                this.currentData.setValue("sex", sex.equals("男") ? 0 : 1);
            }
            // 导入数据配置中 使用 staff_avatar 字段作为用户登录账户的载体 在此处转换回来
            this.currentData.setValue(CmnConst.USER_ACCOUNT, this.currentData.getString("staff_avatar"));
            // 删除载体数据
            this.currentData.remove("staff_avatar");
            // 将show_name 字段(员工名称) 复制到 user表字段上
            this.currentData.setValue(CmnConst.USER_NAME, this.currentData.getString("show_name"));
            String job_post_grade_uuid = this.currentData.getString(CmnConst.FIELD_JOB_POST_GRADE_UUID);
            String job_post_name = this.currentData.getString("job_post_uuid");
 
            // 岗位 & 岗位等级
            List<FieldSetEntity> postDataList = postData.getFieldSetEntity(this.currentData.getString("job_post_uuid"));
            if (postDataList == null || postDataList.size() <= 0) {
                // 没有岗位
                postNotExist.method(this.currentData, "job_post_uuid");
            } else {
                // 记录找到的岗位等级uuid ,多个用逗号分隔
                String grade_uuid = "";
                // 岗位uuid
                String post_uuid = null;
                // 循环系统中岗位数据
                for (FieldSetEntity postFs : postDataList) {
                    // 判断岗位是否在部门下 & 岗位名称和导入数据的岗位名称是否相同
                    if (currentDeptData.getString(CmnConst.FIELD_DEPT_UUID).equals(postFs.getString(CmnConst.FIELD_DEPT_UUID)) && job_post_name.equals(postFs.getUUID())) {
                        // 系统中岗位的uuid
                        String job_post_uuid = postFs.getString("job_post_uuid");
                        // 岗位uuid不为空时 判断当前postFs 中的岗位uuid是否跟已找到的岗位相同
                        if (post_uuid != null && !post_uuid.equals(job_post_uuid)) {
                            continue;
                        }
                        if (post_uuid == null) {
                            // 记录岗位uuid
                            post_uuid = job_post_uuid;
                        }
                        // 获取岗位中的岗位等级名称
                        String job_grade_name = postFs.getString(CmnConst.FIELD_JOB_GRADE_NAME);
                        // 拆分循环导入岗位等级名称
                        if (!BaseUtil.strIsNull(job_post_grade_uuid)) {
                            for (String grade_name : job_post_grade_uuid.split(",")) {
                                // 判断岗位等级名称和系统中相同
                                if (job_grade_name.equals(grade_name)) {
                                    if (!StringUtils.isEmpty(grade_uuid)) {
                                        grade_uuid += ",";
                                    }
                                    grade_uuid += postFs.getString(CmnConst.FIELD_JOB_POST_GRADE);
                                }
                            }
                        }
                    }
 
                }
                // 没有找到岗位
                if (StringUtils.isEmpty(post_uuid)) {
                    // 抛出岗位未找到错误
                    postNotExist.method(this.currentData, "job_post_uuid");
                } else {
                    // 找到岗位
                    this.currentData.setValue("job_post_uuid", post_uuid);
                }
                // 没有找到岗位等级
                if (!StringUtils.isEmpty(grade_uuid)) {
//                    postNotExist.method(this.currentData, CmnConst.FIELD_JOB_POST_GRADE_UUID);
//                } else {
                    this.currentData.setValue(CmnConst.FIELD_JOB_POST_GRADE_UUID, grade_uuid);
                }
            }
            // 导入数据中岗位名称
            String[] role_uuids = this.currentData.getString("role_uuids").split(",");
            String roleUuid = "";
            // 角色处理
            roleFor:
            // 循环角色名称
            for (String roleName : role_uuids) {
                // 使用角色名称获取系统数据
                List<FieldSetEntity> roleList = roleData.getFieldSetEntity(roleName);
                if (roleList != null && roleList.size() > 0) {
                    // 循环系统中的角色
                    for (FieldSetEntity roleFs : roleList) {
                        // 因系统中角色所属公司可多选,在此处前后拼接逗号
                        String roleCompany = "," + roleFs.getString(CmnConst.ORG_LEVEL_UUID) + ",";
                        // 判断角色是否有导入数据的公司
                        if (roleCompany.contains("," + org_level_uuid + ",")) {
                            String role_uuid = roleFs.getString("role_uuid");
                            if (!StringUtils.isEmpty(roleUuid)) {
                                roleUuid += ",";
                            }
                            roleUuid += role_uuid;
                            continue roleFor;
                        }
                    }
                }
 
                // 没有找到角色数据 抛出错误
                roleNotExist.method(this.currentData, roleName);
 
            }
            this.currentData.setValue("role_uuids", roleUuid);
            //装载上级领导code
            this.loadingSuperCode();
            if (staffManagerService == null) {
                staffManagerService = SpringBeanUtil.getBean("staffManagerService", StaffManagerService.class);
            }
            staffManagerService.saveStaffInfo(this.currentData);
        }
 
        /**
         * 抛出错误
         *
         * @throws BaseException
         */
        void throwError() throws BaseException {
            if (errorEntity.isError) {
                errorEntity.newThrowBaseException();
            }
        }
 
        /**
         * 装载上级领导编码
         *
         * @return
         */
        private boolean loadingSuperCode() throws BaseException {
            // 上级领导(员工编码)
            String direct_leader_code = this.currentData.getString(CmnConst.FIELD_DIRECT_LEADER_CODE);
            String superCode = "";
            if (!StringUtils.isEmpty(direct_leader_code)) {
                List<FieldSetEntity> fieldSetEntity = directLeaderData.getFieldSetEntity(direct_leader_code);
                if (staffCodeMap.get(direct_leader_code) == null && (fieldSetEntity == null || fieldSetEntity.size() <= 0)) {
                    // 未找到上级领导
                    this.superStaffNotExist();
                    return false;
                } else {
                    //上级领导在导入数据 或系统中已存在
                    // 根据编码获取上级(导入数据)
                    FieldSetEntity superFs = staffCodeMap.get(direct_leader_code);
                    if (superFs == null && (fieldSetEntity == null || fieldSetEntity.size() <= 0)) {
                        // 未找到上级领导
                        this.superStaffNotExist();
                        return false;
                    } else if (superFs == null) {
                        // 在系统中拿出上级领导
                        superFs = fieldSetEntity.get(0);
                    }
                    String user_id = superFs.getString(CmnConst.USER_ID);
                    if (StringUtils.isEmpty(user_id)) {
                        // user_id 不存在,该上级在导入数据中未保存过
                        String deptAllName = superFs.getString(CmnConst.FIELD_DEPT_UUID);
                        if (superFs.getBoolean("~processed~")) {
                            deptAllName = superFs.getString("~" + CmnConst.FIELD_DEPT_UUID + "~");
                        }
                        FieldSetEntity deptFs = getStaffDept(deptAllName);
                        if (deptFs == null) {
                            return false;
                        }
                        // 克隆当前对象 将父级的部门 & 员工表数据放入克隆对象中
                        ImportStaffDataVo clone = this.clone(deptFs, superFs);
                        // 先让父级数据进行保存操作
                        // 这里会出现递归操作 因为 handleFieldSet 方法调用了当前方法 父级的父级进入一致的判断会一直递归 clone并保存
                        // 如果递归层数过多可能会出现 full gc
                        clone.handleFieldSet();
                    } else {
                        //已保存过的数据 取出已生成的tricode
                        superCode = superFs.getString(CmnConst.TRICODE);
                    }
                }
            }
            this.currentData.setValue(CmnConst.FIELD_DIRECT_LEADER_CODE, superCode);
            return true;
        }
 
        /**
         * 克隆对象
         * 克隆后 sql 成员变量未初始化
         *
         * @param currentDeptData
         * @param currentData
         * @return
         */
        private ImportStaffDataVo clone(FieldSetEntity currentDeptData, FieldSetEntity currentData) throws BaseException {
            ImportStaffDataVo clone = new ImportStaffDataVo(currentData, currentDeptData);
            clone.deptData = this.deptData;
            clone.postData = this.postData;
            clone.roleData = this.roleData;
            clone.deptGroupMap = this.deptGroupMap;
            clone.errorEntity = this.errorEntity;
            clone.direct_leader_codes = this.direct_leader_codes;
            clone.deptMap = this.deptMap;
            clone.dt = this.dt;
            clone.systemData = this.systemData;
            clone.directLeaderData = this.directLeaderData;
            clone.staffCodeMap = this.staffCodeMap;
            clone.companyFilter = this.companyFilter;
            clone.currentUser = this.currentUser;
            clone.fieldType = this.fieldType;
            clone.init_pwd = this.init_pwd;
            clone.client_uuid = this.client_uuid;
            return clone;
        }
 
        /**
         * 根据部门全称获取部门详情
         *
         * @param deptAllName
         * @return
         * @throws BaseException
         */
        private FieldSetEntity getStaffDept(String deptAllName) throws BaseException {
            if (deptMap == null) {
                deptMap = Maps.newHashMap();
            }
            if (deptMap.get(deptAllName) != null) {
                return deptMap.get(deptAllName);
            }
            List<FieldSetEntity> deptDatList = deptData.getFieldSetEntity(deptAllName);
            if (deptDatList == null || deptDatList.size() < 0) {
                return null;
            }
            return deptDatList.get(0);
        }
 
        /**
         * 角色未找到错误回调
         */
        private CallBack<Object> roleNotExist = f -> {
            FieldSetEntity fs = (FieldSetEntity) f[0];
            Object role_name = f[1];
            DataLocation dataLocation = getDataLocation(fs);
            String error = "第 %s 行,第 %s 列,%s 在系统中不存在,请检查列值%s 是否正确";
            error = String.format(error, dataLocation.getRowIndex(), dataLocation.getColIndex("role_uuids"),
                    dataLocation.getColName("role_uuids"), role_name);
            errorEntity.addError(dataLocation.getRowIndex(), error);
        };
        /**
         * 岗位 & 岗位等级未找到错误回调
         */
        private CallBack<Object> postNotExist = f -> {
            FieldSetEntity fs = (FieldSetEntity) f[0];
            String fieldName = (String) f[1];
            DataLocation datLocation = getDataLocation(fs);
            String error = "第 %s 行,第 %s 列,'%s' 数据在系统中不存在,请检查 '%s' 是否正确";
            error = String.format(error, datLocation.getRowIndex(), datLocation.getColIndex(fieldName),
                    datLocation.getColName(fieldName), fs.getString(fieldName));
            errorEntity.addError(datLocation.getRowIndex(), error);
        };
 
        /**
         * 初始化系统数据
         */
        private void initializeSystemData() throws BaseException {
            //查询上级领导
            findDirectLeaderData();
            // 查询管理员管理的(公司下的)部门
            findDeptData();
            // 管理员管理的角色
            findRoleData();
            // 管理员管理的岗位 & 岗位等级
            findPostData();
            this.throwError();
        }
 
        /**
         * 查询所有岗位 & 岗位等级
         */
        private void findPostData() throws BaseException {
            sql = new StringBuilder();
            sql.append(" select a.job_post_name uuid,a.uuid job_post_uuid,c.job_grade_name,c.uuid job_post_grades_uuid,a.dept_uuid FROM product_sys_job_posts a ");
            sql.append(" LEFT join product_sys_job_posts_grades_mapping b on  ");
            sql.append(" a.dept_uuid in (select uuid FROM product_sys_org_levels where org_level_type=1 and  ");
            sql.append(companyFilter);
            sql.append(" ) and a.uuid=b.job_post_uuid ");
            sql.append(" LEFT  join product_sys_job_post_grades c on b.job_grade_uuid=c.uuid ");
            sql.append(" group by  a.job_post_name,a.uuid ,c.job_grade_name,c.uuid,a.dept_uuid ");
            // 查询岗位 & 岗位等级
            postData = getBaseDao().listTable(sql.toString(), new Object[]{});
            if (BaseUtil.dataTableIsEmpty(roleData)) {
                errorEntity.addError(0, "系统中没有可供选择的岗位或岗位等级");
            }
        }
 
        /**
         * 查询所有角色
         */
        private void findRoleData() throws BaseException {
            sql = new StringBuilder();
            sql.append(" select role_name as uuid,uuid as role_uuid,org_level_uuid FROM product_sys_role ");
            sql.append(" where is_used = 1  and ");
            sql.append(companyFilter);
            // 查询角色
            roleData = getBaseDao().listTable(sql.toString(), new Object[]{});
            if (BaseUtil.dataTableIsEmpty(roleData)) {
                errorEntity.addError(0, "系统中没有可供选择的角色");
            }
        }
 
        /**
         * 查询所有部门
         */
        private void findDeptData() throws BaseException {
            // 查询部门
            sql = new StringBuilder();
            sql.append(" select org_level_all uuid,uuid as dept_uuid,org_level_uuid,org_level_code,org_level_code_parent FROM product_sys_org_levels ");
            sql.append(" where org_level_type = 1 and ");
            sql.append(BaseUtil.buildQuestionMarkFilter(CmnConst.FIELD_ORG_LEVEL_ALL, deptGroupMap.size(), true));
            sql.append(" and ");
            sql.append(companyFilter);
            // 查询部门
            deptData = getBaseDao().listTable(sql.toString(), deptGroupMap.keySet().toArray());
            if (BaseUtil.dataTableIsEmpty(deptData)) {
                errorEntity.addError(0, "系统中没有可供选择的部门");
            }
        }
 
        /**
         * 查询导入数据所有的上级领导
         */
        private void findDirectLeaderData() throws BaseException {
            sql = new StringBuilder();
            sql.append(" SELECT staff_code uuid,tricode,user_id FROM product_sys_staffs ");
            sql.append(" where ");
            sql.append(BaseUtil.buildQuestionMarkFilter(CmnConst.FIELD_STAFF_CODE, direct_leader_codes.size(), true));
            if (direct_leader_codes.size() > 0) {
                sql.append(" and ");
            }
            sql.append(" org_level_uuid in (select uuid FROM product_sys_org_levels where org_level_type=0 and client_uuid=?) ");
            // 暂时注释不用公司做限制
//                sql.append(" and ");
//                sql.append(companyFilter);
            // 查询上级领导
            List<String> params = Lists.newArrayList(direct_leader_codes.iterator());
            params.add(currentUser.getClient_uuid());
            directLeaderData = getBaseDao().listTable(sql.toString(), params.toArray());
        }
 
        /**
         * 查询所有员工编码以及user表中的账号
         */
        private void findExistStaff() throws BaseException {
            sql = new StringBuilder(126);
            sql.append(" select  concat(b.client_uuid,'>',staff_code) staff_code,a.user_account as staff_avatar ");
            sql.append(" FROM product_sys_users a left join product_sys_staffs b on (b.client_uuid=? or b.uuid is null) and  a.user_id=b.user_id ");
            systemData = getBaseDao().listTable(sql.toString(), new Object[]{client_uuid});
        }
 
        /**
         * 数据格式效验
         */
        private void repeatValidation() throws BaseException {
            // 注 :staff_avatar 字段为 user表中 user_account 的载体
            fieldRepeatValidation(errorEntity, dt, systemData, new String[]{CmnConst.FIELD_STAFF_CODE, "staff_avatar"});
        }
 
        /**
         * 必填效验 数据处理
         */
        private void requiredValidation() throws BaseException {
            errorEntity.mergeError(fieldRequiredValidation(dt, new String[]{"show_name", "staff_avatar", CmnConst.FIELD_STAFF_CODE,
                    CmnConst.FIELD_DEPT_UUID, "job_post_uuid", "role_uuids"}, fieldType, (FieldSetEntity[] f) -> {
                FieldSetEntity fs = f[0];
                //上级领导编码
                String direct_leader_code = fs.getString(CmnConst.FIELD_DIRECT_LEADER_CODE);
                //工作部门全称
                String deptAllName = fs.getString(CmnConst.FIELD_DEPT_UUID);
                String id_number = fs.getString("id_number");
                String age = fs.getString("age");
                Date birth_date = fs.getDate("birth_date");
                String sex = fs.getString("sex");
                String staff_code = fs.getString(CmnConst.FIELD_STAFF_CODE);
 
                if (!StringUtils.isEmpty(staff_code)) {
                    fs.setValue("~temp_staff_code~", staff_code);
                    fs.setValue(CmnConst.FIELD_STAFF_CODE, client_uuid + this.separator + staff_code);
                    staffCodeMap.put(staff_code, fs);
                }
                fs.setValue(CmnConst.CLIENT_UUID, client_uuid);
                if (!StringUtils.isEmpty(direct_leader_code)) {
                    //将上级领导编码统一放入集合中
                    // 上级领导编码 = 员工编码
                    direct_leader_codes.add(direct_leader_code);
                }
                //按部门分组数据
                if (!StringUtils.isEmpty(deptAllName)) {
                    List<FieldSetEntity> fieldSets = deptGroupMap.get(deptAllName);
                    if (fieldSets == null) {
                        fieldSets = Lists.newArrayList();
                        deptGroupMap.put(deptAllName, fieldSets);
                    }
                    fieldSets.add(fs);
                }
                if (!StringUtils.isEmpty(id_number)) {
                    try {
                        if (birth_date == null) {
                            // 根据身份证取出生日期
                            fs.setValue("birth_date", BaseUtil.getBirthday(id_number));
                        }
                        if (StringUtils.isEmpty(age)) {
                            // 根据身份证取出年龄
                            fs.setValue("age", BaseUtil.getAge(id_number));
                        }
                        if (StringUtils.isEmpty(sex)) {
                            // 根据身份证取出性别
                            fs.setValue("sex", BaseUtil.getSex(id_number));
                        }
                    } catch (Exception e) {
 
                    }
                }
 
 
            }));
 
        }
    }
 
 
    /**
     * 重载导入员工方法
     * 处理公司、部门、岗位、岗位等级、账号 相关数据
     *
     * @param postData     系统岗位&岗位等级数据
     * @param roleData     系统角色数据
     * @param roleNotExist 角色不存在错误回调
     * @param postNotExist 岗位&岗位等级不存在错误回调
     * @param fs           导入数据
     * @param deptFieldSet 部门数据
     * @param deptAllName  部门全称
     * @throws BaseException
     */
    private void importStaffBefore(DataTableEntity postData, DataTableEntity
            roleData, CallBack<Object> roleNotExist, CallBack<Object> postNotExist, FieldSetEntity fs, FieldSetEntity deptFieldSet, String deptAllName) throws BaseException {
 
        String dept_uuid = fs.getString(CmnConst.UUID);
        String org_level_uuid = fs.getString(CmnConst.ORG_LEVEL_UUID);
        fs.setValue(CmnConst.FIELD_STAFF_CODE, fs.getString("~temp_staff_code~"));
        fs.setValue(CmnConst.FIELD_DEPT_UUID, dept_uuid);
        fs.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid);
 
        String job_post_grade_uuid = fs.getString(CmnConst.FIELD_JOB_POST_GRADE);
        String job_post_name = fs.getString("job_post_uuid");
 
        // 岗位 & 岗位等级
        List<FieldSetEntity> postDataList = postData.getFieldSetEntity(deptAllName);
        if (postDataList == null || postDataList.size() <= 0) {
            // 没有岗位
            postNotExist.method(fs, "job_post_uuid");
        } else {
            // 记录找到的岗位等级uuid ,多个用逗号分隔
            String grade_uuid = "";
            // 岗位uuid
            String post_uuid = null;
            // 循环系统中岗位数据
            for (FieldSetEntity postFs : postDataList) {
                // 判断岗位是否在部门下 & 岗位名称和导入数据的岗位名称是否相同
                if (dept_uuid.equals(postFs.getString(CmnConst.FIELD_DEPT_UUID)) && job_post_name.equals(postFs.getString(CmnConst.FIELD_JOB_POST_NAME))) {
                    // 系统中岗位的uuid
                    String job_post_uuid = postFs.getString("job_post_uuid");
                    // 岗位uuid不为空时 判断当前postFs 中的岗位uuid是否跟已找到的岗位相同
                    if (post_uuid != null && !post_uuid.equals(job_post_uuid)) {
                        continue;
                    }
                    if (post_uuid == null) {
                        // 记录岗位uuid
                        post_uuid = job_post_uuid;
                    }
                    // 获取岗位中的岗位等级名称
                    String job_grade_name = postFs.getString(CmnConst.FIELD_JOB_GRADE_NAME);
                    // 拆分循环导入岗位等级名称
                    for (String grade_name : job_post_grade_uuid.split(",")) {
                        // 判断岗位等级名称和系统中相同
                        if (job_grade_name.equals(grade_name)) {
                            if (!StringUtils.isEmpty(grade_uuid)) {
                                grade_uuid += ",";
                            }
                            grade_uuid += postFs.getString(CmnConst.FIELD_JOB_POST_GRADE);
                        }
                    }
                }
                // 没有找到岗位
                if (StringUtils.isEmpty(post_uuid)) {
                    // 抛出岗位未找到错误
                    postNotExist.method(fs, "job_post_uuid");
                } else {
                    // 找到岗位
                    fs.setValue("job_post_uuid", post_uuid);
                }
                // 没有找到岗位等级
                if (StringUtils.isEmpty(grade_uuid)) {
                    postNotExist.method(fs, CmnConst.FIELD_JOB_POST_GRADE);
                } else {
                    fs.setValue(CmnConst.FIELD_JOB_POST_GRADE, grade_uuid);
                }
            }
        }
        // 导入数据中岗位名称
        String[] role_uuids = (String[]) fs.getObject("role_uuids");
        String roleUuid = "";
        // 角色处理
        roleFor:
        // 循环角色名称
        for (String roleName : role_uuids) {
            // 使用角色名称获取系统数据
            List<FieldSetEntity> roleList = roleData.getFieldSetEntity(roleName);
            // 在系统中没有找到角色名称一致的数据
            if (roleList == null || roleList.size() <= 0) {
                roleNotExist.method(fs, roleName);
            } else {
                // 循环系统中的角色
                for (FieldSetEntity roleFs : roleList) {
                    // 因系统中角色所属公司可多选,在此处前后拼接逗号
                    String roleCompany = "," + roleFs.getString(CmnConst.ORG_LEVEL_UUID) + ",";
                    // 判断角色是否有导入数据的公司
                    if (roleCompany.contains("," + org_level_uuid + ",")) {
                        String role_uuid = roleFs.getString("role_uuid");
                        if (!StringUtils.isEmpty(roleUuid)) {
                            roleUuid += ",";
                        }
                        roleUuid += role_uuid;
                        continue roleFor;
                    }
                }
                // 没有找到角色数据 抛出错误
                roleNotExist.method(fs, roleName);
            }
 
        }
    }
 
    /**
     * 获取数据位置对象
     *
     * @param fse
     * @return
     */
    private DataLocation getDataLocation(FieldSetEntity fse) {
        if (fse.getString("~dataLocation~") != null) {
            return (DataLocation) fse.getObject("~dataLocation~");
        } else {
            DataLocation dataLocation = new DataLocation(fse);
            fse.setValue("~dataLocation~", dataLocation);
            return dataLocation;
        }
    }
 
    /**
     * 初始化机构数据
     *
     * @param fse
     * @param level_type
     */
    public void initOrganizationFieldSet(FieldSetEntity fse, String client_uuid, String parentCode,
                                         int level_type) {
        fse.setValue(CmnConst.FIELD_ORG_LEVEL_CODE_PARENT, parentCode);
        fse.setValue(CmnConst.FIELD_ORG_LEVEL_CODE, BaseUtil.createCode(fse, CmnConst.TABLE_PRODUCT_SYS_ORG_LEVELS, CmnConst.FIELD_ORG_LEVEL_CODE, parentCode));
        BaseUtil.createCreatorAndCreationTime(fse);
        fse.remove(CmnConst.ORG_LEVEL_UUID);
        fse.setValue(CmnConst.CLIENT_UUID, client_uuid);
        fse.setValue(CmnConst.FIELD_ORG_LEVEL_STATUS, 0);
        fse.setValue(CmnConst.FIELD_ORG_LEVEL_TYPE, level_type);
        fse.setCodeFieldUpdateFlat(CmnConst.FIELD_ORG_LEVEL_CODE, false);
    }
 
    public void managerPermissionValidation(ImportErrorEntity errorEntity, SystemUser user) throws
            BaseException {
        FieldSetEntity managerFieldSet = user.getCurrentManager();
        if (managerFieldSet == null || user.getUserType() != 2) {
            errorEntity.newThrowBaseException("没有导入此数据的权限,请联系统管理员");
        } else if (StringUtils.isEmpty(managerFieldSet.getString(CmnConst.ORG_LEVEL_UUID))) {
            errorEntity.newThrowBaseException("公司权限不存在,请联系统管理员");
        }
    }
 
 
    /**
     * 错误位置
     */
    class DataLocation {
 
        //当前行
        private Integer rowIndex;
        //列名
        private List<String> colNameList;
        //字段列索引
        private Map<String, String> fieldIndex;
 
        DataLocation(FieldSetEntity fse) {
            //当前行
            this.rowIndex = fse.getInteger("~row~");
            //列名
            this.colNameList = (List<String>) fse.getObject("~colName~");
            //字段列索引
            this.fieldIndex = (Map<String, String>) fse.getObject("~fieldIndex~");
        }
 
        /**
         * 获取当前行索引
         *
         * @return
         */
        public Integer getRowIndex() {
            return rowIndex + 1;
        }
 
        /**
         * 获取当前列索引
         *
         * @param fieldName
         * @return
         */
        public String getColIndex(String fieldName) {
            if (fieldIndex == null || StringUtils.isEmpty(fieldName)) {
                return null;
            }
            String colIndex = fieldIndex.get(fieldName);
            return !StringUtils.isEmpty(colIndex) ? String.valueOf(Integer.valueOf(colIndex) + 1) : "未知";
        }
 
        /**
         * 获取当前列名称
         *
         * @param fieldName
         * @return
         */
        public String getColName(String fieldName) {
            String colIndex = getColIndex(fieldName);
            if (this.colNameList == null || colIndex == null || "未知".equals(colIndex)) {
                return "未知列名";
            }
            return colNameList.get(Integer.valueOf(colIndex) - 1);
        }
 
 
    }
 
 
    static class ImportErrorEntity {
 
        interface errorMsg {
            errorMsg append(Object errorMsg);
        }
 
        private Map<Integer, String> errorMap;
 
        private boolean isError = false;
 
        /**
         * 新增错误 自动换行
         *
         * @param row
         * @param errorMsg
         * @return
         */
        public errorMsg addError(int row, String errorMsg) {
            if (errorMap == null) {
                errorMap = Maps.newHashMap();
            }
            isError = true;
            String error = errorMap.get(row);
            if (!StringUtils.isEmpty(error)) {
                error += "\n";
            } else {
                error = "";
            }
            error += errorMsg;
            errorMap.put(row, error);
            return (emg) -> this.append(row, String.valueOf(emg));
        }
 
        /**
         * 合并错误
         *
         * @param errorEntity
         * @return
         */
        public ImportErrorEntity mergeError(ImportErrorEntity errorEntity) {
            Map<Integer, String> errorMap = errorEntity.errorMap;
            if (errorEntity.isError) {
                if (this.errorMap == null) {
                    this.errorMap = errorMap;
                    this.isError = true;
                    return this;
                } else {
                    errorMap.forEach((k, v) -> {
                        String error = this.errorMap.get(k);
                        if (!StringUtils.isEmpty(error)) {
                            error += "\n";
                        } else {
                            error = "";
                        }
                        error += v;
                        this.isError = true;
                        this.errorMap.put(k, error);
                    });
                }
            }
            return this;
        }
 
        /**
         * 拼接错误
         *
         * @param row
         * @param errorMsg
         * @return
         */
        public errorMsg append(int row, String errorMsg) {
            if (errorMap == null) {
                errorMap = Maps.newHashMap();
            }
            isError = true;
            String error = errorMap.get(row);
            if (StringUtils.isEmpty(error)) {
                error = "";
            }
            error += errorMsg;
            errorMap.put(row, error);
            return (emg) -> this.append(row, String.valueOf(emg));
        }
 
        public void newThrowBaseException() {
            newThrowBaseException(null, null);
        }
 
        public void newThrowBaseException(String msg, Exception exception) {
            if (!StringUtils.isEmpty(msg)) {
                throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), msg);
            } else if (errorMap != null) {
                List<Integer> es = Lists.newArrayList(errorMap.keySet().iterator());
                StringBuilder errorMsg = new StringBuilder();
                for (Integer e : es) {
                    String error = errorMap.get(e);
                    if (!StringUtils.isEmpty(error)) {
                        errorMsg.append(error).append("\n");
                    }
                }
                if (errorMsg.length() > 0) {
                    if (exception != null) {
                        throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), errorMsg.toString(), exception);
                    } else {
                        throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), errorMsg.toString());
                    }
                }
                throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), " 未知错误,请联系管理员,error message is empty for 'throwExportDataError' Method ");
            }
            if (exception != null) {
                throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), " 未知错误,请联系管理员,error message is empty for 'throwExportDataError' Method ", exception);
            }
            throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), " 未知错误,请联系管理员,error message is empty for 'throwExportDataError' Method ");
        }
 
        public void newThrowBaseException(String msg) {
            newThrowBaseException(msg, null);
        }
 
    }
}