354798ggg
2023-10-08 58f6d099d3ba1a0c995efc52ba8558be838322d6
product-server-data-export-import/src/main/java/com/product/module/data/service/SystemDataUpLoadService.java
@@ -562,7 +562,7 @@
      baseDao.saveFieldSetEntity(fse);
      return fse.getUUID();
   }
   /**
    * 业务功能导入-解析并保存到数据库
    *
@@ -572,105 +572,95 @@
   @Override
   @Transactional
   public void recordDataSave(FieldSetEntity fse) {
      //获取当前人信息
      SystemUser curUser = SpringMVCContextHolder.getCurrentUser();
      FieldSetEntity logFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_RECORD, fse.getUUID(), false);
      if (logFse == null) {
      //获取模板导入记录
      FieldSetEntity uploadLog = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_RECORD, fse.getUUID(), false);
      if (uploadLog == null) {
         throw new BaseException(CmnCode.UPLOAD_TEMPLATE_GET_UPLOAD_RECORD_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_GET_UPLOAD_RECORD_FAIL.getText());
      }
      // 获取模板信息
      DataTableEntity templateDte = baseDao.listTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=?", new Object[]{logFse.getString(CmnConst.FIELD_UPLOAD_SIGN)}, null, "sheet_order", Integer.MAX_VALUE, 1, true);
      // 获取导入文件信息
      FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, logFse.getString(CmnConst.FIELD_IMPORT_INFO), false);
      String relativePath = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_URL);
      String templateName = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_TITLE);
      Map<String, List<List<String>>> dataMap = new HashMap<>();
      try {
         String filePath = Global.getSystemConfig("local.dir", "") + File.separator + relativePath + File.separator + templateName;
         dataMap = EasyExcelUtil.readExcelByString(filePath);
         if ("product_project_budget".equals(templateDte.getString(0, CmnConst.TABLE_NAME))) {
            for (int i = 1; i < dataMap.size(); i++) {
               templateDte.addFieldSetEntity(templateDte.getFieldSetEntity(0).clones());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      // 封装到dte中
      //获取导入模板配置信息
      DataTableEntity templateDt = baseDao.listTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=?", new Object[]{uploadLog.getString(CmnConst.FIELD_UPLOAD_SIGN)}, null, "sheet_order", Integer.MAX_VALUE, 1, true);
      //多Sheet原始数据集
      Map<String, List<List<String>>> dataMap = readExcelData(templateDt, uploadLog);
      //多Sheet解析数据集
      List<DataTableEntity> beforeSaveDataList = Lists.newArrayList();
      List<String> titleList;
      List<List<String>> dataList;
      List<String> rowList;
      String singleValue;// 单元格内的值
      int index = 0;
      FieldSetEntity curTemplateFse;
      Map<String, FieldSetEntity> curSubMap;
      //sheet索引
      int sheetIndex = 0;
      /**
       *    参照快速解析(未通用,仅限预算导入)
       */
      Map<String, List<String>> mapRefer = new HashMap<>();
      //表头数据
      List<List<List<String>>> headerDataList = Lists.newArrayList();
      List<Map<String, FieldSetEntity>> fieldInfoList = Lists.newArrayList();
      FieldSetEntity curFieldFse;
      FieldSetEntity recordFse;// 业务表fse
      DataTableEntity recordDte;// 业务表dte
      //遍历Sheet数据
      for (Map.Entry<String, List<List<String>>> entry : dataMap.entrySet()) {
         dataList = entry.getValue();
         if (dataList.isEmpty()) {
         //获取单个Sheet原始数据(单个Sheet所有行)
         List<List<String>> rowList = entry.getValue();
         if (rowList.isEmpty()) {
            continue;
         }
         recordDte = new DataTableEntity();
         curTemplateFse = templateDte.getFieldSetEntity(index);
         curSubMap = dte2Map(curTemplateFse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_EXCEL_HEADER);
         fieldInfoList.add(curSubMap);
         // 字段对应的excel 表头索引
         //单Sheet解析数据集合
         DataTableEntity parseDataDt = new DataTableEntity();
         //当前Sheet对应配置信息
         FieldSetEntity currentTemplate = templateDt.getFieldSetEntity(sheetIndex);
         //excel表头对应字段信息
         Map<String, FieldSetEntity> curSubMap = dte2Map(currentTemplate.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_EXCEL_HEADER);
         //字段对应的excel 表头索引
         Map<String, String> fieldIndex = Maps.newHashMap();
         //表头开始行 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"));
         if (!StringUtils.isEmpty(currentTemplate.getString("start_row_index")) && NumberUtil.isNumber(currentTemplate.getString("start_row_index"))) {
            startRowIndex = Integer.parseInt(currentTemplate.getString("start_row_index"));
            if (startRowIndex < 1) {
               startRowIndex = 1;
            }
         }
         titleList = dataList.get(startRowIndex);
         //获取excel表头最后一行数据(直接与数据对应)
         List<String> sheetTitle = rowList.get(startRowIndex);
         if (startRowIndex > 0) {
            headerDataList.add(dataList.subList(0, startRowIndex));
            headerDataList.add(rowList.subList(0, startRowIndex));
         } else {
            headerDataList.add(new ArrayList<>());
         }
         for (int i = startRowIndex + 1; i < dataList.size(); i++) {
            rowList = dataList.get(i);
            System.out.println(rowList.toString());
            recordFse = new FieldSetEntity();
            recordFse.setTableName(curTemplateFse.getString(CmnConst.FIELD_TABLE_NAME));
         //遍历excel原始数据行
         for (int i = startRowIndex + 1; i < rowList.size(); i++) {
            List<String> rowData = rowList.get(i);
            FieldSetEntity recordInfo = new FieldSetEntity();
            recordInfo.setTableName(currentTemplate.getString(CmnConst.FIELD_TABLE_NAME));
            //字段对应的列
            int j = 0;
            for (; j < titleList.size() && j<14; j++) {
               if (rowList.size() < (j + 1)) {
            for (int j = 0; j < sheetTitle.size() && j<14; j++) {
               if (rowData.size() < (j + 1)) {
                  singleValue = null;
               } else {
                  singleValue = rowList.get(j);
                  singleValue = rowData.get(j);
               }
               curFieldFse = curSubMap.get(titleList.get(j));
               System.out.println(curFieldFse.getString(CmnConst.FIELD_FIELD_NAME)+":"+singleValue);
               recordFse.setValue(curFieldFse.getString(CmnConst.FIELD_FIELD_NAME), singleValue);
               if (i == 1 || rowList.size() > fieldIndex.size()) {
                  fieldIndex.put(curFieldFse.getString(CmnConst.FIELD_FIELD_NAME), String.valueOf(j));
               //获取该值对应的字段信息
               FieldSetEntity currentFieldInfo = curSubMap.get(sheetTitle.get(j));
               //存储读取后的数据
               recordInfo.setValue(currentFieldInfo.getString(CmnConst.FIELD_FIELD_NAME), singleValue);
               if (i == 1 || rowData.size() > fieldIndex.size()) {
                  fieldIndex.put(currentFieldInfo.getString(CmnConst.FIELD_FIELD_NAME), String.valueOf(j));
               }
            }
            recordFse.setValue("~colName~", titleList);
            recordFse.setValue("~fieldIndex~", fieldIndex);
            // fieldSet 对应的行
            recordFse.setValue("~row~", i);
            recordDte.addFieldSetEntity(recordFse);
            recordInfo.setValue("~colName~", sheetTitle);
            recordInfo.setValue("~fieldIndex~", fieldIndex);
            //fieldSet 对应的行
            recordInfo.setValue("~row~", i);
            parseDataDt.addFieldSetEntity(recordInfo);
         }
         beforeSaveDataList.add(recordDte);
         index++;
         beforeSaveDataList.add(parseDataDt);
         sheetIndex++;
      }
      // 数据解析保存
      //数据解析保存
      String orgLevelUUID = curUser.getOrg_level_uuid();
      String actualValue;
      FieldMetaEntity meta;
@@ -680,9 +670,9 @@
      for (int i = 0; i < beforeSaveDataList.size(); i++) {
         tempDte = beforeSaveDataList.get(i);
         // 保存前处理
         //保存前处理
         try {
            tempDte = spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_BEFORE_FUNC), tempDte, headerDataList.get(i));
            tempDte = spDeal(templateDt.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());
            }
@@ -696,65 +686,96 @@
         meta = tempDte.getMeta();
         fieldArr = meta.getFields();
         curTemplateFse = templateDte.getFieldSetEntity(i);
         curSubMap = dte2Map(curTemplateFse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_FIELD_NAME);
         FieldSetEntity currentTemplate = templateDt.getFieldSetEntity(i);
         Map<String, FieldSetEntity> curSubMap = dte2Map(currentTemplate.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_FIELD_NAME);
         boolean isMultiFlag;
         StringBuilder errorMsg = new StringBuilder(8);
         for (int j = 0; j < tempDte.getRows(); j++) {
            recordFse = tempDte.getFieldSetEntity(j);
            FieldSetEntity recordInfo = tempDte.getFieldSetEntity(j);
            for (Object fieldNameObj : fieldArr) {
               if (fieldNameObj == null) {
                  continue;
               }
               fieldName = fieldNameObj.toString();
               singleValue = recordFse.getString(fieldName);
               singleValue = recordInfo.getString(fieldName);
               curFieldFse = curSubMap.get(fieldName);
               if (curFieldFse != null) {
               FieldSetEntity currentFieldInfo = curSubMap.get(fieldName);
               if (currentFieldInfo != null) {
                  // 提取真实值
                  if (!StringUtils.isEmpty(curFieldFse.getString(CmnConst.FIELD_PROMPT_NAME))) {
                     isMultiFlag = curFieldFse.getString(CmnConst.FIELD_IS_MULTI) == null ? false : "1".equals(curFieldFse.getString(CmnConst.FIELD_IS_MULTI));
                  if (!StringUtils.isEmpty(currentFieldInfo.getString(CmnConst.FIELD_PROMPT_NAME))) {
                     isMultiFlag = currentFieldInfo.getString(CmnConst.FIELD_IS_MULTI) == null ? false : "1".equals(currentFieldInfo.getString(CmnConst.FIELD_IS_MULTI));
                     // 根据参照名称,参照类型,显示域的值,获取隐藏域的值
                     actualValue = getRealValue(curFieldFse.getString(CmnConst.FIELD_PROMPT_NAME), singleValue, isMultiFlag);
                     actualValue = getRealValue(currentFieldInfo.getString(CmnConst.FIELD_PROMPT_NAME), singleValue, isMultiFlag);
                  } else {
                     actualValue = singleValue;
                  }
                  recordFse.setValue(curFieldFse.getString(CmnConst.FIELD_FIELD_NAME), actualValue);
                  recordInfo.setValue(currentFieldInfo.getString(CmnConst.FIELD_FIELD_NAME), actualValue);
               }
            }
            try {
               BaseUtil.createCreatorAndCreationTime(curUser, recordFse);
               baseDao.saveFieldSetEntity(recordFse);
            } catch (BaseException e) {
               e.printStackTrace();
//                    errorMsg.append("导入数据错误:\n\t");
               errorMsg.append(String.format("第%s个sheet,第%s行,%s", i + 1, j + 1, e.getMessageInfo() != null ? e.getMessageInfo() : "")).append("\n");
//                    throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), String.format("%s: 第%s个sheet,第%s行", CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getText(), i + 1, j + 1), e);
            } catch (Exception e) {
               errorMsg.append(String.format("第%s个sheet,第%s行,%s", i + 1, j + 1, e.getMessage() != null ? e.getMessage() : "")).append("\n");
//                    throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), String.format("%s: 第%s个sheet,第%s行", CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getText(), i + 1, j + 1));
            }
//            try {
            BaseUtil.createCreatorAndCreationTime(curUser, recordInfo);
//               baseDao.saveFieldSetEntity(recordInfo);
//            } catch (BaseException e) {
//               e.printStackTrace();
//               errorMsg.append(String.format("第%s个sheet,第%s行,%s", i + 1, j + 1, e.getMessageInfo() != null ? e.getMessageInfo() : "")).append("\n");
//            } catch (Exception e) {
//               errorMsg.append(String.format("第%s个sheet,第%s行,%s", i + 1, j + 1, e.getMessage() != null ? e.getMessage() : "")).append("\n");
//            }
         }
         if (errorMsg.length() > 0) {
            throw new BaseException(CmnCode.UPLOAD_TEMPLATE_IMPORT_DATA_FAIL.getValue(), errorMsg.toString());
         }
         // 保存后处理
         //批量保存
         try {
            spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_AFTER_FUNC), tempDte, headerDataList.get(i));
         } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            baseDao.add(tempDte);
         } catch (Exception e) {
            throw new BaseException(CmnCode.UPLOAD_TEMPLATE_AFTER_FUNC_EXEC_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_AFTER_FUNC_EXEC_FAIL.getText());
            e.printStackTrace();
         }
//          保存后处理
//         try {
//            spDeal(templateDt.getFieldSetEntity(i).getString(CmnConst.FIELD_AFTER_FUNC), tempDte, headerDataList.get(i));
//         } catch (BaseException e) {
//            SpringMVCContextHolder.getSystemLogger().error(e);
//         } catch (Exception e) {
//            throw new BaseException(CmnCode.UPLOAD_TEMPLATE_AFTER_FUNC_EXEC_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_AFTER_FUNC_EXEC_FAIL.getText());
//         }
      }
      // 更新记录表情况
      logFse.setValue(CmnConst.FIELD_IS_SUCCESS, 1);
      uploadLog.setValue(CmnConst.FIELD_IS_SUCCESS, 1);
      BaseUtil.updatedRegeneratorAndUpdateTime(curUser, fse);
      baseDao.saveFieldSetEntity(logFse);
      baseDao.saveFieldSetEntity(uploadLog);
   }
   /**
    *    读取上传的EXCEL原始数据,并匹配解析模板
    * @param templateDt
    * @param uploadLog
    * @return
    */
   public Map<String, List<List<String>>> readExcelData(DataTableEntity templateDt, FieldSetEntity uploadLog){
      FieldSetEntity templateInfo = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, uploadLog.getString(CmnConst.FIELD_IMPORT_INFO), false);
      String relativePath = templateInfo.getString(CmnConst.FIELD_ATTACHMENT_URL);
      String templateName = templateInfo.getString(CmnConst.FIELD_ATTACHMENT_TITLE);
      //生成上传EXCEL地址
      String filePath = Global.getSystemConfig("local.dir", "") + File.separator + relativePath + File.separator + templateName;
      try {
         //读取EXCEL多Sheet原始数据
         Map<String, List<List<String>>> dataMap  = EasyExcelUtil.readExcelByString(filePath);
         //多Sheet同表,克隆模板配置
         if ("product_project_budget".equals(templateDt.getString(0, CmnConst.TABLE_NAME))) {
            for (int i = 1; i < dataMap.size(); i++) {
               templateDt.addFieldSetEntity(templateDt.getFieldSetEntity(0).clones());
            }
         }
         return dataMap;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * dte按照指定fse中的值作为key转换为map
    *
@@ -770,12 +791,6 @@
         resultMap.put(tempFse.getString(keyFieldName), tempFse);
      }
      return resultMap;
   }
   public static void main(String[] args) {
      Class<?> a = DataTableEntity.class;
      Class<DataTableEntity> b = DataTableEntity.class;
      System.out.println(a.equals(b));
   }
   /**
@@ -874,7 +889,12 @@
               sql.append(" and ").append(dataFilter);
            }
         }
         FieldSetEntity sourceFse = baseDao.getFieldSetEntityBySQL(sql.toString(), showSet.toArray(), false);
         FieldSetEntity sourceFse=null;
         try {
            sourceFse = baseDao.getFieldSetEntityBySQL(sql.toString(), showSet.toArray(), false);
         } catch (Exception e) {
            e.printStackTrace();
         }
         if (sourceFse != null) {
            realValue = sourceFse.getString("real_value");
         }