package com.engine.cube.entity;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.cube.util.CubeSearchTransMethod;
import com.api.formmode.cache.*;
import com.engine.common.entity.EncryptFieldEntity;
import com.engine.cube.biz.BrowserHelper;
import com.engine.cube.util.CheckCondtionUtil;
import com.engine.encrypt.biz.EncryptFieldConfigComInfo;
import com.weaver.formmodel.util.StringHelper;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.data.ModeDataApproval;
import weaver.formmode.interfaces.action.WorkflowToMode;
import weaver.formmode.log.FormmodeLog;
import weaver.formmode.service.CommonConstant;
import weaver.formmode.setup.ExpandBaseRightInfo;
import weaver.formmode.setup.ModeLayoutUtil;
import weaver.formmode.setup.ModeRightInfo;
import weaver.formmode.setup.ShareExpressions;
import weaver.formmode.tree.CustomTreeData;
import weaver.formmode.virtualform.VirtualFormHandler;
import weaver.formmode.webservices.FieldInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.workflow.WorkflowBillComInfo;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RightConditionEntity {
    private int objid;
    private int conditiontype;
    private String rootdata;
    private String conditionsql;
    private String conditiontext;
    //查询列表和浏览框的java条件
    private String javafileaddress;
    private String type = "";
    private String objFieldName = "";
    private String expressionsTable = "";
    private String expressionbaseTable = "";
    private String baseInfoTable = "";
    private int customid;
	private int isTop;//用于判定是否操作置顶
	private int modeid;
    public static Vector<String> tempTableList = new Vector<String>();
    private static Map<String,String> tableMap = new HashMap<String,String>();
	private DepartmentComInfo deptcominfo;
	private SubCompanyComInfo subcominfo;
	private int nodeid;
	private String detailtable;
	private String browserType;
	private String tablename;
    public RightConditionEntity(String type) {
    	this.type = type;
		deptcominfo = new DepartmentComInfo();
		subcominfo = new SubCompanyComInfo();
    	//根据不同类型获取字段名
    	if(type.equals("modeRight")){

    		this.objFieldName = "rightid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
    		this.baseInfoTable = "moderightinfo";

    	}else if(type.equals("modeTriggerWorkflow")){

    		this.objFieldName = "triggerworkflowsetid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
    		this.baseInfoTable = "mode_triggerworkflowset";

    	}else if(type.equals("pageExtend")){

    		this.objFieldName = "rightid";
    		this.expressionsTable = "expandBaseRightExpressions";
    		this.expressionbaseTable = "expandBaseRightExpressionbase";
    		this.baseInfoTable = "expandBaseRightInfo";

    	}else if(type.equals("workflowToMode")){

    		this.objFieldName = "workflowtomodeid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
    		this.baseInfoTable = "mode_workflowtomodeset";

    	}else if(type.equals("modeRemind")){

    		this.objFieldName = "remindid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
    		this.baseInfoTable = "mode_timedtask_detail";

    	}else if(type.equals("ganttColor")){

    		this.objFieldName = "ganttcolorid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
    		this.baseInfoTable = "mode_ganttColor";

    	}else if(type.equals("searchTransMethod")){

			this.objFieldName = "searchTransMethodId";
			this.expressionsTable = "mode_expressions";
			this.expressionbaseTable = "mode_expressionbase";
			this.baseInfoTable = "customfieldshowchange";

		}else if(type.equals("listCondition")){
			this.objFieldName = "searchcustomid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
			this.baseInfoTable = "mode_customsearch";
		}else if(type.equals("browserCondition")){
			this.objFieldName = "browsercustomid";
    		this.expressionsTable = "mode_expressions";
    		this.expressionbaseTable = "mode_expressionbase";
			this.baseInfoTable = "mode_custombrowser";
		}else if(type.equals("restfulCondition")){
    		this.objFieldName = "interfaceid";
			this.expressionsTable = "mode_expressions";
			this.expressionbaseTable = "mode_expressionbase";
			this.baseInfoTable = "CubeInterfaceConfig";
		} else if (type.equals("checkCondition")) {//xk:数据筛选
			this.objFieldName = "batchsetid";
			this.expressionsTable = "mode_expressions";
			this.expressionbaseTable = "mode_expressionbase";
			this.baseInfoTable = "mode_batchset";
		}
	}

	public boolean saveCondition(){
		boolean f = false;
		if(type.equals("workflowToMode")&&objid==0){// 保存的条件为转数据触发条件时 判断一下objid是否为0 为0时返回false 保存失败
			return false;
		}
		String isTopWhere= "";
		if(!type.equals("pageExtend")){
			isTopWhere = isTop==1?" and istop = 1":"  and istop is null";
		}
    	if(conditiontype==1){
    		f = persistenceRule2db();
		}else if(conditiontype==2){
			//------sql类型重置------

			RecordSet rs = new RecordSet();
			rs.setNoSensitveFilter(true);
			String sql = "delete from  "+expressionsTable+" where "+objFieldName+"="+objid+" "+isTopWhere;
			rs.execute(sql);
			sql = "delete from  "+expressionbaseTable+" where "+objFieldName+"="+objid+" "+isTopWhere;
			rs.execute(sql);
			conditionsql = conditionsql.replace("'","''");
			if(type.equals("modeTriggerWorkflow")){
				sql = "update "+baseInfoTable+" set conditiontype=2,showcondition='"+conditionsql+"',showconditioncn=null where id="+objid;
	    	}else if(type.equals("modeRemind")||type.equals("ganttColor")){
	    		sql = "update "+baseInfoTable+" set conditionstype=2,conditionssql='"+conditionsql+"',conditionsfieldcn=null where id="+objid;
	    	}else if(type.equals("listCondition")){
				if(isTop==1){
					sql = "update "+baseInfoTable+" set conditiontypetop=2,searchconditiontypetop=1,defaultsqltop='"+conditionsql+"',conditionsqltop='"+conditionsql+"',conditiontexttop=null where id="+objid;
				}else{
					sql = "update "+baseInfoTable+" set conditiontype=2,searchconditiontype=1,defaultsql='"+conditionsql+"',conditionsql='"+conditionsql+"',conditiontext=null where id="+objid;
				}
			}else if(type.equals("browserCondition")){
	    		sql = "update "+baseInfoTable+" set conditiontype=2,searchconditiontype=1,defaultsql='"+conditionsql+"',conditionsql='"+conditionsql+"',conditiontext=null where id="+objid;
			} else if (type.equals("checkCondition")) {
				String isfilter = StringUtils.isNotBlank(conditionsql) ? "1" : "0";
				CheckCondtionUtil.deleteOldCondition(objid);//删除旧的形式，采用新形式
				sql = "update " + baseInfoTable + " set conditiontype=2,conditionsql='" + conditionsql + "',conditiontext=null, isfilter=" + isfilter + " where id=" + objid;
			} else if(type.equals("restfulCondition")){
				sql = "update " + baseInfoTable + " set conditiontype=2,searchSqlwhere='" + conditionsql + "',conditiontext=null where id=" + objid;
			}else if(type.equals("workflowToMode")){
                sql = "update " + baseInfoTable + " set conditiontype=2,conditionsql='" + conditionsql + "',conditiontext=null where id=" + objid +" or mainid=" + objid;
            }else{
				sql = "update " + baseInfoTable + " set conditiontype=2,conditionsql='" + conditionsql + "',conditiontext=null where id=" + objid;
			}
			f = rs.execute(sql);
		}else{
			//------sql类型重置------
			RecordSet rs = new RecordSet();
			rs.setNoSensitveFilter(true);
			String sql = "delete from  "+expressionsTable+" where "+objFieldName+"="+objid+" "+isTopWhere;
			rs.execute(sql);
			sql = "delete from  "+expressionbaseTable+" where "+objFieldName+"="+objid+" "+isTopWhere;
			rs.execute(sql);
			conditionsql = conditionsql.replace("'","''");
			if(type.equals("listCondition")){
				if(isTop==1){
					sql = "update "+baseInfoTable+" set conditiontypetop=3,searchconditiontypetop=2,javafileaddresstop='"+javafileaddress+"' where id="+objid;
				}else{
					sql = "update "+baseInfoTable+" set conditiontype=3,searchconditiontype=2,javafileaddress='"+javafileaddress+"' where id="+objid;
				}
	    	}else if(type.equals("browserCondition")){
	    		sql = "update "+baseInfoTable+" set conditiontype=3,searchconditiontype=2,javafileaddress='"+javafileaddress+"' where id="+objid;
	    	}
			f = rs.execute(sql);
		}
    	if(type.equals("listCondition")){
				CustomSearchComInfo customSearchComInfo = new CustomSearchComInfo();
				customSearchComInfo.updateCache(""+objid);
		}
		if(type.equals("browserCondition")){
			ModeBrowserComInfo ModeBrowserComInfo = new ModeBrowserComInfo();
		    ModeBrowserComInfo.updateCache(objid+"");
		}

    	return f;
    }
	public boolean deleteCondition(){
		if(type.equals("workflowToMode")&&objid==0){// 保存的条件为转数据触发条件时 判断一下objid是否为0 为0时返回false 保存失败
			return false;
		}
		RecordSet RecordSet = new RecordSet();
		RecordSet.setNoSensitveFilter(true);
		String sql = "delete from  "+expressionsTable+" where "+objFieldName+"="+objid+" " ;
		RecordSet.execute(sql);
		sql = "delete from  "+expressionbaseTable+" where "+objFieldName+"="+objid+" " ;
		RecordSet.execute(sql);
		if(type.equals("modeTriggerWorkflow")){
			sql = "update "+baseInfoTable+" set conditiontype=null,showcondition=null,showconditioncn=null where id="+objid;
		}else if(type.equals("modeRemind")||type.equals("ganttColor")){
			sql = "update "+baseInfoTable+" set conditionstype=null,conditionsfield=null,conditionssql=null,conditionsfieldcn=null where id="+objid;
		}else if(type.equals("listCondition")){
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null,defaultsql=null,javafileaddress=null,searchconditiontype=1 where id="+objid ;
		}else if(type.equals("browserCondition")){
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null,defaultsql=null,javafileaddress=null,searchconditiontype=1 where id="+objid;
		}else if (type.equals("checkCondition")){
			CheckCondtionUtil.deleteOldCondition(objid);
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null,isfilter=0 where id="+objid;
		}else if(type.equals("restfulCondition")){
			sql = "update "+baseInfoTable+" set conditiontype=null,searchSqlwhere=null,conditiontext=null where id="+objid;
		}else if(type.equals("workflowToMode")){
            sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null where id=" + objid +" or mainid=" + objid;
        }else{
        	sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null where id="+objid;
        }
		RecordSet.setNoSensitveFilter(true);
		boolean issuccess = RecordSet.execute(sql);
		if(type.equals("listCondition")){
			CustomSearchComInfo customSearchComInfo = new CustomSearchComInfo();
			customSearchComInfo.updateCache(""+objid);
		}
		if(type.equals("browserCondition")){
			ModeBrowserComInfo ModeBrowserComInfo = new ModeBrowserComInfo();
			ModeBrowserComInfo.updateCache(objid+"");
		}
		return issuccess;
	}

	/**
	 * 删除置顶规则
	 * @param isTop
	 * @return
	 */
	public boolean deleteCondition(int isTop){
		if(type.equals("workflowToMode")&&objid==0){// 保存的条件为转数据触发条件时 判断一下objid是否为0 为0时返回false 保存失败
			return false;
		}
		RecordSet RecordSet = new RecordSet();
		RecordSet.setNoSensitveFilter(true);
		String sql = "delete from  "+expressionsTable+" where "+objFieldName+"="+objid+" and istop ="+isTop;
		RecordSet.execute(sql);
		sql = "delete from  "+expressionbaseTable+" where "+objFieldName+"="+objid+" and istop ="+isTop;
		RecordSet.execute(sql);
		if(type.equals("modeTriggerWorkflow")){
			sql = "update "+baseInfoTable+" set conditiontype=null,showcondition=null,showconditioncn=null where id="+objid;
		}else if(type.equals("modeRemind")||type.equals("ganttColor")){
			sql = "update "+baseInfoTable+" set conditionstype=null,conditionsfield=null,conditionssql=null,conditionsfieldcn=null where id="+objid;
		}else if(type.equals("listCondition")){
			sql = "update "+baseInfoTable+" set conditiontypetop=null,conditionsqltop=null,conditiontexttop=null,defaultsqltop=null,javafileaddresstop=null,searchconditiontypetop=1 where id="+objid;
		}else if(type.equals("browserCondition")){
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null,defaultsql=null,javafileaddress=null,searchconditiontype=1 where id="+objid;
		}else if (type.equals("checkCondition")){
			CheckCondtionUtil.deleteOldCondition(objid);
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null,isfilter=0 where id="+objid;
		}else if(type.equals("workflowToMode")){
            sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null where id=" + objid +" or mainid=" + objid;
        }else{
			sql = "update "+baseInfoTable+" set conditiontype=null,conditionsql=null,conditiontext=null where id="+objid;
		}
		boolean issuccess = RecordSet.execute(sql);
		if(type.equals("listCondition")){
			CustomSearchComInfo customSearchComInfo = new CustomSearchComInfo();
			customSearchComInfo.updateCache(""+objid);
		}
		if(type.equals("browserCondition")){
			ModeBrowserComInfo ModeBrowserComInfo = new ModeBrowserComInfo();
			ModeBrowserComInfo.updateCache(objid+"");
		}
		return issuccess;
	}

	public JSONObject checkSQL(User user){
		JSONObject jsonObject = new JSONObject();
		if(type.equals("modeRight")){
			ModeRightInfo modeRightInfo = new ModeRightInfo();
			jsonObject = JSONObject.parseObject(modeRightInfo.checkShareSQL(objid,user).toString());
		}else if(type.equals("modeTriggerWorkflow")){
			ModeDataApproval ModeDataApproval = new ModeDataApproval();
			jsonObject = JSONObject.parseObject(ModeDataApproval.checkConditionSQL(objid,user,0).toString());
    	}else if(type.equals("pageExtend")){
    		ExpandBaseRightInfo expandBaseRightInfo = new ExpandBaseRightInfo();
    		jsonObject = JSONObject.parseObject(expandBaseRightInfo.checkShareSQL(objid,user).toString());
    	}else if(type.equals("workflowToMode")){
    		jsonObject = JSONObject.parseObject(checkConditionSQL(objid,user,null).toString());
    	}else if(type.equals("modeRemind")){
    		jsonObject = checkRemindSql(user);
    	}else if(type.equals("ganttColor")){
    		jsonObject = checkGanttColorSql(user);
    	}else if(type.equals("searchTransMethod")){
			jsonObject = checkSearchTransMethodSql(user);
		} else if (type.equals("listCondition") || type.equals("browserCondition") || type.equals("checkCondition")) {
			jsonObject = checkSearchOrBrowserSql(user);
		}else if(type.equals("restfulCondition")){
			jsonObject = checkRestfulCondtion(user);
		}
		return jsonObject;
	}

	public JSONObject checkRestfulCondtion(User user){
		JSONObject jsonObj = new JSONObject();
		int modeId = 0;
		//校验错误时候不输出1=2到页面去
		String temcheckSql = "";
		String conditionsql = "";
		RecordSet rs = new RecordSet();
		String 	sql = "select modeid,searchSqlwhere,conditiontype,conditiontext from CubeInterfaceConfig where id="+objid;
		rs.execute(sql);
		if(rs.next()){
			modeId = rs.getInt("modeid");
			conditionsql = rs.getString("searchSqlwhere");
		}
		if(modeId<=0){
			return jsonObj;
		}

		FieldInfo fieldinfo = new FieldInfo();
		fieldinfo.getMainTableName(modeId);
		String tablename = fieldinfo.getTablename();
		ModeComInfo modeComInfo = new ModeComInfo();
		String formId = modeComInfo.getFormId(Util.null2String(modeId));
		String vdatasource = "";
		if(VirtualFormHandler.isVirtualForm(formId)){
			Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formId);
			vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
			String vformtype = Util.null2String(vFormInfo.get("vformtype"));
			String vsql = Util.null2String(vFormInfo.get("vsql"));
			if("2".equals(vformtype)){
				tablename="("+vsql+") cube_vsql ";
			}else{
				tablename= VirtualFormHandler.getRealFromName(tablename);
			}
		}
		temcheckSql="select 1  from " + tablename +" t1 where  ("+conditionsql+")";
		sql = "select 1  from " + tablename +" t1 where 1=2 and ("+conditionsql+")";
		boolean flag = "".equals(vdatasource)?rs.execute(sql):rs.executeSql(sql,vdatasource);
		if(flag){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
			jsonObj.put("flag", "1");
			jsonObj.put("sql", sql);
			jsonObj.put("vdatasource", vdatasource);
		}else{
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
			jsonObj.put("flag", "0");
			jsonObj.put("sql", temcheckSql);
			jsonObj.put("vdatasource", vdatasource);
		}
		return jsonObj;
	}

	/**
	 * 校验查询显示转换sql是否有效
	 * @return
	 */
	public JSONObject checkSearchTransMethodSql(User user){
		JSONObject jsonObj = new JSONObject();
		RecordSet rs = new RecordSet();
		String 	sql = "select s.formid,s.detailtable,a.conditiontype,a.conditionsql,a.conditiontext from customfieldshowchange a,mode_customsearch s where a.customid = s.id and a.id="+objid;
		rs.execute(sql);
		int formid = 0;
		int conditionstype = 0;
		String conditionsfield = "";
		String tablename = "";
		String detailtable = "";
		if(rs.next()){
			formid = rs.getInt("formid");
			conditionstype = rs.getInt("conditiontype");
			conditionsfield = rs.getString("conditionsql");
			WorkflowBillComInfo wbci = new WorkflowBillComInfo();
			tablename = wbci.getTablename(formid+"");
			detailtable = rs.getString("detailtable");
		}

		if(StringHelper.isEmpty(conditionsfield)){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
			jsonObj.put("flag", "0");
			return jsonObj;
		}else{
			CustomTreeData customTreeData = new CustomTreeData();
			customTreeData.setUser(user);
			conditionsfield = customTreeData.replaceParam(conditionsfield);//替换变量

			//替换除系统字段外的其他$字段$
			String patternString = "\\$\\w+\\$";
			Pattern pattern = Pattern.compile(patternString);
			Matcher matcher = pattern.matcher(conditionsfield);
			while(matcher.find()){
				String e=matcher.group(0);
				conditionsfield = conditionsfield.replace(e, "2021-04-20");
			}
			conditionsfield =  CubeSearchTransMethod.INSTANCE.explainDateGetByCondtionSql(conditionsfield,"",false);
		}
		//校验错误时候不输出1=2到页面去
		String temcheckSql = "select 1 ";
		String sqlwhereStrTemp1="";
		String vprimarykey = "id";
		String vdatasource = "";
		if(VirtualFormHandler.isVirtualForm(formid)){	//虚拟表单
			Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formid);
			vprimarykey = Util.null2String(vFormInfo.get("vprimarykey"));
			vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
		//	tablename = VirtualFormHandler.getRealFromName(tablename);
			String vformtype = Util.null2String(vFormInfo.get("vformtype"));
			String vsql = Util.null2String(vFormInfo.get("vsql"));
			if("2".equals(vformtype)){
				tablename="("+vsql+") cube_vsql ";
			}else{
				tablename= VirtualFormHandler.getRealFromName(tablename);
			}
		}
		String sqlwhere = " 1=2 and ("+conditionsfield+")";
		sqlwhereStrTemp1 = "("+conditionsfield+")";
		String formSql = "from "+tablename+" ";
		if(detailtable.length()>0){
			String detailTable = detailtable;
			formSql += ","+detailTable+"  ";
			sqlwhere += " and "+detailTable+".mainid="+tablename+".id ";
			sqlwhereStrTemp1+=" and "+detailTable+".mainid="+tablename+".id ";
		}
		temcheckSql +=formSql+" where "+sqlwhereStrTemp1;
		sql = "select 1  " + formSql +" where "+sqlwhere;
		boolean flag = false;
 		if(vdatasource.equals("")){
			flag = rs.execute(sql);
		}else{
			flag = rs.executeSql(sql,vdatasource);
		}
		if(flag){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
			jsonObj.put("flag", "1");
			jsonObj.put("sql", sql);
			jsonObj.put("vdatasource", vdatasource);
		}else{
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
			jsonObj.put("flag", "0");
			jsonObj.put("sql", temcheckSql);
			jsonObj.put("vdatasource", vdatasource);
		}
		return jsonObj;
	}

	public JSONObject checkGanttColorSql(User user){
		JSONObject jsonObj = new JSONObject();
		RecordSet rs = new RecordSet();
		int formid = 0;
		rs.executeQuery("select formid from mode_ganttSet where id in (select ganttid from mode_ganttColor where id=?)", objid);
		if(rs.next()){
			formid=rs.getInt("formid");
		}

		String sql  = "SELECT a.conditionstype,a.conditionsfield,a.conditionssql,b.tablename FROM mode_ganttColor a,workflow_bill b WHERE b.id='"+formid+"' and a.id="+objid;
		rs.execute(sql);
		int conditionstype = 0;
		String conditionsfield = "";
		String tablename = "";
		if(rs.next()){
			conditionstype = rs.getInt("conditionstype");
			if(conditionstype==1){
				conditionsfield = rs.getString("conditionsfield");
			}else if(conditionstype==2){
				conditionsfield = rs.getString("conditionssql");
			}
			tablename = rs.getString("tablename");
			if(VirtualFormHandler.isVirtualForm(formid)){
				tablename = VirtualFormHandler.getRealFromName(tablename);
				//如果是sql创建的虚拟表，采用实际sql语句做表名
				if("cube_vsql".equals(tablename)){
					RecordSet rs1 = new RecordSet();
					String sqltemp = "select vsql from modeformextend where formid = ?";
					rs1.executeQuery(sqltemp,formid);
					if(rs1.next()){
						tablename = "("+Util.null2String(rs1.getString("vsql"))+")";
					}
				}
			}

		}

		if(StringHelper.isEmpty(conditionsfield)){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
			jsonObj.put("flag", "0");
			return jsonObj;
		}else{
			CustomTreeData customTreeData = new CustomTreeData();
			customTreeData.setUser(user);
			conditionsfield = customTreeData.replaceParam(conditionsfield);//替换变量
		}
		//校验错误时候不输出1=2到页面去
		String temcheckSql = "select 1 ";
		String sqlwhereStrTemp1="";
		String vprimarykey = "id";
		String vdatasource = "";
		if(VirtualFormHandler.isVirtualForm(formid)){	//虚拟表单
			Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formid);
			vprimarykey = Util.null2String(vFormInfo.get("vprimarykey"));
			vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
		}
		String sqlwhere = " 1=2 and ("+conditionsfield+")";
		sqlwhereStrTemp1 = "("+conditionsfield+")";
		String formSql = "from "+tablename+" t1 ";
		temcheckSql +=formSql+" where "+sqlwhereStrTemp1;

		sql = "select 1  " + formSql +" where "+sqlwhere;
		boolean flag = false;
		if(vdatasource.equals("")){
			flag = rs.execute(sql);
		}else{
			flag = rs.executeSql(sql,vdatasource);
		}
		if(flag){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
			jsonObj.put("flag", "1");
			jsonObj.put("sql", sql);
			jsonObj.put("vdatasource", vdatasource);
		}else{
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
			jsonObj.put("flag", "0");
			jsonObj.put("sql", temcheckSql);
			jsonObj.put("vdatasource", vdatasource);
		}
		return jsonObj;
	}


	public JSONObject checkRemindSql(User user){
		JSONObject jsonObj = new JSONObject();
		RecordSet rs = new RecordSet();
		String sql  = "SELECT a.id,a.formtype,a.formid,a.conditionstype,a.conditionsfield,a.conditionssql,b.tablename FROM mode_timedtask_detail a,workflow_bill b WHERE a.formid=b.id and a.id="+objid;
		rs.execute(sql);
		int formid = 0;
		int formtype = 0;
		int conditionstype = 0;
		String conditionsfield = "";
		String tablename = "";
		if(rs.next()){
			formid = rs.getInt("formid");
			formtype = rs.getInt("formtype");
			conditionstype = rs.getInt("conditionstype");
			if(conditionstype==1){
				conditionsfield = rs.getString("conditionsfield");
			}else if(conditionstype==2){
				conditionsfield = rs.getString("conditionssql");
			}
			tablename = rs.getString("tablename");
			if(VirtualFormHandler.isVirtualForm(formid)){
				tablename = VirtualFormHandler.getRealFromName(tablename);
				//如果是sql创建的虚拟表，采用实际sql语句做表名
				if("cube_vsql".equals(tablename)){
					RecordSet rs1 = new RecordSet();
					String sqltemp = "select vsql from modeformextend where formid = ?";
					rs1.executeQuery(sqltemp,formid);
					if(rs1.next()){
						tablename = "("+Util.null2String(rs1.getString("vsql"))+")";
					}
				}
			}

		}

		if(StringHelper.isEmpty(conditionsfield)){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
			jsonObj.put("flag", "0");
			return jsonObj;
		}else{
			CustomTreeData customTreeData = new CustomTreeData();
			customTreeData.setUser(user);
			conditionsfield = customTreeData.replaceParam(conditionsfield);//替换变量
		}
		//校验错误时候不输出1=2到页面去
		String temcheckSql = "select 1 ";
		String sqlwhereStrTemp1="";
		String vprimarykey = "id";
		String vdatasource = "";
		if(VirtualFormHandler.isVirtualForm(formid)){	//虚拟表单
			Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formid);
			vprimarykey = Util.null2String(vFormInfo.get("vprimarykey"));
			vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
		}
		String sqlwhere = " 1=2 and ("+conditionsfield+")";
		sqlwhereStrTemp1 = "("+conditionsfield+")";
		String formSql = "from "+tablename+" t1 ";
		if(formtype>0){
			String detailTable = tablename+"_dt"+formtype;
			formSql += ","+detailTable+" d1 ";
			sqlwhere += " and d1.mainid=t1.id ";
			sqlwhereStrTemp1+=" and d1.mainid=t1.id ";
		}
		temcheckSql +=formSql+" where "+sqlwhereStrTemp1;

		sql = "select 1  " + formSql +" where "+sqlwhere;
		boolean flag = false;
		if(vdatasource.equals("")){
			flag = rs.execute(sql);
		}else{
			flag = rs.executeSql(sql,vdatasource);
		}
		if(flag){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
			jsonObj.put("flag", "1");
			jsonObj.put("sql", sql);
			jsonObj.put("vdatasource", vdatasource);
		}else{
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
			jsonObj.put("flag", "0");
			jsonObj.put("sql", temcheckSql);
			jsonObj.put("vdatasource", vdatasource);
		}
		return jsonObj;
	}

	//查询列表和浏览框条件检测
	public JSONObject checkSearchOrBrowserSql(User user){
		JSONObject jsonObj = new JSONObject();
		RecordSet rs = new RecordSet();
		String sql  = "";
		//校验错误时候不输出1=2到页面去
		String temcheckSql = "select 1 ";
		String sqlwhereStrTemp1="";
		if(type.equals("listCondition")){
			if(isTop ==1){
				sql = "SELECT a.id,a.detailtable,a.formid,a.searchconditiontypetop searchconditiontype,a.defaultsqltop defaultsql,a.javafileaddresstop javafileaddress,b.tablename  FROM mode_customsearch a,workflow_bill b WHERE a.formid=b.id and a.id="+objid;
			}else{
				sql = "SELECT a.id,a.detailtable,a.formid,a.searchconditiontype,a.defaultsql,a.javafileaddress,b.tablename FROM mode_customsearch a,workflow_bill b WHERE a.formid=b.id and a.id="+objid;
			}
		}else if(type.equals("browserCondition")){
			sql = "SELECT a.id,a.detailtable,a.formid,a.searchconditiontype,a.defaultsql,a.javafileaddress,b.tablename FROM mode_custombrowser a,workflow_bill b WHERE a.formid=b.id and a.id="+objid;
		} else if (type.equals("checkCondition")) {
			sql = "SELECT t2.detailtable,t2.formid,1 searchconditiontype,t1.conditionsql defaultsql,t3.tablename " +
					" FROM mode_batchSet t1,mode_customsearch t2 ,workflow_bill t3 WHERE t1.id = " + objid + " and t1.customsearchid = t2.id and t3.id = t2.formid ";
		}

		rs.execute(sql);
		int formid = 0;
		String detailtable = "";
		int searchconditiontype = 1;
		String defaultsql = "";
		String javafileaddress = "";
		String tablename = "";
		if(rs.next()){
			formid = rs.getInt("formid");
			detailtable = rs.getString("detailtable");
			searchconditiontype = rs.getInt("searchconditiontype");
			if(searchconditiontype==1){
				defaultsql = rs.getString("defaultsql");
				if(StringHelper.isEmpty(defaultsql)){
					jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
					jsonObj.put("flag", "0");
					return jsonObj;
				}
			}else if(searchconditiontype==2){
				javafileaddress = rs.getString("javafileaddress");
				if(StringHelper.isEmpty(javafileaddress)){
					jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
					jsonObj.put("flag", "0");
					return jsonObj;
				}
			}
			tablename = rs.getString("tablename");
			ModeFormComInfo modeFormComInfo = new ModeFormComInfo();
			if(VirtualFormHandler.isVirtualForm(formid)){
				 if("2".equals(modeFormComInfo.getVformtype(formid))){
	     	         	tablename =modeFormComInfo.getVsql(formid);
				 }else{
					 tablename = VirtualFormHandler.getRealFromName(tablename);
				 }
			}

		}


		boolean flag = false;
		String vprimarykey = "id";
		String vdatasource = "";
		if(searchconditiontype == 1){
			if(VirtualFormHandler.isVirtualForm(formid)){	//虚拟表单
				Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formid);
				vprimarykey = Util.null2String(vFormInfo.get("vprimarykey"));
				vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
			}
			//检测条件时，动态参数给定默认值
			/*defaultsql = defaultsql.replace("$UserId$", "1");
			defaultsql = defaultsql.replace("$DepartmentId$", "1");
			defaultsql = defaultsql.replace("$AllDepartmentId$", "1,2");
			defaultsql = defaultsql.replace("$SubcompanyId$", "1");
			defaultsql = defaultsql.replace("$AllSubcompanyId$", "1,2");
			defaultsql = defaultsql.replace("$date$", "2019-09-18");
			defaultsql = defaultsql.replace("$WorkCode$", "123456");*/
			//正则表达式替换PARM(*)
			String patternString = "PARM\\([\\w]*[\\w]\\)";
			Pattern pattern = Pattern.compile(patternString);
			Matcher matcher = pattern.matcher(defaultsql);
			while(matcher.find()){
				String e=matcher.group(0);
				defaultsql = defaultsql.replace(e, "1");
			}


			CustomTreeData customTreeData = new CustomTreeData();
			customTreeData.setUser(user);
			defaultsql = customTreeData.replaceParam(defaultsql);//替换变量

			String sqlwhere = " 1=2 and ("+defaultsql+")";
			sqlwhereStrTemp1 = "("+defaultsql+")";
			String formSql = "from "+tablename+" t1 ";
			if(!StringHelper.isEmpty(detailtable)){
				formSql += ","+detailtable+" d1 ";
				sqlwhere += " and d1.mainid=t1.id ";
				sqlwhereStrTemp1 +=" and d1.mainid=t1.id ";
			}
			temcheckSql +=formSql+" where "+sqlwhereStrTemp1;
			sql = "select 1  " + formSql +" where "+sqlwhere;
			if(vdatasource.equals("")){
				flag = rs.execute(sql);
			}else{
				flag = rs.executeSql(sql,vdatasource);
			}
		}else if (searchconditiontype == 2) {
	            try {
	                if (javafileaddress.endsWith(".java")) {
	                    javafileaddress = javafileaddress.substring(0, javafileaddress.length() - 5);
	                }
	                if (javafileaddress.endsWith(".class")) {
	                    javafileaddress = javafileaddress.substring(0, javafileaddress.length() - 6);
	                }
	                Class cl = Class.forName(javafileaddress);
	                //ICustomJavaCode customJavaCode = (ICustomJavaCode)cl.newInstance();
	                //customJavaCode.execute();
	                flag=true;
	            }catch(Exception e){
	            	flag=false;
	            }

		}
		if(flag){
			jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
			jsonObj.put("flag", "1");
			jsonObj.put("sql", sql);
			jsonObj.put("vdatasource", vdatasource);
		}else{
			jsonObj.put("flag", "0");
			if(searchconditiontype == 1){
				jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
				jsonObj.put("sql", temcheckSql);
				jsonObj.put("vdatasource", vdatasource);
			}else{
				jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
				jsonObj.put("javafileaddress", javafileaddress);
			}
		}
		return jsonObj;
	}

	public JSONObject checkConditionSQL(int objid, User user, Map map) {
		JSONObject jsonObj = new JSONObject();
		WorkflowToMode WorkflowToMode = new WorkflowToMode();
			int requestid = 0;
			int detailid = 0;
			if(map!=null&&map.containsKey("requestid")){
				requestid = Util.getIntValue(Util.null2String(map.get("requestid")),0);
				if(map.containsKey("detailid")){
					detailid = Util.getIntValue(Util.null2String(map.get("detailid")),0);
				}
			}

			RecordSet rs1 = new RecordSet();
			String sql1 = "select a.formid,b.tablename,c.modeid,c.conditionsql,formtype from workflow_base a,workflow_bill b,mode_workflowtomodeset c where a.id=c.workflowid and a.formid=b.id and c.id="+objid;
			rs1.executeSql(sql1);
			int formid = 0;
			String maintableName = "";
			String conditionsql = "";
			String formtype = "";
			if(map!=null&&map.containsKey("formtype")){
				formtype = Util.null2String(map.get("formtype"));
			}
			if(rs1.next()){
				formid = rs1.getInt("formid");
				maintableName =  rs1.getString("tablename");
				if(VirtualFormHandler.isVirtualForm(formid)){
					maintableName = VirtualFormHandler.getRealFromName(maintableName);
				}
				conditionsql = rs1.getString("conditionsql");
			}
			if(StringHelper.isEmpty(conditionsql)){
				jsonObj.put("msg", SystemEnv.getHtmlLabelName(390010,user.getLanguage()));//条件为空
				jsonObj.put("flag", "0");
				jsonObj.put("sql", "");
				jsonObj.put("count", 0);
				return jsonObj;
			}else{
				CustomTreeData customTreeData = new CustomTreeData();
				customTreeData.setUser(user);
				conditionsql = customTreeData.replaceParam(conditionsql);//替换变量
			}

			//触发表单，maintable：主表；detail1：  明细表1；detail2：  明细表2
			String sqlwhere = "";
			if("maintable".equals(formtype)){
				formtype = "";
			}else if(!StringHelper.isEmpty(formtype)&&formtype.startsWith("detail")){
				formtype = formtype.replace("detail","");
				formtype = maintableName+"_dt"+formtype;
				sqlwhere = " and tablename='"+formtype+"' ";
			}

			sql1 = "select tablename from Workflow_billdetailtable where billid="+formid+" "+sqlwhere+" order by orderid";
			rs1.executeSql(sql1);
			String tablenameStr = maintableName +" "+maintableName;
			String sqlwhereStr = "";
			List subtableList = new ArrayList();
			while(rs1.next()){
				String detailtablename = rs1.getString("tablename");
				subtableList.add(detailtablename);
			}
			String vprimarykey = "id";
			String vdatasource = "";

			if(VirtualFormHandler.isVirtualForm(formid)){	//虚拟表单
				Map<String, Object> vFormInfo = VirtualFormHandler.getVFormInfo(formid);
				vprimarykey = Util.null2String(vFormInfo.get("vprimarykey"));
				vdatasource = Util.null2String(vFormInfo.get("vdatasource"));
			}

			//校验错误时候不输出1=2到页面去
			String temcheckSql = "select 1  from "+tablenameStr+" where ";
			String sqlwhereStrTemp1="";

			Map tempMap = WorkflowToMode.getSqlParam(maintableName,0,subtableList,conditionsql,vprimarykey,"0",requestid,detailid);
			tablenameStr = StringHelper.null2String(tempMap.get("tablenameStr"));
			String sqlwhereStrTemp = StringHelper.null2String(tempMap.get("sqlwhereStr"));
			if(!sqlwhereStrTemp.equals("")){
				sqlwhereStrTemp1 = "  ("+sqlwhereStrTemp+")";
				sqlwhereStrTemp = " and ("+sqlwhereStrTemp+")";
				temcheckSql+=sqlwhereStrTemp1+" and ("+conditionsql+")";
			}else{
				temcheckSql+=" ("+conditionsql+")";
			}

			boolean flag = false;
			String befStr = "1=2";
			if(requestid>0){
				befStr = "1=1";
			}
			String checkSql = "select 1  from "+tablenameStr+" where "+befStr+" "+sqlwhereStrTemp+" and ("+conditionsql+")";
			RecordSet rs2 = new RecordSet();
			if(vdatasource.equals("")){
				flag = rs2.executeSql(checkSql);
			}else{
				flag = rs2.executeSql(checkSql,vdatasource);
			}
			if(flag){
				jsonObj.put("msg", SystemEnv.getHtmlLabelName(387162, user.getLanguage()));//"条件检测通过"
				jsonObj.put("flag", "1");
				jsonObj.put("sql", checkSql);
				jsonObj.put("vdatasource", vdatasource);
				jsonObj.put("count", rs2.getCounts());
			}else{
				jsonObj.put("msg", SystemEnv.getHtmlLabelName(390011,user.getLanguage()));//"错误：条件检测未通过"
				jsonObj.put("flag", "0");
				jsonObj.put("sql", temcheckSql);
				jsonObj.put("vdatasource", vdatasource);
				jsonObj.put("count", 0);
			}
		return jsonObj;
	}


    public int persistence2db(RecordSetTrans rs,String objid,JSONObject node) throws Exception {
		rs.setNoSensitveFilter(true);
    	this.objid = Util.getIntValue(objid);
        List<String> childids = new ArrayList<String>();
        JSONArray datas = node.getJSONArray("datas");
        String relationship = node.getString("relationship");
        if(datas!=null){//增加判空逻辑
			for (int i=0; i<datas.size(); i++) {
				JSONObject child = datas.getJSONObject(i);
				String isParent = child.getString("isParent");
				if (isParent.equals("true")) {
					int tempRuleID = persistence2db(rs,objid,child);
					childids.add(String.valueOf(tempRuleID));
				} else {
					int tempRuleID = persistence2dbBase(rs,objid,child);
					childids.add(String.valueOf(tempRuleID));
				}
			}
		}
        int expsid = 0;
        String relationRuleID = listToString(childids);
        if(type.equals("pageExtend")){//expandBaseRightExpressions  id为自增长
            String sql = "insert into "+expressionsTable+"("+objFieldName+", relation, expids) values ( " + this.objid + ", " + relationship + ", '" + relationRuleID + "')";
         	rs.execute(sql);
         	sql = "select max(id) id from "+expressionsTable+" where rightid = ? and relation = ? and expids = ? ";
         	rs.executeQuery(sql, this.objid, relationship, relationRuleID);
         	if(rs.next()){
         		expsid = Util.getIntValue(rs.getString("id"), 0);
         	}
        }else{
			expsid = getdbidWithTable(""+expressionsTable+"");//表id不是 自增长
			if(isTop!=1){
				String sql = "insert into "+expressionsTable+"(id, "+objFieldName+", relation, expids) values (" + expsid + ", " + this.objid + ", " + relationship + ", '" + relationRuleID + "')";
				rs.execute(sql);
			}else{
				String sql = "insert into "+expressionsTable+"(id, "+objFieldName+", relation, expids,isTop) values (" + expsid + ", " + this.objid + ", " + relationship + ", '" + relationRuleID + "',"+isTop+")";
				rs.execute(sql);
			}
        }
        return expsid;
    }

	private boolean persistenceRule2db(){
		if(type.equals("workflowToMode")&&objid==0){// 保存的条件为转数据触发条件时 判断一下objid是否为0 为0时返回false 保存失败
			return false;
		}
		JSONObject root = JSONObject.parseObject(rootdata);
		boolean f = false;
		RecordSetTrans rs = new RecordSetTrans();
		rs.setNoSensitveFilter(true);
		rs.setAutoCommit(false);
		String isTopWhere="";
		if(!type.equals("pageExtend")){
			isTopWhere = isTop==1?" and istop = 1":"  and istop is null";
		}
		try {
			String sql = "delete from "+expressionsTable+" where "+objFieldName+"=" + objid+" "+isTopWhere;
			rs.executeSql(sql);
			sql = "delete from "+expressionbaseTable+" where "+objFieldName+"=" + objid+" "+isTopWhere;
			rs.executeSql(sql);
			persistence2db(rs,objid+"",root);
			conditionsql = conditionsql.replace("'", "''");
			conditiontext = conditiontext.replace("'", "''");
			if(type.equals("modeTriggerWorkflow")){
				sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",showcondition='"+conditionsql+"',showconditioncn='"+conditiontext+"' where id="+objid;
			}else if(type.equals("modeRemind")||type.equals("ganttColor")){
				sql = "update "+baseInfoTable+" set conditionstype="+conditiontype+",conditionsfield='"+conditionsql+"',conditionsfieldcn='"+conditiontext+"' where id="+objid;
			}else if(type.equals("listCondition")){
				if(isTop==1){
					sql = "update "+baseInfoTable+" set conditiontypetop="+conditiontype+",searchconditiontypetop=1,defaultsqltop='"+conditionsql+"',conditionsqltop='"+conditionsql+"',conditiontexttop='"+conditiontext+"' where id="+objid;
				}else{
					sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",searchconditiontype=1,defaultsql='"+conditionsql+"',conditionsql='"+conditionsql+"',conditiontext='"+conditiontext+"' where id="+objid;
				}
			}else if(type.equals("browserCondition")){
				sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",searchconditiontype=1,defaultsql='"+conditionsql+"',conditionsql='"+conditionsql+"',conditiontext='"+conditiontext+"' where id="+objid;
	    	}else if (type.equals("checkCondition")) {
				CheckCondtionUtil.deleteOldCondition(objid);//删除旧的形式，采用新形式
				String isfilter = StringUtils.isNotBlank(conditionsql)&&StringUtils.isNotBlank(conditiontext) ? "1" : "0";
				sql = "update " + baseInfoTable + " set conditiontype=" + conditiontype + ",conditionsql='" + conditionsql + "',conditiontext='" + conditiontext + "', isfilter=" + isfilter + " where id=" + objid;
			}else if(type.equals("restfulCondition")){
				sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",searchSqlwhere='"+conditionsql+"',conditiontext='"+conditiontext+"' where id="+objid;
			}else if(type.equals("workflowToMode")){
                sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",conditionsql='"+conditionsql+"',conditiontext='"+conditiontext+"' where id="+objid+" or mainid=" + objid;
            }else{
				sql = "update "+baseInfoTable+" set conditiontype="+conditiontype+",conditionsql='"+conditionsql+"',conditiontext='"+conditiontext+"' where id="+objid;
			}
			f = rs.executeSql(sql);
		} catch (Exception e) {
			rs.writeLog(e);
			rs.rollback();
		}
		rs.commit();
		if(type.equals("listCondition")){
			CustomSearchComInfo customSearchComInfo = new CustomSearchComInfo();
			customSearchComInfo.updateCache(""+objid);
		}
		if(type.equals("browserCondition")){
			ModeBrowserComInfo ModeBrowserComInfo = new ModeBrowserComInfo();
		    ModeBrowserComInfo.updateCache(objid+"");
		}
		return f;
	}

    /**
     * @Description:把list转换为一个用逗号分隔的字符串
     */
    public static String listToString(List list) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i < list.size() - 1) {
                    sb.append(list.get(i) + ",");
                } else {
                    sb.append(list.get(i));
                }
            }
        }
        return sb.toString();
    }

    /**
     * 持久化表达式
     * @param rs
     * @return
     * @throws Exception
     */
    public int persistence2dbBase(RecordSetTrans rs,String objid,JSONObject base) throws Exception {
        String sql = null;
		rs.setNoSensitveFilter(true);
        int id = getdbidWithTable(""+expressionbaseTable+"");
        int fieldid = base.getIntValue("fieldid");
        String fieldname = base.getString("fieldname");
        String fieldlabel = base.getString("fieldlabel");
        int compareopion = base.getIntValue("compareopion");
        String compareopionlabel = base.getString("compareopionlabel");
        int htmltype = base.getIntValue("htmltype");
        int fieldtype = base.getIntValue("fieldtype");
        String fielddbtype = base.getString("fielddbtype");
        String fieldvalue = base.getString("fieldvalue");
        String fieldtext = base.getString("fieldtext");
        int relationtype = base.getIntValue("relationtype");
        int valetype = base.getIntValue("valetype");
        int expsid = 0;
        int conditiontype4sb = Util.getIntValue(base.getString("conditiontype"),0);
        int oldconditiontype4sb = base.getIntValue("oldconditiontype");
        String fieldOldvalue = base.getString("fieldOldvalue");
        if(type.equals("pageExtend")){//expandBaseRightExpressions  id为自增长
        	sql = "insert into "+expressionbaseTable+"(fieldid, fieldname, fieldlabel, "+objFieldName+", compareopion, compareopionlabel, htmltype, fieldtype,fielddbtype, fieldvalue, fieldtext, relationtype,valetype,fieldparentvalue) " +
        			" values("+fieldid+",'"+fieldname+"','"+fieldlabel+"',"+this.objid+","+compareopion+",'"+compareopionlabel+"',"+htmltype+","+fieldtype+",'"+fielddbtype+"','"+fieldvalue+"','"+fieldtext+"',"+relationtype+","+valetype+",'"+fieldOldvalue+"')";
        	rs.executeSql(sql);
        	sql = "select max(id) id from "+expressionbaseTable+" where fieldid = ? and rightid = ? ";
        	rs.executeQuery(sql, fieldid, this.objid);
        	if(rs.next()){
         		expsid = Util.getIntValue(rs.getString("id"), 0);
         	}
        	sql = "insert into "+expressionsTable+"(rightid, relation, expbaseid) values (" + this.objid + ", " + -1 + ", " + expsid+ ")";
            rs.executeSql(sql);

            sql = "select max(id) id from "+expressionsTable+" where rightid = ? and expbaseid = ? ";
            rs.executeQuery(sql, this.objid, expsid);
            rs.next();
            return Util.getIntValue(rs.getString(1), 0);

        }else if(type.equals("listCondition") || type.equals("browserCondition")){
        	if(isTop!=1){
				sql = "insert into "+expressionbaseTable+"(id, fieldid, fieldname, fieldlabel, "+objFieldName+", compareopion, compareopionlabel, htmltype, fieldtype,fielddbtype, fieldvalue, fieldtext, relationtype,valetype,fieldparentvalue,conditiontype4sb,oldconditiontype4sb) " +
						" values("+id+","+fieldid+",'"+fieldname+"','"+fieldlabel+"',"+this.objid+","+compareopion+",'"+compareopionlabel+"',"+htmltype+","+fieldtype+",'"+fielddbtype+"','"+fieldvalue+"','"+fieldtext+"',"+relationtype+","+valetype+",'"+fieldOldvalue+"',"+conditiontype4sb+","+oldconditiontype4sb+")";
				rs.executeSql(sql);
				expsid = ShareExpressions.getdbid(rs);
				sql = "insert into "+expressionsTable+"(id, "+objFieldName+", relation, expbaseid) values (" + expsid + ", " + this.objid + ", " + -1 + ", " + id + ")";
				rs.executeSql(sql);
			}else{
				sql = "insert into "+expressionbaseTable+"(id, fieldid, fieldname, fieldlabel, "+objFieldName+", compareopion, compareopionlabel, htmltype, fieldtype,fielddbtype, fieldvalue, fieldtext, relationtype,valetype,fieldparentvalue,conditiontype4sb,oldconditiontype4sb,istop) " +
						" values("+id+","+fieldid+",'"+fieldname+"','"+fieldlabel+"',"+this.objid+","+compareopion+",'"+compareopionlabel+"',"+htmltype+","+fieldtype+",'"+fielddbtype+"','"+fieldvalue+"','"+fieldtext+"',"+relationtype+","+valetype+",'"+fieldOldvalue+"',"+conditiontype4sb+","+oldconditiontype4sb+","+isTop+")";
				rs.executeSql(sql);
				expsid = ShareExpressions.getdbid(rs);
				sql = "insert into "+expressionsTable+"(id, "+objFieldName+", relation, expbaseid,isTop) values (" + expsid + ", " + this.objid + ", " + -1 + ", " + id +","+isTop+ ")";
				rs.executeSql(sql);
			}

        }else{
        	sql = "insert into "+expressionbaseTable+"(id, fieldid, fieldname, fieldlabel, "+objFieldName+", compareopion, compareopionlabel, htmltype, fieldtype,fielddbtype, fieldvalue, fieldtext, relationtype,valetype,fieldparentvalue) " +
        			" values("+id+","+fieldid+",'"+fieldname+"','"+fieldlabel+"',"+this.objid+","+compareopion+",'"+compareopionlabel+"',"+htmltype+","+fieldtype+",'"+fielddbtype+"','"+fieldvalue+"','"+fieldtext+"',"+relationtype+","+valetype+",'"+fieldOldvalue+"')";
        	rs.executeSql(sql);
        	expsid = ShareExpressions.getdbid(rs);
        	sql = "insert into "+expressionsTable+"(id, "+objFieldName+", relation, expbaseid) values (" + expsid + ", " + this.objid + ", " + -1 + ", " + id + ")";
        	rs.executeSql(sql);
        }

        return expsid;
    }



    /**
     * 获取自增长id
     * @param //rs
     * @return
     * @throws Exception
     */
    public synchronized static int getdbidWithTable(String tablename) throws Exception {
    	synchronized (ShareExpressions.class) {
    		RecordSet rs = new RecordSet();
    		int result = 0;
    		rs.execute("select max(id) as id from "+tablename);
    		if (rs.next()) {
				//eg 	mode_expressionbase 	maxid   985
				//		mode_expressionbase_tid	maxid	983
    			result = Util.getIntValue(rs.getString("id"), 1);
    		}
    		int pkValStart = result+1; //986
    		String tempTable = tablename+"_tid";
    		if("expandBaseRightExpressions".equals(tablename)){
    			tempTable = "expandBaseRightExp_tid";
    		}
    		if("expandBaseRightExpressionbase".equals(tablename)){
    			tempTable = "expandBaseRightExpBase_tid";
    		}
    		createTempIdTable(tempTable,pkValStart);
    		UUID uuid  =  UUID.randomUUID();
    		String uuidStr = uuid.toString().replaceAll("-", "");
    		String insertSql = "insert into "+tempTable+"(uuid) values(?)";
    		rs.executeUpdate(insertSql,uuidStr);
    		String selectSql = "select id from "+tempTable+" where uuid= ? ";
    		rs.executeQuery(selectSql,uuidStr);
    		if (rs.next()) {
    			result = Util.getIntValue(rs.getString("id"), 1);
    		}
    		while (result<pkValStart){
				uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
				rs.executeUpdate(insertSql,uuidStr);
				rs.executeQuery(selectSql,uuidStr);
				if (rs.next()) {
					result = Util.getIntValue(rs.getString("id"), 1);
				}
			}
    		return result;
    	}
    }





    /**
	 * id自增长表
	 */
	public static void createTempIdTable(String tablename ,int pkStart){
		synchronized (ShareExpressions.class) {
			if(tempTableList.contains(tablename)){
				return;
			}
			RecordSet rs = new RecordSet();
			String sql = "";
			if(rs.getDBType().equalsIgnoreCase("oracle")){
				sql = "select distinct 1 from user_tab_columns where table_name =upper('"+tablename+"')";
			}else if(rs.getDBType().equals("mysql")){
				String dbname = CommonConstant.DB_MYSQL_SCHEMA;
				sql = "select 1 from INFORMATION_SCHEMA.TABLES where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') ";
			}
			else if(rs.getDBType().equals("postgresql"))
				sql = "select 1 from INFORMATION_SCHEMA.TABLES where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') ";
			else if(rs.getDBType().toLowerCase().indexOf("sqlserver")>-1){
				sql = "select 1 from sysobjects where name='"+tablename+"'";
			}
			rs.execute(sql);
			if(rs.next()){
				tempTableList.add(tablename);
				return;
			}

			boolean fly = false;
			try {
				if(rs.getDBType().equalsIgnoreCase("oracle")){
					sql = " create table "+tablename+"( "+
					"  	id	 		integer,"+
					"  	uuid	 	varchar2(40)"+
					") ";
					fly = rs.execute(sql);
					sql = "ALTER TABLE "+tablename+" ADD CONSTRAINT PK_"+tablename+" PRIMARY KEY ( id ) ";
					if(fly){
						fly = rs.execute(sql);
					}
					String appendSql = "";
					appendSql = " MAXVALUE 9223372036854775807 ";
					sql = " create sequence "+tablename+"_id "+
					"  start with  "+pkStart+
					"  increment by 1 "+
					appendSql +
					"  nocycle ";
					if(fly){
						fly = rs.execute(sql);
					}
					sql = " create or replace trigger "+tablename+"_Tri "+
					"  before insert on "+tablename+
					"  for each row " +
					"  begin "+
					"  select "+tablename+"_id.nextval into :new.id from dual; " +
					"  end; ";
					if(fly){
						rs.setChecksql(false);
						fly = rs.execute(sql);
					}
				}else if(rs.getDBType().equalsIgnoreCase("mysql")){
					sql = "create table "+tablename+"("+
					"  	id int not null auto_increment  primary key, "+
					"  	uuid	 	varchar(40)"+
					") ";
					fly = rs.execute(sql);
					if(fly){
						sql = "alter table "+tablename+" AUTO_INCREMENT="+pkStart;
						fly = rs.execute(sql);
					}
				}
				else if(rs.getDBType().equalsIgnoreCase("postgresql")){
					sql = "create table "+tablename+"("+
							"  	id serial  primary key, "+
							"  	uuid	 	varchar(40)"+
							") ";
					fly = rs.execute(sql);
					if(fly){
						sql = "alter table "+tablename+" AUTO_INCREMENT="+pkStart;
						fly = rs.execute(sql);
					}
				}
				else{
					sql = "create table "+tablename+"("+
					"  	id	 int IDENTITY("+pkStart+",1) NOT NULL,"+
					"  	uuid	varchar(40)"+
					"		CONSTRAINT PK_"+tablename+" PRIMARY KEY NONCLUSTERED (id ASC) "+
					") ";
					fly = rs.execute(sql);
				}
				if(fly){
					tempTableList.add(tablename);
				}
			} catch (RuntimeException e) {
				rs.writeLog(e);
			}
		}
	}




	//----------------------------以下为获取数据---------------------------------
	public Map<String, Object> getConditionInfo(User user,int formId){
		return getConditionInfo(user,formId,"");
	}

	public Map<String, Object> getConditionInfo(User user,int formId,String searchDetailtable){
    	Map<String, Object> result = new HashMap<String, Object>();
    	RecordSet rs = new RecordSet();

		ModeLayoutUtil ModeLayoutUtil = new  ModeLayoutUtil();
		ModeLayoutUtil.setUser(user);
		ModeLayoutUtil.setFormId(formId);
		String mainTablename = "";
		String mainTablenameTemp = "";
		RecordSet RecordSet = new RecordSet();
		RecordSet.execute("select tablename from workflow_bill where id="+formId);
		if(RecordSet.next()){
			mainTablename = RecordSet.getString("tablename");
		}
		if(VirtualFormHandler.isVirtualForm(formId)){
			mainTablename = VirtualFormHandler.getRealFromName(mainTablename);
		}
		int formtype = 0;
		if(type.equals("modeRemind")){//提醒
			String sql  = "SELECT formtype FROM mode_timedtask_detail where id="+objid;
			rs.execute(sql);
			if(rs.next()){
				formtype = rs.getInt("formtype");
			}
		}
		/*Map fieldsmap = new HashMap();
		if(searchDetailtable.length()>0){
			fieldsmap = ModeLayoutUtil.getCustomSearchfieldsByDetailTable(customid,user.getLanguage(),searchDetailtable);
		}else{
			fieldsmap = ModeLayoutUtil.getFormfields(user.getLanguage(),1);
		}

		List mainfields = (List)fieldsmap.get("mainfields");				//主表字段
		List detlfields = (List)fieldsmap.get("detlfields");				//子表字段
		List detailGroup = (List)fieldsmap.get("detailGroup");				//子表组信息
		Map detailMap = new HashMap();
		for(int i=0;i<detailGroup.size();i++){
			Map tempMap = (Map)detailGroup.get(i);
			String detailtable = tempMap.get("detailtable")+"";
			detailMap.put(detailtable,tempMap);
		}
		Map indexMap = ModeLayoutUtil.getIndexMap();
		*/
		//***********************
		int conditiontype = 1;
		String conditionsql = "";
		String conditiontext = "";
		String javafileaddress = "";
		String sql = "";
		String rootdata ="";

		boolean isNeedDetail = true;
		if(type.equals("modeTriggerWorkflow")){
			sql = "select conditiontype,showcondition as conditionsql,showconditioncn as conditiontext  "
    				+ " from "+baseInfoTable+" where id="+objid;
    	}else if(type.equals("modeRemind")||type.equals("ganttColor")){
    		sql = "select conditionstype as conditiontype,conditionsfield,conditionssql as conditionsql,conditionsfieldcn as conditiontext  "
    				+ " from "+baseInfoTable+" where id="+objid;
    	}else if(type.equals("listCondition")){
    		//sql = "select conditiontype,conditionsql,conditiontext,searchconditiontype,defaultsql,javafileaddress,detailtable from "+baseInfoTable+" where id="+customid+" and istop is null";
			sql = "select conditiontype,conditionsql,conditiontext,searchconditiontype,defaultsql,javafileaddress,detailtable from "+baseInfoTable+" where id="+customid;
    	}else if(type.equals("browserCondition")){
    		sql = "select conditiontype,conditionsql,conditiontext,searchconditiontype,defaultsql,javafileaddress,detailtable from "+baseInfoTable+" where id="+customid;
    	}else if(type.equals("restfulCondition")){
			sql = "select conditiontype,searchSqlwhere as conditionsql,conditiontext from "+baseInfoTable+" where id="+objid;
		}else if(type.equals("WfBrowser")){//数据定义
			sql = "select conditiontype,conditionsql,conditiontext,rootdata from mode_data_definition ";
			if (nodeid > 0) {//数据定义,树节点信息
				sql += " where fieldid=" + objid + " and modeid =" + modeid+" and nodeid="+nodeid;
			} else {
				sql += " where fieldid=" + objid + " and modeid =" + modeid;
			}
		}else{
        	sql = "select conditiontype,conditionsql,conditiontext from "+baseInfoTable+" where id="+objid;
        }

		RecordSet.execute(sql);
		if(RecordSet.next()){
			conditiontype = Util.getIntValue(RecordSet.getString("conditiontype"),0);
			int searchconditiontype = Util.getIntValue(RecordSet.getString("searchconditiontype"),0);
			rootdata=Util.null2String(RecordSet.getString("rootdata"));
			conditionsql = RecordSet.getString("conditionsql");
			javafileaddress = RecordSet.getString("javafileaddress");
			if(type.equals("modeRemind")&&conditiontype==1){
				conditionsql = RecordSet.getString("conditionsfield");
			}
			if(conditiontype==0 && searchconditiontype==1){//等于零代表空数据 默认为1
				conditiontype = 2;
				conditionsql = RecordSet.getString("defaultsql");
				if(conditionsql.equals("")){
					conditiontype = 1;
				}
			}else if(conditiontype==0 && searchconditiontype==2){
				conditiontype = 3;
				//conditionsql = RecordSet.getString("defaultsql");
				if(javafileaddress.equals("")){
					conditiontype = 1;
				}
			}else if(conditiontype==0){
				conditiontype = 1;
			}
			conditiontext = RecordSet.getString("conditiontext");

			//
			if(type.equals("listCondition") || type.equals("browserCondition")){
				String detailtablename = RecordSet.getString("detailtable");
				if(detailtablename.equals("")){
					isNeedDetail = false;
				}else{
					searchDetailtable = detailtablename;
				}
			}
		}
		Map fieldsmap = new HashMap();
		if(searchDetailtable.length()>0){
			fieldsmap = ModeLayoutUtil.getCustomSearchfieldsByDetailTable(customid,user.getLanguage(),searchDetailtable);
		}else{
			fieldsmap = ModeLayoutUtil.getFormfields(user.getLanguage(),1);
		}
		List mainfields = (List)fieldsmap.get("mainfields");				//主表字段
		List detlfields = (List)fieldsmap.get("detlfields");				//子表字段
		List detailGroup = (List)fieldsmap.get("detailGroup");				//子表组信息
		Map detailMap = new HashMap();
		for(int i=0;i<detailGroup.size();i++){
			Map tempMap = (Map)detailGroup.get(i);
			String detailtable = tempMap.get("detailtable")+"";
			detailMap.put(detailtable,tempMap);
		}
		Map indexMap = ModeLayoutUtil.getIndexMap();
		int count = 0;
		if(!"WfBrowser".equals(type)){
			sql = "select count(1) as count from "+expressionsTable+" where "+objFieldName+"='"+objid+"'";
			RecordSet.execute(sql);
			if(RecordSet.next()){
				count = RecordSet.getInt("count");
			}
			if(count==0){
				count = 1;
			}
		}
		ModeFormFieldEncryptComInfo formFieldEncryptComInfo=new ModeFormFieldEncryptComInfo();
		JSONArray fieldArray = new  JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("key", "");
		obj.put("showname", "");
		fieldArray.add(obj);
		for(int i = 0; i < mainfields.size(); i++){
			 Map maps = (Map)mainfields.get(i);
			 String fieldid = (String)maps.get("fieldid");
			 String fieldlabel = (String)maps.get("fieldlabel");
			 String viewtype = (String)maps.get("viewtype");
			 String fieldname = (String)maps.get("fieldname");
			 if ("seclevel".equals(((String) maps.get("fieldname") + "").toLowerCase())) {
				continue;
			 }
			 String fieldhtmltype = (String)maps.get("fieldhtmltype");
			 String type = (String)maps.get("type");
			 String fielddbtype=(String)maps.get("fielddbtype");
			 String qfws=(String)maps.get("qfws");
			if (this.type.equals("WfBrowser")) {
				if ("256".equals(browserType) || "257".equals(browserType)) {
					if (!this.tablename.equals(mainTablename)) {
						continue;
					}
				}
			}
			 if(fieldhtmltype.equals("6")||fieldhtmltype.equals("7")){//附件或特殊字段
				continue;
			}
			if(fieldhtmltype.equals("2")&&type.equals("2")){//格式文本
				continue;
			}

			 String relationtype = "1";//关系类型---单行文本文本  +  单行文本金额千分位  +  多行文本
			 if(fieldhtmltype.equals("1")&&(type.equals("2")||type.equals("3"))){//整数  +  浮点数
				 relationtype =  "2";
			 }


			 if((fieldhtmltype.equals("1")&&type.equals("4"))||fieldhtmltype.equals("5")){// 单行文本金额转换   +  选择项
				 relationtype = "3";
			 }

			 if(fieldhtmltype.equals("3")){//浏览框
				 if(type.equals("2")||type.equals("19")||type.equals("290")||type.equals("402") || type.equals("403")){//日期  + 时间
					 relationtype = "2";
				 }else{
					 relationtype = "4";
				 }
			 }

			 if(fieldhtmltype.equals("4")){//checkbox
				 relationtype = "4";
			 }

			 String valueshowtype = "1";//单行文本输入框

			 if(fieldhtmltype.equals("3")){
				 if(type.equals("2")){//日期
					 valueshowtype = "2";
				 }else if(type.equals("19") || type.equals("290")){//时间
					 valueshowtype = "3";
				 }else if(type.equals("402") || type.equals("403")){//年、年月
                     valueshowtype = "2";
                 }else{
					 valueshowtype = "4";//浏览框--需动态加载
				 }
			 }

			 if(fieldhtmltype.equals("5")){//select框--需动态加载
				 valueshowtype = "5";
			 }

			 if(fieldhtmltype.equals("4")){//checkbox
				 valueshowtype = "6";//select固定值  0  1  null
			 }
			 mainTablenameTemp = mainTablename;
			if (this.type.equals("ganttColor") ||this.type.equals("modeRemind") || this.type.equals("listCondition") ||
					this.type.equals("browserCondition") || this.type.equals("checkCondition")||this.type.equals("WfBrowser") ||this.type.equals("restfulCondition")) {
				mainTablenameTemp = "t1";
			}
			 String fieldencrypt = formFieldEncryptComInfo.getIsencrypt(fieldid);
			 if(!"1".equals(fieldencrypt)){
				 EncryptFieldEntity encryptFieldEntity = new EncryptFieldConfigComInfo().getFieldEncryptConfig(mainTablename, fieldname);
			     if(encryptFieldEntity != null && ("1".equals(encryptFieldEntity.getDesensitization()) || "1".equals(encryptFieldEntity.getIsEncrypt()))) fieldencrypt="1";
			 }
			 fieldArray.add(BrowserHelper.constructMap(
					 "key", fieldid
					 ,"showname", fieldlabel
					 ,"tablename", mainTablenameTemp
					 ,"fieldname", fieldname
					 ,"fieldid", fieldid
					 ,"fieldhtmltype", fieldhtmltype
					 ,"type", type
					 ,"fielddbtype", fielddbtype
					 ,"relationtype", relationtype
					 ,"valueshowtype", valueshowtype
					 ,"qfws", qfws,"isencrypt",fieldencrypt)
					);
		 }
		if(!type.equals("workflowtomode")&&!type.equals("WfBrowser")){
			String dbtype = RecordSet.getDBType().toLowerCase();
			HashMap columMap = new HashMap();
			if(dbtype.equalsIgnoreCase("oracle")) {
	            sql = "select COLUMN_NAME from user_tab_columns where table_name=upper('" + mainTablename + "') ORDER BY COLUMN_ID";
	        }else if(dbtype.equalsIgnoreCase("mysql")){
				String dbname = CommonConstant.DB_MYSQL_SCHEMA;
				sql = "select column_name from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+mainTablename+"') order by ordinal_position";
			}
			else if(dbtype.equalsIgnoreCase("postgresql"))
				sql = "select column_name from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+mainTablename+"') order by ordinal_position";
			else if(dbtype.toLowerCase().indexOf("sqlserver")>-1) {
	            sql = "select c.name from sysobjects o,syscolumns c where o.id=c.id and o.name='" + mainTablename + "' order by c.colid";
	        }
			RecordSet.executeQuery(sql);
			while(RecordSet.next()){
				String columname = Util.null2String(RecordSet.getString(1)).toLowerCase();
				columMap.put(columname, columname);
			}
			if(columMap.containsKey("modedatacreater")){
				fieldArray.add(BrowserHelper.constructMap(
						 "key", "2"
						 ,"showname", SystemEnv.getHtmlLabelName(882,user.getLanguage())
						 ,"tablename", mainTablenameTemp
						 ,"fieldname", "modedatacreater"
						 ,"fieldid", "2"
						 ,"fieldhtmltype", 3
						 ,"type", 1
						 ,"fielddbtype", "int"
						 ,"relationtype", "4"
						 ,"valueshowtype", "4"
						 ,"qfws", "0","isencrypt","")
						);
			}
			if(columMap.containsKey("modedatacreatedate")){
				fieldArray.add(BrowserHelper.constructMap(
						 "key", "1"
						 ,"showname", SystemEnv.getHtmlLabelName(722,user.getLanguage())
						 ,"tablename", mainTablenameTemp
						 ,"fieldname", "modedatacreatedate"
						 ,"fieldid", "1"
						 ,"fieldhtmltype", 3
						 ,"type", 2
						 ,"fielddbtype", "char(10)"
						 ,"relationtype", "2"
						 ,"valueshowtype", "2"
						 ,"qfws", "0","isencrypt","")
						);
			}
			if(columMap.containsKey("modedatamodifier")){
				fieldArray.add(BrowserHelper.constructMap(
						 "key", "7"
						 ,"showname", SystemEnv.getHtmlLabelName(3002,user.getLanguage())
						 ,"tablename", mainTablenameTemp
						 ,"fieldname", "modedatamodifier"
						 ,"fieldid", "7"
						 ,"fieldhtmltype", 3
						 ,"type", 1
						 ,"fielddbtype", "int"
						 ,"relationtype", "4"
						 ,"valueshowtype", "4"
						 ,"qfws", "0","isencrypt","")
						);
			}
			if(columMap.containsKey("modedatamodifydatetime")){
				fieldArray.add(BrowserHelper.constructMap(
						 "key", "8"
						 ,"showname",SystemEnv.getHtmlLabelName(516915,user.getLanguage())
						 ,"tablename", mainTablenameTemp
						 ,"fieldname", "modedatamodifydatetime"
						 ,"fieldid", "8"
						 ,"fieldhtmltype", "3"
						 ,"type", "290"
						 ,"fielddbtype", "varchar(100)"
						 ,"relationtype", "2"
						 ,"valueshowtype", "3"
						 ,"qfws", "0","isencrypt","")
						);
			}
		}
		 if(this.type.equals("ganttColor")||(this.type.equals("modeRemind")&&formtype==0)||this.type.equals("restfulCondition")||(this.type.equals("checkCondition")&&customid>0&&searchDetailtable=="")){
			 isNeedDetail = false;
		}
		JSONArray detailTables = new JSONArray();
		if(!this.type.equals("ganttColor")){
			List templist = new ArrayList();
			for(int i = 0; isNeedDetail&&i < detlfields.size(); i++){
				 Map maps = (Map)detlfields.get(i);
				 String fieldid = (String)maps.get("fieldid");
				 String fieldlabel = (String)maps.get("fieldlabel");
				 String viewtype = (String)maps.get("viewtype");
				 String fieldname = (String)maps.get("fieldname");
				 String fieldhtmltype = (String)maps.get("fieldhtmltype");
				 String type = (String)maps.get("type");
				 String detailtable = (String)maps.get("detailtable");
				 String fielddbtype=(String)maps.get("fielddbtype");
				 String qfws=(String)maps.get("qfws");
				 if(this.type.equals("modeRemind")&&formtype>0){
					 String index = detailtable.substring(detailtable.lastIndexOf("dt")+2,detailtable.length());
					 if(!index.equals(formtype+"")){
						 continue;
					 }
				 }
			 if(this.type.equals("WfBrowser")){
			 	if("161".equals(browserType)||"162".equals(browserType)){
			 		if(!this.detailtable.equals(detailtable)){
			 			continue;
					}
				}else if("256".equals(browserType)||"257".equals(browserType)){
					if(!this.tablename.equals(detailtable)){
						continue;
					}
				}
			 }
				 Map detailTempMap = new HashMap();
				 String tablenameStr = "";
				 String detailtableStr = detailtable;
				if ((this.type.equals("modeRemind")&&formtype>0)||this.type.equals("listCondition")
						|| this.type.equals("browserCondition") || this.type.equals("checkCondition")
						|| this.type.equals("restfulCondition")||this.type.equals("WfBrowser")||this.type.equals("restfulCondition")) {
					detailtableStr = "d1";
				}
				 if(detailMap.containsKey(detailtable)){
					 detailTempMap = (Map)detailMap.get(detailtable);
					 tablenameStr = "("+detailTempMap.get("titles")+")";
					 if(!templist.contains(detailtable)){
						 templist.add(detailtable);
						 detailTables.add(BrowserHelper.constructMap("detailtable", detailtableStr,"name", detailTempMap.get("titles")));
					 }
				 }


				 if(fieldhtmltype.equals("6")||fieldhtmltype.equals("7")){//附件或特殊字段
					 continue;
				 }
				 if(fieldhtmltype.equals("2")&&type.equals("2")){//格式文本
					 continue;
				 }

				 String relationtype = "1";//关系类型---单行文本文本  +  单行文本金额千分位  +  多行文本
				 if(fieldhtmltype.equals("1")&&(type.equals("2")||type.equals("3"))){//整数  +  浮点数
					 relationtype =  "2";
				 }


				 if((fieldhtmltype.equals("1")&&type.equals("4"))||fieldhtmltype.equals("5")){// 单行文本金额转换   +  选择项
					 relationtype = "3";
				 }

				 if(fieldhtmltype.equals("3")){//浏览框
					 if(type.equals("2")||type.equals("19") || type.equals("290") ){//日期  + 时间
						 relationtype = "2";
					 }else{
						 relationtype = "4";
					 }
				 }

				 if(fieldhtmltype.equals("4")){//checkbox
					 relationtype = "4";
				 }

				 String valueshowtype = "1";//单行文本输入框

				 if(fieldhtmltype.equals("3")){
					 if(type.equals("2")){//日期
						 valueshowtype = "2";
					 }else if(type.equals("19") || type.equals("290") ){//时间
						 valueshowtype = "3";
					 }else{
						 valueshowtype = "4";//浏览框--需动态加载
					 }
				 }

				 if(fieldhtmltype.equals("5")){//select框--需动态加载
					 valueshowtype = "5";
				 }

				 if(fieldhtmltype.equals("4")){//checkbox
					 valueshowtype = "6";//select固定值  0  1  null
				 }
			String fieldencrypt = formFieldEncryptComInfo.getIsencrypt(fieldid);
			if(!"1".equals(fieldencrypt)){
				EncryptFieldEntity encryptFieldEntity = new EncryptFieldConfigComInfo().getFieldEncryptConfig(detailtable, fieldname);
				if(encryptFieldEntity != null && ("1".equals(encryptFieldEntity.getDesensitization()) || "1".equals(encryptFieldEntity.getIsEncrypt()))) fieldencrypt="1";
			}
				 fieldArray.add(BrowserHelper.constructMap("key", fieldid,"showname", fieldlabel+tablenameStr
						 ,"tablename", detailtableStr
						 ,"fieldname", fieldname
						 ,"fieldid", fieldid
						 ,"fieldhtmltype", fieldhtmltype
						 ,"type", type
						 ,"fielddbtype", fielddbtype
						 ,"relationtype", relationtype
						 ,"valueshowtype", valueshowtype
					 ,"qfws", qfws,"isencrypt",fieldencrypt)
						 );
			}
		}

		if(conditiontype==1){
			result.put("conditionsql", conditionsql);
			result.put("conditionsql2", "");
			result.put("conditiontext", conditiontext);
		}else if(conditiontype==2){
			result.put("conditionsql", "");
			result.put("conditionsql2", conditionsql);
			result.put("conditiontext", conditiontext);
		}else{
			result.put("conditionsql", "");
			result.put("conditionsql2", "");
			result.put("conditiontext", conditiontext);
		}
		if("WfBrowser".equals(type)){
			result.put("rootdata", rootdata);
		}
		result.put("conditiontype", conditiontype+"");
		result.put("javafileaddress", javafileaddress);
		result.put("fieldOptions", fieldArray);
		result.put("mainTablename", mainTablenameTemp);
		result.put("detailTables", detailTables);
		result.put("count", count);
    	return result;
    }


    //置顶规则查询
	public Map<String, Object> getConditionTopInfo(User user,int formId,int istop){
		return getConditionTopInfo(user,formId,istop,"");
	}

	public Map<String, Object> getConditionTopInfo(User user,int formId,int istop,String searchDetailtable){
		Map<String, Object> result = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();

		ModeLayoutUtil ModeLayoutUtil = new  ModeLayoutUtil();
		ModeLayoutUtil.setUser(user);
		ModeLayoutUtil.setFormId(formId);
		String mainTablename = "";
		String mainTablenameTemp = "";
		RecordSet RecordSet = new RecordSet();
		RecordSet.execute("select tablename from workflow_bill where id="+formId);
		if(RecordSet.next()){
			mainTablename = RecordSet.getString("tablename");
		}
		if(VirtualFormHandler.isVirtualForm(formId)){
			mainTablename = VirtualFormHandler.getRealFromName(mainTablename);
		}
		int formtype = 0;
		if(type.equals("modeRemind")){//提醒
			String sql  = "SELECT formtype FROM mode_timedtask_detail where id="+objid;
			rs.execute(sql);
			if(rs.next()){
				formtype = rs.getInt("formtype");
			}
		}
		/*Map fieldsmap = new HashMap();
		if(searchDetailtable.length()>0){
			fieldsmap = ModeLayoutUtil.getCustomSearchfieldsByDetailTable(customid,user.getLanguage(),searchDetailtable);
		}else{
			fieldsmap = ModeLayoutUtil.getFormfields(user.getLanguage(),1);
		}

		List mainfields = (List)fieldsmap.get("mainfields");				//主表字段
		List detlfields = (List)fieldsmap.get("detlfields");				//子表字段
		List detailGroup = (List)fieldsmap.get("detailGroup");				//子表组信息
		Map detailMap = new HashMap();
		for(int i=0;i<detailGroup.size();i++){
			Map tempMap = (Map)detailGroup.get(i);
			String detailtable = tempMap.get("detailtable")+"";
			detailMap.put(detailtable,tempMap);
		}
		Map indexMap = ModeLayoutUtil.getIndexMap();
		*/
		//***********************
		int conditiontype = 1;
		String conditionsql = "";
		String conditiontext = "";
		String javafileaddress = "";
		String sql = "";

		boolean isNeedDetail = true;
		if(type.equals("modeTriggerWorkflow")){
			sql = "select conditiontype,showcondition as conditionsql,showconditioncn as conditiontext  "
					+ " from "+baseInfoTable+" where id="+objid;
		}else if(type.equals("modeRemind")){
			sql = "select conditionstype as conditiontype,conditionsfield,conditionssql as conditionsql,conditionsfieldcn as conditiontext  "
					+ " from "+baseInfoTable+" where id="+objid;
		}else if(type.equals("listCondition")){
			sql = "select conditiontypeTop conditiontype ,conditionsqlTop conditionsql,conditiontextTop conditiontext,searchconditiontypeTop searchconditiontype,defaultsqlTop defaultsql,javafileaddressTop javafileaddress,detailtable from "+baseInfoTable+" where id="+customid;
		}else if(type.equals("browserCondition")){
			sql = "select conditiontype,conditionsql,conditiontext,searchconditiontype,defaultsql,javafileaddress,detailtable from "+baseInfoTable+" where id="+customid;
		}else{
			sql = "select conditiontype,conditionsql,conditiontext from "+baseInfoTable+" where id="+objid;
		}

		RecordSet.execute(sql);
		if(RecordSet.next()){
			conditiontype = Util.getIntValue(RecordSet.getString("conditiontype"),0);
			int searchconditiontype = Util.getIntValue(RecordSet.getString("searchconditiontype"),0);

			conditionsql = RecordSet.getString("conditionsql");
			javafileaddress = RecordSet.getString("javafileaddress");
			if(type.equals("modeRemind")&&conditiontype==1){
				conditionsql = RecordSet.getString("conditionsfield");
			}
			if(conditiontype==0 && searchconditiontype==1){//等于零代表空数据 默认为1
				conditiontype = 2;
				conditionsql = RecordSet.getString("defaultsql");
				if(conditionsql.equals("")){
					conditiontype = 1;
				}
			}else if(conditiontype==0 && searchconditiontype==2){
				conditiontype = 3;
				//conditionsql = RecordSet.getString("defaultsql");
				if(javafileaddress.equals("")){
					conditiontype = 1;
				}
			}else if(conditiontype==0){
				conditiontype = 1;
			}
			conditiontext = RecordSet.getString("conditiontext");


			//
			if(type.equals("listCondition") || type.equals("browserCondition")){
				String detailtablename = RecordSet.getString("detailtable");
				if(detailtablename.equals("")){
					isNeedDetail = false;
				}else{
					searchDetailtable = detailtablename;
				}
			}
		}
		Map fieldsmap = new HashMap();
		if(searchDetailtable.length()>0){
			fieldsmap = ModeLayoutUtil.getCustomSearchfieldsByDetailTable(customid,user.getLanguage(),searchDetailtable);
		}else{
			fieldsmap = ModeLayoutUtil.getFormfields(user.getLanguage(),1);
		}
		List mainfields = (List)fieldsmap.get("mainfields");				//主表字段
		List detlfields = (List)fieldsmap.get("detlfields");				//子表字段
		List detailGroup = (List)fieldsmap.get("detailGroup");				//子表组信息
		Map detailMap = new HashMap();
		for(int i=0;i<detailGroup.size();i++){
			Map tempMap = (Map)detailGroup.get(i);
			String detailtable = tempMap.get("detailtable")+"";
			detailMap.put(detailtable,tempMap);
		}
		Map indexMap = ModeLayoutUtil.getIndexMap();

		sql = "select count(1) as count from "+expressionsTable+" where "+objFieldName+"='"+objid+"'";
		RecordSet.execute(sql);
		int count = 0;
		if(RecordSet.next()){
			count = RecordSet.getInt("count");
		}
		if(count==0){
			count = 1;
		}
		ModeFormFieldEncryptComInfo formFieldEncryptComInfo=new ModeFormFieldEncryptComInfo();
		JSONArray fieldArray = new  JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("key", "");
		obj.put("showname", "");
		fieldArray.add(obj);
		for(int i = 0; i < mainfields.size(); i++){
			Map maps = (Map)mainfields.get(i);
			String fieldid = (String)maps.get("fieldid");
			String fieldlabel = (String)maps.get("fieldlabel");
			String viewtype = (String)maps.get("viewtype");
			String fieldname = (String)maps.get("fieldname");
			String fieldhtmltype = (String)maps.get("fieldhtmltype");
			String type = (String)maps.get("type");
			String fielddbtype=(String)maps.get("fielddbtype");
			String qfws=(String)maps.get("qfws");
			if ("seclevel".equals(((String) maps.get("fieldname") + "").toLowerCase())) {
				continue;
			}
			if(fieldhtmltype.equals("6")||fieldhtmltype.equals("7")){//附件或特殊字段
				continue;
			}
			if(fieldhtmltype.equals("2")&&type.equals("2")){//格式文本
				continue;
			}

			String relationtype = "1";//关系类型---单行文本文本  +  单行文本金额千分位  +  多行文本
			if(fieldhtmltype.equals("1")&&(type.equals("2")||type.equals("3"))){//整数  +  浮点数
				relationtype =  "2";
			}


			if((fieldhtmltype.equals("1")&&type.equals("4"))||fieldhtmltype.equals("5")){// 单行文本金额转换   +  选择项
				relationtype = "3";
			}

			if(fieldhtmltype.equals("3")){//浏览框
				if(type.equals("2")||type.equals("19")||type.equals("290")){//日期  + 时间
					relationtype = "2";
				}else{
					relationtype = "4";
				}
			}

			if(fieldhtmltype.equals("4")){//checkbox
				relationtype = "4";
			}

			String valueshowtype = "1";//单行文本输入框

			if(fieldhtmltype.equals("3")){
				if(type.equals("2")){//日期
					valueshowtype = "2";
				}else if(type.equals("19") || type.equals("290")){//时间
					valueshowtype = "3";
				}else{
					valueshowtype = "4";//浏览框--需动态加载
				}
			}

			if(fieldhtmltype.equals("5")){//select框--需动态加载
				valueshowtype = "5";
			}

			if(fieldhtmltype.equals("4")){//checkbox
				valueshowtype = "6";//select固定值  0  1  null
			}
			mainTablenameTemp = mainTablename;
			if (this.type.equals("modeRemind") || this.type.equals("listCondition") || this.type.equals("browserCondition") || this.type.equals("checkCondition")) {
				mainTablenameTemp = "t1";
			}
			String fieldencrypt = formFieldEncryptComInfo.getIsencrypt(fieldid);
			if(!"1".equals(fieldencrypt)){
				EncryptFieldEntity encryptFieldEntity = new EncryptFieldConfigComInfo().getFieldEncryptConfig(mainTablename, fieldname);
				if(encryptFieldEntity != null && ("1".equals(encryptFieldEntity.getDesensitization()) || "1".equals(encryptFieldEntity.getIsEncrypt()))) fieldencrypt="1";
			}
			fieldArray.add(BrowserHelper.constructMap(
					"key", fieldid
					,"showname", fieldlabel
					,"tablename", mainTablenameTemp
					,"fieldname", fieldname
					,"fieldid", fieldid
					,"fieldhtmltype", fieldhtmltype
					,"type", type
					,"fielddbtype", fielddbtype
					,"relationtype", relationtype
					,"valueshowtype", valueshowtype
					,"qfws", qfws,"isencrypt",fieldencrypt)
			);
		}
		if(type.equals("listCondition") || type.equals("browserCondition")){
			String dbtype = RecordSet.getDBType().toLowerCase();
			HashMap columMap = new HashMap();
			if(dbtype.equalsIgnoreCase("oracle")) {
				sql = "select COLUMN_NAME from user_tab_columns where table_name=upper('" + mainTablename + "') ORDER BY COLUMN_ID";
			}else if(dbtype.equalsIgnoreCase("mysql")){
				String dbname = CommonConstant.DB_MYSQL_SCHEMA;
				sql = "select column_name from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+mainTablename+"') order by ordinal_position";
			}
			else if(dbtype.equalsIgnoreCase("postgresql"))
			{
				sql = "select column_name from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+mainTablename+"') order by ordinal_position";
			}
			else if(dbtype.toLowerCase().indexOf("sqlserver")>-1) {
				sql = "select c.name from sysobjects o,syscolumns c where o.id=c.id and o.name='" + mainTablename + "' order by c.colid";
			}
			RecordSet.executeQuery(sql);
			while(RecordSet.next()){
				String columname = Util.null2String(RecordSet.getString(1)).toLowerCase();
				columMap.put(columname, columname);
			}
			if(columMap.containsKey("modedatacreater")){
				fieldArray.add(BrowserHelper.constructMap(
						"key", "2"
						,"showname", SystemEnv.getHtmlLabelName(882,user.getLanguage())
						,"tablename", mainTablenameTemp
						,"fieldname", "modedatacreater"
						,"fieldid", "2"
						,"fieldhtmltype", 3
						,"type", 1
						,"fielddbtype", "int"
						,"relationtype", "4"
						,"valueshowtype", "4"
						,"qfws", "0","isencrypt","")
				);
			}
			if(columMap.containsKey("modedatacreatedate")){
				fieldArray.add(BrowserHelper.constructMap(
						"key", "1"
						,"showname", SystemEnv.getHtmlLabelName(722,user.getLanguage())
						,"tablename", mainTablenameTemp
						,"fieldname", "modedatacreatedate"
						,"fieldid", "1"
						,"fieldhtmltype", 3
						,"type", 2
						,"fielddbtype", "char(10)"
						,"relationtype", "2"
						,"valueshowtype", "2"
						,"qfws", "0","isencrypt","")
				);
			}
			if(columMap.containsKey("modedatamodifier")){
				fieldArray.add(BrowserHelper.constructMap(
						"key", "7"
						,"showname", SystemEnv.getHtmlLabelName(3002,user.getLanguage())
						,"tablename", mainTablenameTemp
						,"fieldname", "modedatamodifier"
						,"fieldid", "7"
						,"fieldhtmltype", 3
						,"type", 1
						,"fielddbtype", "int"
						,"relationtype", "4"
						,"valueshowtype", "4"
						,"qfws", "0","isencrypt","")
				);
			}
			if(columMap.containsKey("modedatamodifydatetime")){
				fieldArray.add(BrowserHelper.constructMap(
						"key", "8"
						,"showname",SystemEnv.getHtmlLabelName(516915,user.getLanguage())
						,"tablename", mainTablenameTemp
						,"fieldname", "modedatamodifydatetime"
						,"fieldid", "8"
						,"fieldhtmltype", "3"
						,"type", "290"
						,"fielddbtype", "varchar(100)"
						,"relationtype", "2"
						,"valueshowtype", "3"
						,"qfws", "0","isencrypt","")
				);
			}
		}
		if((this.type.equals("modeRemind")&&formtype==0)||this.type.equals("restfulCondition")){
			isNeedDetail = false;
		}
		JSONArray detailTables = new JSONArray();
		List templist = new ArrayList();
		for(int i = 0; isNeedDetail&&i < detlfields.size(); i++){
			Map maps = (Map)detlfields.get(i);
			String fieldid = (String)maps.get("fieldid");
			String fieldlabel = (String)maps.get("fieldlabel");
			String viewtype = (String)maps.get("viewtype");
			String fieldname = (String)maps.get("fieldname");
			String fieldhtmltype = (String)maps.get("fieldhtmltype");
			String type = (String)maps.get("type");
			String detailtable = (String)maps.get("detailtable");
			String fielddbtype=(String)maps.get("fielddbtype");
			String qfws=(String)maps.get("qfws");
			if(this.type.equals("modeRemind")&&formtype>0){
				String index = detailtable.substring(detailtable.lastIndexOf("dt")+2,detailtable.length());
				if(!index.equals(formtype+"")){
					continue;
				}
			}
			Map detailTempMap = new HashMap();
			String tablenameStr = "";
			String detailtableStr = detailtable;
			if(this.type.equals("modeRemind")&&formtype>0){
				detailtableStr = "d1";
			}
			if (this.type.equals("listCondition") || this.type.equals("browserCondition") || this.type.equals("checkCondition")) {
				detailtableStr = "d1";
			}
			if(detailMap.containsKey(detailtable)){
				detailTempMap = (Map)detailMap.get(detailtable);
				tablenameStr = "("+detailTempMap.get("titles")+")";
				if(!templist.contains(detailtable)){
					templist.add(detailtable);
					detailTables.add(BrowserHelper.constructMap("detailtable", detailtableStr,"name", detailTempMap.get("titles")));
				}
			}


			if(fieldhtmltype.equals("6")||fieldhtmltype.equals("7")){//附件或特殊字段
				continue;
			}
			if(fieldhtmltype.equals("2")&&type.equals("2")){//格式文本
				continue;
			}

			String relationtype = "1";//关系类型---单行文本文本  +  单行文本金额千分位  +  多行文本
			if(fieldhtmltype.equals("1")&&(type.equals("2")||type.equals("3"))){//整数  +  浮点数
				relationtype =  "2";
			}


			if((fieldhtmltype.equals("1")&&type.equals("4"))||fieldhtmltype.equals("5")){// 单行文本金额转换   +  选择项
				relationtype = "3";
			}

			if(fieldhtmltype.equals("3")){//浏览框
				if(type.equals("2")||type.equals("19") || type.equals("290") ){//日期  + 时间
					relationtype = "2";
				}else{
					relationtype = "4";
				}
			}

			if(fieldhtmltype.equals("4")){//checkbox
				relationtype = "4";
			}

			String valueshowtype = "1";//单行文本输入框

			if(fieldhtmltype.equals("3")){
				if(type.equals("2")){//日期
					valueshowtype = "2";
				}else if(type.equals("19") || type.equals("290") ){//时间
					valueshowtype = "3";
				}else{
					valueshowtype = "4";//浏览框--需动态加载
				}
			}

			if(fieldhtmltype.equals("5")){//select框--需动态加载
				valueshowtype = "5";
			}

			if(fieldhtmltype.equals("4")){//checkbox
				valueshowtype = "6";//select固定值  0  1  null
			}
			String fieldencrypt = formFieldEncryptComInfo.getIsencrypt(fieldid);
			if(!"1".equals(fieldencrypt)){
				EncryptFieldEntity encryptFieldEntity = new EncryptFieldConfigComInfo().getFieldEncryptConfig(detailtable, fieldname);
				if(encryptFieldEntity != null && ("1".equals(encryptFieldEntity.getDesensitization()) || "1".equals(encryptFieldEntity.getIsEncrypt()))) fieldencrypt="1";
			}
			fieldArray.add(BrowserHelper.constructMap("key", fieldid,"showname", fieldlabel+tablenameStr
					,"tablename", detailtableStr
					,"fieldname", fieldname
					,"fieldid", fieldid
					,"fieldhtmltype", fieldhtmltype
					,"type", type
					,"fielddbtype", fielddbtype
					,"relationtype", relationtype
					,"valueshowtype", valueshowtype
					,"qfws", qfws,"isencrypt",fieldencrypt)
			);
		}
		if(conditiontype==1){
			result.put("conditionsql", conditionsql);
			result.put("conditionsql2", "");
			result.put("conditiontext", conditiontext);
		}else if(conditiontype==2){
			result.put("conditionsql", "");
			result.put("conditionsql2", conditionsql);
			result.put("conditiontext", conditiontext);
		}else{
			result.put("conditionsql", "");
			result.put("conditionsql2", "");
			result.put("conditiontext", conditiontext);
		}
		result.put("conditiontype", conditiontype+"");
		result.put("javafileaddress", javafileaddress);
		result.put("fieldOptions", fieldArray);
		result.put("mainTablename", mainTablenameTemp);
		result.put("detailTables", detailTables);
		result.put("count", count);
		return result;
	}


	/**
     * 获取指定规则显示用的html
     * @param objid
     * @return
     */
    public JSONObject getRoot(int objid,User user) {
    	JSONObject root = new JSONObject();
        RecordSet rs = new RecordSet();//排除临时文件
        String isTopWhere= "";
		if(!type.equals("pageExtend")){
			isTopWhere = "  and istop is null ";
		}
		String sql = "select id from "+expressionsTable+" where "+objFieldName+"=" + objid +isTopWhere +" ORDER BY id desc";
        rs.execute(sql);//按照id排序，第一个就是根节点

        if (rs.next()) {
        	int id = Util.getIntValue(rs.getString("id"));
        	root.put("key", id+"");
        	root.put("parentNode", "");
        	getRuleByExpID(id,user, root);
        }
        return root;
    }





	/**
	 * 获取指定规则显示用的html重载，用户置顶规则显示查询
	 * @param objid
	 * @return
	 */
	public JSONObject getRoot(int objid,User user,int isTop) {
		JSONObject root = new JSONObject();
		RecordSet rs = new RecordSet();//排除临时文件
		rs.execute("select id from "+expressionsTable+" where "+objFieldName+"=" + objid +"  and isTop=" +isTop+ " ORDER BY id desc");//按照id排序，第一个就是根节点

		if (rs.next()) {
			int id = Util.getIntValue(rs.getString("id"));
			root.put("key", id+"");
			root.put("parentNode", "");
			getRuleByExpID(id,user, root);
		}
		return root;
	}

    /**
     * 递归获取显示用html
     * @param expressionid
     * @param flag
     * @return
     */
    private JSONArray getRuleByExpID(int expressionid,User user, JSONObject node) {
        //显示的html结构，用于显示
    	JSONArray jsonArray = new JSONArray();
        JSONObject exps = getExpressions(expressionid);
        //如果是表达式集合
        if (!"".equals(exps.getString("expids"))) {
        	node.put("relationship", exps.getString("relation"));
        	node.put("isParent", true);
            List<String> expsidsArray = Util.TokenizerString(exps.getString("expids"), ",");
            JSONArray datas = new JSONArray();
            for (int i=0; i<expsidsArray.size(); i++) {
            	int id = Integer.parseInt((String)expsidsArray.get(i));
            	JSONObject json= new JSONObject();
            	json.put("key", id+"");
            	json.put("parentNode", expressionid+"");
            	getRuleByExpID(id,user, json);
            	datas.add(json);
            }
            node.put("datas", datas);
        } else {
        	JSONObject expBean = getExpressionBean(exps.getIntValue("expbaseid"));
        	node.put("isParent", false);
        	node.putAll(expBean);
            jsonArray.add(node);
        }
        return jsonArray;
    }


    /**
     * 根据id获取表达式对象
     * @param expid
     * @return
     */
    public JSONObject getExpressionBean(int expid) {
        JSONObject expbean = new  JSONObject();
        String expinfosql = "select a.*,b.billid,b.viewtype,b.detailtable from "+expressionbaseTable+" a,workflow_billfield b WHERE a.fieldid=b.id and a.fieldid not in (1,2,7,8) and a.id=" + expid;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        rs.execute(expinfosql);
        if (rs.next()) {
        	String viewtype = rs.getString("viewtype");
        	String tablename = Util.null2String(rs.getString("detailtable"));
        	if(type.equals("ganttColor") || type.equals("modeRemind") || type.equals("restfulCondition")){
        		if(viewtype.equals("0")){
        			tablename = "t1";
        		}else{
        			tablename = "d1";
        		}
        	}else if(type.equals("listCondition") || type.equals("browserCondition")||type.equals("checkCondition")){
        		if(tablename.equals("")){
        			tablename = "t1";
        		}else{
        			tablename = "d1";
        		}
        	}else{
        		if(tablename.equals("")){
        			String billid = rs.getString("billid");
        			if(tableMap.containsKey(billid)){
        				tablename = tableMap.get(billid);
        			}else{
        				String sql = "SELECT tablename FROM workflow_bill WHERE id="+billid;
        				rs1.execute(sql);
        				if(rs1.next()){
							boolean isvirtualform=VirtualFormHandler.isVirtualForm(billid);
							if(isvirtualform){
								tablename = VirtualFormHandler.getRealFromName(rs1.getString("tablename"));
							}else{
								tablename = rs1.getString("tablename");
							}
        					tableMap.put(billid, tablename);
        				}
        			}
        		}
        	}

        	String fieldname = rs.getString("fieldname");
        	if(fieldname.indexOf(tablename+".")!=-1){
        		fieldname = fieldname.replace(tablename+".", "");
        	}

        	String compareopion = Util.null2String(rs.getString("compareopion")); // 10 属于(含下级) 、11 不属于(含下级)
			if(compareopion.equals("10")||compareopion.equals("11")) {
				String fieldOldvalue = Util.null2String(rs.getString("fieldparentvalue"));
				expbean.put("fieldOldvalue", fieldOldvalue);
			}

        	expbean.put("key", rs.getString("id"));
    		expbean.put("fieldid", rs.getString("fieldid"));
    		expbean.put("fieldname", fieldname);
    		expbean.put("fieldlabel", rs.getString("fieldlabel"));
    		expbean.put("objid", rs.getString(objFieldName));

    		expbean.put("htmltype", rs.getString("htmltype"));
    		expbean.put("fieldtype", rs.getString("fieldtype"));
    		expbean.put("fielddbtype", rs.getString("fielddbtype"));
    		expbean.put("compareopion", rs.getString("compareopion"));
    		expbean.put("compareopionlabel", rs.getString("compareopionlabel"));

    		expbean.put("fieldvalue", rs.getString("fieldvalue"));
    		expbean.put("fieldtext", rs.getString("fieldtext"));
    		expbean.put("relationtype", rs.getString("relationtype"));
    		expbean.put("valetype", rs.getString("valetype"));
    		//查询以及浏览框
    		expbean.put("conditiontype",rs.getString("conditiontype4sb"));
    		expbean.put("oldconditiontype",rs.getInt("oldconditiontype4sb"));
    		expbean.put("tablename", tablename);
        }else if(!type.equals("workflowToMode")){
        	expinfosql = "select a.* from "+expressionbaseTable+" a WHERE a.fieldid in (1,2,7,8) and a.id=" + expid;
        	rs.execute(expinfosql);
        	if (rs.next()) {
        		//String viewtype = rs.getString("viewtype");
            	String tablename = "";
            	if(type.equals("listCondition") || type.equals("browserCondition")||type.equals("checkCondition") || type.equals("modeRemind") || type.equals("restfulCondition")){
            		tablename = "t1";
            	}else{
            		//rightid、triggerworkflowsetid、workflowtomodeid、remindid、searchTransMethodId、interfaceid、batchsetid

            		String rightid = Util.null2String(rs.getString("rightid"));
            		String triggerworkflowsetid = Util.null2String(rs.getString("triggerworkflowsetid"));
            		//String workflowtomodeid = Util.null2String(rs.getString("workflowtomodeid"));
            		//String remindid = Util.null2String(rs.getString("remindid"));
            		String searchTransMethodId = Util.null2String(rs.getString("searchTransMethodId"));
            		//String interfaceid = Util.null2String(rs.getString("interfaceid"));
            		//String batchsetid = Util.null2String(rs.getString("batchsetid"));
            		RecordSet rSet = new RecordSet();
            		String formid = "";
            		//根据不同类型获取字段名
                	if(type.equals("modeRight")){
                		rSet.executeQuery("select b.formid from moderightinfo a,modeinfo b where a.modeid=b.id and a.id=?", rightid);
                	}else if(type.equals("modeTriggerWorkflow")){
                		rSet.executeQuery("select b.formid from mode_triggerworkflowset a,modeinfo b where a.modeid=b.id and a.id=?", triggerworkflowsetid);
                	}else if(type.equals("pageExtend")){
                		rSet.executeQuery("select b.formid from moderightinfo a,expandBaseRightInfo b where a.modeid=b.id and a.id=?", rightid);
                	}else if(type.equals("searchTransMethod")){
                		rSet.executeQuery("select b.formid from customfieldshowchange a,mode_customsearch b where a.customid=b.id and a.id=?", searchTransMethodId);
            		}
                	if(rSet.next()){
            			formid = Util.null2String(rSet.getString("formid"));
            		}
                	if(tableMap.containsKey(formid)){
        				tablename = tableMap.get(formid);
        			}else{
        				String sql = "SELECT tablename FROM workflow_bill WHERE id="+formid;
        				rs1.execute(sql);
        				if(rs1.next()){
							boolean isvirtualform=VirtualFormHandler.isVirtualForm(formid);
							if(isvirtualform){
								tablename = VirtualFormHandler.getRealFromName(rs1.getString("tablename"));
							}else{
								tablename = rs1.getString("tablename");
							}
        					tableMap.put(formid, tablename);
        				}
        			}
            	}
        		String fieldname = rs.getString("fieldname");
            	if(fieldname.indexOf(tablename+".")!=-1){
            		fieldname = fieldname.replace(tablename+".", "");
            	}
            	String compareopion = Util.null2String(rs.getString("compareopion")); // 10 属于(含下级) 、11 不属于(含下级)
    			if(compareopion.equals("10")||compareopion.equals("11")) {
    				String fieldOldvalue = Util.null2String(rs.getString("fieldparentvalue"));
    				expbean.put("fieldOldvalue", fieldOldvalue);
    			}
        		expbean.put("key", rs.getString("id"));
        		expbean.put("fieldid", rs.getString("fieldid"));
        		expbean.put("fieldname", fieldname);
        		expbean.put("fieldlabel", rs.getString("fieldlabel"));
        		expbean.put("objid", rs.getString(objFieldName));

        		expbean.put("htmltype", rs.getString("htmltype"));
        		expbean.put("fieldtype", rs.getString("fieldtype"));
        		expbean.put("fielddbtype", rs.getString("fielddbtype"));
        		expbean.put("compareopion", rs.getString("compareopion"));
        		expbean.put("compareopionlabel", rs.getString("compareopionlabel"));

        		expbean.put("fieldvalue", rs.getString("fieldvalue"));
        		expbean.put("fieldtext", rs.getString("fieldtext"));
        		expbean.put("relationtype", rs.getString("relationtype"));
        		expbean.put("valetype", rs.getString("valetype"));
        		//查询以及浏览框
        		expbean.put("conditiontype",rs.getString("conditiontype4sb"));
        		expbean.put("oldconditiontype",rs.getInt("oldconditiontype4sb"));
        		expbean.put("tablename", tablename);
        	}
        }
        return expbean;
    }

    public JSONObject getExpressions(int expsid) {
    	JSONObject exps = new JSONObject();

        RecordSet rs = new RecordSet();
        rs.execute("select * from "+expressionsTable+" where id=" + expsid);
        if (rs.next()) {
        	exps.put("expids", Util.null2String(rs.getString("expids")));
        	exps.put("expbaseid", Util.null2String(rs.getString("expbaseid")));
        	exps.put("relation", Util.null2String(rs.getString("relation")));
        	exps.put("objid", Util.null2String(rs.getString(objFieldName)));
        }
        return exps;
    }

	public int getObjid() {
		return objid;
	}

	public void setObjid(int objid) {
		this.objid = objid;
	}

	public int getConditiontype() {
		return conditiontype;
	}
	public void setConditiontype(int conditiontype) {
		this.conditiontype = conditiontype;
	}
	public String getRootdata() {
		return rootdata;
	}
	public void setRootdata(String rootdata) {
		this.rootdata = rootdata;
	}
	public String getConditionsql() {
		return conditionsql;
	}
	public void setConditionsql(String conditionsql) {
		this.conditionsql = conditionsql;
	}
	public String getConditiontext() {
		return conditiontext;
	}
	public void setConditiontext(String conditiontext) {
		this.conditiontext = conditiontext;
	}
	public String getJavafileaddress() {
		return javafileaddress;
	}
	public void setJavafileaddress(String javafileaddress) {
		this.javafileaddress = javafileaddress;
	}

	public int getIsTop() {
		return isTop;
	}//置顶标识

	public void setIsTop(int isTop) {
		this.isTop = isTop;
	}

	public String getObjFieldName() {
		return objFieldName;
	}

	public int getCustomid() {
		return customid;
	}

	public void setCustomid(int customid) {
		this.customid = customid;
	}

	public int getModeid() {
		return modeid;
	}

	public void setModeid(int modeid) {
		this.modeid = modeid;
	}

	public int getNodeid() {
		return nodeid;
	}

	public void setNodeid(int nodeid) {
		this.nodeid = nodeid;
	}

	public String getDetailtable() {
		return detailtable;
	}

	public void setDetailtable(String detailtable) {
		this.detailtable = detailtable;
	}

	public String getBrowserType() {
		return browserType;
	}

	public void setBrowserType(String browserType) {
		this.browserType = browserType;
	}

	public String getTablename() {
		return tablename;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}
}