许鹏程
2023-06-30 b09803bb3baffb75a3b739924b29ba9ada481403
数据模板导入
已修改1个文件
56 ■■■■ 文件已修改
src/main/java/com/product/module/data/service/SystemDataUpLoadService.java 56 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/module/data/service/SystemDataUpLoadService.java
@@ -1,6 +1,9 @@
package com.product.module.data.service;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@@ -21,7 +24,6 @@
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.core.util.ReflectUtil;
import com.product.file.service.FileManagerService;
import com.product.module.data.config.CmnCode;
import com.product.module.data.config.CmnConst;
@@ -37,6 +39,9 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
@Service
@@ -159,7 +164,7 @@
                    }
                    subDte = new DataTableEntity();
                    mainFse = null;
                    for (String excelHeader : list.get(0)) {
                    for (String excelHeader : list.get(list.size() - 1)) {
                        if (StringUtils.isEmpty(excelHeader)) {
                            throw new BaseException(CmnCode.UPLOAD_TEMPLATE_EXISTS_EMPTY_COL.getValue(), CmnCode.UPLOAD_TEMPLATE_EXISTS_EMPTY_COL.getText());
                        }
@@ -190,6 +195,7 @@
                    } else {
                        mainFse.removeSubData(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB);
                    }
                    mainFse.setValue("start_row_index", list.size() - 1);
                    mainFse.addSubDataTable(subDte);
                    index++;
                }
@@ -594,6 +600,7 @@
        int index = 0;
        FieldSetEntity curTemplateFse;
        Map<String, FieldSetEntity> curSubMap;
        List<List<List<String>>> headerDataList = Lists.newArrayList();
        List<Map<String, FieldSetEntity>> fieldInfoList = Lists.newArrayList();
        FieldSetEntity curFieldFse;
        FieldSetEntity recordFse;// 业务表fse
@@ -608,10 +615,24 @@
            curTemplateFse = templateDte.getFieldSetEntity(index);
            curSubMap = dte2Map(curTemplateFse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_EXCEL_HEADER);
            fieldInfoList.add(curSubMap);
            titleList = dataList.get(0);
            // 字段对应的excel 表头索引
            Map<String, String> fieldIndex = Maps.newHashMap();
            for (int i = 1; i < dataList.size(); i++) {
            //表头开始行 cheng update 2023年6月30日15:48:22 多行头时,表头开始行不一定是第一行
            int startRowIndex = 1;
            if (!StringUtils.isEmpty(curTemplateFse.getString("start_row_index")) && NumberUtil.isNumber(curTemplateFse.getString("start_row_index"))) {
                startRowIndex = Integer.parseInt(curTemplateFse.getString("start_row_index")) + 1;
                if (startRowIndex < 1) {
                    startRowIndex = 1;
                }
            }
            titleList = dataList.get(startRowIndex);
            if (startRowIndex > 0) {
                headerDataList.add(dataList.subList(0, startRowIndex));
            } else {
                headerDataList.add(new ArrayList<>());
            }
            for (int i = startRowIndex; i < dataList.size(); i++) {
                rowList = dataList.get(i);
                recordFse = new FieldSetEntity();
                recordFse.setTableName(curTemplateFse.getString(CmnConst.FIELD_TABLE_NAME));
@@ -651,7 +672,7 @@
            // 保存前处理
            try {
                tempDte = spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_BEFORE_FUNC), tempDte);
                tempDte = spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_BEFORE_FUNC), tempDte, headerDataList.get(i));
                if (BaseUtil.dataTableIsEmpty(tempDte)) {
                    throw new BaseException(CmnCode.UPLOAD_TEMPLATE_BEFORE_FUNC_EXEC_NO_DATA.getValue(), CmnCode.UPLOAD_TEMPLATE_BEFORE_FUNC_EXEC_NO_DATA.getText());
                }
@@ -710,7 +731,7 @@
            // 保存后处理
            try {
                spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_AFTER_FUNC), tempDte);
                spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_AFTER_FUNC), tempDte, headerDataList.get(i));
            } catch (BaseException e) {
                SpringMVCContextHolder.getSystemLogger().error(e);
            } catch (Exception e) {
@@ -746,8 +767,10 @@
     *
     * @param functionContent
     * @param dte
     * @param headerDataList
     * @return
     */
    private DataTableEntity spDeal(String functionContent, DataTableEntity dte) throws BaseException {
    private DataTableEntity spDeal(String functionContent, DataTableEntity dte, List<List<String>> headerDataList) throws BaseException {
        if (functionContent == null) {
            return dte;
        }
@@ -757,8 +780,23 @@
        }
        String className = functionContent.substring(0, functionContent.indexOf("."));
        String methodName = functionContent.substring(functionContent.indexOf(".") + 1, functionContent.indexOf("("));
        Object resultObj = ReflectUtil.invoke(className, methodName, new Object[]{dte});
        return (DataTableEntity) resultObj;
        //根据className获取类
        Object bean = SpringUtil.getBean(className);
        //使用huTool的反射工具类获取方法,方法的参数类型为DataTableEntity、List<List<String>>
        Method method = ReflectUtil.getMethod(bean.getClass(), methodName, DataTableEntity.class, List.class);
        if (method != null) {
            //判断method返回值为DataTableEntity
            boolean isBackDataTable = !method.getReturnType().equals(DataTableEntity.class);
            //判断第二个参数的泛型类型
            Type[] types = method.getGenericParameterTypes();
            Type type = types[1];
            //判断类型是否为List<List<String>>,如果是则调用方法
            if (isBackDataTable && type.getTypeName().equals("java.util.List<java.util.List<java.lang.String>>")) {
                return ReflectUtil.invoke(bean, methodName, dte, headerDataList);
            }
        }
        return ReflectUtil.invoke(bean, methodName, dte);
    }
    /**