| | |
| | | } |
| | | |
| | | List<String> list = codesAsList(codes); |
| | | removeDuplicate(list); |
| | | list = copeDirCodes(list); |
| | | setRightLike(list, field); |
| | | |
| | | return "(" + StringHelper.join(list, " or ") + ")"; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 去除重复 |
| | | * 处理目录编码 |
| | | */ |
| | | private static void removeDuplicate(List<String> list) { |
| | | int i = 0; |
| | | while (i < list.size()) { |
| | | int j = findStr(list, i); |
| | | if (j > -1) { |
| | | list.remove(j); |
| | | continue; |
| | | } |
| | | |
| | | i++; |
| | | private static List<String> copeDirCodes(List<String> list) { |
| | | List<String> prjList = getDirCodesByLen(list, 0, 2); |
| | | List<String> appList = getDirCodesByLen(list, 3, 30); |
| | | if (prjList.isEmpty() && appList.isEmpty()) { |
| | | return list; |
| | | } |
| | | if (prjList.isEmpty()) { |
| | | return appList; |
| | | } |
| | | if (appList.isEmpty()) { |
| | | return prjList; |
| | | } |
| | | |
| | | return filterCodes(prjList, appList); |
| | | } |
| | | |
| | | /** |
| | | * 查找字符串 |
| | | * 根据长度获取单位编码 |
| | | */ |
| | | private static int findStr(List<String> list, int i) { |
| | | String source = list.get(i); |
| | | for (int j = i + 1, c = list.size(); j < c; j++) { |
| | | if (list.get(j).startsWith(source)) { |
| | | return j; |
| | | private static List<String> getDirCodesByLen(List<String> list, int start, int end) { |
| | | List<String> rs = new ArrayList<>(); |
| | | for (String code : list) { |
| | | if (StringHelper.isEmpty(code)) { |
| | | continue; |
| | | } |
| | | |
| | | if (code.length() >= start && code.length() <= end) { |
| | | rs.add(code); |
| | | } |
| | | } |
| | | |
| | | return -1; |
| | | return rs; |
| | | } |
| | | |
| | | /** |
| | | * 过滤项目编码 |
| | | */ |
| | | private static List<String> filterCodes(List<String> prjList, List<String> appList) { |
| | | int i = 0; |
| | | while (i < appList.size()) { |
| | | boolean flag = false; |
| | | for (String prj : prjList) { |
| | | if (appList.get(i).startsWith(prj)) { |
| | | flag = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (!flag) { |
| | | appList.remove(i); |
| | | continue; |
| | | } |
| | | i++; |
| | | } |
| | | |
| | | return appList; |
| | | } |
| | | |
| | | /** |
| | | * 设置 右like |
| | | */ |
| | | private static void setRightLike(List<String> list, String field) { |
| | | if (list.isEmpty()) { |
| | | list.add("1 = 2"); |
| | | return; |
| | | } |
| | | |
| | | for (int i = 0, c = list.size(); i < c; i++) { |
| | | String str = String.format("%s like '%s%%'", field, list.get(i)); |
| | | list.set(i, str); |