package com.product.module.data.service; import cn.hutool.extra.spring.SpringUtil; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.product.common.collect.SetUtils; import com.product.common.excel.EasyExcelUtil; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.config.Global; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.entity.RequestParameterEntity; import com.product.core.exception.BaseException; import com.product.core.permission.PermissionService; import com.product.core.service.support.AbstractBaseService; 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; import com.product.module.data.service.idel.ISystemDataUpLoadService; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; import com.product.util.SystemParamReplace; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.util.*; @Service public class SystemDataUpLoadService extends AbstractBaseService implements ISystemDataUpLoadService { @Autowired private BaseDao baseDao; @Autowired private FileManagerService fileManagerService; @Autowired private QueryFilterService queryFilterService; @Autowired private PermissionService permissionService; public List> getPrompt() throws BaseException { StringBuilder sql = new StringBuilder(); sql.append(" select prompt_name `value` ,prompt_name `label` ,'prompt' type FROM product_sys_prompt "); sql.append(" union all "); sql.append(" select concat('《',dict_name,'》') dict_name,dict_name,'dict' type FROM product_sys_dict where is_used=1 group by dict_name "); FieldSetEntity f = new FieldSetEntity(); f.setTableName("x"); f.setValue("desc", "高级参照,普通参照"); DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{}); List> result = Lists.newArrayList(); if (!BaseUtil.dataTableIsEmpty(dt)) { List> promptList = Lists.newArrayList(); List> dictList = Lists.newArrayList(); Map item; for (int i = 0; i < dt.getRows(); i++) { String type = dt.getString(i, "type"); String value = dt.getString(i, "value"); String label = dt.getString(i, "label"); item = Maps.newHashMap(); item.put("value", value); item.put("label", label); if ("prompt".equals(type)) { promptList.add(item); } else { dictList.add(item); } } item = Maps.newHashMap(); item.put("label", "高级参照"); item.put("children", promptList); item.put("value", "~高级参照~"); result.add(item); item = Maps.newHashMap(); item.put("label", "普通参照"); item.put("children", dictList); item.put("value", "~普通参照~"); result.add(item); } return result; } /** * 模板上传 * * @param rpe * @return */ @Override @Transactional public JSONObject uploadTemplate(RequestParameterEntity rpe) { FieldSetEntity paramFse = rpe.getFormData(); String sourceFileName = paramFse.getString(CmnConst.FIELD_TEMPLATE_INFO); if (StringUtils.isEmpty(sourceFileName) || !sourceFileName.endsWith(".xlsx")) { throw new BaseException(CmnCode.UPLOAD_TEMPLATE_NOT_ALLOWED_FORMAT.getValue(), CmnCode.UPLOAD_TEMPLATE_NOT_ALLOWED_FORMAT.getText()); } FieldSetEntity fse = fileManagerService.uploadFile(rpe); JSONObject resultObj = new JSONObject(); resultObj.put(CmnConst.FIELD_TEMPLATE_INFO, fse.getString(CmnConst.FIELD_TEMPLATE_INFO)); return resultObj; } /** * 解析模板,获取Excel表头信息 * * @param fse * @return */ public JSONObject analysisTemplate(FieldSetEntity fse) { String attachmentUUID = fse.getString(CmnConst.FIELD_TEMPLATE_INFO); DataTableEntity modelDte = null; String modelUUID = fse.getUUID(); boolean needAnalysisFlag = true; if (!StringUtils.isEmpty(modelUUID)) { FieldSetEntity preModelFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, modelUUID, true); String preAttachmentUUID = preModelFse.getString(CmnConst.FIELD_TEMPLATE_INFO); modelDte = baseDao.listTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=?", new Object[]{preModelFse.getString(CmnConst.FIELD_UPLOAD_SIGN)}, null, null, Integer.MAX_VALUE, 1, true); if (preAttachmentUUID.equals(attachmentUUID)) { // 同样的文件,不需要重新解析 needAnalysisFlag = false; } } FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, attachmentUUID, false); if (needAnalysisFlag) { // 需要重新解析 String relativePath = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_URL); String templateName = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_TITLE); String path = Global.getSystemConfig("local.dir", "") + File.separator + relativePath + File.separator + templateName; File templateFile = new File(path); // 创建文件流操作对象 try (InputStream inputStream = new FileInputStream(templateFile)) { Map>> map = EasyExcelUtil.readExcelByStringFromInputStream(inputStream, null); DataTableEntity subDte; Map subMap; FieldSetEntity mainFse; FieldSetEntity subFse; List> list; int index = 0; for (Map.Entry>> entry : map.entrySet()) { list = entry.getValue(); if (list.isEmpty()) { continue; } subDte = new DataTableEntity(); mainFse = null; for (String excelHeader : list.get(0)) { if (StringUtils.isEmpty(excelHeader)) { throw new BaseException(CmnCode.UPLOAD_TEMPLATE_EXISTS_EMPTY_COL.getValue(), CmnCode.UPLOAD_TEMPLATE_EXISTS_EMPTY_COL.getText()); } subFse = null; if (modelDte != null && modelDte.getRows() > index) { mainFse = modelDte.getFieldSetEntity(index); subMap = dte2Map(mainFse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), CmnConst.FIELD_EXCEL_HEADER); if (subMap.containsKey(excelHeader)) { // 原有的字段 subFse = subMap.get(excelHeader); } } if (subFse == null) { // 本身没有的字段 subFse = new FieldSetEntity(); subFse.setTableName(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB); subFse.setValue(CmnConst.FIELD_EXCEL_HEADER, excelHeader); } subDte.addFieldSetEntity(subFse); } if (mainFse == null) { mainFse = new FieldSetEntity(); mainFse.setTableName(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); if (modelDte == null) { modelDte = new DataTableEntity(); } modelDte.addFieldSetEntity(mainFse); } else { mainFse.removeSubData(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB); } mainFse.addSubDataTable(subDte); index++; } } catch (Exception e) { e.printStackTrace(); throw new BaseException(CmnCode.UPLOAD_TEMPLATE_GET_INFO_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_GET_INFO_FAIL.getText()); } } JSONObject resultObj = new JSONObject(); resultObj.put("sheet_info", BaseUtil.dataTableEntityToJson(modelDte)); resultObj.put(CmnConst.FIELD_UPLOAD_SIGN, SpringMVCContextHolder.getCurrentUser().getClient_uuid() + "_" + attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_TITLE) + "_" + System.currentTimeMillis()); return resultObj; } /** * 保存 * * @param fse */ @Override @Transactional public void saveTemplate(FieldSetEntity fse) { if (StringUtils.isEmpty(fse.getUUID())) { addTemplate(fse); } else { updateTemplate(fse); } } /** * 新增模板 * * @param fse */ private void addTemplate(FieldSetEntity fse) { SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); String orgLevelUUID = curUser.getOrg_level_uuid(); String clientUUID = curUser.getClient_uuid(); String attachmentUUID = fse.getString(CmnConst.FIELD_TEMPLATE_INFO); FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, attachmentUUID, false); String uploadSign = clientUUID + "_" + attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_TITLE) + "_" + System.currentTimeMillis(); int sheetOrder = 1; fse.setValue(CmnConst.FIELD_SHEET_ORDER, sheetOrder); fse.setValue(CmnConst.FIELD_UPLOAD_SIGN, uploadSign); // 保存数据导入模板信息 DataTableEntity modelDte = fse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); if (!BaseUtil.dataTableIsEmpty(modelDte)) { FieldSetEntity tempFse; for (int i = 0; i < modelDte.getRows(); i++) { tempFse = modelDte.getFieldSetEntity(i); tempFse.setValue(CmnConst.FIELD_UPLOAD_SIGN, uploadSign); tempFse.setValue(CmnConst.FIELD_SHEET_ORDER, ++sheetOrder); BaseUtil.addOrgLeveLUUID(tempFse, Collections.singletonList(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), orgLevelUUID, curUser); baseDao.saveFieldSetEntity(tempFse); } fse.removeSubData(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); } BaseUtil.addOrgLeveLUUID(fse, Collections.singletonList(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB), orgLevelUUID, curUser); baseDao.saveFieldSetEntity(fse); String mvcUUID = fse.getString(CmnConst.FIELD_FUNCTION_UUID); String buttonTitle = fse.getString(CmnConst.FILED_BUTTON_TITLE); // todo 待测试,创建mvc相关内容 dealMvcInfo(mvcUUID, buttonTitle, uploadSign); } /** * 处理mvc中导入配置信息 * * @param mvcUUID * @param buttonTitle */ private void dealMvcInfo(String mvcUUID, String buttonTitle, String uploadSign) { FieldSetEntity firstPageFse = getFirstPageFse(mvcUUID); if (firstPageFse == null) { throw new BaseException(CmnCode.GET_MVC_FIRST_PAGE_INFO_FAIL.getValue(), CmnCode.GET_MVC_FIRST_PAGE_INFO_FAIL.getText()); } FieldSetEntity preImportButtonFse = getImportButtonFse(firstPageFse, mvcUUID); SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); if (preImportButtonFse == null) { // 本身没有导入按钮,自动创建 todo // 按钮 FieldSetEntity importButtonFse = new FieldSetEntity(); importButtonFse.setTableName(CmnConst.TABLE_PRODUCT_SYS_BUTTONS); importButtonFse.setValue(CmnConst.FILED_FUNCTION_UUID, mvcUUID); importButtonFse.setValue(CmnConst.FILED_BUTTON_NAME, "default_import"); importButtonFse.setValue(CmnConst.FILED_BUTTON_TITLE, buttonTitle); importButtonFse.setValue(CmnConst.FILED_STATUS_UUID, 1); importButtonFse.setValue(CmnConst.FILED_CATEGORY_UUID, "import"); importButtonFse.setValue(CmnConst.FILED_CLIENT_TYPE_UUID, "Web"); importButtonFse.setValue(CmnConst.FILED_ROUTE_NAME, BaseUtil.getPageCode()); importButtonFse.setValue("params", "{\"upload_sign\":\"" + uploadSign + "\"}"); importButtonFse.setValue("button_category_uuid", "import"); importButtonFse.setValue("button_type", 2); importButtonFse.setValue("is_main", 0); BaseUtil.createCreatorAndCreationTime(curUser, importButtonFse); baseDao.saveFieldSetEntity(importButtonFse); // // 页面 // FieldSetEntity importPageFse = new FieldSetEntity(); // importPageFse.setTableName(CmnConst.TABLE_PRODUCT_SYS_MVC_PAGE); // importPageFse.setValue(CmnConst.FILED_FUNCTION_UUID, mvcUUID); // importPageFse.setValue(CmnConst.FIELD_PAGE_NAME, "默认导入页面"); // importPageFse.setValue(CmnConst.FIELD_PAGE_TYPE, 2); // importPageFse.setValue(CmnConst.FIELD_PAGE_TYPE, 2); // BaseUtil.createCreatorAndCreationTime(curUser, importPageFse); // baseDao.saveFieldSetEntity(importPageFse); // 连线-列表界面-导入按钮 FieldSetEntity importLinkFse1 = new FieldSetEntity(); importLinkFse1.setTableName(CmnConst.TABLE_PRODUCT_SYS_MVC_LINK); importLinkFse1.setValue(CmnConst.FILED_FUNCTION_UUID, mvcUUID); importLinkFse1.setValue(CmnConst.FIELD_LINK_TYPE, 0); importLinkFse1.setValue(CmnConst.FIELD_LINE_FROM, firstPageFse.getUUID()); importLinkFse1.setValue(CmnConst.FIELD_FROM_TYPE, 2); importLinkFse1.setValue(CmnConst.FIELD_LINE_TO, importButtonFse.getUUID()); importLinkFse1.setValue(CmnConst.FIELD_TO_TYPE, 1); BaseUtil.createCreatorAndCreationTime(curUser, importLinkFse1); baseDao.saveFieldSetEntity(importLinkFse1); // // 连线-导入按钮-导入页面 // FieldSetEntity importLinkFse2 = new FieldSetEntity(); // importLinkFse2.setTableName(CmnConst.TABLE_PRODUCT_SYS_MVC_LINK); // importLinkFse2.setValue(CmnConst.FILED_FUNCTION_UUID, mvcUUID); // importLinkFse2.setValue(CmnConst.FIELD_LINK_TYPE, 0); // importLinkFse2.setValue(CmnConst.FIELD_LINE_FROM, importButtonFse.getUUID()); // importLinkFse2.setValue(CmnConst.FIELD_FROM_TYPE, 1); // importLinkFse2.setValue(CmnConst.FIELD_LINE_TO, importPageFse.getUUID()); // importLinkFse2.setValue(CmnConst.FIELD_TO_TYPE, 2); // BaseUtil.createCreatorAndCreationTime(curUser, importLinkFse2); // baseDao.saveFieldSetEntity(importLinkFse2); } else { // 本身存在导入按钮,直接替换相应的数据 todo preImportButtonFse.setValue("", ""); BaseUtil.updatedRegeneratorAndUpdateTime(curUser, preImportButtonFse); baseDao.saveFieldSetEntity(preImportButtonFse); } } /** * 提取指定mvc中【入口】后第一个页面 * * @param mvcUUID * @return */ private FieldSetEntity getFirstPageFse(String mvcUUID) { StringBuilder sql = new StringBuilder(256); sql.append("select p.* from (") .append("\n select * FROM product_sys_function_buttons where is_main=1 and function_uuid=?") .append("\n) b") .append("\ninner join product_sys_link l on b.uuid=l.line_from") .append("\ninner join product_sys_mvc_page p on l.line_to=p.uuid"); FieldSetEntity firstPageFse = baseDao.getFieldSetEntityBySQL(sql.toString(), new Object[]{mvcUUID}, false); return firstPageFse; } /** * 入口后第一个页面之后的导入 * * @param firstPageFse * @param mvcUUID * @return */ private FieldSetEntity getImportButtonFse(FieldSetEntity firstPageFse, String mvcUUID) { StringBuilder sql = new StringBuilder(256); sql.append("select b.* from (") .append("\n select * FROM product_sys_mvc_page where uuid=?") .append("\n) p") .append("\ninner join product_sys_link l on p.uuid=l.line_to") .append("\ninner join (") .append("\n select * FROM product_sys_function_buttons where function_uuid=? and button_category_uuid='import'") .append("\n) b on l.line_from=b.uuid"); FieldSetEntity preImportButtonFse = baseDao.getFieldSetEntityBySQL(sql.toString(), new Object[]{firstPageFse.getUUID(), mvcUUID}, false); return preImportButtonFse; } /** * 修改模板 * * @param fse */ private void updateTemplate(FieldSetEntity fse) { SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); // 保存数据导入模板信息 DataTableEntity modelDte = fse.getSubDataTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); if (!BaseUtil.dataTableIsEmpty(modelDte)) { FieldSetEntity tempFse; for (int i = 0; i < modelDte.getRows(); i++) { tempFse = modelDte.getFieldSetEntity(i); BaseUtil.updatedRegeneratorAndUpdateTime(curUser, tempFse); baseDao.saveFieldSetEntity(tempFse); } } fse.removeSubData(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); BaseUtil.updatedRegeneratorAndUpdateTime(curUser, fse); baseDao.saveFieldSetEntity(fse); } /** * 查询模板配置详情 * * @param fse * @return */ public FieldSetEntity findTemplate(FieldSetEntity fse) { String uploadSign = fse.getString(CmnConst.FIELD_UPLOAD_SIGN); DataTableEntity dt = baseDao.listTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=?", new Object[]{uploadSign}, null, "sheet_order", Integer.MAX_VALUE, 1, true); if (!BaseUtil.dataTableIsEmpty(dt)) { FieldSetEntity fs = new FieldSetEntity(); fs.setTableName(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL); DataTableEntity sub = new DataTableEntity(); for (int i = 0; i < dt.getRows(); i++) { if (i == 0) { fs.setValue(CmnConst.FIELD_TITLE, dt.getString(0, CmnConst.FIELD_TITLE)); fs.setValue(CmnConst.FIELD_FUNCTION_UUID, dt.getString(0, CmnConst.FIELD_FUNCTION_UUID)); fs.setValue(CmnConst.FILED_BUTTON_TITLE, dt.getString(0, CmnConst.FILED_BUTTON_TITLE)); fs.setValue(CmnConst.UUID, dt.getString(0, CmnConst.UUID)); fs.setValue(CmnConst.FIELD_UPLOAD_SIGN, dt.getString(0, CmnConst.FIELD_UPLOAD_SIGN)); fs.setValue(CmnConst.FIELD_TEMPLATE_INFO, dt.getString(0, CmnConst.FIELD_TEMPLATE_INFO)); } String before_func = dt.getString(i, CmnConst.FIELD_BEFORE_FUNC); String after_func = dt.getString(i, CmnConst.FIELD_AFTER_FUNC); String table_name = dt.getString(i, CmnConst.TABLE_NAME); FieldSetEntity f = new FieldSetEntity(); f.setTableName("sheet_info"); f.setValue(CmnConst.FIELD_BEFORE_FUNC, before_func); f.setValue(CmnConst.FIELD_AFTER_FUNC, after_func); f.setValue(CmnConst.UUID, dt.getString(i, CmnConst.UUID)); f.setValue(CmnConst.TABLE_NAME, table_name); f.setSubData(dt.getFieldSetEntity(i).getSubData()); sub.addFieldSetEntity(f); } fs.addSubDataTable(sub); return fs; } return null; } /** * 模板台账-左边树结构 * * @param fse * @return */ public DataTableEntity listTemplate(FieldSetEntity fse) { int pageSize = fse.getInteger(CmnConst.PAGESIZE) == null ? Integer.MAX_VALUE : fse.getInteger(CmnConst.PAGESIZE); int curPage = fse.getInteger(CmnConst.CPAGE) == null ? 1 : fse.getInteger(CmnConst.CPAGE); SystemUser user = SpringMVCContextHolder.getCurrentUser(); StringBuilder filter = new StringBuilder(128); filter.append("org_level_uuid=? and length(title)>0"); String queryFilter = queryFilterService.getQueryFilter(fse); if (!StringUtils.isEmpty(queryFilter)) { filter.append(" and ").append(queryFilter); } List paramList = Lists.newArrayList(); paramList.add(user.getOrg_level_uuid()); String[] filterParamArr = !StringUtils.isEmpty(fse.getString("~select_params~")) ? fse.getString("~select_params~").split(",") : null; if (filterParamArr != null) { Collections.addAll(paramList, filterParamArr); } return baseDao.listTable(fse.getTableName(), filter.toString(), paramList.toArray(), null, fse.getString(""), pageSize, curPage); } /** * 数据上传模板删除 * * @param fse * @throws BaseException */ @Override @Transactional public void deleteModel(FieldSetEntity fse) throws BaseException { String uploadSign = fse.getString(CmnConst.FIELD_UPLOAD_SIGN); DataTableEntity modelDte = baseDao.listTable(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=?", new Object[]{uploadSign}); if (BaseUtil.dataTableIsEmpty(modelDte)) { throw new BaseException(CmnCode.UPLOAD_TEMPLATE_DELETE_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_DELETE_FAIL.getText()); } FieldSetEntity mainModelFse = null; FieldSetEntity tempFse; List uuidParamList = Lists.newArrayList(); for (int i = 0; i < modelDte.getRows(); i++) { tempFse = modelDte.getFieldSetEntity(i); uuidParamList.add(tempFse.getUUID()); if (!StringUtils.isEmpty(tempFse.getString(CmnConst.FIELD_FUNCTION_UUID))) { mainModelFse = tempFse; } } if (mainModelFse == null) { throw new BaseException(CmnCode.UPLOAD_TEMPLATE_GET_MAIN_INFO_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_GET_MAIN_INFO_FAIL.getText()); } // 删除mvc相关的表信息 String mvcUUID = mainModelFse.getString(CmnConst.FIELD_FUNCTION_UUID); FieldSetEntity firstPageFse = getFirstPageFse(mvcUUID); // 导入按钮 FieldSetEntity importButtonFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_BUTTONS, "function_uuid=? and button_name='default_import'", new Object[]{mvcUUID}, false); if (importButtonFse != null) { // 连线-导入按钮-导入页面 // FieldSetEntity secondLinkFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_MVC_LINK, "function_uuid=? and link_type=0 and from_type=1 and line_from=?", new Object[]{mvcUUID, importButtonFse.getUUID()}, false); // 删除-导入页面 // baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_MVC_PAGE, new Object[]{secondLinkFse.getString(CmnConst.FIELD_LINE_TO)}); // 删除-连线-导入按钮-导入页面 // baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_MVC_LINK, "function_uuid=? and link_type=0 and from_type=1 and line_from=?", new Object[]{mvcUUID, importButtonFse.getUUID()}); // 删除-连线-列表界面-导入按钮 baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_MVC_LINK, "function_uuid=? and link_type=0 and from_type=2 and to_type=1 and line_from=? and line_to=?", new Object[]{mvcUUID, firstPageFse.getUUID(), importButtonFse.getUUID()}); // 删除-导入按钮 baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_BUTTONS, new Object[]{importButtonFse.getUUID()}); } // 删除附件 FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, mainModelFse.getString(CmnConst.FIELD_TEMPLATE_INFO), false); if (attachmentFse != null) { String relativePath = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_URL); String templateName = attachmentFse.getString(CmnConst.FIELD_ATTACHMENT_TITLE); String path = Global.getSystemConfig("local.dir", "") + File.separator + relativePath + File.separator + templateName; File templateFile = new File(path); templateFile.delete(); // 删除附件表 baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_ATTACHMENT, new Object[]{attachmentFse.getUUID()}); } // 删除模板子表 String filter = BaseUtil.buildQuestionMarkFilter(CmnConst.FIELD_MODEL_UUID, uuidParamList.size(), true); baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL_SUB, filter, uuidParamList.toArray()); // 删除模板主表 baseDao.delete(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, uuidParamList.toArray()); } /** * 模板下载 * * @param fse * @param response * @throws IOException */ public void downloadTemplate(FieldSetEntity fse, HttpServletResponse response) throws IOException { String upload_sign = fse.getString("upload_sign"); FieldSetEntity templateFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_DATA_UPLOAD_MODEL, "upload_sign=? AND length(title)>0", new Object[]{upload_sign}, false); String attachmentUUID = templateFse.getString(CmnConst.FIELD_TEMPLATE_INFO); fse.setValue(CmnConst.UUID, attachmentUUID); fileManagerService.getFileContent(fse, response); } /** * 业务功能导入-上传文件 * * @param rpe * @return */ @Override @Transactional public String recordDataImport(RequestParameterEntity rpe) { FieldSetEntity ff = fileManagerService.uploadFile(rpe); FieldSetEntity fse=rpe.getFormData(); fse.setValue(CmnConst.FIELD_IS_SUCCESS, 0); SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); fse.setValue(CmnConst.FIELD_ORG_LEVEL_UUID, curUser.getOrg_level_uuid()); BaseUtil.createCreatorAndCreationTime(curUser, fse); fse.setValue("import_info",ff.getString("import_info")); baseDao.saveFieldSetEntity(fse); return fse.getUUID(); } /** * 业务功能导入-解析并保存到数据库 * * @param fse * @return */ @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) { 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>> dataMap= new HashMap<>(); try{ String filePath = Global.getSystemConfig("local.dir", "") + File.separator + relativePath + File.separator + templateName; dataMap = EasyExcelUtil.readExcelByString(filePath); }catch (Exception e){ e.printStackTrace(); } // 封装到dte中 List beforeSaveDataList = Lists.newArrayList(); List titleList; List> dataList; List rowList; String singleValue;// 单元格内的值 int index = 0; FieldSetEntity curTemplateFse; Map curSubMap; List> fieldInfoList = Lists.newArrayList(); FieldSetEntity curFieldFse; FieldSetEntity recordFse;// 业务表fse DataTableEntity recordDte;// 业务表dte for (Map.Entry>> entry : dataMap.entrySet()) { dataList = entry.getValue(); if (dataList.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); titleList = dataList.get(0); // 字段对应的excel 表头索引 Map fieldIndex = Maps.newHashMap(); for (int i = 1; i < dataList.size(); i++) { rowList = dataList.get(i); recordFse = new FieldSetEntity(); recordFse.setTableName(curTemplateFse.getString(CmnConst.FIELD_TABLE_NAME)); //字段对应的列 int j = 0; for (; j < titleList.size(); j++) { if (rowList.size() < (j + 1)) { singleValue = null; } else { singleValue = rowList.get(j); } curFieldFse = curSubMap.get(titleList.get(j)); 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)); } } recordFse.setValue("~colName~", titleList); recordFse.setValue("~fieldIndex~", fieldIndex); // fieldSet 对应的行 recordFse.setValue("~row~", i); recordDte.addFieldSetEntity(recordFse); } beforeSaveDataList.add(recordDte); index++; } // 数据解析保存 String orgLevelUUID = curUser.getOrg_level_uuid(); String actualValue; FieldMetaEntity meta; Object[] fieldArr; DataTableEntity tempDte; String fieldName; for (int i = 0; i < beforeSaveDataList.size(); i++) { tempDte = beforeSaveDataList.get(i); // 保存前处理 try { tempDte = spDeal(templateDte.getFieldSetEntity(i).getString(CmnConst.FIELD_BEFORE_FUNC), tempDte); 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()); } } catch (BaseException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new BaseException(CmnCode.UPLOAD_TEMPLATE_BEFORE_FUNC_EXEC_FAIL.getValue(), CmnCode.UPLOAD_TEMPLATE_BEFORE_FUNC_EXEC_FAIL.getText()); } 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); boolean isMultiFlag; StringBuilder errorMsg = new StringBuilder(8); for (int j = 0; j < tempDte.getRows(); j++) { recordFse = tempDte.getFieldSetEntity(j); for (Object fieldNameObj : fieldArr) { if (fieldNameObj == null) { continue; } fieldName = fieldNameObj.toString(); singleValue = recordFse.getString(fieldName); curFieldFse = curSubMap.get(fieldName); if (curFieldFse != 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)); // 根据参照名称,参照类型,显示域的值,获取隐藏域的值 actualValue = getRealValue(curFieldFse.getString(CmnConst.FIELD_PROMPT_NAME), singleValue, isMultiFlag); } else { actualValue = singleValue; } recordFse.setValue(curFieldFse.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)); } } 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); } 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); BaseUtil.updatedRegeneratorAndUpdateTime(curUser, fse); baseDao.saveFieldSetEntity(logFse); } /** * dte按照指定fse中的值作为key转换为map * * @param dte * @param keyFieldName * @return */ private Map dte2Map(DataTableEntity dte, String keyFieldName) { Map resultMap = Maps.newHashMap(); FieldSetEntity tempFse; for (int i = 0; i < dte.getRows(); i++) { tempFse = dte.getFieldSetEntity(i); resultMap.put(tempFse.getString(keyFieldName), tempFse); } return resultMap; } /** * 事件前或者事件后调用逻辑方法的特殊处理 * * @param functionContent * @param dte */ private DataTableEntity spDeal(String functionContent, DataTableEntity dte) throws BaseException { if (functionContent == null) { return dte; } functionContent = functionContent.trim(); if (StringUtils.isEmpty(functionContent)) { return dte; } 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; } /** * 获取真实值 * * @param promptName * @param showValue * @return */ private String getRealValue(String promptName, String showValue, boolean isMultiFlag) { String realValue = showValue; DataTableEntity promptDte; FieldSetEntity promptFse; if (promptName.startsWith("《")) { // 普通参照 promptName = promptName.replace("《", "").replace("》", ""); promptDte = DataPoolCacheImpl.getInstance().getCacheData(CmnConst.CACHE_COMMON_PROMPT, new String[]{promptName}); if (!BaseUtil.dataTableIsEmpty(promptDte)) { Set showSet = SetUtils.string2Set(showValue, ","); Set realSet = Sets.newLinkedHashSet(); for (int i = 0; i < promptDte.getRows(); i++) { promptFse = promptDte.getFieldSetEntity(i); if (isMultiFlag) { if (showSet.contains(promptFse.getString(CmnConst.FIELD_DICT_LABEL))) { realSet.add(promptFse.getString(CmnConst.FIELD_DICT_VALUE)); } } else { if (showValue.equals(promptFse.getString(CmnConst.FIELD_DICT_LABEL))) { realValue = promptFse.getString(CmnConst.FIELD_DICT_VALUE); break; } } } if (!realSet.isEmpty()) { realValue = SetUtils.set2String(realSet, ","); } } } else { // 高级参照 promptFse = BaseUtil.getSingleInfoByCache(CmnConst.CACHE_ADVANCED_PROMPT, new String[]{promptName}); String sourceTableName = promptFse.getString(CmnConst.FILED_SOURCE_TABLE); String filter = promptFse.getString(CmnConst.FILED_FILTER); String showFieldName = promptFse.getString(CmnConst.FILED_VIEW_FIELD); Set showSet = SetUtils.string2Set(showValue, ","); String promptFilter = BaseUtil.buildQuestionMarkFilter(showFieldName, isMultiFlag ? showSet.size() : 1, true); StringBuilder sql = new StringBuilder(256); String realFieldName = promptFse.getString(CmnConst.FILED_VALUE_FIELD); sql.append("select GROUP_CONCAT(").append(realFieldName).append(") real_value from ").append(sourceTableName).append(" where ").append(promptFilter); if (!StringUtils.isEmpty(filter)) { filter = SystemParamReplace.systemParamsReplace(filter); sql.append(" and ").append(filter); } // 补充特定字段数据过滤 if (!StringUtils.isEmpty(promptFse.getString("org_filter_field")) || !StringUtils.isEmpty(promptFse.getString("user_filter_field"))) { String dataFilter = permissionService.getDataFilter(sourceTableName, promptFse.getString("user_filter_field"), promptFse.getString("org_filter_field")); if (!StringUtils.isEmpty(dataFilter)) { sql.append(" and ").append(dataFilter); } } FieldSetEntity sourceFse = baseDao.getFieldSetEntityBySQL(sql.toString(), showSet.toArray(), false); if (sourceFse != null) { realValue = sourceFse.getString("real_value"); } } return realValue; } }