Commit cc998321 by 刘泽志

数据导入全部完成

parent c1714303
...@@ -11,6 +11,7 @@ import com.tbyf.his.common.annotation.IgnoreWebSecurity; ...@@ -11,6 +11,7 @@ import com.tbyf.his.common.annotation.IgnoreWebSecurity;
import com.tbyf.his.common.core.domain.AjaxResult; import com.tbyf.his.common.core.domain.AjaxResult;
import com.tbyf.his.common.core.page.TableDataInfo; import com.tbyf.his.common.core.page.TableDataInfo;
import com.tbyf.his.common.core.text.StrFormatter; import com.tbyf.his.common.core.text.StrFormatter;
import com.tbyf.his.common.exception.base.BaseException;
import com.tbyf.his.common.utils.StringUtils; import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.common.utils.bean.BeanUtils; import com.tbyf.his.common.utils.bean.BeanUtils;
import com.tbyf.his.framework.datasource.DataSourceUtil; import com.tbyf.his.framework.datasource.DataSourceUtil;
...@@ -21,6 +22,7 @@ import com.tbyf.his.web.dataImport.core.DiConfig; ...@@ -21,6 +22,7 @@ import com.tbyf.his.web.dataImport.core.DiConfig;
import com.tbyf.his.web.dataImport.core.RuleVO; import com.tbyf.his.web.dataImport.core.RuleVO;
import com.tbyf.his.web.dataImport.core.RuleValidator; import com.tbyf.his.web.dataImport.core.RuleValidator;
import com.tbyf.his.web.dataImport.domain.param.*; import com.tbyf.his.web.dataImport.domain.param.*;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
import com.tbyf.his.web.dataImport.entity.*; import com.tbyf.his.web.dataImport.entity.*;
import com.tbyf.his.web.dataImport.service.*; import com.tbyf.his.web.dataImport.service.*;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -43,10 +45,8 @@ import javax.servlet.http.HttpServletResponse; ...@@ -43,10 +45,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.io.OutputStream;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
...@@ -94,6 +94,28 @@ public class DataImportController { ...@@ -94,6 +94,28 @@ public class DataImportController {
} }
@IgnoreWebSecurity @IgnoreWebSecurity
@GetMapping("/template/allField")
@ApiOperation("查询模板的所有字段数据")
public AjaxResult getAllTemplateField(@RequestParam String templateId) {
return AjaxResult.success(dataFieldService.list(Wrappers.lambdaQuery(DataField.class)
.eq(DataField::getTemplateId, templateId)));
}
@IgnoreWebSecurity
@GetMapping("/template/allRule")
@ApiOperation("查询模板的所有规则数据")
public AjaxResult getAllTemplateRule(@RequestParam String templateId) {
final List<BindRule> bindRules = bindRuleService.list(Wrappers.lambdaQuery(BindRule.class)
.eq(BindRule::getDataId, templateId));
if (CollectionUtils.isEmpty(bindRules)) {
return AjaxResult.success(Collections.emptyList());
}
return AjaxResult.success(dataRuleService.list(Wrappers.lambdaQuery(DataRule.class)
.eq(DataRule::getType, "模板规则")
.in(DataRule::getId, bindRules.stream().map(BindRule::getRuleId).collect(Collectors.toList()))));
}
@IgnoreWebSecurity
@PostMapping("/template") @PostMapping("/template")
@ApiOperation("模板新增") @ApiOperation("模板新增")
public AjaxResult addTemplate(@RequestBody @Validated AddTemplateParam param) { public AjaxResult addTemplate(@RequestBody @Validated AddTemplateParam param) {
...@@ -413,8 +435,9 @@ public class DataImportController { ...@@ -413,8 +435,9 @@ public class DataImportController {
@RequestParam String excelId) { @RequestParam String excelId) {
final ExcelData excel = excelDataService.getById(excelId); final ExcelData excel = excelDataService.getById(excelId);
if (excel != null) { if (excel != null) {
try { try (OutputStream os = response.getOutputStream()) {
response.getOutputStream().write(excel.getFile()); os.write(excel.getFile());
os.flush();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -497,9 +520,9 @@ public class DataImportController { ...@@ -497,9 +520,9 @@ public class DataImportController {
List<DataField> fieldList = new ArrayList<>(); List<DataField> fieldList = new ArrayList<>();
for (int i = 1; i < rows; i++) { for (int i = 1; i < rows; i++) {
final Row row = sheet.getRow(i); final Row row = sheet.getRow(i);
String code = DiConfig.getValue(row.getCell(1)); String code = DiConfig.getValue(row.getCell(1, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL));
String title = DiConfig.getValue(row.getCell(2)); String title = DiConfig.getValue(row.getCell(2, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL));
String unit = DiConfig.getValue(row.getCell(4)); String unit = DiConfig.getValue(row.getCell(4, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL));
if (StringUtils.isAllBlank(code, title, unit)) { if (StringUtils.isAllBlank(code, title, unit)) {
continue; continue;
} }
...@@ -511,7 +534,7 @@ public class DataImportController { ...@@ -511,7 +534,7 @@ public class DataImportController {
dataField.setCode(code); dataField.setCode(code);
dataField.setTitle(title); dataField.setTitle(title);
dataField.setUnit(unit); dataField.setUnit(unit);
dataField.setCoordinate("F," + (i + 1)); dataField.setCoordinate("F," + i);
dataField.setSort(i + 1); dataField.setSort(i + 1);
dataField.createField(); dataField.createField();
fieldList.add(dataField); fieldList.add(dataField);
...@@ -627,10 +650,99 @@ public class DataImportController { ...@@ -627,10 +650,99 @@ public class DataImportController {
@IgnoreWebSecurity @IgnoreWebSecurity
@GetMapping("/analyze/export") @GetMapping("/analyze/export")
@ApiOperation("数据分析并导出") @ApiOperation("数据分析并导出")
public AjaxResult createTable(@RequestParam String templateId, public void analyzeExport(@RequestParam String templateId,
HttpServletResponse response) { HttpServletResponse response) {
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getTemplateId, templateId)
.eq(ExcelData::getType, "1"), false);
if (excelData == null) {
try {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().println(JSON.toJSONString(AjaxResult.error("请先导入数据文件")));
response.getWriter().flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return;
}
excelDataService.analyzeExport(excelData, response);
}
@IgnoreWebSecurity
@PostMapping("/clearTemp")
@ApiOperation("清空临时表数据")
public AjaxResult clearTemp(@RequestBody TemplateVO vo) {
final DataImportTemplate template = dataImportService.getById(vo.getId());
try {
DataSourceUtil.switchDs(template.getDataSourceId());
String sql = "DELETE FROM " + template.getTableName() + "_TEMP";
//sql = sql + " WHERE YEAR = '"+template.getYear()+"'";
log.info("执行的sql为:[{}]", sql);
jdbcTemplate.execute(sql);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
DataSourceUtil.switchDefaultDs();
}
excelDataService.remove(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getType, "1")
.eq(ExcelData::getTemplateId, template.getId()));
return AjaxResult.success();
}
@IgnoreWebSecurity
@PostMapping("/syncDb")
@ApiOperation("一键导入正式库")
public AjaxResult syncDb(@RequestBody TemplateVO vo) {
final DataImportTemplate template = dataImportService.getById(vo.getId());
final List<DataField> fieldList = dataFieldService.list(Wrappers.lambdaQuery(DataField.class)
.eq(DataField::getTemplateId, template.getId())
.isNotNull(DataField::getField)
.select(DataField::getField));
final String fieldSql = fieldList.stream().map(DataField::getField).collect(Collectors.joining(","));
try {
DataSourceUtil.switchDs(template.getDataSourceId());
// 先查询出来所有的数据
String selectSql = "SELECT " + fieldSql + " FROM " + template.getTableName() + "_TEMP";
log.info("查询sql为:[{}]", selectSql);
final List<Map<String, Object>> dataList = jdbcTemplate.queryForList(selectSql);
if (CollectionUtils.isEmpty(dataList)) {
return AjaxResult.error("临时表数据为空");
}
String[] sqlArr = new String[dataList.size()];
for (int i = 0; i < dataList.size(); i++) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ").append(template.getTableName()).append("(").append(fieldSql).append(") VALUES(");
final Map<String, Object> objectMap = dataList.get(i);
for (DataField field : fieldList) {
final Object o = objectMap.get(field.getField());
if (o == null) {
sb.append("'',");
} else {
sb.append("'").append(o).append("',");
}
}
sb.deleteCharAt(sb.length() - 1);
sb.append(")");
log.info("插入数据sql为:[{}]", sb);
sqlArr[i] = sb.toString();
}
jdbcTemplate.batchUpdate(sqlArr);
// 导入完毕直接清空临时表
jdbcTemplate.execute("DELETE FROM " + template.getTableName() + "_TEMP");
} catch (Exception e) {
throw new BaseException(e.getMessage());
} finally {
DataSourceUtil.switchDefaultDs();
}
DataImportTemplate updateTemplate = new DataImportTemplate();
updateTemplate.setId(template.getId());
updateTemplate.setImportStatus("1");
updateTemplate.setImportTime(new Date());
dataImportService.updateById(updateTemplate);
return AjaxResult.success(); return AjaxResult.success();
} }
} }
package com.tbyf.his.web.dataImport.core; package com.tbyf.his.web.dataImport.core;
import com.tbyf.his.common.utils.StringUtils; import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.NumberToTextConverter; import org.apache.poi.ss.util.NumberToTextConverter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -13,9 +11,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -13,9 +11,7 @@ import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -32,9 +28,53 @@ public class DiConfig { ...@@ -32,9 +28,53 @@ public class DiConfig {
public static final Map<String, RuleValidator> VALIDATOR_MAP = new ConcurrentHashMap<>(8); public static final Map<String, RuleValidator> VALIDATOR_MAP = new ConcurrentHashMap<>(8);
public static final List<String> WORD = Arrays.asList(
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" /**
); * 获取对应列的数据起始位置
*
* @param vo
* @param sheet
* @return
*/
public static String getCoordinate(VerifyVO vo, Sheet sheet) {
final int rows = sheet.getLastRowNum() + 1;
int start = StringUtils.isBlank(vo.getCode()) ? 1 : 0;
for (int i = 0; i < rows; i++) {
final Row row = sheet.getRow(i);
int flag = start;
for (Cell cell : row) {
if (cell.getColumnIndex() == 0) {
break;
}
if (flag == 0 && StringUtils.equals(getValue(cell), vo.getCode())) {
flag++;
} else if (flag == 1 && StringUtils.equals(getValue(cell), vo.getTitle())) {
// 代表code与title都能对应上
return getWord(cell.getColumnIndex() + 1) + "," + cell.getRowIndex();
}
}
}
return null;
}
/**
* 获取数据长度
*
* @param vo
* @param sheet
* @return
*/
public static int getLength(VerifyVO vo, Sheet sheet) {
final String[] coords = vo.getCoordinate().split(",");
int start = DiConfig.getIndex(coords[0]);
Row row = sheet.getRow(Integer.parseInt(coords[1]));
for (int i = start; ; i++) {
final Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
if (StringUtils.isBlank(getValue(cell))) {
return i - start;
}
}
}
@PostConstruct @PostConstruct
public void init() { public void init() {
...@@ -52,17 +92,44 @@ public class DiConfig { ...@@ -52,17 +92,44 @@ public class DiConfig {
return VALIDATOR_MAP.get(DiConstants.SERVICE_PREFIX + mode); return VALIDATOR_MAP.get(DiConstants.SERVICE_PREFIX + mode);
} }
/**
* 将excel表格中列号字母转成列索引,从0对应A开始
*
* @param word
* @return
*/
public static int getIndex(String word) { public static int getIndex(String word) {
for (int i = 0; i < WORD.size(); i++) { if (!word.matches("[A-Z]+")) {
if (StringUtils.equals(word, WORD.get(i))) { return -1;
return i;
}
} }
return -1; int index = 0;
char[] chars = word.toUpperCase().toCharArray();
for (int i = 0; i < chars.length; i++) {
index += ((int) chars[i] - (int) 'A' + 1)
* (int) Math.pow(26, chars.length - i - 1);
}
return index - 1;
} }
/**
* 将excel表格中列索引转成列号字母,从A对应0开始
*
* @param index
* @return
*/
public static String getWord(int index) { public static String getWord(int index) {
return WORD.get(index); if (index <= 0) {
return "A";
}
String column = "";
do {
if (column.length() > 0) {
index--;
}
column = ((char) (index % 26 + (int) 'A')) + column;
index = (index - index % 26) / 26;
} while (index > 0);
return column;
} }
/** /**
......
package com.tbyf.his.web.dataImport.domain.vo;
import com.tbyf.his.common.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* excel模型
*
* @author lzz
* @date 2023/2/12 21:20
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class ExcelVO {
@Excel(name = "机构类型")
private String orgType;
@Excel(name = "医疗机构名称", width = 20)
private String orgName;
@Excel(name = "医疗机构代码")
private String orgCode;
@Excel(name = "统一社会信用代码")
private String unifiedCode;
@Excel(name = "数据代码")
private String code;
@Excel(name = "指标名称", width = 24)
private String title;
@Excel(name = "信息/数值")
private String value;
@Excel(name = "错误原因", width = 30)
private String message;
public ExcelVO(String message, String value) {
this.message = message;
this.value = value;
}
}
package com.tbyf.his.web.dataImport.domain.vo;
import com.tbyf.his.web.dataImport.core.RuleVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* 校验模型
*
* @author lzz
* @date 2023/2/12 21:20
*/
@Data
@Accessors(chain = true)
public class VerifyVO {
@ApiModelProperty(value = "字段ID")
private String fieldId;
@ApiModelProperty(value = "代码")
private String code;
@ApiModelProperty(value = "指标名称")
private String title;
@ApiModelProperty(value = "计量单位")
private String unit;
@ApiModelProperty(value = "数据起始坐标,例如 C,4")
private String coordinate;
@ApiModelProperty(value = "排序字段")
private Integer sort;
@ApiModelProperty(value = "数据库字段名")
private String field;
@ApiModelProperty(value = "待校验的值列表")
private List<String> values;
@ApiModelProperty(value = "校验规则列表")
private List<RuleVO> rules;
@ApiModelProperty(value = "错误结果列表")
private List<ExcelVO> errors;
public VerifyVO addValue(String value) {
if (CollectionUtils.isEmpty(values)) {
values = new ArrayList<>();
}
values.add(value);
return this;
}
public VerifyVO addError(ExcelVO error) {
if (CollectionUtils.isEmpty(errors)) {
errors = new ArrayList<>();
}
errors.add(error);
return this;
}
}
...@@ -31,7 +31,7 @@ public class DataRule implements Serializable { ...@@ -31,7 +31,7 @@ public class DataRule implements Serializable {
@TableField("name") @TableField("name")
private String name; private String name;
@ApiModelProperty(value = "规则类型 1-基础规则 2-组合规则 3-字段复合规则") @ApiModelProperty(value = "规则类型 1-基础规则 2-组合规则 3-模板规则")
@TableField("type") @TableField("type")
private String type; private String type;
......
...@@ -3,6 +3,7 @@ package com.tbyf.his.web.dataImport.mapper; ...@@ -3,6 +3,7 @@ package com.tbyf.his.web.dataImport.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam; import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO; import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate; import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import java.util.List; import java.util.List;
...@@ -29,4 +30,12 @@ public interface DataImportTemplateMapper extends BaseMapper<DataImportTemplate> ...@@ -29,4 +30,12 @@ public interface DataImportTemplateMapper extends BaseMapper<DataImportTemplate>
* @return * @return
*/ */
String queryDbType(String dataSourceId); String queryDbType(String dataSourceId);
/**
* 获取综合校验对象
*
* @param id
* @return
*/
List<VerifyVO> getVerify(String id);
} }
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.DbType; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam; import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO; import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate; import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import java.util.List; import java.util.List;
...@@ -25,4 +26,11 @@ public interface DataImportTemplateService extends IService<DataImportTemplate> ...@@ -25,4 +26,11 @@ public interface DataImportTemplateService extends IService<DataImportTemplate>
DbType getDbType(String dataSourceId); DbType getDbType(String dataSourceId);
/**
* 获取综合校验对象
*
* @param id
* @return
*/
List<VerifyVO> getVerify(String id);
} }
...@@ -3,10 +3,21 @@ package com.tbyf.his.web.dataImport.service; ...@@ -3,10 +3,21 @@ package com.tbyf.his.web.dataImport.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.tbyf.his.web.dataImport.entity.ExcelData; import com.tbyf.his.web.dataImport.entity.ExcelData;
import javax.servlet.http.HttpServletResponse;
/** /**
* @author lzz * @author lzz
* @date 2023/2/7 11:23 * @date 2023/2/7 11:23
*/ */
public interface ExcelDataService extends IService<ExcelData> { public interface ExcelDataService extends IService<ExcelData> {
/**
* 数据分析并导出
*
* @param excelData
* @param response
*/
void analyzeExport(ExcelData excelData, HttpServletResponse response);
} }
...@@ -7,6 +7,7 @@ import com.tbyf.his.common.enums.DataSourceType; ...@@ -7,6 +7,7 @@ import com.tbyf.his.common.enums.DataSourceType;
import com.tbyf.his.common.utils.StringUtils; import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam; import com.tbyf.his.web.dataImport.domain.param.QueryTemplateParam;
import com.tbyf.his.web.dataImport.domain.vo.TemplateVO; import com.tbyf.his.web.dataImport.domain.vo.TemplateVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate; import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import com.tbyf.his.web.dataImport.mapper.DataImportTemplateMapper; import com.tbyf.his.web.dataImport.mapper.DataImportTemplateMapper;
import com.tbyf.his.web.dataImport.service.DataImportTemplateService; import com.tbyf.his.web.dataImport.service.DataImportTemplateService;
...@@ -45,4 +46,9 @@ public class DataImportTemplateServiceImpl extends ServiceImpl<DataImportTemplat ...@@ -45,4 +46,9 @@ public class DataImportTemplateServiceImpl extends ServiceImpl<DataImportTemplat
return DbType.ORACLE; return DbType.ORACLE;
} }
} }
@Override
public List<VerifyVO> getVerify(String id) {
return dataImportTemplateMapper.getVerify(id);
}
} }
...@@ -2,12 +2,37 @@ package com.tbyf.his.web.dataImport.service.impl; ...@@ -2,12 +2,37 @@ package com.tbyf.his.web.dataImport.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbyf.his.common.annotation.DataSource; import com.tbyf.his.common.annotation.DataSource;
import com.tbyf.his.common.core.text.StrFormatter;
import com.tbyf.his.common.enums.DataSourceType; import com.tbyf.his.common.enums.DataSourceType;
import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.common.utils.poi.ExcelUtil;
import com.tbyf.his.framework.datasource.DataSourceUtil;
import com.tbyf.his.web.dataImport.core.DiConfig;
import com.tbyf.his.web.dataImport.core.RuleVO;
import com.tbyf.his.web.dataImport.core.RuleValidator;
import com.tbyf.his.web.dataImport.domain.vo.ExcelVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import com.tbyf.his.web.dataImport.entity.ExcelData; import com.tbyf.his.web.dataImport.entity.ExcelData;
import com.tbyf.his.web.dataImport.mapper.ExcelDataMapper; import com.tbyf.his.web.dataImport.mapper.ExcelDataMapper;
import com.tbyf.his.web.dataImport.service.DataImportTemplateService;
import com.tbyf.his.web.dataImport.service.ExcelDataService; import com.tbyf.his.web.dataImport.service.ExcelDataService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* @author lzz * @author lzz
...@@ -17,4 +42,133 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +42,133 @@ import org.springframework.stereotype.Service;
@Service @Service
@DataSource(DataSourceType.MASTER) @DataSource(DataSourceType.MASTER)
public class ExcelDataServiceImpl extends ServiceImpl<ExcelDataMapper, ExcelData> implements ExcelDataService { public class ExcelDataServiceImpl extends ServiceImpl<ExcelDataMapper, ExcelData> implements ExcelDataService {
@Autowired
private DataImportTemplateService dataImportTemplateService;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void analyzeExport(ExcelData excelData, HttpServletResponse response) {
final DataImportTemplate template = dataImportTemplateService.getById(excelData.getTemplateId());
List<VerifyVO> verifyList = dataImportTemplateService.getVerify(template.getId());
try (InputStream is = new ByteArrayInputStream(excelData.getFile()); Workbook workbook = WorkbookFactory.create(is)) {
final Sheet sheet = workbook.getSheetAt(0);
//final int rows = sheet.getLastRowNum() + 1;
final Optional<VerifyVO> first = verifyList.stream()
.filter(item -> StringUtils.isNotBlank(item.getCoordinate()))
.findFirst();
// 数据长度/条数
final int length = DiConfig.getLength(first.get(), sheet);
// 第一步解析数据到values
for (int a = 0; a < verifyList.size(); a++) {
final VerifyVO vo = verifyList.get(a);
if (StringUtils.isBlank(vo.getCoordinate())) {
final String s = DiConfig.getCoordinate(vo, sheet);
if (StringUtils.isBlank(s)) {
vo.addError(new ExcelVO("没有符合代码与指标名称的字段行", ""));
continue;
}
vo.setCoordinate(s);
}
final String[] coords = vo.getCoordinate().split(",");
int columnStart = DiConfig.getIndex(coords[0]);
int rowStart = Integer.parseInt(coords[1]);
final Row row = sheet.getRow(rowStart);
for (int b = columnStart; b < columnStart + length; b++) {
try {
final Cell cell = row.getCell(b, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
vo.addValue(DiConfig.getValue(cell));
} catch (Exception ignore) {
}
}
}
// 获取每一步的核心机构等信息
ExcelVO[] excels = new ExcelVO[length];
for (int e = 0; e < length; e++) {
excels[e] = new ExcelVO();
excels[e].setOrgType(template.getOrgName());
for (VerifyVO v : verifyList) {
if (StringUtils.equals("ORG_NAME", v.getField())) {
excels[e].setOrgName(v.getValues().get(e));
continue;
}
if (StringUtils.equals("ORG_CODE", v.getField())) {
excels[e].setOrgCode(v.getValues().get(e));
continue;
}
if (StringUtils.equals("UNIFIED_CODE", v.getField())) {
excels[e].setUnifiedCode(v.getValues().get(e));
continue;
}
}
}
// 第二步进行数据校验
verifyList.stream()
.filter(item -> !CollectionUtils.isEmpty(item.getValues()) && !CollectionUtils.isEmpty(item.getRules()))
.forEach(item -> {
for (int i = 0; i < item.getValues().size(); i++) {
for (int j = 0; j < item.getRules().size(); j++) {
final RuleVO vo = item.getRules().get(j);
vo.setValue(item.getValues().get(i));
// 置空错误信息
vo.setResult(null);
final RuleValidator validator = DiConfig.getValidator(vo.getMode());
validator.validate(vo);
if (StringUtils.isNotBlank(vo.getResult())) {
ExcelVO excelVO = new ExcelVO();
BeanUtils.copyProperties(excels[i], excelVO);
excelVO.setCode(item.getCode())
.setTitle(item.getTitle())
.setValue(item.getValues().get(i))
.setMessage(vo.getResult());
item.addError(excelVO);
}
}
}
});
// 第三步导入临时表
String sql = "INSERT INTO {}(" + verifyList.stream()
.map(VerifyVO::getField).collect(Collectors.joining(","))
+ ") VALUES ({})";
String[] sqlArr = new String[length];
for (int i = 0; i < length; i++) {
StringBuilder sb = new StringBuilder();
for (VerifyVO v : verifyList) {
sb.append("'").append(v.getValues().get(i)).append("',");
}
sb.deleteCharAt(sb.length() - 1);
sqlArr[i] = StrFormatter.format(sql, template.getTableName() + "_TEMP", sb);
}
try {
DataSourceUtil.switchDs(template.getDataSourceId());
// 需要先清空临时表的数据
jdbcTemplate.execute("DELETE FROM " + template.getTableName() + "_TEMP");
jdbcTemplate.batchUpdate(sqlArr);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
DataSourceUtil.switchDefaultDs();
}
// 第四步导出异常excel
List<ExcelVO> list = new ArrayList<>();
verifyList.forEach(item -> {
if (!CollectionUtils.isEmpty(item.getErrors())) {
list.addAll(item.getErrors());
}
});
ExcelUtil<ExcelVO> util = new ExcelUtil<>(ExcelVO.class);
util.exportExcel(response, list, "异常数据分析");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String resetCoordinate(String coordinate, int index) {
final String[] coords = coordinate.split(",");
int columnStart = DiConfig.getIndex(coords[0]);
return DiConfig.getWord(columnStart + index) + "," + coords[1];
}
} }
...@@ -7,15 +7,9 @@ ...@@ -7,15 +7,9 @@
SELECT id, SELECT id,
name, name,
org_name as orgName, org_name as orgName,
import_status as importStatus, import_status as importStatus, year, import_time as importTime, datasource_id as dataSourceId, table_name as
year, tableName, create_time as createTime, update_time as updateTime, remarks, (select id from excel_data where
import_time as importTime, template_id = data_import_template.id and type = '2' limit 1) as excelId
datasource_id as dataSourceId,
table_name as tableName,
create_time as createTime,
update_time as updateTime,
remarks,
(select id from excel_data where template_id = data_import_template.id and type = '2' limit 1) as excelId
FROM data_import_template FROM data_import_template
<where> <where>
<if test="year != null and year != ''"> <if test="year != null and year != ''">
...@@ -25,8 +19,46 @@ ...@@ -25,8 +19,46 @@
</select> </select>
<select id="queryDbType" resultType="java.lang.String"> <select id="queryDbType" resultType="java.lang.String">
SELECT driver_class FROM sys_datasource SELECT driver_class
where datasource_name = #{dataSourceId} FROM sys_datasource
limit 1 where datasource_name = #{dataSourceId} limit 1
</select>
<resultMap id="verifyVo" type="com.tbyf.his.web.dataImport.domain.vo.VerifyVO">
<id property="fieldId" column="fieldId"/>
<result property="code" column="code"/>
<result property="title" column="title"/>
<result property="unit" column="unit"/>
<result property="coordinate" column="coordinate"/>
<result property="sort" column="sort"/>
<result property="field" column="field"/>
<collection property="rules" ofType="com.tbyf.his.web.dataImport.core.RuleVO">
<id property="id" column="ruleId"/>
<result property="name" column="name"/>
<result property="type" column="type"/>
<result property="mode" column="mode"/>
<result property="content" column="content"/>
</collection>
</resultMap>
<select id="getVerify" resultMap="verifyVo">
select df.id as fieldId,
df.code as code,
df.title as title,
df.unit as unit,
df.coordinate as coordinate,
df.sort as sort,
df.field as field,
dr.id as ruleId,
dr.name as name,
dr.type as type,
dr.mode as mode,
dr.content as content
from data_field df
left join bind_rule br on df.id = br.data_id
left join data_rule dr on br.rule_id = dr.id
where df.template_id = #{id}
and df.field is not null
order by df.sort
</select> </select>
</mapper> </mapper>
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