package com.sa.ibiz.demo.ibizutil.helper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

public class DataGridCustomSearchHelper
{
	private static DataGridCustomSearchHelper instance;

	private DataGridCustomSearchHelper(){}

	public static DataGridCustomSearchHelper getInstance(){
		if(instance==null)
			instance = new DataGridCustomSearchHelper();
		return instance;
	}

	/**
	 * 解析自定义查询表达式
	 * @param groupObj 自定义查询表达式对象
	 * @return 自定义查询SQL
	 */
	public String parseCustomVal(JSONObject groupObj){
		String customSql=this.parseGroup(groupObj);
		customSql=customSql.replaceAll("NOTEQ","<>").replaceAll("GTANDEQ",">=").replaceAll("GT",">").replaceAll("LTANDEQ","<=").replaceAll("LT","<").replaceAll("EQ","=").replaceAll("ISNULL","IS NULL").replaceAll("ISNOTNULL","IS NOT NULL").replaceAll("OR","||");;
		return customSql;
	}
	/**
	 * 组查询
	 * @param groupObj
	 * @return
	 */
	private String parseGroup(JSONObject groupObj){
		if(StringUtils.isEmpty(groupObj))
			return "";

		String conn=groupObj.getString("groupcond");  //获取组合方式
		JSONArray fieldArr=groupObj.getJSONArray("field"); //获取值项数组
		String strFieldCond=parseFieldList(fieldArr,conn); //获取值项表达式

		JSONArray groupArr=groupObj.getJSONArray("group");//获取组合条件
		String strGroupCond="";
		if((!StringUtils.isEmpty(groupArr)) && groupArr.size()>0){
			strGroupCond=parseGroupList(groupArr,conn);  //获取组合条件表达式
		}
		String strRuleCond=getRuleCond(strFieldCond,strGroupCond,conn);//获取总表达式（值项+组合）
		return strRuleCond;
	}

	/**
	 *  （值项+组合）
	 * @param strFieldCond	值项表达式
	 * @param strGroupCond 组合表达式
	 * @param conn		   组合方式
	 * @return
	 */
	private String getRuleCond(String strFieldCond,String strGroupCond,String conn)
	{
		String strRuleCond="";
		if((!StringUtils.isEmpty(strFieldCond) && (!StringUtils.isEmpty(strGroupCond)))){
			strRuleCond=String.format("(%s %s %s)",strFieldCond,conn,strGroupCond);
		}
		else if(!StringUtils.isEmpty(strFieldCond)){
			strRuleCond=String.format("%s",strFieldCond);
		}
		else if(!StringUtils.isEmpty(strGroupCond)){
			strRuleCond=String.format("%s",strGroupCond);
		}
		return strRuleCond;

	}

	/**
	 * 组查询
	 * @param GroupArr
	 * @param conn
	 * @return
	 */
	private String parseGroupList(JSONArray GroupArr,String conn){
		if(StringUtils.isEmpty(GroupArr)||GroupArr.size()==0)
			return "";
		StringBuffer sbf=new StringBuffer();
		for(int a=0;a<GroupArr.size();a++){
			String strGroupCond=parseGroup(GroupArr.getJSONObject(a));
			sbf.append(strGroupCond);
			if(a!=GroupArr.size()-1)//去掉最后的连接符
				sbf.append(conn+" ");
		}
		return String.format("(%s)",sbf.toString());
	}

	/**
	 * @param fieldArr field数组
	 * @param conn	field连接符
	 * @return
	 */
	private String parseFieldList(JSONArray fieldArr,String conn){
		if(StringUtils.isEmpty(fieldArr)||fieldArr.size()==0)
			return "";
		StringBuffer sbf=new StringBuffer();
		for(int a=0;a<fieldArr.size();a++){
			JSONObject fieldObj=fieldArr.getJSONObject(a);
			String fieldCond=parseField(fieldObj);
			sbf.append(fieldCond);
			if(a!=fieldArr.size()-1)//去掉最后的连接符
				sbf.append(conn+" ");
		}
		return String.format("(%s)",sbf.toString());
	}

	/**
	 * 拼接field表达式
	 * @param fieldObj
	 * @return
	 */
	private String parseField(JSONObject fieldObj){
		String fieldName=fieldObj.getString("fieldname");
		String fieldCond=fieldObj.getString("fieldcond");
		String condValue=fieldObj.getString("condvalue");
		return String.format("(%s %s '%s' )",fieldName,fieldCond,condValue);

	}
}