Browse Source

feat: 薪资转换兼容更多情况

JimZhang 3 years ago
parent
commit
d4475bdb2b
1 changed files with 18 additions and 7 deletions
  1. 18 7
      src/main/java/com/winhc/bigdata/udf/ToIntervalSalary.java

+ 18 - 7
src/main/java/com/winhc/bigdata/udf/ToIntervalSalary.java

@@ -16,27 +16,39 @@ import java.util.regex.Pattern;
  */
 public class ToIntervalSalary extends UDF {
     private static final List<Integer> MIAN_YI = Lists.newArrayList(null, null);
-    private static final Pattern p = Pattern.compile("[^0-9-]");
+    private static final Pattern p = Pattern.compile("[^0-9-.]");
 
     public List<Integer> evaluate(String salary) {
-        if (StringUtils.isEmpty(salary)) {
+        if (StringUtils.isEmpty(salary) || salary.contains("电话")) {
             return MIAN_YI;
         }
+        salary = salary.replaceAll("[—~到]", "-");
         boolean returnSmall = false;
         boolean returnBig = false;
-        if (salary.contains("以上")) {
+        if (salary.contains("以上") || salary.contains("起") || salary.contains("底薪")) {
             returnSmall = true;
         }
         if (salary.contains("以下")) {
             returnBig = true;
         }
         int factor = 1;
-        if (salary.contains("万")) {
+        if (salary.contains("万") || salary.contains("w") || salary.contains("W")) {
             factor = 10000;
         }
+        if (salary.contains("k") || salary.contains("K") || salary.contains("千")) {
+            factor = 1000;
+        }
+        if (salary.contains("年薪")) {
+            factor = factor / 12;
+        }
         salary = p.matcher(salary).replaceAll("");
         int f = factor;
-        int[] ints = Arrays.stream(salary.split("-")).filter(NumberUtils::isNumber).mapToInt(Integer::valueOf).map(i -> i * f).toArray();
+        int[] ints = Arrays.stream(salary.split("[-wWKk万千元]"))
+                .filter(NumberUtils::isNumber)
+                .filter(s -> s.length() < 8)
+                .mapToDouble(Double::valueOf)
+                .mapToInt(d -> (int) d).map(i -> i * f)
+                .toArray();
         if (ints.length == 0) {
             return MIAN_YI;
         } else if (ints.length == 1) {
@@ -49,11 +61,10 @@ public class ToIntervalSalary extends UDF {
         } else if (ints.length == 2) {
             return Lists.newArrayList(ints[0], ints[1]);
         }
-
         return MIAN_YI;
     }
 
     public static void main(String[] args) {
-        System.out.println(new ToIntervalSalary().evaluate("5000-8000\n"));
+        System.out.println(new ToIntervalSalary().evaluate("6-10万\n"));
     }
 }