package psdi.webclient.beans.dept;
import java.rmi.RemoteException;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.security.ConnectionKey;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXException;
import psdi.util.MXSession;
import psdi.webclient.system.beans.DataBean;
import psdi.webclient.system.beans.AppBean;
import psdi.webclient.system.controller.Utility;
import com.dbms.dbcon.*;
import com.report.*;
import com.coolservlets.beans.method.DateConvertor;
import com.coolservlets.beans.method.genMethod;
public class DeptAppBean extends AppBean {
	String[] FldMain = {"SITEID","DESCRIPTION","ACTIVE"};

	public DeptAppBean()
	{
		//super();
	}
	public void initializeApp() throws MXException, RemoteException
	{
		super.initializeApp();	
	}
	public void initialize() throws MXException, RemoteException
	{
		super.initialize();
	}
	public synchronized boolean fetchRecordData() throws MXException, RemoteException
	{
		super.fetchRecordData();
		if(this.getMbo() != null){
			int loop = FldMain.length;
			for(int i=0;i<loop;i++){
				getMbo().setFieldFlag(FldMain[i],7L,true);
			}
		}
		return true;
	}

	public int SAVE()throws MXException, RemoteException{
		super.SAVE();
		return 1;
	}

	public int INSERT() throws MXException, RemoteException {
		super.INSERT();
		//setValue("SITEID","Principaux messages",11L);//SITE Site
		//setValue("ACTIVE","Autres informations",11L);//SITE Active
		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;
	}
	/**
     * 删除行记录
     */
	public int DELETE() throws MXException, RemoteException {
		MboRemote mboMain = this.app.getAppBean().getMbo();
		MboSetRemote lineSet = mboMain.getMboSet("ASSAYGROUP_ASSAYPROJECT");
		int nloop = lineSet.count();
		for (int i = 0; i < nloop; i++) {
			MboRemote line = lineSet.getMbo(i);
			line.delete();
		}
		this.SAVE();
		return super.DELETE();
	}
}
