package com.webclient.beans.assetdispo;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.oa.EAM2OAAssetdispoHandler;
import psdi.app.iface.oa.EAM2OAOutSerHandler;
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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;

/**
 * 资产处置AppBean
 * edit 2023-09-14
 */
public class AssetdispoAppBean extends AppBean {

    public void ASSTOA() throws IOException, MXException {
        MboRemote appMbo=app.getAppBean().getMbo();
        WebClientEvent webclientevent;
        webclientevent = this.clientSession.getCurrentEvent();
        if("COMP".equalsIgnoreCase(appMbo.getString("STATUS")) || "APPR".equalsIgnoreCase(appMbo.getString("STATUS"))){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_dup", new String[0]));
            return;
        }

        EAM2OAAssetdispoHandler eam2OAAssetdispoHandler = new EAM2OAAssetdispoHandler();
        JSONObject result=eam2OAAssetdispoHandler.Z_EAM_ASSETDISPO_REQUISITION(appMbo);
        if(null!=result){
            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("SUCCESS".equalsIgnoreCase(code)){
                        String doCreReqJsoData = RETURN_DATA.getString("data");
                        JSONObject doCreReqJso2 = JSONObject.parseObject(doCreReqJsoData);
                        String requestid=doCreReqJso2.getString("requestid");
                        if(null!=requestid && !"".equalsIgnoreCase(requestid)){
                            appMbo.setValue("STATUS", "COMP",2L);
                            if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                                appMbo.setValue("OARETURN", result.toString(), 2L);
                            }else{
                                appMbo.setValue("OARETURN", result.toString().substring(0,1333), 2L);
                            }
                            int pcount=appMbo.getInt("PCOUNT");
                            if(pcount<=0){
                                appMbo.setValue("PCOUNT", (1+0), 2L);
                            }else{
                                appMbo.setValue("PCOUNT", (1+pcount), 2L);
                            }
                            appMbo.getThisMboSet().save();
                            app.getAppBean().reloadTable();
                            app.getAppBean().refreshTable();
                            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_srm", new String[]{"OA流程ID号"+requestid+""}));
                        }else{
                            if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                                appMbo.setValue("OARETURN", result.toString(), 2L);
                            }else{
                                appMbo.setValue("OARETURN", result.toString().substring(0,1333), 2L);
                            }
                            appMbo.getThisMboSet().save();
                            app.getAppBean().reloadTable();
                            app.getAppBean().refreshTable();
                            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_oaerro", new String[]{result.toString()}));
                        }
                    }else{
                        if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                            appMbo.setValue("OARETURN", result.toString(), 2L);
                        }else{
                            appMbo.setValue("OARETURN", result.toString().substring(0,1333), 2L);
                        }
                        appMbo.getThisMboSet().save();
                        app.getAppBean().reloadTable();
                        app.getAppBean().refreshTable();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_oaerro", new String[]{RETURN_DATA.toString()}));
                    }
                }
            }
        }else{
            appMbo.setValue("OARETURN", "获取OA人员接口失败，请联系OA确认！", 2L);
            appMbo.getThisMboSet().save();
            app.getAppBean().reloadTable();
            app.getAppBean().refreshTable();
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("subtofactory", "subtofactory_oaerro", new String[]{"获取OA人员接口失败，请联系OA确认！"}));

        }
        System.gc();
    }

    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(" ORGID='"+orgid+"' and 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();

    }

}
