package com.webclient.beans.udassetmodif;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.gfss.Gfss2EamHandler;
import psdi.app.iface.oa.OA2EamHandler;
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;
import java.text.ParseException;

/**
 * 资产修改AppBean
 * edit  20240815
 */
public class UdAssetModifAppbean 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();

    }

    //资产修改-推送财务共享系统（GFSS）
    public void ASSETMODIFGFSS() throws Exception {
        System.out.println("--------start GFSS---------");
        String type = "FC";//固定值   FB：对应在建工程转固接口    FA：创建资产卡片接口   FC:资产修改
        MboRemote appMbo=app.getAppBean().getMbo();
        String SITEID = appMbo.getString("SITEID");
        WebClientEvent webclientevent;
        webclientevent = this.clientSession.getCurrentEvent();
        if("TOGFSS".equalsIgnoreCase(appMbo.getString("STATUS"))){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_dup", new String[0]));//已提交到财务共享系统，请勿重复提交！
            return;
        }else if(!"EDIT".equalsIgnoreCase(appMbo.getString("STATUS")) && !"CAN".equalsIgnoreCase(appMbo.getString("STATUS")) && !"Draft".equalsIgnoreCase(appMbo.getString("STATUS"))){
            this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_xoa", new String[0]));//该状态下不允许提交，请在编辑状态下提交！
            return;
        }

        //验证通过，推送财务共享审批
        JSONObject result = new Gfss2EamHandler().Z_EAM_FSSC(appMbo,type);
        System.out.println("-------------result="+result);
        //返回结果
 /*       if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
            appMbo.setValue("JRESULT", result.toString(), 2L);
        }else{
            appMbo.setValue("JRESULT", result.toString().substring(0,1333), 2L);
        }*/
        JSONObject RESPONSE = (JSONObject) result.get("RESPONSE");
        if(null!=RESPONSE) {
            JSONObject RETURN_DATA = (JSONObject) RESPONSE.get("RETURN_DATA");
            if (null != RETURN_DATA) {
                String codeStr = RETURN_DATA.getString("TYPE");//  S成功 E失败 R(可选:代表重复请求处理)
                String MESSAGE = RETURN_DATA.getString("MESSAGE");//返回消息
                JSONObject dataObj = RETURN_DATA.getJSONObject("DATA");
                if(dataObj!=null){
                    JSONObject RETURN_DATA2 = dataObj.getJSONObject("head");//接收头部信息
                    String Status = "";//状态
                    String TRANSFERNUM ="";//eam单号
                    String FSSCNUM ="";//报账单号
                    if(RETURN_DATA2!=null){
                        TRANSFERNUM = RETURN_DATA2.getString("sourceDocNumber");//EAM单号
                        FSSCNUM = RETURN_DATA2.getString("businessId");//报账系统单号（财务共享单号）
                    }
                    if ("S".equalsIgnoreCase(codeStr)) {
                        //传输成功
                        appMbo.setValue("STATUS","TOGFSS",2L);//给一个状态值
                        appMbo.setValue("FSSCNUM",FSSCNUM,2L);
                        appMbo.getThisMboSet().save();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_sus", new String[]{FSSCNUM}));
                    }else if("E".equalsIgnoreCase(codeStr)){
                        //传输失败
                        appMbo.setValue("STATUS","TOFAILGFSS",2L);//给一个状态值
                        appMbo.setValue("FSSCNUM",FSSCNUM,2L);
                        appMbo.getThisMboSet().save();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_failure", new String[]{FSSCNUM}));
                    }else if("R".equalsIgnoreCase(codeStr)){
                        //重新提交--手动
                        appMbo.setValue("STATUS","TOAGAINGFSS",2L);//给一个状态值
                        appMbo.setValue("FSSCNUM",FSSCNUM,2L);
                        appMbo.getThisMboSet().save();
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_again", new String[]{FSSCNUM}));
                        //重新提交--自动，若一直有问题，就会导致不断地重复调用
                        //Z_EAM_FSSC(mbo,Type);
                    }
                }else{
                    if("E".equalsIgnoreCase(codeStr)){
                        //传输失败
                        this.clientSession.showMessageBox(webclientevent, new MXApplicationException("prsubtogfss", "prsubtogfss_failure", new String[]{MESSAGE}));
                    }
                }
            }
        }
        System.out.println("-------end GFSS-------------");
    }

}
