package com.webclient.beans.udpdplan;

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

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

/**
 * 盘点任务AppBean
 * edit 20240624
 */
public class UdpdAppBean extends AppBean {

    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 SUBMIT() throws Exception {
        MboRemote appMbo=app.getAppBean().getMbo();
        WebClientEvent webclientevent;
        webclientevent = this.clientSession.getCurrentEvent();
        if(!"EDIT".equalsIgnoreCase(appMbo.getString("STATUS")) ){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("release", "release_no", new String[0]));
            return;
        }

        int wpcount=appMbo.getMboSet("$UDPDLINE", "UDPDLINE", "PDNUM='"+appMbo.getString("PDNUM")+"' and STATUS='WP' ").count();
        if(wpcount>0){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("release", "release_wpcount", new String[0]));
            return;
        }
        String pdjl=appMbo.getString("PDRESULT");
        if(null==pdjl || "".equalsIgnoreCase(pdjl)){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("release", "release_pdjl", new String[0]));
            return;
        }

        appMbo.setValue("STATUS", "COMP", 2L);
        app.getAppBean().save();
        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("release", "releasesub_success", new String[]{}));
        app.getAppBean().reloadTable();
        app.getAppBean().refreshTable();

        }
    }



