package com.webclient.beans.it_costcenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.oa.OA2EamHandler;
import psdi.app.iface.sap.Sap2EamHandler;
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.rmi.RemoteException;
import java.text.ParseException;
import java.util.Date;

/**
 * SAP成本中心数据存储表AppBean
 * edit 2024-10-21
 */
public class It_costcenterAppBean extends AppBean {

    public void initializeApp() throws MXException, RemoteException {
        super.initializeApp();
    }


    /**
     * 拉取SAP成本中心获取
     * @throws IOException
     * @throws MXException
     * @throws ParseException
     */
    public void IT_COSTCENTER() throws IOException, MXException, ParseException {
        Sap2EamHandler Sap2EamHandler = new Sap2EamHandler();
        JSONObject result=Sap2EamHandler.Z_PS_GET_COSTCENTER();
        JSONObject RESPONSE=result.getJSONObject("RESPONSE");
        if(null!=RESPONSE){
            JSONObject RETURN_DATA=RESPONSE.getJSONObject("RETURN_DATA");
            if(null!=RETURN_DATA){
                String O_TYPE=RETURN_DATA.getString("O_TYPE");//返回值
                if("S".equalsIgnoreCase(O_TYPE)){
                    Date curdate=new Date();
                    JSONArray IT_COSTCENTER = result.getJSONObject("RESPONSE").getJSONObject("RETURN_DATA").getJSONArray("IT_COSTCENTER_DESC");
                    MboSetRemote mobSet = MXServer.getMXServer().getMboSet("IT_COSTCENTER",MXServer.getMXServer().getSystemUserInfo());
                    for (int i = 0; i < IT_COSTCENTER.size(); i++) {
                        JSONObject jobj = (JSONObject) IT_COSTCENTER.get(i);//获取每个json对象
                        String KOSTL = (String) jobj.get("KOSTL"); //成本中心
                        String SPRAS = (String) jobj.get("SPRAS");
                        mobSet.setWhere("KOSTL='"+KOSTL+"' AND SPRAS='"+SPRAS+"' ");
                        mobSet.reset();
                        MboRemote mbo=null;
                        if(null==mobSet || mobSet.count()<=0){
                            //add row
                            mbo=mobSet.add();
                            mbo.setValue("KOSTL", KOSTL, 2L);
                            mbo.setValue("SPRAS", SPRAS, 2L);
                            mbo.setValue("LANGUAGE", (String) jobj.get("LANGUAGE"), 2L);
                            mbo.setValue("KTEXT", (String) jobj.get("KTEXT"), 2L);
                            mbo.setValue("DATBI", (String) jobj.get("DATBI"), 2L);
                            mbo.setValue("DATAB", (String) jobj.get("DATAB"), 2L);
                            mbo.setValue("LTEXT", (String) jobj.get("LTEXT"), 2L);
                            mbo.setValue("CREATEDATE", curdate, 2L);
                            mbo.getThisMboSet().save();
                        }else{
                            //update row
                            mbo=mobSet.getMbo(0);
                            mbo.setValue("LANGUAGE", (String) jobj.get("LANGUAGE"), 2L);
                            mbo.setValue("KTEXT", (String) jobj.get("KTEXT"), 2L);
                            mbo.setValue("DATBI", (String) jobj.get("DATBI"), 2L);
                            mbo.setValue("DATAB", (String) jobj.get("DATAB"), 2L);
                            mbo.setValue("LTEXT", (String) jobj.get("LTEXT"), 2L);
                            mbo.setValue("CREATEDATE", curdate, 2L);
                            mbo.getThisMboSet().save();
                        }
                    }
                    WebClientEvent webclientevent = this.clientSession.getCurrentEvent();
                    this.clientSession.showMessageBox(webclientevent, new MXApplicationException("lqbudget", "lqbudget_succ", new String[0]));
                    app.getAppBean().refreshTable();
                    app.getAppBean().reloadTable();
                }else{
                    //sap返回报错
                    WebClientEvent webclientevent = this.clientSession.getCurrentEvent();
                    this.clientSession.showMessageBox(webclientevent, new MXApplicationException("itcost", "itcost_erro", new String[0]));
                }
            }else{
                //sap返回报错
                WebClientEvent webclientevent = this.clientSession.getCurrentEvent();
                this.clientSession.showMessageBox(webclientevent, new MXApplicationException("itcost", "itcost_erro", new String[0]));
            }
        }else{
            //sap返回报错
            WebClientEvent webclientevent = this.clientSession.getCurrentEvent();
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("itcost", "itcost_erro", new String[0]));
        }

    }

}
