package com.webclient.beans.udlld;

import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.util.Date;
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.MXApplicationYesNoCancelException;
import psdi.util.MXException;
import psdi.webclient.system.beans.AppBean;
import psdi.webclient.system.beans.ResultsBean;
import psdi.webclient.system.controller.WebClientEvent;

/**
 * 领料申请单AppBean
 * edit 20230423
 */
public class UdLldAppBean 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_OVERSEA_REQUISITION(appMbo);
            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(),2L);
                        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("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                        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("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                    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("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                    appMbo.setValue("SPMSRETURN", result.toString(), 2L);
                }else{
                    appMbo.setValue("SPMSRETURN", result.toString().substring(0,1333), 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) {
            e.printStackTrace();
        }
    }


    public int DELETE() throws MXException, RemoteException {
        MboRemote appMbo=app.getAppBean().getMbo();
        String personid=appMbo.getThisMboSet().getUserInfo().getPersonId();
        String createby=appMbo.getString("CREATEBY");
        if(!personid.equalsIgnoreCase(createby)){
            throw new MXApplicationException("udlld", "udlld_d", new String[]{} );
        }
        return super.DELETE();
    }
}