package psdi.webclient.beans.mxobjcfg;
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 MxattrcfgTableBean 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 = {"ALIAS1","TITLE","HIDDENCONDITION","ORGATTR","ORGTYPE","READONLY","REQUIRED"};
    public MxattrcfgTableBean()
    {
    }

    public void initialize() throws MXException, RemoteException
    {
        /*
         * 初始化表格,重写了DataBean 中的 initialize() 方法。
         */
        super.initialize();
        /*
         * 初始化获得runlogtext_LOG_TEXT表的数据Bean
         */
        DataBean databean = app.getDataBean("mxattrcfg_mxattrcfg_21_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("mxattrcfg_mxattrcfg_21_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("MXATTRCFGID","填写具体值",11L);//MXATTRCFG Unique identification
            //setValue("ALIAS1","Informations de base",11L);// APP attribute name
            setValue("AUTOCAL","0",11L);//MXATTRCFG Automatic calculation
            //setValue("HIDDENCONDITION","Informations sur les champs",11L);// Hide field conditions
            setValue("APPID",getMbo().getOwner().getString("APPID"),11L);//
            setValue("OBJECTNAME",getMbo().getOwner().getString("OBJECTNAME"),11L);//
            setValue("CHILD",getMbo().getOwner().getString("OBJECTNAME"),11L);//
            setValue("READONLY","1",11L);//READONLY
            setValue("REQUIRED","0",11L);//READONLY
            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;
    }
}
