parent
bb1d1f2c49
commit
b8ac7e93e3
|
@ -1,17 +1,27 @@
|
|||
package com.hzya.frame.mdm.mdmModule.controller;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.metadata.Head;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hzya.frame.mdm.mdmModule.service.IMdmModuleService;
|
||||
import com.hzya.frame.mdm.mdmModule.vo.ExcelTemplateVO;
|
||||
import com.hzya.frame.mdm.mdmModule.vo.ImportExcelVO;
|
||||
import com.hzya.frame.mdm.mdmModuleDbFileds.entity.MdmModuleDbFiledsEntity;
|
||||
import com.hzya.frame.mdm.service.IMdmService;
|
||||
import com.hzya.frame.web.entity.JsonResultEntity;
|
||||
import jline.internal.Log;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -21,6 +31,29 @@ import java.util.Map;
|
|||
public class ImportExcelController {
|
||||
@Autowired
|
||||
private IMdmModuleService iMdmModuleService;
|
||||
@Autowired
|
||||
private IMdmService iMdmService;
|
||||
|
||||
|
||||
/**
|
||||
* 下载字段模版
|
||||
*/
|
||||
@RequestMapping(value = "generateExcelTemplate",method = RequestMethod.POST)
|
||||
public void generateExcelTemplate(HttpServletResponse response) throws IOException {
|
||||
// 设置响应内容类型
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
|
||||
// 设置文件名,处理中文编码
|
||||
String fileName = URLEncoder.encode("表字段定义模版", "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
|
||||
List<ExcelTemplateVO> demoData = new ArrayList<>();
|
||||
// 使用EasyExcel写入响应输出流
|
||||
EasyExcel.write(response.getOutputStream(), ExcelTemplateVO.class)
|
||||
.sheet("表字段定义模版")
|
||||
.doWrite(demoData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入模版
|
||||
|
@ -28,21 +61,76 @@ public class ImportExcelController {
|
|||
@RequestMapping(value = "/importTemplateFile" ,method = RequestMethod.POST)
|
||||
public JsonResultEntity importTemplateFile(@RequestParam("file") MultipartFile file,
|
||||
@ModelAttribute ImportExcelVO importExcelVO){
|
||||
List<MdmModuleDbFiledsEntity> entities = iMdmModuleService.importTemplateFile(file,importExcelVO);
|
||||
// 新增字段
|
||||
for (MdmModuleDbFiledsEntity entity : entities) {
|
||||
// JSONObject object = (JSONObject) JSONObject.toJSON(entity);
|
||||
|
||||
List<MdmModuleDbFiledsEntity> entities = iMdmModuleService.importTemplateFile(file,importExcelVO);
|
||||
// 新增字段
|
||||
for (MdmModuleDbFiledsEntity entity : entities) {
|
||||
// JSONObject object = (JSONObject) JSONObject.toJSON(entity);
|
||||
String str = JSONObject.toJSONString(entity);
|
||||
Map<String,String> jsonStr=new HashMap<>();
|
||||
jsonStr.put("jsonStr",str);
|
||||
String jsonS = JSON.toJSONString(jsonStr);
|
||||
JSONObject jsonObject = JSONObject.parseObject(jsonS);
|
||||
|
||||
String str = JSONObject.toJSONString(entity);
|
||||
Map<String,String> jsonStr=new HashMap<>();
|
||||
jsonStr.put("jsonStr",str);
|
||||
String jsonS = JSON.toJSONString(jsonStr);
|
||||
JSONObject jsonObject = JSONObject.parseObject(jsonS);
|
||||
iMdmModuleService.saveMdmDbField(jsonObject);
|
||||
}
|
||||
return new JsonResultEntity("导入模版成功",true,200);
|
||||
}
|
||||
|
||||
iMdmModuleService.saveMdmDbField(jsonObject);
|
||||
}
|
||||
return new JsonResultEntity("导入模版成功",true,200);
|
||||
/**
|
||||
* 下载数据模版
|
||||
*/
|
||||
@RequestMapping(value = "generateDataTemplate",method = RequestMethod.POST)
|
||||
public void generateDataTemplate(HttpServletResponse response,@RequestParam("mdmId") String mdmId,@RequestParam("dbId") String dbId) throws IOException {
|
||||
// 设置响应内容类型
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
|
||||
// 设置文件名,处理中文编码
|
||||
String fileName = URLEncoder.encode("数据模版", "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
|
||||
List<String> dataTemplate = iMdmService.selectFieldsByMdmId(mdmId,dbId);
|
||||
// 动态表头信息
|
||||
List<List<String>> headers = new ArrayList<>();
|
||||
// 每个字符串作为一个表头列
|
||||
for (String field : dataTemplate) {
|
||||
List<String> header = new ArrayList<>();
|
||||
header.add(field);
|
||||
headers.add(header);
|
||||
}
|
||||
List<List<String>> data = new ArrayList<>();
|
||||
// 使用EasyExcel写入动态表头
|
||||
EasyExcel.write(response.getOutputStream())
|
||||
.head(headers) // 设置动态表头
|
||||
.sheet("数据模板")
|
||||
.registerWriteHandler(new AbstractColumnWidthStyleStrategy() {
|
||||
@Override
|
||||
protected void setColumnWidth(WriteSheetHolder writeSheetHolder,
|
||||
List<WriteCellData<?>> cellDataList,
|
||||
Cell cell,
|
||||
Head head,
|
||||
Integer relativeRowIndex,
|
||||
Boolean isHead) {
|
||||
// 1. 获取当前单元格的列索引
|
||||
int columnIndex = cell.getColumnIndex();
|
||||
int width = isHead ? 20 * 256 : 15 * 256;
|
||||
// 3. 通过Sheet设置列宽
|
||||
writeSheetHolder.getSheet().setColumnWidth(columnIndex, width);
|
||||
}
|
||||
})
|
||||
.doWrite(data); // 写入数据
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据模版
|
||||
*/
|
||||
@PostMapping("/importDataTemplate")
|
||||
public JsonResultEntity importExcel(@RequestParam("file") MultipartFile file,@RequestParam("mdmCode") Long mdmCode,@RequestParam("dbName")String dbName) throws IOException {
|
||||
iMdmService.importDataTemplate(file,mdmCode,dbName);
|
||||
return new JsonResultEntity("导入模版成功",true,200);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -409,7 +409,7 @@ public interface IMdmModuleService extends IBaseService<MdmModuleEntity, String>
|
|||
* 下载导入模版
|
||||
* @return
|
||||
*/
|
||||
void generateExcelTemplate(HttpServletResponse response) throws IOException;
|
||||
// void generateExcelTemplate(HttpServletResponse response) throws IOException;
|
||||
|
||||
/**
|
||||
* 导入模版
|
||||
|
|
|
@ -462,7 +462,7 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
if ("1".equals(mdmModuleDbEntities.get(i).getDbType()) || "2".equals(mdmModuleDbEntities.get(i).getDbType())) {
|
||||
dbEntities.add(mdmModuleDbEntities.get(i));
|
||||
//查询合并规则。add by zyd 20250707
|
||||
List<String> filedNames=new ArrayList<>();
|
||||
List<String> filedNames = new ArrayList<>();
|
||||
if (mdmModuleDbEntities.get(i).getMergeRules() != null && !"".equals(mdmModuleDbEntities.get(i).getMergeRules())) {
|
||||
String mergeRulesIds = mdmModuleDbEntities.get(i).getMergeRules();
|
||||
String[] split = mergeRulesIds.split(",");
|
||||
|
@ -470,13 +470,13 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
|
||||
mdmModuleDbFiledsEntity.setId(filedId);
|
||||
List<MdmModuleDbFiledsEntity> fileds = mdmModuleDbFiledsDao.query(mdmModuleDbFiledsEntity);
|
||||
if(fileds.size()!=0){
|
||||
if (fileds.size() != 0) {
|
||||
filedNames.add(fileds.get(0).getChName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
mdmModuleDbEntities.get(i).setMergeRulesStr(String.join(",",filedNames));
|
||||
mdmModuleDbEntities.get(i).setMergeRulesStr(String.join(",", filedNames));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1434,6 +1434,13 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
return BaseResult.getFailureMessageEntity("系统错误");
|
||||
}
|
||||
|
||||
// 校验dbId
|
||||
List<MdmModuleDbEntity> all = mdmModuleDbDao.getAll();
|
||||
for (MdmModuleDbEntity mdmModuleDbEntity : all) {
|
||||
if (!entity.getDbId().equals(mdmModuleDbEntity.getId()))
|
||||
return BaseResult.getFailureMessageEntity("数据库不存在");
|
||||
}
|
||||
|
||||
MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
|
||||
mdmModuleDbFiledsEntity.setMdmId(entity.getMdmId());
|
||||
mdmModuleDbFiledsEntity.setSts("Y");
|
||||
|
@ -2982,7 +2989,7 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
|
||||
/**
|
||||
* 生成导入模版
|
||||
*/
|
||||
*//*
|
||||
@Override
|
||||
public void generateExcelTemplate(HttpServletResponse response) throws IOException {
|
||||
// 设置响应内容类型
|
||||
|
@ -2992,12 +2999,12 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
String fileName = URLEncoder.encode("表字段定义模版", "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
// 准备模板的示例数据
|
||||
List<ExcelTemplateVO> demoData = new ArrayList<>(); //createTemplateDemoData()
|
||||
List<ExcelTemplateVO> demoData = new ArrayList<>(); //createTemplateDemoData()
|
||||
// 使用EasyExcel直接写入响应输出流
|
||||
EasyExcel.write(response.getOutputStream(), ExcelTemplateVO.class)
|
||||
.sheet("表字段定义模版")
|
||||
.doWrite(demoData);
|
||||
}
|
||||
}*/
|
||||
|
||||
/*private List<ExcelTemplateVO> createTemplateDemoData() {
|
||||
List<ExcelTemplateVO> demoData = new ArrayList<>();
|
||||
|
@ -3021,12 +3028,13 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
return demoData;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 导入模版
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public List<MdmModuleDbFiledsEntity> importTemplateFile(MultipartFile file , ImportExcelVO importExcelVO) {
|
||||
public List<MdmModuleDbFiledsEntity> importTemplateFile(MultipartFile file, ImportExcelVO importExcelVO) {
|
||||
// 校验文件合法性
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new IllegalArgumentException("导入文件不能为空");
|
||||
|
@ -3059,10 +3067,11 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
// 获取最大列索引,确保顺序正确
|
||||
int maxColIndex = headMap.keySet().stream().mapToInt(Integer::intValue).max().orElse(-1);
|
||||
for (int i = 0; i <= maxColIndex; i++) {
|
||||
header.add(headMap.getOrDefault(i, "")); // 无数据的列填空
|
||||
header.add(headMap.getOrDefault(i, ""));
|
||||
}
|
||||
headerList.add(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 每解析一行数据触发
|
||||
*/
|
||||
|
@ -3076,6 +3085,7 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
validateData(data, context.readRowHolder().getRowIndex() + 1);
|
||||
dataList.add(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有数据解析完成后触发
|
||||
*/
|
||||
|
@ -3088,7 +3098,7 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
}
|
||||
}
|
||||
})
|
||||
.doRead(); // 执行读取操作
|
||||
.doRead();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("文件读取失败:" + e.getMessage(), e);
|
||||
}
|
||||
|
@ -3096,20 +3106,21 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
}
|
||||
|
||||
private boolean isRowEmpty(ExcelTemplateVO data) {
|
||||
if(data == null) {
|
||||
if (data == null) {
|
||||
return true;
|
||||
}
|
||||
return StringUtils.isEmpty(data.getChName() ) &&
|
||||
return StringUtils.isEmpty(data.getChName()) &&
|
||||
StringUtils.isEmpty(data.getFiledType()) &&
|
||||
StringUtils.isEmpty(data.getEnName()) ;
|
||||
StringUtils.isEmpty(data.getEnName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据,新增字段
|
||||
*
|
||||
* @param headerList
|
||||
* @param dataList
|
||||
*/
|
||||
private List<MdmModuleDbFiledsEntity> processImportData(List<List<String>> headerList, List<ExcelTemplateVO> dataList,ImportExcelVO importExcelVO) {
|
||||
private List<MdmModuleDbFiledsEntity> processImportData(List<List<String>> headerList, List<ExcelTemplateVO> dataList, ImportExcelVO importExcelVO) {
|
||||
// 获取表头列名
|
||||
List<String> headers = headerList.get(0);
|
||||
// 数据库字段数据
|
||||
|
@ -3172,7 +3183,8 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
|
||||
/**
|
||||
* 数据校验方法,验证Excel导入的数据合法性
|
||||
* @param data 导入的Excel数据对象
|
||||
*
|
||||
* @param data 导入的Excel数据对象
|
||||
* @param rowNum 行号
|
||||
*/
|
||||
private void validateData(ExcelTemplateVO data, int rowNum) {
|
||||
|
@ -3193,7 +3205,7 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
}
|
||||
}
|
||||
|
||||
// 3. 校验长度(数字类型且当字段类型为1、2、3时必填)
|
||||
// 3. 校验长度
|
||||
if (StringUtils.isNotBlank(data.getFiledType()) &&
|
||||
(data.getFiledType().equals("1") || data.getFiledType().equals("2") || data.getFiledType().equals("3"))) {
|
||||
|
||||
|
@ -3209,28 +3221,31 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
}
|
||||
}
|
||||
|
||||
// 4. 校验系统主键标识合法性
|
||||
if (StringUtils.isNotBlank(data.getIsSysPk()) &&
|
||||
!"Y".equals(data.getIsSysPk()) && !"N".equals(data.getIsSysPk())) {
|
||||
errors.add("第" + rowNum + "行:是否系统主键只能为Y或N");
|
||||
// 4. 校验系统主键标识合法性(允许为空,非空时只能是Y/N)
|
||||
if (data.getIsSysPk() != null && !data.getIsSysPk().trim().isEmpty()) {
|
||||
if (!"Y".equals(data.getIsSysPk()) && !"N".equals(data.getIsSysPk())) {
|
||||
errors.add("第" + rowNum + "行:是否系统主键只能为Y或N");
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 校验系统数据编码标识合法性
|
||||
if (StringUtils.isNotBlank(data.getIsSysCode()) &&
|
||||
!"Y".equals(data.getIsSysCode()) && !"N".equals(data.getIsSysCode())) {
|
||||
errors.add("第" + rowNum + "行:是否系统数据编码只能为Y或N");
|
||||
// 5. 校验系统数据编码标识合法性(允许为空,非空时只能是Y/N)
|
||||
if (data.getIsSysCode() != null && !data.getIsSysCode().trim().isEmpty()) {
|
||||
if (!"Y".equals(data.getIsSysCode()) && !"N".equals(data.getIsSysCode())) {
|
||||
errors.add("第" + rowNum + "行:是否系统数据编码只能为Y或N");
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 校验系统数据名称标识合法性
|
||||
if (StringUtils.isNotBlank(data.getIsSysName()) &&
|
||||
!"Y".equals(data.getIsSysName()) && !"N".equals(data.getIsSysName())) {
|
||||
errors.add("第" + rowNum + "行:是否系统数据名称只能为Y或N");
|
||||
// 6. 校验系统数据名称标识合法性(允许为空,非空时只能是Y/N)
|
||||
if (data.getIsSysName() != null && !data.getIsSysName().trim().isEmpty()) {
|
||||
if (!"Y".equals(data.getIsSysName()) && !"N".equals(data.getIsSysName())) {
|
||||
errors.add("第" + rowNum + "行:是否系统数据名称只能为Y或N");
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 校验系统日期标识合法性
|
||||
if (StringUtils.isNotBlank(data.getIsSysDate()) &&
|
||||
!"Y".equals(data.getIsSysDate()) && !"N".equals(data.getIsSysDate())) {
|
||||
errors.add("第" + rowNum + "行:是否系统日期只能为Y或N");
|
||||
// 7. 校验系统日期标识合法性(允许为空,非空时只能是Y/N)
|
||||
if (data.getIsSysDate() != null && !data.getIsSysDate().trim().isEmpty()) {
|
||||
if (!"Y".equals(data.getIsSysDate()) && !"N".equals(data.getIsSysDate())) {
|
||||
errors.add("第" + rowNum + "行:是否系统日期只能为Y或N");
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 校验是否必填标识合法性
|
||||
|
@ -3262,7 +3277,4 @@ public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> i
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.hzya.frame.mdm.service;
|
|||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hzya.frame.mdm.entity.MdmDataFiledDto;
|
||||
import com.hzya.frame.web.entity.JsonResultEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -141,4 +142,21 @@ public interface IMdmService {
|
|||
* @return void
|
||||
**/
|
||||
void updateTreeUseData(String mdmId, String tableName, String id, List<MdmDataFiledDto> mdmDataFiledDtos);
|
||||
|
||||
/**
|
||||
* @Author lvleigang
|
||||
* @Description 根据主数据id查询字段(中文名称/英文名称)
|
||||
* @param mdmId
|
||||
* @return
|
||||
*/
|
||||
List<String> selectFieldsByMdmId(String mdmId,String dbId);
|
||||
|
||||
/**
|
||||
* 导入数据模版
|
||||
* @param file
|
||||
* @param mdmCode
|
||||
* @param dbName
|
||||
* @return
|
||||
*/
|
||||
void importDataTemplate(MultipartFile file, Long mdmCode, String dbName);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@ package com.hzya.frame.mdm.service.impl;
|
|||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
@ -43,12 +46,9 @@ import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
|
|||
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
|
||||
import com.hzya.frame.sysnew.application.script.dao.ISysApplicationScriptDao;
|
||||
import com.hzya.frame.sysnew.application.script.entity.SysApplicationScriptEntity;
|
||||
import com.hzya.frame.sysnew.buttonConfig.dao.ISysButtonConfigDao;
|
||||
import com.hzya.frame.sysnew.grovy.service.IGroovyIntegrationService;
|
||||
import com.hzya.frame.sysnew.integtationTaskLivingDetails.entity.IntegrationTaskLivingDetailsEntity;
|
||||
import com.hzya.frame.sysnew.integtationTaskLivingDetails.service.IIntegrationTaskLivingDetailsService;
|
||||
import com.hzya.frame.sysnew.menuConfig.dao.ISysMenuConfigDao;
|
||||
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
|
||||
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
|
||||
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
|
||||
import com.hzya.frame.uuid.UUIDUtils;
|
||||
|
@ -66,9 +66,12 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -1639,7 +1642,6 @@ public class MdmServiceImpl implements IMdmService {
|
|||
return BaseResult.getFailureMessageEntity("系统错误");
|
||||
}
|
||||
|
||||
|
||||
String res = checkDataOnly(jsonObject.getInteger("mdmCode"), saveData, false, mdmModuleEntity, mdmModuleDbEntityList, "界面新增");
|
||||
|
||||
if (res == null || "".equals(res)) {
|
||||
|
@ -3077,6 +3079,156 @@ public class MdmServiceImpl implements IMdmService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*查询中文名称和英文名称
|
||||
* @param mdmId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<String> selectFieldsByMdmId(String mdmId,String dbId) {
|
||||
//查询数据源表下面的字段
|
||||
MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
|
||||
mdmModuleDbFiledsEntity.setMdmId(mdmId);
|
||||
mdmModuleDbFiledsEntity.setDbId(dbId);
|
||||
mdmModuleDbFiledsEntity.setSts("Y");
|
||||
|
||||
List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
|
||||
if(mdmModuleDbFiledsEntities == null){
|
||||
logger.error("数据源表为空");
|
||||
}
|
||||
List<MdmModuleDbFiledsEntity> dbEntities = new ArrayList<>();
|
||||
if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
|
||||
for (int i = 0; i < mdmModuleDbFiledsEntities.size(); i++) {
|
||||
if (dbId.equals(mdmModuleDbFiledsEntities.get(i).getDbId()) && "1".equals(mdmModuleDbFiledsEntities.get(i).getViewType())) {
|
||||
dbEntities.add(mdmModuleDbFiledsEntities.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> dataTemplate = new ArrayList<>();
|
||||
for (MdmModuleDbFiledsEntity dbEntity : dbEntities) {
|
||||
String data = dbEntity.getChName()+"/"+dbEntity.getEnName();
|
||||
dataTemplate.add(data);
|
||||
}
|
||||
return dataTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据模版
|
||||
* @param file
|
||||
* @param mdmCode
|
||||
* @param dbName
|
||||
*/
|
||||
@Override
|
||||
public void importDataTemplate(MultipartFile file, Long mdmCode, String dbName) {
|
||||
// 校验文件合法性
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new IllegalArgumentException("导入文件不能为空");
|
||||
}
|
||||
// 校验文件格式
|
||||
String fileName = file.getOriginalFilename();
|
||||
if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
|
||||
throw new IllegalArgumentException("请上传Excel格式文件");
|
||||
}
|
||||
// 存储动态表头信息
|
||||
List<String> dynamicHeaders = new ArrayList<>();
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
// 使用EasyExcel读取,不指定实体类,用Map接收动态数据
|
||||
EasyExcel.read(inputStream)
|
||||
.sheet(0) // 读取第一个sheet
|
||||
.headRowNumber(1) // 表头所在行(从1开始)
|
||||
.autoTrim(true) // 自动去除空格
|
||||
.registerReadListener(new AnalysisEventListener<Map<Integer, Object>>() {
|
||||
// 存储表头索引与字段名的映射关系
|
||||
private Map<Integer, String> headerIndexMap;
|
||||
/**
|
||||
* 处理表头信息
|
||||
*/
|
||||
@Override
|
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||
// 保存表头索引与字段名的映射
|
||||
this.headerIndexMap = headMap;
|
||||
// 转换为有序的表头列表
|
||||
if (!headMap.isEmpty()) {
|
||||
int maxIndex = headMap.keySet().stream()
|
||||
.mapToInt(Integer::intValue)
|
||||
.max()
|
||||
.orElse(-1);
|
||||
for (int i = 0; i <= maxIndex; i++) {
|
||||
String headerName = headMap.getOrDefault(i, "").trim();
|
||||
dynamicHeaders.add(headerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 处理每行数据
|
||||
*/
|
||||
@Override
|
||||
public void invoke(Map<Integer, Object> rowData, AnalysisContext context) {
|
||||
int rowNum = context.readRowHolder().getRowIndex() + 1; // 行号从1开始
|
||||
// 将索引映射的行数据转换为表头字段映射的Map
|
||||
Map<String, Object> dataMap = new HashMap<>(16);
|
||||
rowData.forEach((index, value) -> {
|
||||
String name = headerIndexMap.getOrDefault(index, "");
|
||||
String headerName = name.contains("/")
|
||||
? name.substring(name.lastIndexOf("/") + 1).trim()
|
||||
: name.trim(); // 取/之后的字符串作为值
|
||||
String cellValue;
|
||||
if (value == null) {
|
||||
cellValue = null;
|
||||
} else {
|
||||
// 针对不同类型进行特殊处理
|
||||
if (value instanceof Date) {
|
||||
// 日期类型
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
cellValue = sdf.format((Date) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
// 布尔类型
|
||||
cellValue = ((Boolean) value) ? "Y" : "N";
|
||||
} else if (value instanceof Number) {
|
||||
// 数字类型
|
||||
cellValue = new BigDecimal(value.toString()).toPlainString();
|
||||
} else {
|
||||
// 其他类型:直接转换为字符串并去空格
|
||||
cellValue = value.toString().trim();
|
||||
}
|
||||
}
|
||||
dataMap.put(headerName, cellValue);
|
||||
});
|
||||
processDynamicData(dataMap, mdmCode, dbName);
|
||||
}
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
|
||||
}
|
||||
})
|
||||
.doRead();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("文件读取失败:" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理模版数据
|
||||
* @param dataMap
|
||||
* @param mdmCode
|
||||
* @param dbName
|
||||
*/
|
||||
private void processDynamicData(Map<String, Object> dataMap, Long mdmCode, String dbName) {
|
||||
JSONObject dataObject = new JSONObject();
|
||||
dataObject.put(dbName,dataMap);
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("mdmCode",mdmCode);
|
||||
result.put("data",dataObject);
|
||||
JSONObject jsonObject = getStrObj("jsonStr", result);
|
||||
saveMdmShowDetailsData(jsonObject);
|
||||
}
|
||||
private JSONObject getStrObj(String key, Object value) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(key, value);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param jsonObject
|
||||
|
|
Loading…
Reference in New Issue