/**
* @category
* 原:strIDs in ( 100001,100002,100003,....................,110001,120001,130001,140001,150001)
* 转后:( strIds in (100001,100002,100003) or strIds in (....,.....,.....) or strIds in (130001,140001,150001))
* @see 处理strIDs查找的数量超过1000时进行分组处理
* @param orignal 括号内的IDs字符串
* @param spiltNum 分割后的小组内的数量
* @param flag 分割符号(仅限于单字符,格式如上)
* @param titlestrIDs名称
*/
public static String dealQuerySize(String orignal,String flag,String title,int spiltNum){
//返回无需处理的情况
if(StringUtil.isEmpty(orignal) || StringUtil.isEmpty(flag)){
return orignal;
}
int IDSNum = orignal.length()-orignal.replaceAll(flag,"").length()+1;//统计原字符串中ID的数量
//按1组处理的情况
if(IDSNum <=1000 && spiltNum > IDSNum){
return packageQueryStyle(orignal,title);
}
//按照数量分组
if(spiltNum<=0){
spiltNum = 1;
}
//ID分组
StringBuffer totalBuilder = new StringBuffer();
Pattern p = Pattern.compile("([0-9 ]+,){"+spiltNum+"}");//编译模式
Matcher m = p.matcher(orignal);//产生匹配结果
while(m.find()){//结果查找
String matchersIDS = m.group();
String apartIDS = " or " + packageQueryStyle(matchersIDS.substring(0, matchersIDS.length()-1),title);
m.appendReplacement(totalBuilder, apartIDS);
}
StringBuffer lastIDS = m.appendTail(new StringBuffer());
totalBuilder.append(" or "+packageQueryStyle(lastIDS.toString(),title));
return "(" +totalBuilder.toString().replaceFirst("or", "") + ")";
}
private static String packageQueryStyle(String str,String title){
return title+" in ("+str+")";
}
//统计数量的2种方式,这里只统计符号,需要的话自己+1