package com.webclient.beans.udlld;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.spms.Spms2EamHandler;
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 20230423
 */
public class UdTkdAppBean 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 CREATEBY='"+userid+"'");
            }else{
                //没有等级
                set.setWhere("1=2");
            }
        }
        pset.close();

    }

    public void COMSPMS() throws RemoteException, MXException {
        MboRemote appMbo = this.app.getAppBean().getMbo();
        WebClientEvent webclientevent = this.clientSession.getCurrentEvent();
        if ("COMP".equalsIgnoreCase(appMbo.getString("STATUS")) || "CAN".equalsIgnoreCase(appMbo.getString("STATUS"))) {
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_dup", new String[0]));
            return;
        }
        try {
            JSONObject result = new Spms2EamHandler().Z_EAM_RETURN_REQUISITION(appMbo);
            appMbo.setValue("SPMSRETURN", result.toString(), 2L);
            JSONObject RESPONSE = (JSONObject) result.get("RESPONSE");
            if(null!=RESPONSE){
                JSONObject RETURN_DATA = (JSONObject) RESPONSE.get("RETURN_DATA");
                if(null!=RETURN_DATA){
                    String code=  RETURN_DATA.getString("code");
                    if("200".equalsIgnoreCase(code)){
                        appMbo.setValue("SPMSSTAUTS", "TOSPMSSUCCESS", 2L);
                        appMbo.setValue("STATUSDATE", new Date());
                        appMbo.setValue("SPMSRETURN", result.toString(), 2L);
                        appMbo.getThisMboSet().save();
                        app.getAppBean().refreshTable();
                        app.getAppBean().reloadTable();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_success", new String[]{RETURN_DATA.getString("msg")}));
                    }else{
                        appMbo.setValue("SPMSRETURN", result.toString(), 2L);
                        appMbo.getThisMboSet().save();
                        app.getAppBean().refreshTable();
                        app.getAppBean().reloadTable();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_error", new String[]{RETURN_DATA.getString("msg")}));
                    }
                }else{
                    appMbo.setValue("SPMSRETURN", result.toString(), 2L);
                    appMbo.getThisMboSet().save();
                    app.getAppBean().refreshTable();
                    app.getAppBean().reloadTable();
                    this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_error", new String[0]));
                }
            }else{
                appMbo.setValue("SPMSRETURN", result.toString(), 2L);
                appMbo.getThisMboSet().save();
                app.getAppBean().refreshTable();
                app.getAppBean().reloadTable();
                this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_error", new String[0]));
            }
        } catch (Exception e) {
            appMbo.getThisMboSet().save();
            e.printStackTrace();
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("comspms", "comspms_error", new String[0]));
        }
    }
}