package psdi.webclient.beans.dept;
import java.rmi.RemoteException;
import psdi.util.MXApplicationException;
import psdi.util.MXException;
import psdi.webclient.system.beans.DataBean;
import psdi.webclient.system.controller.*;
import com.coolservlets.beans.method.DateConvertor;
import com.coolservlets.beans.method.genMethod;
import com.dbms.dbcon.*;
import com.report.*;
import psdi.security.ConnectionKey;
public class DeptcostTableBean extends DataBean
{
    //String[] FldSingle = {"PROJECTID","PROJECTNAME","PROVINCE","CITY","COUNTY","VILLAGE","POSITION","LAND","LANDFORM","POWERTYPE","HEIGHTASL","FRESOURCE","DESIGDEPT","CONSTRUCTION","CAPINSTALL","UNITCAPACITY","POWERYEAR","EQUHOURS","STAINVESTMENT","DYNINVESTMENT","INVUNITKW","INVUNITELEDEG","CONINTEREST","WPCOSTUNIT","TDCOSTUNIT","FUCOSTUNIT","SBUNITPRICE","CONEXCAVATION","CONBACKFILL","CONCRETE","REINFORCED","TOWERDRUM","PERMANENTLAND","TEMPLAND","FIRGENERATING","TOTALTIME"};
	String[] FldSingle = {"COSTUM","DESCRIPTION","COSTRANGE","EFFSTART","EFFFINISH","ENDESCRIPTION","REMARKS"};
	public DeptcostTableBean()
	{
	}

	public void initialize() throws MXException, RemoteException
	{
		/*
		 * 初始化表格,重写了DataBean 中的 initialize() 方法。
		 */
		super.initialize();
		/*
		 * 初始化获得runlogtext_LOG_TEXT表的数据Bean
		 */
		DataBean databean = app.getDataBean("deptcost_deptcost_28_table");
		int loop = databean.count();
		for(int i=0;i<loop;i++){
			if(databean.getMbo(i) != null){
				int nloop = FldSingle.length;
				for(int n=0;n<nloop;n++){
					databean.getMbo(i).setFieldFlag(FldSingle[n],7L,false);
				}
			}
		}
		databean.refreshTable();
		sessionContext.queueRefreshEvent();
	}
	
	public int addrow() throws MXException 

	{
		try {
			super.addrow();
			DataBean databean = app.getDataBean("deptcost_deptcost_28_table");
			int count = databean.count();
			//String OwnerName = getMbo().getOwner().getName();
			//String sKeystr  = "";
			//if(OwnerName.equalsIgnoreCase("BIDPLAN")){
			//	sKeystr = getMbo().getOwner().getString("BIDCODE");
			//	sKeystr = (sKeystr == null)?"":sKeystr.trim(); 
			//}
			//setValue("PREKEYNUM",sKeystr,11L);
			//setValue("DOCNUM","BFNY-DOC-"+Integer.toString(count),11L);
			//setValue("DEPTCOSTID","填写具体值",11L);//DEPTCOST Unique identification
			//setValue("DEPTNUM","填写具体值",11L);//DEPTCOST Department No
			//setValue("COSTUM","Principaux messages",11L);// Cost center No
			//setValue("COSTRANGE","Informations sur la date",11L);// Range
			//setValue("ENDESCRIPTION","Autres informations",11L);// English description
			setValue("ORGID",getMbo().getOwner().getString("ORGID"),11L);//
			setValue("SITEID",getMbo().getOwner().getString("SITEID"),11L);//
			setValue("DEPTNUM",getMbo().getOwner().getString("DEPTNUM"),11L);//DEPTCOST Department No
			return 1;
		}
		catch (RemoteException remoteexception) {
			remoteexception.printStackTrace();
			throw new MXApplicationException("", "添加数据行有误，请与系统管理员联系！");
			//return 0;
		}
	}
	public int showdialog() throws MXException, RemoteException {
		RequestHandler.showDialog(sessionContext, "tabledocedit");
		return 1;
	}

	/*
	 *开始自动设置编号
	 *
	 */
	private  synchronized String  getAutoKey(String p_orgIdStr,String p_siteIdStr,String p_preixStr,String p_startnumStr,ConnectionKey connectionkey) throws MXException, RemoteException
	{
		//MXSession mxsession = sessionContext.getMXSession();
		String reKeyStr = "";
		try
		{
			//MXSession mxsession = sessionContext.getMXSession();
			//查找本年月自动编号的值
			StringBuffer sqllocal = new StringBuffer(300);
			sqllocal.append("select PREFIX,STARTNUM,SEED,SEEDLENGTH ").append(" from ").append("WTAUTOIDRULE").append(" where ORGID ="+"\'"+p_orgIdStr+"\'" + " AND SITEID ="+"\'"+p_siteIdStr+"\'"+ " AND RULETYPE ="+"\'"+"DATECYL"+"\'"
					+ " AND PREFIX ="+"\'"+p_preixStr+"\'"+ " AND STARTNUM ="+"\'"+p_startnumStr+"\'");
			//执行查询，返回一个结果集合
			//System.out.println("sqllocal.toString()"+sqllocal.toString());
			QueryResult result = DBCon.runQuery(sqllocal.toString(),connectionkey);
			if(result.size() >0){
				ResultRow resultRow = result.getRow(0);
				String seedStr = resultRow.getValue("SEED");
				String seedLenStr = resultRow.getValue("SEEDLENGTH");
				int seedLenInt = Integer.parseInt(seedLenStr);
				int newSeedInt = Integer.parseInt(seedStr);
				newSeedInt++;
				String setNewSeed = Integer.toString(newSeedInt);
				int loopCount = seedLenInt - seedStr.length();
				String newSeedStr = "";
				//补前导零
				for(int i=0;i<loopCount;i++){
					newSeedStr = newSeedStr  + "0";
				}
				seedStr = newSeedStr + seedStr;
				reKeyStr = p_preixStr + p_startnumStr + "-"+seedStr;
				StringBuffer sqlupdate = new StringBuffer(300);
				sqlupdate.append("update WTAUTOIDRULE ").append("set SEED =" + setNewSeed).append(" where ORGID ="+"\'"+p_orgIdStr+"\'" + " AND SITEID ="+"\'"+p_siteIdStr+"\'"+ " AND RULETYPE ="+"\'"+"DATECYL"+"\'"
						+ " AND PREFIX ="+"\'"+p_preixStr+"\'"+ " AND STARTNUM ="+"\'"+p_startnumStr+"\'");
				DBCon.runUpdate(sqlupdate.toString(),connectionkey);
			}else{
				reKeyStr = p_preixStr + p_startnumStr + "-001";
				int newSeedInt = 2;
				String setNewSeed = Integer.toString(newSeedInt);
				StringBuffer sqlinsert = new StringBuffer(300);
				sqlinsert.append("insert into WTAUTOIDRULE(ORGID,SITEID,RULETYPE,PREFIX,STARTNUM,SEED,SEEDLENGTH,ROWSTAMP,WTAUTOIDRULEID,HASLD) ").append("values(").append("\'"+p_orgIdStr+"\',\'"+p_siteIdStr+"\',\'"+"DATECYL"+"\',\'"+
						p_preixStr+"\',\'"+p_startnumStr+"\',"+setNewSeed+","+"3"+",\'\',WTAUTOIDRULEIDSEQ.NEXTVAL,0)");
				DBCon.runUpdate(sqlinsert.toString(),connectionkey);
			}
			//p_conn.commit();
		}
		catch(Exception exception) { }
		return reKeyStr;
	}
}
