package com.app.hxqh.udlld;

import psdi.common.action.ActionCustomClass;
import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.spms.Spms2EamHandler;
import psdi.mbo.MboRemote;
import psdi.util.MXException;

import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.util.Date;

/**
 * 领料申请单-车间主任审核同意操作类
 * edit 2023-06-15
 */
public class ToSpmsAction implements ActionCustomClass {
    @Override
    public void applyCustomAction(MboRemote mboRemote, Object[] objects) throws MXException, RemoteException {
        String name = mboRemote.getName();
        String appname = mboRemote.getString("APPNAME");
        if ("UDLLD".equalsIgnoreCase(appname) || "UDCLLLD".equalsIgnoreCase(appname)) {//领料申请单
//            if ("TOSPMS".equalsIgnoreCase(status)) {
                try {
                    JSONObject result = new Spms2EamHandler().Z_EAM_OVERSEA_REQUISITION(mboRemote);
                    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)) {
                                JSONObject data = RETURN_DATA.getJSONObject("data");
                                String SPMSID = data.getString("SPMS_ID");//领用单号
                                String COMMENTS = data.getString("SPMS_TITLE");//领用标题
                                mboRemote.setValue("STATUS", "SPMSSUC", 2L);
                                mboRemote.setValue("SPMSSTAUTS", "TOSPMSSUCCESS", 2L);
                                mboRemote.setValue("STATUSDATE", new Date(), 2L);
                                mboRemote.setValue("SPMSID",SPMSID, 2L);
                                mboRemote.setValue("COMMENTS",COMMENTS, 2L);
                                if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                                    mboRemote.setValue("SPMSRETURN", result.toString(), 2L);
                                }else{
                                    mboRemote.setValue("SPMSRETURN", result.toString().substring(0,1333), 2L);
                                }
                            } else {
                                mboRemote.setValue("STATUS", "SPMSSFA", 2L);
                                mboRemote.setValue("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                                if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                                    mboRemote.setValue("SPMSRETURN", result.toString(), 2L);
                                }else{
                                    mboRemote.setValue("SPMSRETURN", result.toString().substring(0,1333), 2L);
                                }
                            }
                        } else {
                            mboRemote.setValue("STATUS", "SPMSSFA", 2L);
                            mboRemote.setValue("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                            if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                                mboRemote.setValue("SPMSRETURN", result.toString(), 2L);
                            }else{
                                mboRemote.setValue("SPMSRETURN", result.toString().substring(0,1333), 2L);
                            }
                        }
                    } else {
                        mboRemote.setValue("STATUS", "SPMSSFA", 2L);
                        mboRemote.setValue("SPMSSTAUTS", "TOSPMSFAIL", 2L);
                        if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                            mboRemote.setValue("SPMSRETURN", result.toString(), 2L);
                        }else{
                            mboRemote.setValue("SPMSRETURN", result.toString().substring(0,1333), 2L);
                        }
                    }
//                    mboRemote.getThisMboSet().save();
                } catch (Exception e) {
                    e.printStackTrace();
                }
//            }
        }
    }
}
