一、导出模板
1、controller
@GetMapping("/exportRuleModel")
@ApiOperation("模板导出")
public void exportRuleModel(@RequestParam(value = "fileType") Integer fileType,HttpServletResponse response) {
stdImportStdRuleService.exportRuleModel(response,fileType);
}
2、实现:
@Override
public void exportRuleModel(HttpServletResponse response, Integer fileType) {
String fileName = "";
String note = "#注意:导入前检测下数据格式!";
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
try {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
// 防止中文乱码 所以这么写
response.setHeader("Content-disposition", String.format("attachment; filename="%s"", fileName));
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(note.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
log.error("导出模板失败!");
throw new StandardException(ErrorCodeEnum.EXPORT_FAILED, "导出模板失败!");
} finally {
try {
buff.close();
outStr.close();
} catch (Exception e) {
log.error("流关闭失败!");
}
}
}
二、导入数据
1、controller
@PostMapping(value = "/importStdRuleByFile", consumes = "multipart/*", headers = "content-type=multipart/form-data")
@ApiOperation("规则")
public Result importStdRuleByFile(@ApiParam(value = "上传文件", required = true) MultipartFile file,
@RequestParam(name = "versionName") String versionName,
@RequestParam(name = "fileType") Integer fileType) throws Exception {
stdImportStdRuleService.importStdRuleByFile(file,versionName,fileType);
return ResultUtil.success();
}
2、实现:
public void importStdRuleByFile(MultipartFile mFile, String versionName, Integer fileType) throws Exception {
if (mFile == null) {
throw new StandardException(ErrorCodeEnum.NULL_PARAM, "导入的文件为空!");
}
File file = this.loadSql(mFile);
//版本是导入脚本的时间,具体到日
Long version = Long.parseLong(ScUtil.getDataYear(new Date(), Constant.FORMAT_DAY));
stdDataQualityCzbService.importDataQuailtyCzbRule(file, version);
//保存记录表
StdCheckRuleImportRecord record = new StdCheckRuleImportRecord();
record.setCreateTime(new Date());
record.setRemark(versionName);
record.setPubVersion(version);
record.setRuleName(ImportRuleFileTypeEnum.getByValue(fileType).getName());
stdCheckRuleImportRecordMapper.insert(record);
}
public File loadSql(MultipartFile multipartFile) throws Exception {
File file = null;
try {
file = ScUtil.multipartFileToFile(multipartFile);
return file;
} catch (Exception e) {
e.printStackTrace();
throw new StandardException(e.getMessage());
// } finally {
// ScUtil.delteTempFile(file);
}
}
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.isEmpty() || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
// 获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void importDataQuailtyCzbRule(File file,Long version) throws Exception {
log.info(【StdDataQualityCzbServiceImpl】导入开始,当前线程{}", Thread.currentThread().getName());
long timeMillis = System.currentTimeMillis();
StdDataQualityCzb dataQualityCzb = new StdDataQualityCzb();
dataQualityCzb.setIsDeleted(Constant.STD_NOT_DELETED);
//流中获取数据
DataQualityReadByFile readByFile = this.loadSqlByFile(file);
List msgList = readByFile.getMsgList();
List sqlList = readByFile.getSqlList();
List hasDataSqlList = readByFile.getHasDataSqlList();
for (int i = 0; i < msgList.size(); i++) {
//表相关数据
getTableAndFiledMsg(msgList.get(i), dataQualityCzb);
if (StringUtils.isNotEmpty(sqlList.get(i))) {
// sql 相关数据
getSqlMsg(sqlList.get(i), dataQualityCzb);
//保存检测数据为空的第一条sql
dataQualityCzb.setCheckHasDataSql(hasDataSqlList.get(i));
} else {
log.info("拆分sql失败,{}规则对应的sql为空", dataQualityCzb.getCzbCode());
dataQualityCzb.setCheckErrorMsg("截取的SQL为空;");
}
//保存数据
dataQualityCzb.setUpdateTime(new Date());
// 版本是导入脚本的时间,具体到日 Long.parseLong(ScUtil.getDataYear(new Date(), Constant.FORMAT_DAY))
dataQualityCzb.setPubVersion(version);
dataQualityCzb.setPubChangeVersion(Constant.PUB_CHANGE_VERSION);
//获取businessId
StdVersion stdVersion = stdVersionMapper.getCurrentPublicVersion(Constant.STD_TYPE_TECH);
StdTable table = stdTableMapper.selectStdByTableName(dataQualityCzb.getTableName(), stdVersion.getVersion(), Constant.STD_NOT_DELETED);
if (!ObjectUtils.isEmpty(table) && table.getCatalogEnumId() != null) {
dataQualityCzb.setBusinessId(table.getCatalogEnumId());
}
//查询是否已经存在
StdDataQualityCzb dbQuality = stdDataQualityCzbMapper.getQualityByCode(dataQualityCzb.getCzbCode(), dataQualityCzb.getPubVersion());
if (!ObjectUtils.isEmpty(dbQuality)) {
if (!dbQuality.getOriSql().equals(dataQualityCzb.getOriSql())) {
//已经存在,当前版本的数据 只更新原版数据
if(version.equals(dbQuality.getPubVersion())){
stdDataQualityCzbMapper.updateById(dbQuality);
}else {
//修改 changeVersion
dbQuality.setPubChangeVersion(dataQualityCzb.getPubVersion());
stdDataQualityCzbMapper.updateById(dbQuality);
stdDataQualityCzbMapper.insert(dataQualityCzb);
}
}
} else {
stdDataQualityCzbMapper.insert(dataQualityCzb);
}
}
long endMillis = System.currentTimeMillis() - timeMillis;
log.info("导入规则结束,耗时{}", endMillis);
}



