Commit c394e9c9 by 刘泽志

最终版本

parent cc998321
......@@ -578,8 +578,7 @@ public class DataImportController {
fieldList.forEach(field -> sb.append(field.getField())
.append(" varchar(32) null comment '")
.append(field.createComment()).append("',"));
sb.deleteCharAt(sb.length() - 1)
.append(" ) comment '")
sb.append(" YEAR varchar(32) null").append(" ) comment '")
.append(template.createComment())
.append("';");
// 同时创建数据表与临时表
......@@ -612,7 +611,7 @@ public class DataImportController {
prodSqlList.add(StrFormatter.format(comment, finalTableName, field.getField(), comm));
tempSqlList.add(StrFormatter.format(comment, tempTableName, field.getField(), comm));
});
sql.deleteCharAt(sql.length() - 1)
sql.append(" YEAR VARCHAR2(255)")
.append(" )");
prodSqlList.add(0, StrFormatter.format(sql.toString(), finalTableName));
tempSqlList.add(0, StrFormatter.format(sql.toString(), tempTableName));
......@@ -700,7 +699,7 @@ public class DataImportController {
.eq(DataField::getTemplateId, template.getId())
.isNotNull(DataField::getField)
.select(DataField::getField));
final String fieldSql = fieldList.stream().map(DataField::getField).collect(Collectors.joining(","));
final String fieldSql = fieldList.stream().map(DataField::getField).collect(Collectors.joining(",")) + ",YEAR";
try {
DataSourceUtil.switchDs(template.getDataSourceId());
// 先查询出来所有的数据
......@@ -723,7 +722,7 @@ public class DataImportController {
sb.append("'").append(o).append("',");
}
}
sb.deleteCharAt(sb.length() - 1);
sb.append("'").append(template.getYear()).append("'");
sb.append(")");
log.info("插入数据sql为:[{}]", sb);
sqlArr[i] = sb.toString();
......@@ -744,5 +743,41 @@ public class DataImportController {
return AjaxResult.success();
}
@IgnoreWebSecurity
@PostMapping("/rule/template")
@ApiOperation("规则新增By Template")
public AjaxResult addRuleByTemplate(@RequestBody @Validated AddTemplateRuleParam param) {
DataRule rule = new DataRule();
BeanUtils.copyProperties(param, rule);
dataRuleService.save(rule);
final BindRule bindRule = new BindRule();
bindRule.setDataId(param.getTid())
.setRuleId(rule.getId());
bindRuleService.save(bindRule);
return AjaxResult.success();
}
@IgnoreWebSecurity
@PutMapping("/rule/template")
@ApiOperation("修改规则By Template")
public AjaxResult updateRuleByTemplate(@RequestBody @Validated UpdateRuleParam param) {
DataRule rule = new DataRule();
BeanUtils.copyProperties(param, rule);
dataRuleService.updateById(rule);
return AjaxResult.success();
}
@IgnoreWebSecurity
@DeleteMapping("/rule/template")
@ApiOperation("删除规则By Template")
public AjaxResult deleteRuleByTemplate(@RequestParam String ruleId,
@RequestParam String templateId) {
dataRuleService.removeById(ruleId);
bindRuleService.remove(Wrappers.lambdaQuery(BindRule.class)
.eq(BindRule::getRuleId, ruleId)
.eq(BindRule::getDataId, templateId));
return AjaxResult.success();
}
}
package com.tbyf.his.web.dataImport.domain.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author lzz
* @date 2023/1/10 15:36
*/
@Data
@Accessors(chain = true)
@ApiModel(value = "新增模板规则", description = "新增模板规则")
public class AddTemplateRuleParam implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "规则名称")
private String name;
@ApiModelProperty(value = "规则类型 1-基础规则 2-组合规则 3-字段复合规则")
private String type;
@ApiModelProperty(value = "规则验证模式")
private String mode;
@ApiModelProperty(value = "规则验证内容")
private String content;
@ApiModelProperty(value = "备注")
private String remarks;
@ApiModelProperty(value = "模板ID")
private String tid;
}
......@@ -5,6 +5,7 @@ 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.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import com.tbyf.his.web.dataImport.entity.DataRule;
import java.util.List;
......@@ -38,4 +39,10 @@ public interface DataImportTemplateMapper extends BaseMapper<DataImportTemplate>
* @return
*/
List<VerifyVO> getVerify(String id);
/**
* @param templateId
* @return
*/
List<DataRule> getAllRule(String templateId);
}
......@@ -6,6 +6,7 @@ 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.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import com.tbyf.his.web.dataImport.entity.DataRule;
import java.util.List;
......@@ -33,4 +34,12 @@ public interface DataImportTemplateService extends IService<DataImportTemplate>
* @return
*/
List<VerifyVO> getVerify(String id);
/**
* 获取模板校验规则
*
* @param templateId
* @return
*/
List<DataRule> getAllRule(String templateId);
}
......@@ -9,6 +9,7 @@ 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.VerifyVO;
import com.tbyf.his.web.dataImport.entity.DataImportTemplate;
import com.tbyf.his.web.dataImport.entity.DataRule;
import com.tbyf.his.web.dataImport.mapper.DataImportTemplateMapper;
import com.tbyf.his.web.dataImport.service.DataImportTemplateService;
import lombok.extern.slf4j.Slf4j;
......@@ -51,4 +52,9 @@ public class DataImportTemplateServiceImpl extends ServiceImpl<DataImportTemplat
public List<VerifyVO> getVerify(String id) {
return dataImportTemplateMapper.getVerify(id);
}
@Override
public List<DataRule> getAllRule(String templateId) {
return dataImportTemplateMapper.getAllRule(templateId);
}
}
......@@ -13,6 +13,7 @@ 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.DataRule;
import com.tbyf.his.web.dataImport.entity.ExcelData;
import com.tbyf.his.web.dataImport.mapper.ExcelDataMapper;
import com.tbyf.his.web.dataImport.service.DataImportTemplateService;
......@@ -29,7 +30,9 @@ import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
......@@ -129,6 +132,86 @@ public class ExcelDataServiceImpl extends ServiceImpl<ExcelDataMapper, ExcelData
}
}
});
List<ExcelVO> list = new ArrayList<>();
verifyList.forEach(item -> {
if (!CollectionUtils.isEmpty(item.getErrors())) {
list.addAll(item.getErrors());
}
});
// 模板规则的数据校验
final List<DataRule> allRule = dataImportTemplateService.getAllRule(template.getId());
for (DataRule rule : allRule) {
if (StringUtils.isNotBlank(rule.getContent())) {
if (StringUtils.equals(rule.getMode(), "逻辑相加")) {
final String[] split = rule.getContent().split("=");
if (split.length == 2) {
String[] leftIds = split[0].split(",");
String[] rightIds = split[1].split(",");
final List<String> leftList = Arrays.stream(leftIds).filter(StringUtils::isNotBlank).collect(Collectors.toList());
final List<String> rightList = Arrays.stream(rightIds).filter(StringUtils::isNotBlank).collect(Collectors.toList());
if (leftList.size() > 0 && rightList.size() > 0) {
for (int r = 0; r < length; r++) {
final List<VerifyVO> leftObjs = verifyList.stream().filter(v -> leftList.contains(v.getFieldId())).collect(Collectors.toList());
final List<VerifyVO> rightObjs = verifyList.stream().filter(v -> rightList.contains(v.getFieldId())).collect(Collectors.toList());
try {
BigDecimal left = new BigDecimal("0");
BigDecimal right = new BigDecimal("0");
for (VerifyVO leftVo : leftObjs) {
try {
left = left.add(new BigDecimal(leftVo.getValues().get(r)));
} catch (Exception ignore) {
}
}
for (VerifyVO rightVo : rightObjs) {
try {
right = right.add(new BigDecimal(rightVo.getValues().get(r)));
} catch (Exception ignore) {
}
}
if (left.compareTo(right) != 0) {
ExcelVO excelVO = new ExcelVO();
BeanUtils.copyProperties(excels[r], excelVO);
excelVO.setMessage("逻辑验证错误,正确公式为:" + getFormula(leftObjs, rightObjs));
leftObjs.addAll(rightObjs);
int finalR = r;
final List<ExcelVO> errList = leftObjs.stream().map(item -> {
final ExcelVO temp = new ExcelVO();
temp.setCode(item.getCode())
.setTitle(item.getTitle())
.setValue(item.getValues().get(finalR));
return temp;
}).collect(Collectors.toList());
final ExcelVO firstVo = errList.get(0);
firstVo.setMessage(excelVO.getMessage()).setOrgName(excelVO.getOrgName())
.setOrgType(excelVO.getOrgType()).setOrgCode(excelVO.getOrgCode())
.setUnifiedCode(excelVO.getUnifiedCode());
list.addAll(errList);
}
} catch (Exception e) {
ExcelVO excelVO = new ExcelVO();
BeanUtils.copyProperties(excels[r], excelVO);
excelVO.setMessage("逻辑验证错误,正确公式为:" + getFormula(leftObjs, rightObjs));
leftObjs.addAll(rightObjs);
int finalR = r;
final List<ExcelVO> errList = leftObjs.stream().map(item -> {
final ExcelVO temp = new ExcelVO();
temp.setCode(item.getCode())
.setTitle(item.getTitle())
.setValue(item.getValues().get(finalR));
return temp;
}).collect(Collectors.toList());
final ExcelVO firstVo = errList.get(0);
firstVo.setMessage(excelVO.getMessage()).setOrgName(excelVO.getOrgName())
.setOrgType(excelVO.getOrgType()).setOrgCode(excelVO.getOrgCode())
.setUnifiedCode(excelVO.getUnifiedCode());
list.addAll(errList);
}
}
}
}
}
}
}
// 第三步导入临时表
String sql = "INSERT INTO {}(" + verifyList.stream()
.map(VerifyVO::getField).collect(Collectors.joining(","))
......@@ -153,12 +236,6 @@ public class ExcelDataServiceImpl extends ServiceImpl<ExcelDataMapper, ExcelData
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) {
......@@ -166,9 +243,35 @@ public class ExcelDataServiceImpl extends ServiceImpl<ExcelDataMapper, ExcelData
}
}
/**
* 获取逻辑校验公式
*
* @param leftObjs
* @param rightObjs
* @return
*/
private String getFormula(List<VerifyVO> leftObjs, List<VerifyVO> rightObjs) {
return leftObjs.stream().map(item -> getValue(item.getCode()) + "(" + getValue(item.getTitle()) + ")")
.collect(Collectors.joining("+"))
+ " = " +
rightObjs.stream().map(item -> getValue(item.getCode()) + "(" + getValue(item.getTitle()) + ")")
.collect(Collectors.joining("+"));
}
public String getValue(String s) {
if (s == null) {
return "";
}
return s;
}
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];
}
public static void main(String[] args) {
System.out.println(new BigDecimal("0").add(new BigDecimal("")));
}
}
......@@ -7,9 +7,20 @@
SELECT id,
name,
org_name as orgName,
import_status as importStatus, year, import_time as importTime, 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
import_status as importStatus,
year,
import_time as importTime,
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
<where>
<if test="year != null and year != ''">
......@@ -21,7 +32,8 @@
<select id="queryDbType" resultType="java.lang.String">
SELECT driver_class
FROM sys_datasource
where datasource_name = #{dataSourceId} limit 1
where datasource_name = #{dataSourceId}
limit 1
</select>
<resultMap id="verifyVo" type="com.tbyf.his.web.dataImport.domain.vo.VerifyVO">
......@@ -61,4 +73,11 @@
and df.field is not null
order by df.sort
</select>
<select id="getAllRule" resultType="com.tbyf.his.web.dataImport.entity.DataRule">
select dr.id as id, dr.name as name, dr.mode as mode, dr.content as content
from bind_rule br
left join data_rule dr on br.rule_id = dr.id
where br.data_id = #{templateId}
</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