kimi
2020-01-13 9edfa82bf71e4bab8e7dc125baa30e1ddd505d71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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);
    }
}