shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
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
package com.product.org.admin.util;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.poi.ss.usermodel.*;
 
import com.alibaba.fastjson.JSONObject;
import com.product.admin.util.SystemParamReplace;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.org.admin.config.CmnConst;
import com.product.org.admin.config.SystemCode;
 
/**
 * 
* Copyright  LX-BASE
* @Title: LX-BASE-
* @Project: ImportDataUtil
* @Date:  2021-01-12 17:47   
* @Author: 杜洪波
* @Description:
 */
public class ImportDataUtil {
    
    /**
     *     导入特殊字段处理参照(例:dept_uuid)
     * @param fieldName
     * @param tableName
     * @return
     */
    public static String fieldSpecialJude(String fieldName,String tableName) {
        if (CmnConst.PRODUCT_SYS_JOB_POSTS.equals(tableName) && CmnConst.DEPT_UUID.equals(fieldName)) {    //岗位等级表
            return "data-import-dept";
        }else if (CmnConst.PRODUCT_SYS_STAFFS.equals(tableName) && CmnConst.DEPT_UUID.equals(fieldName)) {    //员工信息表
            return "data-import-dept";
 
        }else {
            return null;
        }
    }
    
    
    /**
     *     根据字段名获取字段索引
     * @param fieldNames
     * @param fieldNmae
     * @return
     */
    public static int getIndexByFieldName(List<String>fieldNames,String fieldNmae) {
        for (int i = 0; i < fieldNames.size(); i++) {
            if (fieldNmae.equals(fieldNames.get(i))) {
                return i;
            }
        }
        return 999;
    }
    
    
    /**
     *     根据列数获取数据集合
     * @param listBaseData
     * @param col
     * @param jsonErrorInfo
     */
    public static JSONObject getDataByCol(List<List<Object>>listBaseData,int col, JSONObject jsonErrorInfo) {
        
        JSONObject jsonData = new JSONObject();
        
        for (int i = 0; i < listBaseData.size(); i++) {
            String dataValue=listBaseData.get(i).get(col).toString();
            if (StringUtils.isEmpty(jsonData.get(dataValue))) {
                if (dataValue.isEmpty()) {
                    jsonData.put("", i);
                }else {
                    jsonData.put(dataValue, i);
                }
            }else {
                jsonErrorInfo.put(i+","+col, SystemParamReplace.paramEXCELError(ImportDataUtil.errorlanguage(SystemCode.SYSTEM_DATA_IMPORT_FIELD_UNIQUE_ERROR.getValue()), col, i, dataValue));
            }
        }
        return jsonData;
    }
    
    
    /**
     *     读取EXCEL数据
     * @param dataSheet    EXCEL数据
     * @param startCol    起始列
     * @param endCol    结束列
     * @return
     */
    public static List<List<Object>> readSheetData(Sheet dataSheet,int startCol,int endCol){
        //创建数据储存集合 行<列<值>>
        List<List<Object>> baseData=new ArrayList<>();
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
        
        for (int i = 2; i <= dataSheet.getLastRowNum(); i++) {
            Row row=dataSheet.getRow(i);
            List<Object> rowBaseData=new ArrayList<>();
            int isNUll=0;//空值单元格
            for (int j = startCol; j <= endCol; j++) {
                Cell cell=row.getCell(j);
                if (cell!=null) {
                    int cellType=cell.getCellType();
                    if (cellType==Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)) {
                        rowBaseData.add(sdf.format(cell.getDateCellValue()));
                    }else if (cellType!=Cell.CELL_TYPE_BLANK) {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        rowBaseData.add(cell.getStringCellValue());
                    }else {
                        rowBaseData.add(null);
                        isNUll++;
                    }
                }else {
                    rowBaseData.add(null);
                    isNUll++;
                }
                
            }
            //判断空值单元格个数是否与列数相等
            if(isNUll==(endCol-startCol+1)) {
                break;
            }
            baseData.add(rowBaseData);
        }
        return baseData;
    }
    
    
    /**
     *     国际化封装
     * @param currentField    当前字段
     * @param language_code    语言代码
     * @param languageValue    语言内容
     * @return
     * @throws BaseException
     */
    public static FieldSetEntity parseInternational(String currentField,String language_code,String languageValue) throws BaseException {
        FieldSetEntity fseLanguage=new FieldSetEntity();
        fseLanguage.setTableName(currentField);
        fseLanguage.setValue(language_code, languageValue);
        return fseLanguage;
    }
    
    
    /**
     *     获取异常信息国际化
     * @param code
     * @return
     */
    public static String errorlanguage(String code) throws BaseException{
         String language_code=SpringMVCContextHolder.getCurrentUser().getDefault_language();
         if (StringUtils.isEmpty(language_code)) {
            language_code="zh-CN";
         }
         String message=DataPoolCacheImpl.getInstance().getErrorLanguageInternation(code, language_code);
         return message;
    }
    
    
    /**
     *     组织机构分级
     * @param dtBaseData
     * @return 
     * @throws BaseException 
     */
    public static List<List<FieldSetEntity>> orgLevelGrades(DataTableEntity dtBaseData) throws BaseException {
        
        List<List<FieldSetEntity>> array =new ArrayList<>();
        for (int i = 0; i < dtBaseData.getRows(); i++) {
            FieldSetEntity fseBaseData=dtBaseData.getData().get(i);
            int grade=0;
            //获取上级组织机构全称
            String org_level_code_parent=fseBaseData.getString("org_level_code_parent");
            if (!StringUtils.isEmpty(org_level_code_parent)) {
                grade = org_level_code_parent.split(">").length;
            }
            // 如果数据集中没有对应该等级的下标则往里面新增
            while(array.size()-1<grade){
                array.add(new ArrayList<>());
            }
            array.get(grade).add(fseBaseData);
        }
        return array;
    }
    
    
    /**
     *     获取过滤条件参数
     * @param filter    高级参照过滤条件
     * @return
     */
    public static Map<String,String> getFilterPara(String filter) {
        // 按指定模式在字符串查找
        String pattern = "~\\w+~";
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(filter);
        Map<String,String> v = new HashMap<>();
        if (!StringUtils.isEmpty(filter)) {
            while (m.find()) {
                String c = m.group();
                v.put(c.replace("~", ""), c);
            }
        }
        return v;
    }
    
    
    /**
     *     导入表名验证
     * @param dataMap    导入数据信息
     * @param tableNames    表名
     * @throws BaseException 
     */
    public static void tableNameValidation(Map<String, DataTableEntity>dataMap,String tableNames) throws BaseException {
        String[] tableName=tableNames.split(",");
        for (int i = 0; i < tableName.length; i++) {
            if (dataMap.get(tableName[i])==null) {
                throw new BaseException(SystemCode.SYSTEM_IMPORT_TEBLENAME_ERROR.getValue(), SystemCode.SYSTEM_IMPORT_TEBLENAME_ERROR.getText());
            }
        }
    }
}