package weaver.formmode.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.engine.cube.biz.ModeFormHelper;
import com.weaver.formmodel.util.StringHelper;

import weaver.conn.ConnectionPool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.conn.WeaverConnection;
import weaver.formmode.log.FormmodeLog;
import weaver.formmode.service.CommonConstant;
import weaver.general.BaseBean;
import weaver.general.Util;

public enum  ModeDataIDUpdateSingle{
	INSTANCE;
	
	private FormmodeLog formmodeLog = new FormmodeLog();
	/**
	 * 获得新插入的模块id的数据
	 * 
	 * @param billtablename
	 * @return
	 */
	
	private static Map<String, Object> columnCache=new HashMap<String, Object>();
	private static Map<String, Byte[]> myLock=new HashMap<String, Byte[]>();
	
	private synchronized Byte[] getLockKey(String billtablename){
		if(!myLock.containsKey(billtablename)){
			myLock.put(billtablename, new Byte[0]);
		}
		return myLock.get(billtablename);
	} 
	
	/**
	 * 给表上添加MODEUUID列，并添加索引,如果列已经存在，则仅在列上添加索引
	 * @param tablename
	 */
	public void addModeUUIDColumnForTable(String tablename){
		ConnectionPool pool = ConnectionPool.getInstance();
		WeaverConnection conn = pool.getConnection();
		if (conn == null) {
            new SQLException("Null connection");
        }
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			if(st == null) {
				new SQLException("Null statement");
			}
			if(!columnCache.containsKey(tablename)){
				synchronized (getLockKey(tablename)) {
					if(columnCache.containsKey(tablename)){
						return ;
					}
					//判断模块表单是否有UUID
					String dbType = conn.getDBType();
					if("oracle".equals(dbType)){
						String sql = "select COUNT(column_name) from user_tab_columns where table_name = UPPER('"+tablename+"') and column_name= UPPER('MODEUUID')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEUUID VARCHAR2(100) ";
								st.execute(sql);
							}
						}
					}else if("mysql".equals(dbType)){
						String dbname = CommonConstant.DB_MYSQL_SCHEMA;
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modeuuid')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEUUID VARCHAR(100) ";
								st.execute(sql);
							}
						}
					}
					else if("postgresql".equals(dbType))
					{
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modeuuid')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEUUID VARCHAR(100) ";
								st.execute(sql);
							}
						}
					}
				    else if("sqlserver".equals(dbType)){
						String sql = "SELECT SYS.objects.name tablename,SYS.columns.name coluname FROM SYS.objects JOIN SYS.columns ON SYS.objects.object_id=SYS.columns.object_id  WHERE SYS.columns.name='MODEUUID'  and SYS.objects.name='"+tablename+"'";
						rs = st.executeQuery(sql);				
						if(!rs.next()){
							sql = "ALTER TABLE "+tablename+" ADD MODEUUID VARCHAR(100) ";
							st.execute(sql);
						}
					}	
					//在MODEUUID上添加索引
					ModeFormHelper.addIndexByTable(tablename,"MODEUUID");
					columnCache.put(tablename, true);
				}
			}else{
				//在MODEUUID上添加索引
				ModeFormHelper.addIndexByTable(tablename,"MODEUUID");
			}
		} catch (SQLException e) {
			writeLog(e);
		}finally {
			 try {
				if(st != null) st.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				writeLog(e);
			}
		}
	}
	public void removeKey(String key) {
		columnCache.remove(key);
	}
	
	public int getModeDataNewIdByUUID(String billtablename,
			int formmodeid, int userid, int usertype, String createdata,
			String createtime) {
		int newbillid = 0;
		try {
			newbillid = getModeDataNewIdByUUID(billtablename, formmodeid, userid, usertype, createdata, createtime, null);
		} catch (Exception e) {
			writeLog(e);
			e.printStackTrace();
		}
		return newbillid;
	}
	/**
	 * 获得新插入的模块id的数据
	 * 
	 * @param billtablename
	 * @return
	 * @throws Exception 
	 */
	public int getModeDataNewIdByUUID(String billtablename,
			int formmodeid, int userid, int usertype, String createdata,
			String createtime, final RecordSetTrans rst) throws Exception {

		BaseBean bb = new BaseBean();
		RecordSet rs = new RecordSet();
		RecordSet rs1 = new RecordSet();
		int newbillid = -1;
		addModeUUIDColumnForTable(billtablename);
		String formmodeidStr = "NULL";
		String useridStr = "NULL";
		String usertypeStr = "NULL";
		String createdataStr = "NULL";
		String createtimeStr = "NULL";
		if(formmodeid>0){
			formmodeidStr = formmodeid+"";
		}
		if(formmodeid>0){
			useridStr = userid+"";
		}
		if(formmodeid>0){
			usertypeStr = usertype+"";
		}
		if(!StringHelper.isEmpty(createdata)){
			createdataStr = "'"+createdata+"'";
		}
		if(!StringHelper.isEmpty(createtime)){
			createtimeStr = "'"+createtime+"'";
		}
		
		
		String modeuuid = UUID.randomUUID().toString();
		bb.writeLog("getModeDataNewIdByUUID-modeuuid:"+modeuuid);
		bb.writeLog("getModeDataNewIdByUUID-billtablename:"+billtablename);

		String sql = "insert into "
				+ billtablename
				+ "(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID) values( "
				+ formmodeidStr + "," + useridStr + "," + usertypeStr + ","
				+ createdataStr + "," + createtimeStr + ",'"+modeuuid+"' ) ";

		bb.writeLog("getModeDataNewIdByUUID-sql:"+sql);

		if (billtablename != null && !billtablename.trim().equals("")) {
			boolean flag = rs.executeUpdate(sql);
			bb.writeLog("getModeDataNewIdByUUID-flag:"+flag);
			if(flag){
				String sql2 = " select id from "+ billtablename +" where MODEUUID='"+modeuuid+"'";
				rs.executeQuery(sql2);
				if(rs.next()) {
					int newbillid2 = rs.getInt(1);
					bb.writeLog("getModeDataNewIdByUUID=newbillid2="+newbillid2);
					newbillid = Util.getIntValue(Util.null2String(rs.getString("id")));
				}
				bb.writeLog("getModeDataNewIdByUUID===debug===生成billid==="+newbillid+"===sql==="+ sql);
				formmodeLog.debug("===debug===生成billid==="+newbillid+"===sql==="+ sql);
				if(newbillid < 0){
//					int new_billid = queryNewBillid(billtablename,modeuuid,sql);
//					if(new_billid >0){
//						newbillid = new_billid ;
//					}
					sql = " delete from "+billtablename+" where MODEUUID='"+modeuuid+"'";
					bb.writeLog("getModeDataNewIdByUUID===delete-sql:"+sql);
					boolean isTrue = rs.executeUpdate(sql);
					bb.writeLog("getModeDataNewIdByUUID===isTrue:"+isTrue);
					if(isTrue){
						Thread.sleep(1000); // 延迟1秒
						boolean moreFlag = rs.executeUpdate(sql);
						bb.writeLog("getModeDataNewIdByUUID===moreFlag:"+moreFlag);
						if(moreFlag) {
							String moreSql = " select id from " + billtablename + " where MODEUUID='" + modeuuid + "'";
							bb.writeLog("getModeDataNewIdByUUID===moreSql:"+moreSql);
							rs.executeQuery(moreSql);
							if (rs.next()) {
								int newbillid2 = rs.getInt(1);
								newbillid = Util.getIntValue(Util.null2String(rs.getString("id")));
								bb.writeLog("getModeDataNewIdByUUID=newbillid5=" + newbillid);

							}
						}
					}
				}
				bb.writeLog("getModeDataNewIdByUUID===new_billid==="+newbillid);

			}else if ("postgresql".equalsIgnoreCase(rs1.getDBType())) {
				//select *  from pg_class where relkind='S' and relname like '%uf_tyb_202106_jdzd%'
				String tempSql = " select setval('"+billtablename+"_id_seq', max(id)) from  " + billtablename ;
				boolean flag1 = rs1.executeSql(tempSql);
				if (flag1) {
					sql = "select id from "+ billtablename +" where MODEUUID='"+modeuuid+"'";
					rs1.executeSql(sql);
					if(rs1.next()) {
						newbillid = rs1.getInt(1);
					}
					formmodeLog.debug("===debug===postgresql=billid==="+newbillid+"===sql==="+ sql);
				} else {
					formmodeLog.debug("=postgresql==setNewPk=errsql=="+tempSql);
				}
			}else{
				writeLog(">>>>生成billid错误>>>>sql="+ sql);
			}
		}
			
		return newbillid;
	}
	
	
	/**
	 * 获得新插入流程转数据日志id的数据
	 * 
	 * @param billtablename
	 * @return
	 * @throws Exception 
	 */
	public synchronized int getModeDataNewIdByUUID(String billtablename, final RecordSetTrans rst) throws Exception {
		RecordSet rs1 = new RecordSet();
		int newbillid = -1;
		RecordSet rs = new RecordSet();
		addModeUUIDColumnForTable(billtablename);
		String modeuuid = UUID.randomUUID().toString();			
		String sql = "insert into "
				+ billtablename
				+ "(MODEUUID) values( '"+modeuuid+"' ) ";
				if (billtablename != null && !billtablename.trim().equals("")) {
					rs1.executeSql(sql);
					writeLog(">>>>insert>>>>sql="+ sql);
					sql = "select id from "+ billtablename +" where MODEUUID='"+modeuuid+"'";
					rs1.executeSql(sql);
					if(rs1.next()) {					
						newbillid = rs1.getInt(1);
					}
					writeLog(">>>>newbillid=" + newbillid + ">>>>sql="+ sql);
				}
			
		return newbillid;
	}
	
	
	
	
	/**
	 * 获得新插入的模块id的数据
	 * 
	 * @param billtablename
	 * @return
	 */
	public synchronized int getModeDataNewId(String billtablename,
			int formmodeid, int userid, int usertype, String createdata,
			String createtime) {
		int id = 0;
		try {
			id =  getModeDataNewIdByUUID(billtablename, formmodeid, userid, usertype, createdata, createtime, null);
		} catch (Exception e) {
			writeLog(e);
		}
		return id;
	}
	private void writeLog(Object obj) {
		this.formmodeLog.writeLog(obj);
	}
	
	/**
	 * 更新修改人以及修改日期时间信息
	 * 
	 * @param billtablename
	 * @return
	 */
	public void updateModifyInfo(String billtablename) {
		RecordSet rs = new RecordSet();
		String dbtype = rs.getDBType().toLowerCase();
		HashMap columMap = new HashMap();
		String sql = "";
		if(dbtype.equalsIgnoreCase("oracle")) {
            sql = "select COLUMN_NAME from user_tab_columns where table_name=upper('" + billtablename + "') 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('"+billtablename+"') 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('"+billtablename+"') 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='" + billtablename + "' order by c.colid";
        }
        rs.executeQuery(sql);
		while(rs.next()){
			String columname = Util.null2String(rs.getString(1)).toLowerCase();
			columMap.put(columname, columname);
		}
		//如果表里没有modedatamodifier，和modedatamodifydatetime两个字段则加上
		if(dbtype.equals("oracle")){
			if(!columMap.containsKey("modedatamodifier")){
				String altersql = "alter table "+billtablename+" add modedatamodifier integer";
				rs.executeUpdate(altersql);
			}
			if(!columMap.containsKey("modedatamodifydatetime")){
				String altersql ="alter table "+billtablename+" add modedatamodifydatetime varchar2(100)";
				rs.executeUpdate(altersql);
			}
		}else{
			if(!columMap.containsKey("modedatamodifier")){
				String altersql ="alter table "+billtablename+" add modedatamodifier int";
				rs.executeUpdate(altersql);
			}
			if(!columMap.containsKey("modedatamodifydatetime")){
				String altersql = "alter table "+billtablename+" add modedatamodifydatetime varchar(100)";
				rs.executeUpdate(altersql);
			}
		}
//		if(billid>0){
//			//更新最后修改人和最后修改时间
//			sql = "update "+billtablename+" set modedatamodifier=?,modedatamodifydatetime=? where id=?";
//			rs.executeUpdate(sql, userid,modifydatatime,billid);
//		}
	}



	/**
	 * 给表上添加SECLEVEL列
	 * @param tablename
	 */
	public void addSeclevelForTable(String tablename){
		ConnectionPool pool = ConnectionPool.getInstance();
		WeaverConnection conn = pool.getConnection();
		if (conn == null) {
			new SQLException("Null connection");
		}
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			if(st == null) {
				new SQLException("Null statement");
			}
			if(!columnCache.containsKey(tablename+"_seclevel")){
				synchronized (getLockKey(tablename+"_seclevel")) {
					if(columnCache.containsKey(tablename+"_seclevel")){
						return ;
					}
					//判断模块表单是否有SECLEVEL
					String dbType = conn.getDBType();
					if("oracle".equals(dbType)){
						String sql = "select COUNT(column_name) from user_tab_columns where table_name = UPPER('"+tablename+"') and column_name= UPPER('SECLEVEL')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD SECLEVEL INT default  4 ";
								st.execute(sql);
							}
						}
					}
					else if("postgresql".equals(dbType))
					{
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('seclevel')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD SECLEVEL INT   default  4  ";
								st.execute(sql);
							}
						}
					}
					else if("mysql".equals(dbType)){
						String dbname = CommonConstant.DB_MYSQL_SCHEMA;
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('seclevel')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD SECLEVEL INT   default  4  ";
								st.execute(sql);
							}
						}
					}else if("sqlserver".equals(dbType)){
						String sql = "SELECT SYS.objects.name tablename,SYS.columns.name coluname FROM SYS.objects JOIN SYS.columns ON SYS.objects.object_id=SYS.columns.object_id  WHERE SYS.columns.name='SECLEVEL'  and SYS.objects.name='"+tablename+"'";
						rs = st.executeQuery(sql);
						if(!rs.next()){
							sql = "ALTER TABLE "+tablename+" ADD SECLEVEL INT  default  4  ";
							st.execute(sql);

							RecordSet rsUpdateOldData = new RecordSet();
							String sqlUpdateOldData = " update " +tablename+" set SECLEVEL=4 ";
							rsUpdateOldData.execute(sqlUpdateOldData);
						}
					}
					columnCache.put(tablename+"_seclevel", true);
				}
			}
		} catch (SQLException e) {
			writeLog(e);
		}finally {
			try {
				if(st != null) st.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				writeLog(e);
			}
		}
	}


	/**
	 * 给表上添加modesecrettime列 密级期限
	 * @param tablename
	 */
	public void addSecretTimeForTable(String tablename){
		ConnectionPool pool = ConnectionPool.getInstance();
		WeaverConnection conn = pool.getConnection();
		if (conn == null) {
			new SQLException("Null connection");
		}
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			if(st == null) {
				new SQLException("Null statement");
			}
			if(!columnCache.containsKey(tablename+"_modesecrettime")){
				synchronized (getLockKey(tablename+"_modesecrettime")) {
					if(columnCache.containsKey(tablename+"_modesecrettime")){
						return ;
					}
					//判断模块表单是否有modesecrettime
					String dbType = conn.getDBType();
					if("oracle".equals(dbType)){
						String sql = "select COUNT(column_name) from user_tab_columns where table_name = UPPER('"+tablename+"') and column_name= UPPER('MODESECRETTIME')";
						rs = st.executeQuery(sql);

						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODESECRETTIME VARCHAR2(200) ";
								st.execute(sql);
							}
						}
					}
					else if("postgresql".equals(dbType))
					{
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modesecrettime')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODESECRETTIME VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}
					else if("mysql".equals(dbType)){
						String dbname = CommonConstant.DB_MYSQL_SCHEMA;
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modesecrettime')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODESECRETTIME VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}else if("sqlserver".equals(dbType)){
						String sql = "SELECT SYS.objects.name tablename,SYS.columns.name coluname FROM SYS.objects JOIN SYS.columns ON SYS.objects.object_id=SYS.columns.object_id  WHERE SYS.columns.name='MODESECRETTIME'  and SYS.objects.name='"+tablename+"'";
						rs = st.executeQuery(sql);
						if(!rs.next()){
							sql = "ALTER TABLE "+tablename+" ADD MODESECRETTIME VARCHAR(200)  ";
							st.execute(sql);
						}
					}
					columnCache.put(tablename+"_modesecrettime", true);
				}
			}
		} catch (SQLException e) {
			writeLog(e);
		}finally {
			try {
				if(st != null) st.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				writeLog(e);
			}
		}
	}



	/**
	 * 给表上添加ModeEnckey列 密钥
	 * @param tablename
	 */
	public void addModeEnckeyForTable(String tablename){
		ConnectionPool pool = ConnectionPool.getInstance();
		WeaverConnection conn = pool.getConnection();
		if (conn == null) {
			new SQLException("Null connection");
		}
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			if(st == null) {
				new SQLException("Null statement");
			}
			if(!columnCache.containsKey(tablename+"_modeenckey")){
				synchronized (getLockKey(tablename+"_modeenckey")) {
					if(columnCache.containsKey(tablename+"_modeenckey")){
						return ;
					}
					//判断模块表单是否有modesecrettime
					String dbType = conn.getDBType();
					if("oracle".equals(dbType)){
						String sql = "select COUNT(column_name) from user_tab_columns where table_name = UPPER('"+tablename+"') and column_name= UPPER('MODEENCKEY')";
						rs = st.executeQuery(sql);

						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEENCKEY VARCHAR2(200) ";
								st.execute(sql);
							}
						}
					}
					else if("postgresql".equals(dbType))
					{
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modeenckey')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEENCKEY VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}
					else if("mysql".equals(dbType)){
						String dbname = CommonConstant.DB_MYSQL_SCHEMA;
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modeenckey')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODEENCKEY VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}else if("sqlserver".equals(dbType)){
						String sql = "SELECT SYS.objects.name tablename,SYS.columns.name coluname FROM SYS.objects JOIN SYS.columns ON SYS.objects.object_id=SYS.columns.object_id  WHERE SYS.columns.name='MODEENCKEY'  and SYS.objects.name='"+tablename+"'";
						rs = st.executeQuery(sql);
						if(!rs.next()){
							sql = "ALTER TABLE "+tablename+" ADD MODEENCKEY VARCHAR(200)  ";
							st.execute(sql);
						}
					}
					columnCache.put(tablename+"_modeenckey", true);
				}
			}
		} catch (SQLException e) {
			writeLog(e);
		}finally {
			try {
				if(st != null) st.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				writeLog(e);
			}
		}
	}



	/**
	 * 给表上添加ModeCrc列 校验码  modecrc
	 * @param tablename
	 */
	public void addModeCrcForTable(String tablename){
		ConnectionPool pool = ConnectionPool.getInstance();
		WeaverConnection conn = pool.getConnection();
		if (conn == null) {
			new SQLException("Null connection");
		}
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			if(st == null) {
				new SQLException("Null statement");
			}
			if(!columnCache.containsKey(tablename+"_modecrc")){
				synchronized (getLockKey(tablename+"_modecrc")) {
					if(columnCache.containsKey(tablename+"_modecrc")){
						return ;
					}
					//判断模块表单是否有modesecrettime
					String dbType = conn.getDBType();
					if("oracle".equals(dbType)){
						String sql = "select COUNT(column_name) from user_tab_columns where table_name = UPPER('"+tablename+"') and column_name= UPPER('MODECRC')";
						rs = st.executeQuery(sql);

						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODECRC VARCHAR2(200) ";
								st.execute(sql);
							}
						}
					}
					else if("postgresql".equals(dbType))
					{
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('public') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modecrc')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODECRC VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}
					else if("mysql".equals(dbType)){
						String dbname = CommonConstant.DB_MYSQL_SCHEMA;
						String sql = "select COUNT(column_name) from information_schema.COLUMNS where upper(TABLE_SCHEMA)=upper('"+dbname+"') and upper(table_name)=upper('"+tablename+"') and UPPER(column_name)= UPPER('modecrc')";
						rs = st.executeQuery(sql);
						if(rs.next()){
							int column = rs.getInt(1);
							if(column == 0){
								sql = "ALTER TABLE "+tablename+" ADD MODECRC VARCHAR(200)  ";
								st.execute(sql);
							}
						}
					}else if("sqlserver".equals(dbType)){
						String sql = "SELECT SYS.objects.name tablename,SYS.columns.name coluname FROM SYS.objects JOIN SYS.columns ON SYS.objects.object_id=SYS.columns.object_id  WHERE SYS.columns.name='MODECRC'  and SYS.objects.name='"+tablename+"'";
						rs = st.executeQuery(sql);
						if(!rs.next()){
							sql = "ALTER TABLE "+tablename+" ADD MODECRC VARCHAR(200)  ";
							st.execute(sql);
						}
					}
					columnCache.put(tablename+"_modecrc", true);
				}
			}
		} catch (SQLException e) {
			writeLog(e);
		}finally {
			try {
				if(st != null) st.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				writeLog(e);
			}
		}
	}


	public int queryNewBillid(String billtablename,String modeuuid,String sql){
		RecordSet rs = new RecordSet();
		BaseBean bb = new BaseBean();
		int newbillid = -1;
		for(int i=0;i<10;i++){
			try {
				Thread.sleep(500); // 延迟0.5秒
				String sql2 = "select id from "+ billtablename +" where MODEUUID='"+modeuuid+"'";
				rs.executeQuery(sql2);
				if(rs.next()) {
					int newbillid2 = rs.getInt(1);
					bb.writeLog("getModeDataNewIdByUUID===debug===newbillid222==="+newbillid2);
					newbillid = Util.getIntValue(Util.null2String(rs.getString("id")));
				}

				if(newbillid > 0){
					break;
				}
				bb.writeLog("getModeDataNewIdByUUID===debug===生成billid222==="+newbillid+"===sql==="+ sql2);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}

		if(newbillid < 0){
			boolean flag = rs.executeUpdate(sql);
			bb.writeLog("getModeDataNewIdByUUID-flag33:"+flag);
			if(flag) {
				String sql2 = "select id from " + billtablename + " where MODEUUID='" + modeuuid + "'";
				rs.executeQuery(sql2);
				if (rs.next()) {
					int newbillid2 = rs.getInt(1);
					newbillid = Util.getIntValue(Util.null2String(rs.getString("id")));
					bb.writeLog("getModeDataNewIdByUUID==newbillid333===" + newbillid2);
				}
				bb.writeLog("getModeDataNewIdByUUID===debug===生成billid333===" + newbillid + "===sql===" + sql);
				formmodeLog.debug("===debug===生成billid333===" + newbillid + "===sql===" + sql);
			}
		}
		return newbillid;
	}

}
