package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.dataimport.util.AppUtil;
import com.foreverwin.modular.core.meext.MEServices;
import com.sap.me.inventory.*;
import com.visiprise.globalization.DateGlobalizationServiceInterface;
import com.visiprise.globalization.GlobalizationService;

import javax.xml.namespace.QName;
import java.lang.reflect.UndeclaredThrowableException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class InventoryWSClient {
    public static String validateAndUpdate(JSONObject data, Inventory inv) {
        try {

            Object[] param2 = new Object[3];
            String site = (String) data.get("site");
            InventoryValidateAndUpdateRequest request = new InventoryValidateAndUpdateRequest();
            String id = data.getString("id");
            request.setInventoryId(id);
            //request.sets
            JSONArray customArray = data.getJSONArray("customData");
            List<InventoryDataField> assemblyDataList = new ArrayList<InventoryDataField>();
            if (customArray != null && customArray.size() > 0) {
                for (int i = 0; i < customArray.size(); i++) {
                    InventoryDataField _data = new InventoryDataField();
                    JSONObject custObject = customArray.getJSONObject(i);
                    String cust = custObject.getString("attribute");
                    String value = custObject.getString("value");
                    _data.setAttribute(cust);
                    _data.setValue(value);
                    assemblyDataList.add(_data);
                }
            }
            request.setAssemblyDataList(assemblyDataList);
            request.setQuantityOnHand(new BigDecimal(data.getString("qty")));
            String itemRef = "ItemBO:" + site + "," + data.getString("item") + "," + data.getString("itemVer");
            request.setItemRef(itemRef);
            DateGlobalizationServiceInterface dateService = (DateGlobalizationServiceInterface) GlobalizationService.getUserService("com.visiprise.globalization.DateGlobalizationService");
            request.setModifiedDateTime(inv.getModifiedDateTime());
            if (AppUtil.isNotNull(data.getString("shoporder"))) {
                String soRef = "ShopOrderBO:" + site + "," + data.getString("shoporder");
                request.setShopOrderLocationRef(soRef);
            }
            //	request.setQuantityOnHand(value);
            param2[0] = site;
            param2[1] = "";
            param2[2] = request;
            //InventoryService
            InventoryServiceInterface inventoryService = MEServices.create("com.sap.me.inventory", "InventoryService", site);
            inventoryService.validateAndUpdate(request);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof UndeclaredThrowableException){
                return ((UndeclaredThrowableException)e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }

    public static String validateAndAdd(JSONObject data) {
        Object[] result = null;
        try {
            //	QName methodName = new QName("mepapi:com:sap:me:inventory","validateAndAdd"); //如果有命名空间需要加上这个，第一个参数为命名空间名称，第二个参数为WebService方法名称
            //	Object param1;
            Object[] param2 = new Object[3];
            String site = (String) data.get("site");
            InventoryValidateAndAddRequest request = new InventoryValidateAndAddRequest();
            String id = data.getString("id");
            request.setInventoryId(id);
            //request.sets
            JSONArray customArray = data.getJSONArray("customData");
            List<InventoryDataField> assemblyDataList = new ArrayList<InventoryDataField>();
            if (customArray != null && customArray.size() > 0) {
                for (int i = 0; i < customArray.size(); i++) {
                    InventoryDataField _data = new InventoryDataField();
                    JSONObject custObject = customArray.getJSONObject(i);
                    String cust = custObject.getString("attribute");
                    String value = custObject.getString("value");
                    _data.setAttribute(cust);
                    _data.setValue(value);
                    assemblyDataList.add(_data);
                }
            }
            request.setAssemblyDataList(assemblyDataList);
            request.setQuantityOnHand(new BigDecimal(data.getString("qty")));
            String itemRef = "ItemBO:" + site + "," + data.getString("item") + "," + data.getString("itemVer");
            request.setItemRef(itemRef);
            if (AppUtil.isNotNull(data.getString("shoporder"))) {
                String soRef = "ShopOrderBO:" + site + "," + data.getString("shoporder");
                request.setShopOrderLocationRef(soRef);
            }
            //	request.setQuantityOnHand(value);
            param2[0] = site;
            param2[1] = "";
            param2[2] = request;
            InventoryServiceInterface inventoryService = MEServices.create("com.sap.me.inventory", "InventoryService", site);
            inventoryService.validateAndAdd(request);
            //result = client.invoke(methodName, param2);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof UndeclaredThrowableException){
                return ((UndeclaredThrowableException)e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }

    public static String insert(JSONObject data) {
        Object[] result = null;
        try {
            QName methodName = new QName("mepapi:com:sap:me:inventory", "createInventory"); //如果有命名空间需要加上这个，第一个参数为命名空间名称，第二个参数为WebService方法名称
            //	Object param1;
            Object[] param2 = new Object[9];
            String site = (String) data.get("site");
            HandleInventoryRequest request = new HandleInventoryRequest();
            String itemRef = "ItemBO:" + site + "," + data.getString("item") + "," + data.getString("itemVer");
            request.setItemRef(itemRef);
            request.setQuantity(new BigDecimal(data.getString("qty")));
            if (AppUtil.isNotNull(data.getString("shoporder"))) {
                String soRef = "ShopOrderBO:" + site + "," + data.getString("shoporder");
                request.setShopOrderRef(soRef);
            }
            request.setStatusRef("StatusBO:" + site + ",1001");
            String id = data.getString("id");
            InventoryId inv = new InventoryId();
            inv.setInventoryId(id);
            inv.setQuantity(new BigDecimal(data.getString("qty")));
            request.getInventoryIds().add(inv);
            request.setSite(site);
            InventoryServiceInterface inventoryService = MEServices.create("com.sap.me.inventory", "InventoryService", site);
            inventoryService.createInventory(request);
            return validateAndAdd(data);
            //return null;
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof UndeclaredThrowableException){
                return ((UndeclaredThrowableException)e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }

    public static Inventory find(JSONObject data) {
        try {
            //如果有命名空间的话
            //	QName methodName = new QName("mepapi:com:sap:me:inventory","findInventory"); //如果有命名空间需要加上这个，第一个参数为命名空间名称，第二个参数为WebService方法名称
            Object[] param2 = new Object[9];
            String site = (String) data.get("site");
            String id = data.getString("id");
            InventoryIdentifier request = new InventoryIdentifier();
            request.setInventoryId(id);
            InventoryServiceInterface inventoryService = MEServices.create("com.sap.me.inventory", "InventoryService", site);
            Inventory op = inventoryService.findInventory(request);
            return op;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
