package com.webclient.beans.udpr;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXApplicationYesNoCancelException;
import psdi.util.MXException;
import psdi.webclient.beans.pr.PRAppBean;
import psdi.webclient.system.beans.ResultsBean;
import psdi.webclient.system.controller.WebClientEvent;

import java.rmi.RemoteException;
import java.util.Date;


public class UdPrAppBean extends PRAppBean {

    public void initializeApp() throws MXException, RemoteException {
        super.initializeApp();
        ResultsBean resultsbean = app.getResultsBean();
        MboSetRemote set=resultsbean.getMboSet();
        String userid=set.getUserInfo().getPersonId();
        MboSetRemote pset= MXServer.getMXServer().getMboSet("person", set.getUserInfo());
        pset.setWhere("personid='"+userid+"'");
        if(!pset.isEmpty() && pset.count()>0){
            MboRemote per=pset.getMbo(0);
            //权限等级
            String aut=per.getString("UDAUTGRADE");
            String orgid=per.getString("LOCATIONORG");
            String siteid=per.getString("LOCATIONSITE");

            if("Level 1".equalsIgnoreCase(aut)){
                set.setWhere(" 1=1 ");
            }else if("Level 2".equalsIgnoreCase(aut)){
                set.setWhere(" ORGID='"+orgid+"' ");
            }else if("Level 3".equalsIgnoreCase(aut)){
                set.setWhere(" SITEID='"+siteid+"' ");
            }else if("Level 4".equalsIgnoreCase(aut)){
                set.setWhere(" ORGID='"+orgid+"' and SITEID='"+siteid+"' and REQUESTEDBY='"+userid+"'");
            }else{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    /**
     * 合并车间需求申请池
     */
    public void MWDAPOOL()throws RemoteException, MXException {
        app.getAppBean().save();
        MboRemote appMbo=app.getAppBean().getMbo();
        String mainStatus=appMbo.getString("STATUS");
        if("WAPPR".equalsIgnoreCase(mainStatus)){
            String siteid=appMbo.getString("SITEID");
            String orgid=appMbo.getString("ORGID");
            String deptnum=appMbo.getString("DEPTNUM");

            //待合并的需求明细行
            MboSetRemote prlineSet = MXServer.getMXServer().getMboSet("PRLINE",MXServer.getMXServer().getSystemUserInfo());
            String sqlWhere="PRLINEID IN(SELECT max(PRLINEID)  FROM PRLINE WHERE PRNUM " +
                    "in(SELECT PRNUM FROM PR WHERE APPNAME='UDPRTB' AND STATUS='WAPPR' " +
                    "AND ORGID='"+orgid+"' AND SITEID='"+siteid+"' AND DEPTNUM='"+deptnum+"') " +
                    "AND ORGID='"+orgid+"' AND SITEID='"+siteid+"'  GROUP BY ITEMNUM,ORGID,SITEID)";
            prlineSet.setWhere(sqlWhere);
            prlineSet.reset();
            if(null!=prlineSet && !prlineSet.isEmpty()){
                //车间需求申请明细行表
                MboSetRemote cjprlineSet = appMbo.getMboSet("PRLINE");
                for(int i=0;i<prlineSet.count();i++){
                    MboRemote prinleMbo=prlineSet.getMbo(i);
                    String itemnum=prinleMbo.getString("ITEMNUM");
                    MboSetRemote prlineSet2=appMbo.getMboSet("$PRLINE", "PRLINE", "PRNUM in(SELECT PRNUM FROM PR WHERE APPNAME='UDPRTB' AND STATUS='WAPPR' AND ORGID='"+orgid+"' AND SITEID='"+siteid+"' AND DEPTNUM='"+deptnum+"') AND ORGID='"+orgid+"' AND SITEID='"+siteid+"'  AND ITEMNUM ='"+itemnum+"'");
                    double orderqtySum=prlineSet2.sum("ORDERQTY");
                    String sqlstr="itemnum='"+itemnum+"' and prnum='"+appMbo.getString("prnum")+"' and siteid='"+siteid+"' and orgid='"+orgid+"'";
                    MboSetRemote prlineAddSet = MXServer.getMXServer().getMboSet("PRLINE", MXServer.getMXServer().getSystemUserInfo());
                    prlineAddSet.setWhere(sqlstr);
                    prlineAddSet.reset();
                    if(null!=prlineAddSet && !prlineAddSet.isEmpty()){
                        MboRemote prlineXyMbo=prlineAddSet.getMbo(0);
                        double hbhOrderqty=prlineXyMbo.getDouble("ORDERQTY")+orderqtySum;
                        prlineXyMbo.setValue("ORDERQTY", hbhOrderqty,2L);
                        prlineXyMbo.setValue("APPRQUANTITY",hbhOrderqty,2L);
                        prlineXyMbo.getThisMboSet().save();
                    }else{
                        MboRemote cjprlineMbo=cjprlineSet.add();
                        //prnum=:prnum and siteid=:siteid
                        cjprlineMbo.setValue("prnum", appMbo.getString("prnum"),2L);
                        cjprlineMbo.setValue("siteid", appMbo.getString("siteid"),2L);
                        cjprlineMbo.setValue("ITEMNUM",itemnum);
                        cjprlineMbo.setValue("CONVERSION",1,2L);
                        if(null!=prlineSet2 && !prlineSet2.isEmpty()){

                            cjprlineMbo.setValue("ORDERQTY",orderqtySum);
                            cjprlineMbo.setValue("APPRQUANTITY",orderqtySum);
                            for(int j=0;j<prlineSet2.count();j++){
                                MboRemote prlineMbo2=prlineSet2.getMbo(j);
                                prlineMbo2.setValue("UDPRNUM", appMbo.getString("prnum"), 2L);
                                prlineMbo2.setValue("UDPRLINENUM", cjprlineMbo.getInt("PRLINENUM")+"", 2L);
                                prlineMbo2.getThisMboSet().save();
                            }
                        }
                        cjprlineMbo.getThisMboSet().save();
                    }

                }
                MboSetRemote prSet = MXServer.getMXServer().getMboSet("PR",MXServer.getMXServer().getSystemUserInfo());
                prSet.setWhere("APPNAME='UDPRTB' AND STATUS='WAPPR' AND ORGID='"+orgid+"' AND SITEID='"+siteid+"' AND DEPTNUM='"+deptnum+"'");
                prSet.reset();
                if(null!=prSet && !prSet.isEmpty()){
                    for(int k=0;k<prSet.count();k++){
                        MboRemote prMbo=prSet.getMbo(k);
                        prMbo.setValue("STATUS", "APPR",11L);
                    }
                    prSet.save();
                }
                System.gc();
                throw new MXApplicationException("udpr", "udpr_hb", new String[0] );
            }else{
                throw new MXApplicationException("udpr", "udpr_nohb", new String[0] );
            }
        }else{
            throw new MXApplicationException("udpr", "udpr_snohb", new String[0] );
        }

    }


    /**
     * 提交到工厂按钮
     */
    public void SUBTOFACTORY() throws RemoteException, MXException {
        app.getAppBean().save();
        MboRemote appMbo=app.getAppBean().getMbo();
        WebClientEvent webclientevent;
        webclientevent = this.clientSession.getCurrentEvent();
        if("COMTF".equalsIgnoreCase(appMbo.getString("STATUS"))){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_comtf", new String[0]));
            return;
        }

        String userid = this.app.getBeanForApp().getMbo().getUserInfo().getPersonId();//当前登陆人
        String reqBy=appMbo.getString("REQUESTEDBY");//申请人
        if(!userid.equalsIgnoreCase(reqBy)){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_fbrtj", new String[0]));
            return;
        }

        MboSetRemote prlineSetkc = MXServer.getMXServer().getMboSet("PRLINE", MXServer.getMXServer().getSystemUserInfo());
        prlineSetkc.setWhere("PRLINEID in(SELECT max(PRLINEID) FROM PRLINE WHERE prnum='"+appMbo.getString("PRNUM")+"' and siteid='"+appMbo.getString("SITEID")+"' AND ORGID='"+appMbo.getString("ORGID")+"' GROUP BY ITEMNUM)");
        prlineSetkc.reset();
        if(null!=prlineSetkc && !prlineSetkc.isEmpty()){
            String str="";
            boolean sqFlag=false;
            for(int i=0;i<prlineSetkc.count();i++){
                MboRemote prlineMbo=prlineSetkc.getMbo(i);
                //系统自动触发数量+手动申请数量
                MboSetRemote prhset=appMbo.getMboSet("$PRLINE","PRLINE","prnum='"+appMbo.getString("PRNUM")+"' and siteid='"+appMbo.getString("SITEID")+"' AND ORGID='"+appMbo.getString("ORGID")+"' AND ITEMNUM='"+prlineMbo.getString("ITEMNUM")+"'");
                double apprquantity=0.0;
                if(null!=prhset && !prhset.isEmpty()){
                    apprquantity=prhset.sum("APPRQUANTITY");//批准数量(申请数量)
                }

                //库存数量
                MboSetRemote udkcSet=appMbo.getMboSet("$UDKC","UDKC","ITEMNUM='"+prlineMbo.getString("ITEMNUM")+"' and SITEID='"+appMbo.getString("SITEID")+"'");
                double kcsl=0.0;
                if(null!=udkcSet && !udkcSet.isEmpty()){
                    kcsl=udkcSet.sum("CURBALTOTAL");
                }

                //在途数量
                MboSetRemote uditemsafeSet=appMbo.getMboSet("UDITEMSAFE", "UDITEMSAFE", "ITEMNUM='"+prlineMbo.getString("ITEMNUM")+"' and SITEID='"+appMbo.getString("SITEID")+"' AND ORGID='"+appMbo.getString("ORGID")+"'");
                double ztsl=0.0;
                double zgkcl=0.0;
                if(null!=uditemsafeSet && !uditemsafeSet.isEmpty()){
                    ztsl=uditemsafeSet.sum("TRANSQT");//在途数量
                    zgkcl=uditemsafeSet.sum("UPQT");//最高库存量
                }

                //清单数量
                MboSetRemote qdslSet=appMbo.getMboSet("$UDMATUSETRANS", "UDMATUSETRANS","ITEMNUM ='"+prlineMbo.getString("ITEMNUM")+"' AND ORGID='"+appMbo.getString("ORGID")+"' AND SITEID='"+appMbo.getString("SITEID")+"'" );
                double qdsl=0.0;
                if(null!=qdslSet && !qdslSet.isEmpty()){
                    qdsl=qdslSet.sum("QUANTITY");
                }

                if((apprquantity+kcsl+ztsl+qdsl)>zgkcl && zgkcl>0){
                    str+=prlineMbo.getString("ITEMNUM")+",";
                    sqFlag=true;
                }
            }
            if(sqFlag){
                this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_qgdyz", new String[]{str}));
                return;
            }
        }

        appMbo.setFieldFlag("STATUS", 7L, false);
        appMbo.setFieldFlag("STATUSDATE", 7L, false);
        appMbo.setValue("STATUS", "COMTF",2L);
        appMbo.setValue("STATUSDATE", new Date());
        appMbo.setFieldFlag("STATUS", 7L, true);
        appMbo.setFieldFlag("STATUSDATE", 7L, true);
        this.SAVE();
        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_success", new String[0]));
    }

    @Override
    public int DELETE() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        if(appMbo.isNew()){
            return super.DELETE();
        }
        int msgRet = MXApplicationYesNoCancelException.getUserInput("confirmcontinueid", MXServer.getMXServer(),this.clientSession.getUserInfo());
        if (msgRet < 0) {
            throw new MXApplicationYesNoCancelException("confirmcontinueid", "jspmessages", "deletconfirm");
        }
        if (msgRet == 8) {
            MboSetRemote pmSet=appMbo.getMboSet("$PR", "PR", "UDPRNUM='"+appMbo.getString("UDPRNUM")+"' AND ORGID='"+appMbo.getString("ORGID")+"' AND SITEID='"+appMbo.getString("SITEID")+"' AND STATUS='WAPPR' ");
            if(null!=pmSet && !pmSet.isEmpty()){
                pmSet.deleteAll(2L);
                pmSet.save();
                this.clientSession.showMessageBox(this.clientSession.getCurrentEvent(), "system", "deleterecord", (Object[])null);
                this.gotoTab(this.clientSession, "list");
                initializeApp();
            }else{
                return super.DELETE();
            }
        }
        return 1;
    }
}
