package com.highdatas.mdm.service.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.highdatas.mdm.entity.TQualityRule;
|
import com.highdatas.mdm.mapper.TQualityRuleMapper;
|
import com.highdatas.mdm.pojo.CodeMsg;
|
import com.highdatas.mdm.pojo.Result;
|
import com.highdatas.mdm.service.ITQualityRuleService;
|
import com.highdatas.mdm.service.MasterDataService;
|
import com.highdatas.mdm.util.Constant;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Arrays;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.regex.Pattern;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 规则表 服务实现类
|
* </p>
|
*
|
* @author kimi
|
* @since 2019-12-18
|
*/
|
@Service
|
public class TQualityRuleServiceImpl extends ServiceImpl<TQualityRuleMapper, TQualityRule> implements ITQualityRuleService {
|
|
@Autowired
|
MasterDataService masterDataService;
|
|
@Override
|
public Result rule(String tableName, String field, String ruleId) {
|
if (StringUtils.isEmpty(field)) {
|
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
|
}
|
TQualityRule tQualityRule = selectOne(new EntityWrapper<TQualityRule>().eq("rule_id", ruleId));
|
if (tQualityRule == null) {
|
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
|
}
|
|
String[] split = field.split(Constant.COMMA_TRIM);
|
List<String> collect = Arrays.stream(split).collect(Collectors.toList());
|
Result result = masterDataService.selectList(tableName, collect);
|
JSONObject object = (JSONObject)result.getData();
|
List<Map<String, Object>> datas = (List<Map<String, Object>>) object.get("grid");
|
String codeContent = tQualityRule.getCodeContent();
|
int total = datas.size();
|
int matched = 0;
|
for (Map<String, Object> data : datas) {
|
if (data == null) {
|
continue;
|
}
|
String tableVal = String.valueOf(data.get(field));
|
boolean matches = Pattern.matches(codeContent, tableVal);
|
if (matches) {
|
matched++;
|
}
|
}
|
HashMap<String, Object> resultMap = new HashMap<>();
|
resultMap.put("total", total);
|
resultMap.put("matched", matched);
|
return Result.success(resultMap);
|
}
|
}
|