package com.webclient.beans.udrecovery;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.sap.Sap2EamHandler;
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 java.io.IOException;
import java.rmi.RemoteException;

/**
 * 旧件回收任务AppBean
 * edit 2023-10-17
 */
public class UdrecoveryAppBean 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(" 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();
    }


    //旧件回收任务提交到spms系统
    public void SBRCSPMS() throws IOException, MXException {
        System.out.println("--------start---------");
        MboRemote mboRemote=app.getAppBean().getMbo();//当前对象
        System.out.println("mboRemote="+mboRemote.getName());
        Spms2EamHandler spms2EamHandler = new Spms2EamHandler();
        try {
            JSONObject result = spms2EamHandler.Z_EAM_ITEM_RUKU(mboRemote);//调用spms接口，返回值
            if(result!=null){
                String codeStr = String.valueOf(result.getJSONObject("RESPONSE").getJSONObject("RETURN_DATA").getString("code"));//返回值
                if(codeStr!=null && !"".equals(codeStr) && "200".equals(codeStr)){
//                    app.getAppBean().reloadTable();//加载
//                    app.getAppBean().refreshTable();//刷新
                }else{
                    //前端界面的消息提示
                    throw new MXApplicationException("UDRECOVERY", "WFLOW", new String[0] );
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        System.out.println("-------end-------------");
    }

}
