Commit 6488c0dc by 刘泽志

最终版

parent 2ef99ec2
package com.tbyf.his.web.controller.dataImport;
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;
......@@ -9,11 +10,13 @@ import com.tbyf.his.common.core.page.TableDataInfo;
import com.tbyf.his.common.core.text.StrFormatter;
import com.tbyf.his.common.utils.StringUtils;
import com.tbyf.his.common.utils.bean.BeanUtils;
import com.tbyf.his.web.dataImport.DataImportUtils;
import com.tbyf.his.web.dataImport.core.DiConfig;
import com.tbyf.his.web.dataImport.domain.param.AddFieldParam;
import com.tbyf.his.web.dataImport.domain.param.QueryFieldParam;
import com.tbyf.his.web.dataImport.domain.param.QueryMetaFieldParam;
import com.tbyf.his.web.dataImport.domain.param.UpdateFieldParam;
import com.tbyf.his.web.dataImport.domain.vo.CreateFieldVO;
import com.tbyf.his.web.dataImport.entity.*;
import com.tbyf.his.web.dataImport.service.*;
import io.swagger.annotations.Api;
......@@ -208,6 +211,54 @@ public class DataFieldController {
return AjaxResult.success(metaFieldService.list());
}
@IgnoreWebSecurity
@GetMapping("/querySyncField")
@ApiOperation("字段同步查询")
public AjaxResult querySyncField(@RequestParam String templateId) {
final DataTemplate template = dataTemplateService.getById(templateId);
final List<CreateFieldVO> fields = dataFieldService.getCreateFields(template.getId());
try {
final DbType dbType = dataTemplateService.getDbType(template.getDataSourceId());
DataSourceService.switchDb(template.getDataSourceId());
String sql = StrFormatter.format("SELECT UTC.COLUMN_NAME AS VALUE,UTC.DATA_TYPE AS TYPE,( SELECT UCC.COMMENTS FROM user_col_comments UCC WHERE UCC.COLUMN_NAME = UTC.COLUMN_NAME AND UCC.TABLE_NAME = UTC.TABLE_NAME ) AS LABEL FROM user_tab_columns UTC WHERE UTC.TABLE_NAME = '{}'", template.getTableName());
if (dbType.equals(DbType.MYSQL)) {
sql = StrFormatter.format("SELECT column_name AS VALUE, DATA_TYPE AS TYPE, column_comment AS LABEL FROM information_schema.COLUMNS WHERE table_name = '{}'", template.getTableName());
}
final List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
final List<String> collect = maps.stream().map(item -> DataImportUtils.str(item.get("VALUE"))).collect(Collectors.toList());
return AjaxResult.success(fields.stream().filter(item -> !collect.contains(item.getFieldName())).collect(Collectors.toList()));
} finally {
DataSourceService.switchDefault();
}
}
@IgnoreWebSecurity
@PostMapping("/syncField")
@ApiOperation("字段同步操作")
public AjaxResult syncField(@RequestBody List<CreateFieldVO> list) {
final String fieldId = list.get(0).getId();
final DataField dataField = dataFieldService.getById(fieldId);
final DataTemplate dataTemplate = dataTemplateService.getById(dataField.getTemplateId());
List<String> sqlList = new ArrayList<>();
String comment = "COMMENT ON COLUMN {}.{} IS '{}'";
String alter = "ALTER TABLE {} ADD {} {} ";
list.forEach(item -> {
sqlList.add(StrFormatter.format(alter, dataTemplate.getTableName(), item.getFieldName(), item.getFieldType()));
sqlList.add(StrFormatter.format(comment, dataTemplate.getTableName(), item.getFieldName(), StringUtils.isBlank(item.getFieldComment()) ? item.getTitle() : item.getFieldComment()));
sqlList.add(StrFormatter.format(alter, dataTemplate.getTableName() + "_TEMP", item.getFieldName(), item.getFieldType()));
sqlList.add(StrFormatter.format(comment, dataTemplate.getTableName() + "_TEMP", item.getFieldName(), StringUtils.isBlank(item.getFieldComment()) ? item.getTitle() : item.getFieldComment()));
});
String[] sqlArr = new String[sqlList.size()];
sqlList.toArray(sqlArr);
try {
DataSourceService.switchDb(dataTemplate.getDataSourceId());
jdbcTemplate.batchUpdate(sqlArr);
} finally {
DataSourceService.switchDefault();
}
return AjaxResult.success();
}
@IgnoreWebSecurity
//@GetMapping("/testQuick")
......
......@@ -4,9 +4,13 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tbyf.his.common.annotation.IgnoreWebSecurity;
import com.tbyf.his.common.core.domain.AjaxResult;
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.web.dataImport.DataImportUtils;
import com.tbyf.his.web.dataImport.core.DiConfig;
import com.tbyf.his.web.dataImport.domain.param.UploadExcelParam;
import com.tbyf.his.web.dataImport.domain.vo.AreaDict;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataTemplate;
import com.tbyf.his.web.dataImport.entity.ExcelData;
import com.tbyf.his.web.dataImport.service.DataFieldService;
......@@ -17,6 +21,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -24,10 +29,14 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lzz
......@@ -127,12 +136,12 @@ public class ExcelDataController {
@IgnoreWebSecurity
@GetMapping("/queryArea")
@ApiOperation("获取机构数据")
public AjaxResult queryArea(@RequestParam String orgName,@RequestParam String year) {
public AjaxResult queryArea(@RequestParam String name, @RequestParam String year) {
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getOrgName, orgName)
.eq(ExcelData::getOrgName, name)
.eq(ExcelData::getYear, year)
.eq(ExcelData::getType, "1"), false);
if (excelData == null){
if (excelData == null) {
return AjaxResult.error("请先上传数据并分析");
}
DataTemplate template = dataTemplateService.getById(excelData.getTemplateId());
......@@ -140,14 +149,95 @@ public class ExcelDataController {
try {
DataSourceService.switchDb(template.getDataSourceId());
List<AreaDict> areaDictList = jdbcTemplate.query(
StrFormatter.format(sql, template.getOrgName(),template.getTableName(),template.getYear()),
new Object[]{},
StrFormatter.format(sql, template.getOrgName(), template.getTableName() + "_TEMP", template.getYear()),
new BeanPropertyRowMapper<>(AreaDict.class));
DataSourceService.switchDefault();
return AjaxResult.success(areaDictList);
}finally {
} finally {
DataSourceService.switchDefault();
}
}
@IgnoreWebSecurity
@PostMapping("/updateArea")
@ApiOperation("更新机构数据")
public AjaxResult updateArea(@RequestBody AreaDict area) {
String sql = "UPDATE {} SET ORG_NAME = '{}' ,MAILING_ADDRESS='{}',AREA_NUMBER_CODE='{}' WHERE ROWCODE = '{}'";
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getOrgName, area.getName())
.eq(ExcelData::getYear, area.getYear())
.eq(ExcelData::getType, "1"), false);
DataTemplate template = dataTemplateService.getById(excelData.getTemplateId());
try {
DataSourceService.switchDb(template.getDataSourceId());
jdbcTemplate.execute(StrFormatter.format(sql,
template.getTableName() + "_TEMP",
DataImportUtils.str(area.getORG_NAME()),
DataImportUtils.str(area.getMAILING_ADDRESS()),
DataImportUtils.str(area.getAREA_NUMBER_CODE()),
DataImportUtils.str(area.getROWCODE())));
return AjaxResult.success();
} finally {
DataSourceService.switchDefault();
}
}
@IgnoreWebSecurity
@GetMapping("/export")
@ApiOperation("excel导出")
public void queryArea(@RequestParam String name, @RequestParam String year, HttpServletResponse response) {
ExcelData excelData = excelDataService.getOne(Wrappers.lambdaQuery(ExcelData.class)
.eq(ExcelData::getOrgName, name)
.eq(ExcelData::getYear, year)
.eq(ExcelData::getType, "1"), false);
DataTemplate template = dataTemplateService.getById(excelData.getTemplateId());
// 查询每个指定的字段在第几行
List<VerifyVO> verifyVOList = dataFieldService.getFieldsInfo(Arrays.asList("ORG_CODE", "ORG_NAME", "MAILING_ADDRESS", "AREA_NUMBER_CODE"), template.getId());
Map<String, String> fieldMap = new HashMap<>();
verifyVOList.forEach(item -> fieldMap.put(item.getFieldName(), item.getCoordinate()));
final String orgCodeCo = fieldMap.get("ORG_CODE");
if (StringUtils.isBlank(orgCodeCo)) {
return;
}
final int rowStart = Integer.parseInt(orgCodeCo.split(",")[1]);
try (InputStream is = new ByteArrayInputStream(excelData.getFile()); Workbook workbook = WorkbookFactory.create(is)) {
final Sheet sheet = workbook.getSheetAt(0);
String sql = "SELECT ORG_CODE,ORG_NAME,MAILING_ADDRESS,AREA_NUMBER_CODE FROM {} WHERE YEAROOFDATARECORD='{}年'";
try {
DataSourceService.switchDb(template.getDataSourceId());
final List<Map<String, Object>> mapList = jdbcTemplate.queryForList(
StrFormatter.format(sql, template.getTableName() + "_TEMP", template.getYear()));
root:
for (Map<String, Object> map : mapList) {
final Row row = sheet.getRow(rowStart - 1);
for (Cell cell : row) {
if (StringUtils.equals(DiConfig.getValue(cell), String.valueOf(map.get("ORG_CODE")))) {
// 确定纵坐标直接赋值
final int columnIndex = cell.getColumnIndex();
Arrays.asList("ORG_NAME", "MAILING_ADDRESS", "AREA_NUMBER_CODE").forEach(item -> {
final String coordinate = fieldMap.get(item);
if (StringUtils.isNotBlank(coordinate)) {
final int rowIndex = Integer.parseInt(coordinate.split(",")[1]);
Cell tempCell = sheet.getRow(rowIndex - 1).getCell(columnIndex);
if (tempCell == null) {
tempCell = sheet.getRow(rowIndex - 1).createCell(columnIndex);
}
tempCell.setCellValue(DataImportUtils.str(map.get(item)));
}
});
continue root;
}
}
}
workbook.write(response.getOutputStream());
} finally {
DataSourceService.switchDefault();
}
} catch (IOException e) {
throw new BaseException(e.getMessage());
}
}
}
......@@ -123,4 +123,8 @@ public class DataImportUtils {
return "'" + vo.getValues().get(index) + "'";
}
public static String str(Object o) {
return o == null ? StringUtils.EMPTY : o.toString();
}
}
......@@ -40,12 +40,16 @@ public class AreaDict implements Serializable {
@JsonProperty(value = "AREA_NUMBER_CODE")
private String AREA_NUMBER_CODE;
@ApiModelProperty(value = "所属市")
/* @ApiModelProperty(value = "所属市")
@JsonProperty(value = "CITY")
private String CITY;
@ApiModelProperty(value = "所属区")
@JsonProperty(value = "AREA")
private String AREA;
private String AREA;*/
private String name;
private String year;
}
......@@ -40,4 +40,7 @@ public class CreateFieldVO implements Serializable {
@ApiModelProperty(value = "元字段类型")
private String fieldType;
@ApiModelProperty(value = "字段注释")
private String fieldComment;
}
......@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tbyf.his.web.dataImport.domain.param.QueryFieldParam;
import com.tbyf.his.web.dataImport.domain.vo.CreateFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.DataFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataField;
import org.apache.ibatis.annotations.Param;
......@@ -41,4 +42,13 @@ public interface DataFieldMapper extends BaseMapper<DataField> {
* @return
*/
List<CreateFieldVO> getCreateFields(String templateId);
/**
* 查询指定数据库字段在模板的位置
*
* @param fieldList
* @param templateId
* @return
*/
List<VerifyVO> getFieldsInfo(@Param("fieldList") List<String> fieldList, @Param("templateId") String templateId);
}
......@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.tbyf.his.web.dataImport.domain.param.QueryFieldParam;
import com.tbyf.his.web.dataImport.domain.vo.CreateFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.DataFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataField;
import com.tbyf.his.web.dataImport.entity.MetaField;
......@@ -48,4 +49,13 @@ public interface DataFieldService extends IService<DataField> {
* @return
*/
List<CreateFieldVO> getCreateFields(String templateId);
/**
* 查询指定数据库字段在模板的位置
*
* @param fieldList
* @param templateId
* @return
*/
List<VerifyVO> getFieldsInfo(List<String> fieldList, String templateId);
}
......@@ -9,6 +9,7 @@ import com.tbyf.his.web.dataImport.DataImportUtils;
import com.tbyf.his.web.dataImport.domain.param.QueryFieldParam;
import com.tbyf.his.web.dataImport.domain.vo.CreateFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.DataFieldVO;
import com.tbyf.his.web.dataImport.domain.vo.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataField;
import com.tbyf.his.web.dataImport.entity.MetaField;
import com.tbyf.his.web.dataImport.mapper.DataFieldMapper;
......@@ -109,4 +110,9 @@ public class DataFieldServiceImpl extends ServiceImpl<DataFieldMapper, DataField
public List<CreateFieldVO> getCreateFields(String templateId) {
return dataFieldMapper.getCreateFields(templateId);
}
@Override
public List<VerifyVO> getFieldsInfo(List<String> fieldList, String templateId) {
return dataFieldMapper.getFieldsInfo(fieldList, templateId);
}
}
......@@ -58,11 +58,21 @@
df.sort,
mf.id as fieldId,
mf.field_name as fieldName,
mf.field_type as fieldType
mf.field_type as fieldType,
mf.field_comment as fieldComment
from data_field df
left join meta_field mf on df.field = mf.id
where df.template_id = #{templateId}
and mf.id is not null
order by df.sort
</select>
<select id="getFieldsInfo" resultType="com.tbyf.his.web.dataImport.domain.vo.VerifyVO">
select df.coordinate,mf.field_name from data_field df left join meta_field mf on df.field = mf.id
where df.template_id =#{templateId}
and mf.field_name in
<foreach item="item" collection="fieldList" open="(" separator="," close=")">
#{item}
</foreach>
</select>
</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