package com.engine.salary.formlua.util;


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @className：
 * @Description：正则表达式工具
 * @Author：
 * @date：
 */
public class RegularUtil {
	public static void validateIDCard(String idCard){
		Pattern pattern = Pattern.compile("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$");
		Matcher matcher = pattern.matcher(idCard);
		if (!matcher.matches()) {
			throw new RuntimeException("身份证号码不合法");
		}
	}
	public static void checkAggCnd(String formula){
		String startPartternStr="^(OR\\(|AND\\()";
		Pattern startPattern = Pattern.compile(startPartternStr);
		Matcher startMatcher = startPattern.matcher(formula);
		if(!startMatcher.find()){
			throw new RuntimeException("条件筛选函数只允许AND函数与OR函数");
		}
//		else {
//			String partternStr="(AND|OR)+\\(+(.)+\\)+";
//			Pattern pattern = Pattern.compile(partternStr);
//			Matcher matcher = pattern.matcher(formula);
//			while (matcher.find()){
//				String fr=matcher.group();
//				logger.info(fr);
//				fr=fr.replaceAll("(AND|OR)","");
//				String partternStr2="(.)+\\(+(.)+\\)+";
//				Pattern pattern2 = Pattern.compile(partternStr2);
//				Matcher matcher2 = pattern2.matcher(fr);
//				if(matcher2.find()){
//					throw new RuntimeException("条件筛选参数不正确");
//				}
//			}
//		}
	}
	public static boolean isNumber(Object obj) {
		if(obj==null){
			return false;
		}
		String nstr=obj.toString();
		String reg = "\\d+(\\.\\d+)?";
		if(nstr.startsWith("-")){
			nstr=nstr.substring(1);
		}
		Boolean strResult = nstr.matches(reg);
		return strResult;
	}

	/**
	 * 判断是否是0或正整数
	 * @param obj
	 * @return
	 */
	public static boolean isNonNegativeInteger(Object obj) {
		if(obj == null) return false;
		String nstr = obj.toString();

		String reg = "^[1-9]\\d*|0$";
		return nstr.matches(reg);
	}

	/**
	 * 是否是整数，正整数、0、负整数
	 * @param obj
	 * @return
	 */
	public static boolean isInteger(Object obj){
		if(obj == null) return false;
		String nstr = obj.toString();

		String reg = "^-?[1-9]\\d*$";
		return nstr.matches(reg);
	}

	/**
	 * IF函数的规则判断
	 * @param str
	 * @return
	 */
	public static Map<String,Object> checkIfFunc(String str){
		Map<String,Object> rmap=new HashMap<String,Object>();
		str=str.trim().replaceAll(" ","");
		boolean r=true;
		String patt="if{1}\\({1}\\w+[<|>|!=｜==]+\\w+\\){1}[\\w\\{\\}]*";
		String charCheckIf="if{1}";
		String charCheckLf="if{1}\\({1}";
		String charCheckRf="\\){1}";
		String paramLeft="\\({1}\\w+[<|>|!=]+";
		String paramRight="[<|>|!=]+\\w+\\){1}";
		String paramOperSym="[<|>|!=]+";
		Pattern rPattern = Pattern.compile(patt);
		Pattern ifPattern=Pattern.compile(charCheckIf);
		Pattern leftPattern=Pattern.compile(charCheckLf);
		Pattern rightPattern=Pattern.compile(charCheckRf);
		Pattern leftParamPatt=Pattern.compile(paramLeft);
		Pattern rightParamPatt = Pattern.compile(paramRight);
		Pattern operSybmPattern=Pattern.compile(paramOperSym);

		//先测试整条语句是否符合规则
		boolean pOneResult=rPattern.matcher(str).find();


		if(!pOneResult){
			//以下为逐一排查问题的位置
			boolean pIfResult =ifPattern.matcher(str).find();
			boolean leftResult=leftPattern.matcher(str).find();
			boolean rightResult=rightPattern.matcher(str).find();
			boolean lpResult=leftParamPatt.matcher(str).find();
			boolean rpResult=rightParamPatt.matcher(str).find();
			boolean operSybmResult=operSybmPattern.matcher(str).find();
			r=false;
			rmap.put("result",r);
			if(!pIfResult){
				rmap.put("info","格式不正确缺少if标识符");
			}else if(!leftResult){
				rmap.put("info","格式不正确缺少左括号(标识符");
			}else if(!rightResult){
				rmap.put("info","格式不正确缺少右括号)标识符");
			}else if(!lpResult){
				rmap.put("info","格式不正确缺少左侧参数");
			}else if(!rpResult){
				rmap.put("info","格式不正确缺少右侧参数");
			}else if(!operSybmResult){
				rmap.put("info","格式不正确缺少操作符");
			}else{
				rmap.put("info","格式未通过格式验证");
			}

		}
		return rmap;
	}
	/**
	 * 判断是否含有字母
	 * @param str
	 * @return
	 */
	public static boolean containsLetter(String str) {
		String patt="[a-z|A-Z]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.find();
	}

	/**
	 * 判断是否含有数字
	 * @param str
	 * @return
	 */
	public static boolean containsNum(String str) {
		String patt="[0-9]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.find();
	}

	/**
	 * 判断是否含有非字母
	 * @param str
	 * @return
	 */
	public static boolean containsNotLetter(String str) {
		String patt="[^a-z|^A-Z]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.find();
	}

	/**
	 * 判断是否包含非数字
	 * @param str
	 * @return
	 */
	public static boolean containsNotNum(String str){
		String patt="[^0-9]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.find();
	}
	/**
	 * 替换非字母
	 * @param str
	 * @return
	 */
	public static String containsNotLetterR(String str,String replaceStr) {
		String patt="[^a-z|^A-Z]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.replaceAll(replaceStr);
	}
	/**
	 * 替换非数字
	 * @param str
	 * @param replaceStr
	 * @return
	 */
	public static String containsNotNumR(String str,String replaceStr){
		String patt="[^0-9]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.replaceAll(replaceStr);
	}

	/**
	 * 判断是否含有字母并替换
	 * @param str
	 * @param replaceStr
	 * @return
	 */
	public static String containsLetterR(String str,String replaceStr) {
		String patt="[a-z|A-Z]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		str=matcher.replaceAll(replaceStr);
		return str;
	}

	/**
	 * 判断是否含有数字并替换
	 * @param str
	 * @param replaceStr
	 * @return
	 */
	public static String containsNumR(String str,String replaceStr) {
		String patt="[0-9]";
		Pattern r = Pattern.compile(patt);
		Matcher matcher = r.matcher(str);
		return matcher.replaceAll(replaceStr);
	}

}
