package com.app.hxqh.spotrun;

import com.coolservlets.beans.method.DateConvertor;
import com.dbms.dbcon.DBCon;
import com.dbms.dbcon.QueryResult;
import com.dbms.dbcon.ResultRow;
import psdi.mbo.*;
import psdi.security.ConnectionKey;
import psdi.server.MXServer;
import psdi.util.MXException;

import java.rmi.RemoteException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SpotrunMbo extends Mbo implements MboRemote {
    public SpotrunMbo(MboSet ms) throws RemoteException, MXException {
        super(ms);
    }

    @Override
    public void init() throws MXException {
        super.init();
        try {
            String[] mainFields = new String[] {"DESCRIPTION"};
            setFieldFlag(mainFields, 7L, true);
            getMboSet("SPOTRUN_SPOTRUNLINES").setFlag(7L, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void add() throws MXException, RemoteException {
        super.add();
//        String app = getThisMboSet().getApp();
//        if("pmexec".equalsIgnoreCase(app)){
//            String orgid=getString("ORGID");
//            String siteid=getString("SITEID");
//            String preixstr ="EXE";
//            String dateStr=new SimpleDateFormat("yyyyMMdd").format(new Date());
//            MboSetRemote sptrunSet=getMboSet("$SPOTRUN", "SPOTRUN","ORGID='"+orgid+"' AND SITEID='"+siteid+"' AND CREATEDATE>=TO_DATE(TO_CHAR(SYSDATE,'yyyy-MM-dd'),'yyyy-MM-dd') and CREATEDATE<TO_DATE(TO_CHAR(SYSDATE+1,'yyyy-MM-dd'),'yyyy-MM-dd')");
//            if(null!=sptrunSet){
//                int count=sptrunSet.count()+1;
//                DecimalFormat de=new DecimalFormat("0000");
//                String sKeynum = preixstr+dateStr+"-"+de.format(count);
//                setValue("SPOTRUNNUM",sKeynum,11L);//SPOTRUN Spot check Case No
//            }
//        }

    }


    public static String getAutoKey(String p_orgIdStr, String p_siteIdStr, String p_preixStr, String p_startnumStr, ConnectionKey connectionkey) throws MXException, RemoteException {
        String reKeyStr = "";
        try {
            //查找本年月自动编号的值
            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+"\'");
            //执行查询，返回一个结果集合
            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;
                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);
            }
        } catch(Exception exception) {
            exception.getCause();
            exception.getMessage();
        }
        return reKeyStr;
    }


}
