Commit c1714303 by 刘泽志

文档上传

parent 30ada97d
......@@ -3,6 +3,7 @@ package com.tbyf.his.web.controller.dataImport;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
......@@ -38,6 +39,7 @@ import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -404,13 +406,57 @@ public class DataImportController {
return AjaxResult.success();
}
@IgnoreWebSecurity
@GetMapping("/template/download")
@ApiOperation("模板下载")
public void downloadTemplate(HttpServletResponse response,
@RequestParam String excelId) {
final ExcelData excel = excelDataService.getById(excelId);
if (excel != null) {
try {
response.getOutputStream().write(excel.getFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@IgnoreWebSecurity
@GetMapping("/template/download/filename")
@ApiOperation("获取模板文件名")
public AjaxResult getTemplateFileName(@RequestParam String excelId) {
final ExcelData excel = excelDataService.getOne(
Wrappers.lambdaQuery(ExcelData.class)
.select(ExcelData::getFileName)
.eq(ExcelData::getId, excelId)
);
if (excel != null) {
return AjaxResult.success(excel);
} else {
return AjaxResult.error("未查询到指定的模板文件");
}
}
@SneakyThrows
@IgnoreWebSecurity
@PostMapping("/upload")
@ApiOperation("文件导入")
public AjaxResult bindRule(UploadExcelParam param) {
if (StringUtils.equals(param.getType(), "1")) {
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getTemplateId, param.getTemplateId())
.eq(ExcelData::getType, "1"), false);
if (excelData == null) {
excelData = new ExcelData();
excelData.setType("1");
excelData.setTemplateId(param.getTemplateId());
excelData.setYear(param.getYear());
excelData.setOrgName(param.getOrgName());
}
excelData.setFile(param.getFile().getBytes());
excelData.setFileName(param.getFile().getOriginalFilename());
excelDataService.saveOrUpdate(excelData);
} else if (StringUtils.equals(param.getType(), "2")) {
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getTemplateId, param.getTemplateId())
......@@ -477,5 +523,114 @@ public class DataImportController {
return AjaxResult.success();
}
@IgnoreWebSecurity
@GetMapping("/table/create")
@ApiOperation("物理表生成")
public AjaxResult createTable(@RequestParam String tableName,
@RequestParam String templateId) {
// 默认表名全大写
tableName = tableName.toUpperCase();
final DataImportTemplate template = dataImportService.getById(templateId);
if (StringUtils.isBlank(template.getDataSourceId())) {
return AjaxResult.error("请在模板编辑界面选择对应的数据源");
}
final List<DataField> fieldList = dataFieldService.list(Wrappers.lambdaQuery(DataField.class)
.isNotNull(DataField::getField)
.eq(DataField::getTemplateId, template.getId())
.orderByAsc(DataField::getSort));
if (CollectionUtils.isEmpty(fieldList)) {
return AjaxResult.error("没有可生成表的字段配置,请新建字段");
}
try {
final DbType dbType = dataImportService.getDbType(template.getDataSourceId());
DataSourceUtil.switchDs(template.getDataSourceId());
if (dbType.equals(DbType.MYSQL)) {
// mysql
final Integer count = jdbcTemplate.queryForObject(StrFormatter.format("select count(table_name) from information_schema.TABLES where TABLE_NAME = '{}'", tableName), Integer.class);
if (count > 0) {
return AjaxResult.error("此表已存在");
}
StringBuilder sb = new StringBuilder();
sb.append("create table {} ( ");
fieldList.forEach(field -> sb.append(field.getField())
.append(" varchar(32) null comment '")
.append(field.createComment()).append("',"));
sb.deleteCharAt(sb.length() - 1)
.append(" ) comment '")
.append(template.createComment())
.append("';");
// 同时创建数据表与临时表
log.info("生成物理表的sql为:{}", sb);
jdbcTemplate.execute(StrFormatter.format(sb.toString(), tableName));
jdbcTemplate.execute(StrFormatter.format(sb.toString(), tableName + "_TEMP"));
// 修改模板表绑定的表名
final DataImportTemplate updateTemplate = new DataImportTemplate();
updateTemplate.setId(template.getId());
updateTemplate.setTableName(tableName);
DataSourceUtil.switchDefaultDs();
dataImportService.updateById(updateTemplate);
} else {
// oracle
final Integer count = jdbcTemplate.queryForObject(StrFormatter.format("select count(*) from user_tables where table_name =upper('{}')", tableName), Integer.class);
if (count > 0) {
return AjaxResult.error("此表已存在");
}
List<String> prodSqlList = new ArrayList<>();
List<String> tempSqlList = new ArrayList<>();
final String tempTableName = tableName + "_TEMP";
String comment = "COMMENT ON COLUMN {}.{} IS '{}'";
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE {} ( ");
String finalTableName = tableName;
fieldList.forEach(field -> {
sql.append(field.getField())
.append(" VARCHAR2(255),");
final String comm = field.createComment();
prodSqlList.add(StrFormatter.format(comment, finalTableName, field.getField(), comm));
tempSqlList.add(StrFormatter.format(comment, tempTableName, field.getField(), comm));
});
sql.deleteCharAt(sql.length() - 1)
.append(" )");
prodSqlList.add(0, StrFormatter.format(sql.toString(), finalTableName));
tempSqlList.add(0, StrFormatter.format(sql.toString(), tempTableName));
prodSqlList.add(StrFormatter.format("COMMENT ON TABLE {} IS '{}'", finalTableName, template.createComment()));
tempSqlList.add(StrFormatter.format("COMMENT ON TABLE {} IS '{}'", tempTableName, template.createComment()));
// 同时创建数据表与临时表
String[] prod = new String[prodSqlList.size()];
String[] temp = new String[tempSqlList.size()];
prodSqlList.toArray(prod);
tempSqlList.toArray(temp);
/*prodSqlList.forEach(sql1->{
jdbcTemplate.execute(sql1);
});
tempSqlList.forEach(sql2->{
jdbcTemplate.execute(sql2);
});*/
jdbcTemplate.batchUpdate(prod);
jdbcTemplate.batchUpdate(temp);
// 修改模板表绑定的表名
final DataImportTemplate updateTemplate = new DataImportTemplate();
updateTemplate.setId(template.getId());
updateTemplate.setTableName(finalTableName);
DataSourceUtil.switchDefaultDs();
dataImportService.updateById(updateTemplate);
}
return AjaxResult.success();
} catch (Exception e) {
log.error("物理表生成失败", e);
return AjaxResult.error(e.getMessage());
} finally {
DataSourceUtil.switchDefaultDs();
}
}
@IgnoreWebSecurity
@GetMapping("/analyze/export")
@ApiOperation("数据分析并导出")
public AjaxResult createTable(@RequestParam String templateId,
HttpServletResponse response) {
return AjaxResult.success();
}
}
......@@ -23,7 +23,7 @@ public class UploadExcelParam implements Serializable {
@ApiModelProperty(value = "文件类型")
private String type;
@ApiModelProperty(value = "规则ID集合")
@ApiModelProperty(value = "文件数据 1-数据文件 2-标准模板文件")
private MultipartFile file;
@ApiModelProperty(value = "模板ID")
......
......@@ -17,6 +17,8 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import java.io.Serializable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author lzz
......@@ -64,12 +66,14 @@ public class DataField implements Serializable {
@SneakyThrows
public void createField() {
if (StringUtils.equals(title, "统一社会信用代码")) {
if (StringUtils.equalsAny(title, "统一社会信用代码", "统一社会信用代码:", "统一社会信用代码:")) {
field = "UNIFIED_CODE";
} else if (StringUtils.equals(title, "组织机构代码")) {
} else if (StringUtils.equalsAny(title, "组织机构代码", "组织机构代码:", "组织机构代码:")) {
field = "ORG_CODE";
} else if (StringUtils.equals(title, "机构名称")) {
} else if (StringUtils.equalsAny(title, "机构名称", "机构名称:", "机构名称:")) {
field = "ORG_NAME";
} else if (StringUtils.equalsAny(title, "行政区划代码", "行政区划代码:", "行政区划代码:")) {
field = "REGION_CODE";
} else {
final HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
......@@ -85,4 +89,10 @@ public class DataField implements Serializable {
}
}
public String createComment() {
return Stream.of(code, title)
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining(" - "));
}
}
......@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.web.dataImport.domain.BaseMp;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......@@ -14,6 +15,8 @@ import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author lzz
......@@ -61,4 +64,10 @@ public class DataImportTemplate extends BaseMp implements Serializable {
@TableField("table_name")
private String tableName;
public String createComment() {
return Stream.of(name, orgName, year)
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining(" - "));
}
}
......@@ -21,4 +21,12 @@ public interface DataImportTemplateMapper extends BaseMapper<DataImportTemplate>
* @return
*/
List<TemplateVO> queryTemplate(QueryTemplateParam param);
/**
* 查询数据源类型
*
* @param dataSourceId
* @return
*/
String queryDbType(String dataSourceId);
}
package com.tbyf.his.web.dataImport.service;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
......@@ -22,4 +23,6 @@ public interface DataImportTemplateService extends IService<DataImportTemplate>
*/
List<TemplateVO> queryTemplate(QueryTemplateParam param);
DbType getDbType(String dataSourceId);
}
package com.tbyf.his.web.dataImport.service.impl;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbyf.his.common.annotation.DataSource;
import com.tbyf.his.common.enums.DataSourceType;
import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
......@@ -30,4 +32,17 @@ public class DataImportTemplateServiceImpl extends ServiceImpl<DataImportTemplat
public List<TemplateVO> queryTemplate(QueryTemplateParam param) {
return dataImportTemplateMapper.queryTemplate(param);
}
@Override
public DbType getDbType(String dataSourceId) {
final String dbType = dataImportTemplateMapper.queryDbType(dataSourceId);
if (StringUtils.isBlank(dbType)) {
return DbType.MYSQL;
}
if (dbType.toUpperCase().contains("MYSQL")) {
return DbType.MYSQL;
} else {
return DbType.ORACLE;
}
}
}
......@@ -23,4 +23,10 @@
</if>
</where>
</select>
<select id="queryDbType" resultType="java.lang.String">
SELECT driver_class FROM sys_datasource
where datasource_name = #{dataSourceId}
limit 1
</select>
</mapper>
......@@ -62,6 +62,6 @@ create table excel_data
type varchar(64) null comment '文件类型 1-初始导入文件 2-标准模板文件',
template_id varchar(32) null comment '模板ID',
year varchar(32) null comment '数据年份',
org_name varchar(64) null comment '机构类型',
org_name varchar(64) null comment '机构类型'
) comment 'excel存储表';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment