package com.webclient.beans.udworkorder;

import com.sun.source.tree.LambdaExpressionTree;
import org.apache.bcel.generic.RETURN;
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.system.beans.DataBean;

import javax.swing.plaf.synth.SynthTextAreaUI;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

/**
 * 日常维修工单-计划-物料子表DataBean
 * edit 2023-07-03
 */
public class ShowPlanItemLabDataBean extends DataBean {

    public void createLld() throws RemoteException, MXException {
        app.getAppBean().save();
        MboRemote appMbo=app.getAppBean().getMbo();
        String mainStatus=appMbo.getString("STATUS");
        String siteid=appMbo.getString("SITEID");
        if("WAPPR".equalsIgnoreCase(mainStatus) || "EDIT".equalsIgnoreCase(mainStatus)){
            String limtiStr="";
            boolean limflag=false;
            boolean flag=false;
            boolean flag2=false;
            boolean flag3=false;
            DataBean assetDataBean= app.getDataBean("1690359761067");
            Vector<MboRemote> vec=assetDataBean.getSelection();
            if (!vec.isEmpty() && appMbo!=null) {
                for(int i=0;i<vec.size();i++){
                    MboRemote vMbo = (MboRemote)vec.get(i);
                    String smallcatelimti=vMbo.getString("UDCATE.SMALLCATELIMIT");
                    if("Y".equalsIgnoreCase(smallcatelimti)){//	小类(第三层次)限制领用
                        limtiStr+=vMbo.getString("ITEMNUM")+",";
                        limflag=true;
                    }
                    double itemqty=vMbo.getDouble("ITEMQTY");
                    double curtotal=vMbo.getDouble("UDKC.CURBALTOTAL");
                    if(itemqty>curtotal){
                        flag=true;
                    }
                    if(curtotal<=0){
                        flag3=true;
                    }

                }
                if(limflag){
                    throw new MXApplicationException("udworkorder", "smallcatelimti", new String[]{limtiStr} );
                }
                if(flag3){
                    throw new MXApplicationException("udworkorder", "flag3", new String[]{} );
                }

                int msgRet = MXApplicationYesNoCancelException.getUserInput("confirmcontinueid", MXServer.getMXServer(),this.clientSession.getUserInfo());
                if(flag){
                    if (msgRet < 0) {
                        throw new MXApplicationYesNoCancelException("confirmcontinueid", "jspmessages", "canconfirmcontinue");
                    }
                    if (msgRet == 8) {
                        flag2=true;
                    }
                }else{
                    flag2=true;
                }

                if (flag2) {
                    int workorderid=appMbo.getInt("UDWORKORDERID");
                    String userid = this.app.getBeanForApp().getMbo().getUserInfo().getPersonId();
                    MboSetRemote lldSet = appMbo.getMboSet("$UDLLD","UDLLD","UDWORKORDERID="+workorderid+" ");
                    MboRemote lldMbo=null;

                    if(null!=lldSet && !lldSet.isEmpty()){
                        lldMbo=lldSet.getMbo(0);
                        String status=lldMbo.getString("STATUS");
                        //领料单状态为EDIT
                        if("EDIT".equalsIgnoreCase(status)){
                            MboSetRemote lldLineSet=lldMbo.getMboSet("UDLLDLINE");
                            if(null!=lldLineSet ){
                                lldLineSet.deleteAll(2L);
                                lldLineSet.save(11L);
                                if (!vec.isEmpty() && appMbo!=null) {
                                    for(int i=0;i<vec.size();i++){
                                        MboRemote mbo = (MboRemote)vec.get(i);
                                        MboRemote lldLineMbo=lldLineSet.add();
                                        //PARENTID=:UDLLDID AND ORGID=:ORGID AND SITEID=:SITEID
                                        lldLineMbo.setValue("PARENTID", lldMbo.getInt("UDLLDID"));
                                        lldLineMbo.setValue("ORGID", lldMbo.getString("ORGID"));
                                        lldLineMbo.setValue("SITEID", lldMbo.getString("SITEID"));
                                        lldLineMbo.setValue("LINENUM", mbo.getInt("LINENUM"),2L);//序号
                                        lldLineMbo.setValue("ITEMNUM", mbo.getString("ITEMNUM"));
                                        lldLineMbo.setValue("ORDERQTY", mbo.getDouble("ITEMQTY"));
                                        lldLineMbo.setValue("ORDERUNIT", mbo.getString("ITEMQTYUNTI"));
                                        lldLineMbo.setValue("AUFNR", mbo.getString("AUFNR"));
                                        lldLineMbo.setValue("SJYDTYPE", mbo.getString("UDYDLX"));
                                        lldLineMbo.setValue("REMARKS", mbo.getString("REMARKS"));
                                        lldLineMbo.setValue("UDKCID", mbo.getInt("UDKCID"));
                                        lldLineMbo.setValue("BINNUM", mbo.getString("BINNUM"));
                                        lldLineMbo.getThisMboSet().save();
                                    }
                                }
                            }
                        }else{
                                throw new MXApplicationException("udworkorder", "udworkorder_lldnot", new String[]{lldMbo.getString("WONUM")} );
                        }
                    }else{
                        lldMbo=lldSet.add();
                        lldMbo.setValue("CREATEBY", userid);
                        lldMbo.setValue("WOAPPNAME", "UDWORKORDER");
                        lldMbo.setValue("WORKORDER", appMbo.getString("WONUM"));
                        if("GH01".equalsIgnoreCase(siteid) || "TZ01".equalsIgnoreCase(siteid)){
                            lldMbo.setValue("APPNAME", "UDCLLLD");
                            lldMbo.setValue("SJYDTYPE", appMbo.getString("SJYDTYPE"),11L);
                        }else{
                            lldMbo.setValue("APPNAME", "UDLLD");
                            lldMbo.setValue("SJYDTYPE", "Z43",11L);
                        }

                        lldMbo.setValue("UDWORKORDERID", workorderid);
                        String wonum="EAM"+lldMbo.getInt("UDLLDID");
                        lldMbo.setValue("WONUM", wonum,2L);
                        lldMbo.setValue("STATUS", "EDIT",2L);
                        lldMbo.setValue("STATUSDATE", new Date(),2L);
                        lldMbo.setValue("KOSTL", appMbo.getString("KOSTL"),11L);
//                        MboSetRemote personSet=appMbo.getMboSet("$PERSON", "PERSON","PERSONID='"+userid+"' ");
//                        if(null!=personSet && !personSet.isEmpty()){
//                            lldMbo.setValue( "WORKSHOP", personSet.getMbo(0).getString("DEPARTMENT"),11L);
//                        }
//                        String siteid=appMbo.getString("SITEID");
                        lldMbo.setValue( "WORKSHOP", appMbo.getString("ASSET.SDSYBM"));
                        MboSetRemote locSet=appMbo.getMboSet("$LOCATIONS", "LOCATIONS","SITEID='"+appMbo.getString("SITEID")+"' and TYPE='STOREROOM' ");
                        if(null!=locSet && !locSet.isEmpty()){
                            lldMbo.setValue("LOCNUM", locSet.getMbo(0).getString("LOCATION"),11L);
                        }
                        MboSetRemote deptSet=appMbo.getMboSet("$DEPT","DEPT","DEPTNUM='"+appMbo.getString("ASSET.SDSYBM")+"' AND SITEID='"+siteid+"' ");
                        if(null!=deptSet && !deptSet.isEmpty()){
                            lldMbo.setValue("UDZGR", deptSet.getMbo(0).getString("DIRECTOR"),11L);
                        }

                        Date date= MXServer.getMXServer().getDate();
                        String createdate=new SimpleDateFormat("yyyyMMdd").format(date);
                        lldMbo.setValue("DESCRIPTION", siteid+"-"+userid+"-"+createdate,2L);

                        lldMbo.getThisMboSet().save();
                        MboSetRemote lldLineSet=lldMbo.getMboSet("UDLLDLINE");
                        if(null!=lldLineSet ){
                            if (!vec.isEmpty() && appMbo!=null) {
                                for(int i=0;i<vec.size();i++){
                                    MboRemote mbo = (MboRemote)vec.get(i);
                                    MboRemote lldLineMbo=lldLineSet.add();
                                    //PARENTID=:UDLLDID AND ORGID=:ORGID AND SITEID=:SITEID
                                    lldLineMbo.setValue("PARENTID", lldMbo.getInt("UDLLDID"));
                                    lldLineMbo.setValue("ORGID", lldMbo.getString("ORGID"));
                                    lldLineMbo.setValue("SITEID", lldMbo.getString("SITEID"));
                                    lldLineMbo.setValue("LINENUM", mbo.getInt("LINENUM"),2L);//序号
                                    lldLineMbo.setValue("ITEMNUM", mbo.getString("ITEMNUM"));
                                    lldLineMbo.setValue("ORDERQTY", mbo.getDouble("ITEMQTY"));
                                    lldLineMbo.setValue("ORDERUNIT", mbo.getString("ITEMQTYUNTI"));
                                    lldLineMbo.setValue("AUFNR", mbo.getString("AUFNR"));
                                    lldLineMbo.setValue("SJYDTYPE", mbo.getString("UDYDLX"));
                                    lldLineMbo.setValue("REMARKS", mbo.getString("REMARKS"));
                                    lldLineMbo.setValue("UDKCID", mbo.getInt("UDKCID"));
                                    lldLineMbo.setValue("BINNUM", mbo.getString("BINNUM"));
                                    lldLineMbo.getThisMboSet().save();
                                }
                            }
                        }
                    }

                    StringBuffer newUrl = new StringBuffer(this.clientSession.getMaximoRequestURI());
                    if("GH01".equalsIgnoreCase(siteid) || "TZ01".equalsIgnoreCase(siteid)){
                        newUrl.append("?event=gotoapp&value=udcllld&uniqueid="+lldMbo.getInt("UDLLDID"));
                    }else{
                        newUrl.append("?event=gotoapp&value=udlld&uniqueid="+lldMbo.getInt("UDLLDID"));
                    }
                    this.clientSession.getCurrentApp().put("forcereload", "true");
                    this.clientSession.gotoApplink(newUrl.toString());
                }
            }else{
                throw new MXApplicationException("udworkorder", "nogetselection", new String[0] );
            }
        }else{
            throw new MXApplicationException("udworkorder", "udworkorder_not", new String[0] );
        }
    }





    /**
     * 加入需求申请池
     * @throws RemoteException
     * @throws MXException
     */
    public void createprtb() throws RemoteException, MXException {
        app.getAppBean().save();
        MboRemote appMbo=app.getAppBean().getMbo();
        String mainStatus=appMbo.getString("STATUS");
        int workorderid=appMbo.getInt("UDWORKORDERID");
        String userid = this.app.getBeanForApp().getMbo().getUserInfo().getPersonId();
        MboSetRemote prSet = appMbo.getMboSet("$PR","PR","APPNAME='UDPRTB' AND UDWORKORDERID="+workorderid+" ");
        MboRemote prMbo=null;
//        String limtiStr="";
        boolean limflag=false;
        boolean flag=true;
        DataBean assetDataBean= app.getDataBean("1690359761067");
        Vector<MboRemote> jhWlSet=assetDataBean.getSelection();
        if (!jhWlSet.isEmpty() && appMbo!=null) {
            for (int i = 0; i < jhWlSet.size(); i++) {
                MboRemote vMbo = (MboRemote) jhWlSet.get(i);
                String spmssc=vMbo.getString("ITEM.Z_SPMSSC");
                if(!"是".equalsIgnoreCase(spmssc)){
//                    limtiStr+=vMbo.getString("ITEMNUM")+",";
                    limflag=true;
                    flag=false;
                }
            }
        }

        if(limflag){
//            throw new MXApplicationException("udworkorder", "limtiStrlimflag", new String[]{limtiStr} );
            int msgRet = MXApplicationYesNoCancelException.getUserInput("confirmcontinueid", MXServer.getMXServer(),this.clientSession.getUserInfo());
            if (msgRet < 0) {
                throw new MXApplicationYesNoCancelException("confirmcontinueid", "createprtbmessages", "canconfirmcontinue");
            }
            if (msgRet == 8) {
                flag=true;
            }
        }

        if(flag && null!=jhWlSet && !jhWlSet.isEmpty()){
            if(null!=prSet && !prSet.isEmpty()){
                prMbo=prSet.getMbo(0);
                String status=prMbo.getString("STATUS");
                if("WAPPR".equalsIgnoreCase(status)){
                    MboSetRemote prlineSet=prMbo.getMboSet("PRLINE");
                    if(null!=prlineSet ){
                        prlineSet.deleteAll(2L);
                        prlineSet.save(11L);
                        if(null!=jhWlSet && !jhWlSet.isEmpty()){
                            for(int i=0;i<jhWlSet.size();i++){
                                MboRemote mbo=jhWlSet.get(i);
                                MboSetRemote itemSet=mbo.getMboSet("$ITEM","ITEM","ITEMNUM='"+mbo.getString("ITEMNUM")+"'");
                                if(null!=itemSet && !itemSet.isEmpty()){
                                    String zspmssc=itemSet.getMbo(0).getString("Z_SPMSSC");
                                    if("是".equalsIgnoreCase(zspmssc)){
                                        MboRemote prlineMbo=prlineSet.add();
                                        //prnum=:prnum and siteid=:siteid
                                        prlineMbo.setValue("prnum", prMbo.getString("prnum"),2L);
                                        prlineMbo.setValue("SITEID", prMbo.getString("SITEID"),2L);
                                        prlineMbo.setValue("ORGID", prMbo.getString("ORGID"),2L);
                                        prlineMbo.setValue("PRLINENUM", mbo.getInt("LINENUM")+"",2L);//序号
                                        prlineMbo.setValue("ITEMNUM", mbo.getString("ITEMNUM"),2L);
                                        prlineMbo.setValue("ORDERQTY", mbo.getDouble("ITEMQTY"),2L);
                                        prlineMbo.setValue("ORDERUNIT", mbo.getString("ITEMQTYUNTI"),2L);
                                        prlineMbo.setValue("CONVERSION",1,2L);
                                        prlineMbo.getThisMboSet().save(2L);
                                    }
                                }
                            }
                        }
                    }
                }else{
                    throw new MXApplicationException("udworkorder", "udworkorder_prnot", new String[]{prMbo.getString("UDPRNUM")} );
                }
            }else{
                prMbo=prSet.add();
                prMbo.setValue("requestedby", userid,2L);
                prMbo.setValue("APPNAME", "UDPRTB",2L);
                prMbo.setValue("UDWORKORDERID", workorderid,2L);
                prMbo.setValue("UDPRNUM", prMbo.getInt("PRID")+"",2L);
                prMbo.setValue("STATUS", "WAPPR",2L);
                prMbo.setValue("STATUSDATE", new Date(),2L);
                prMbo.getThisMboSet().save();

                MboSetRemote prlineSet=prMbo.getMboSet("PRLINE");
                if(null!=prlineSet ){
                    if(null!=jhWlSet && !jhWlSet.isEmpty()){
                        for(int i=0;i<jhWlSet.size();i++){
                            MboRemote mbo=jhWlSet.get(i);
                            MboRemote prlineMbo=prlineSet.add();
                            //prnum=:prnum and siteid=:siteid
                            prlineMbo.setValue("prnum", prMbo.getString("prnum"),2L);
                            prlineMbo.setValue("SITEID", prMbo.getString("SITEID"),2L);
                            prlineMbo.setValue("ORGID", prMbo.getString("ORGID"),2L);
                            prlineMbo.setValue("PRLINENUM", mbo.getInt("LINENUM")+"",2L);//序号
                            prlineMbo.setValue("ITEMNUM", mbo.getString("ITEMNUM"),2L);
                            prlineMbo.setValue("ORDERQTY", mbo.getDouble("ITEMQTY"),2L);
                            prlineMbo.setValue("ORDERUNIT", mbo.getString("ITEMQTYUNTI"),2L);
                            prlineMbo.setValue("CONVERSION",1,2L);
                            prlineMbo.getThisMboSet().save();
                        }
                    }
                }

            }
            StringBuffer newUrl = new StringBuffer(this.clientSession.getMaximoRequestURI());
            newUrl.append("?event=gotoapp&value=udprtb&uniqueid="+prMbo.getInt("PRID"));
            this.clientSession.getCurrentApp().put("forcereload", "true");
            this.clientSession.gotoApplink(newUrl.toString());
        }else{
            throw new MXApplicationException("udworkorder", "nogetselection", new String[0] );
        }

    }
}
