ErpApiClient.java 3.89 KB
package com.huaheng.api.erp.domain;

import com.google.gson.Gson;
import com.kingdee.bos.webapi.sdk.RepoError;
import com.kingdee.bos.webapi.sdk.RepoRet;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class ErpApiClient {


    public static <T> List<T> loadDataList(String fieldKeys, Class type, String json) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        List<T> rets = new ArrayList();
        Gson gson = new Gson();
        List<ArrayList<Object>> rows = (List)gson.fromJson(json, rets.getClass());
        Method[] pes = type.getMethods();
        String[] fields = fieldKeys.split(",");
        Method[] setPes = getMethodsByFields(pes, "set", fields);
        Method[] getPes = getMethodsByFields(pes, "get", fields);
        Iterator var12 = rows.iterator();

        while(var12.hasNext()) {
            List<Object> darray = (List)var12.next();
            T ret = (T)type.newInstance();

            for(int i = 0; i < fields.length; ++i) {
                if (setPes[i] != null && getPes[i] != null) {
                    Object v = convertToDest(getPes[i].getReturnType(), darray.get(i));
                    if (v != null) {
                        setPes[i].invoke(ret, v);
                    }
                }
            }

            rets.add(ret);
        }

        return rets;
    }

    String loadErrorMsg(String json) {
        if (json.startsWith("[[") && json.endsWith("]]")) {
            Gson gson = new Gson();
            return ((RepoError)((RepoRet)gson.fromJson(json.substring(2, json.length() - 2), RepoRet.class)).getResult().getResponseStatus().getErrors().get(0)).getMessage();
        } else {
            return String.format("fail for ExecuteBillQuery:%s", json);
        }
    }

    static Method[] getMethodsByFields(Method[] pes, String pre, String[] fields) {
        Method[] rets = new Method[fields.length];

        for(int i = 0; i < fields.length; ++i) {
            for(int j = 0; j < pes.length; ++j) {
                if (pes[j].getName().toLowerCase().equals(pre + fields[i].toLowerCase())) {
                    rets[i] = pes[j];
                }
            }
        }

        return rets;
    }

    Method getMethodFromT(Method[] pes, String pname) {
        for(int i = 0; i < pes.length; ++i) {
            if (pes[i].getName().toLowerCase().equals(pname.toLowerCase())) {
                return pes[i];
            }
        }

        return null;
    }

    static Object convertToDest(Type type, Object val) {
        if (val == null) {
            return null;
        } else if (type.getTypeName().equals(String.class.getTypeName())) {
            return val.toString();
        } else if (!type.getTypeName().equals(Integer.TYPE.getTypeName()) && !type.getTypeName().equals(Short.TYPE.getTypeName()) && !type.getTypeName().equals(Long.TYPE.getTypeName())) {
            if (type.getTypeName().equals(BigDecimal.class.getTypeName())) {
                return new BigDecimal(val.toString());
            } else if (type.getTypeName().equals(Date.class.getTypeName())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

                try {
                    return sdf.parse(val.toString());
                } catch (ParseException var5) {
                    var5.printStackTrace();
                    return null;
                }
            } else {
                return val;
            }
        } else {
            String v = val.toString();
            if (v.toString().indexOf(".") > -1) {
                v = v.substring(0, v.toString().indexOf("."));
            }

            return new Integer(v.toString());
        }
    }
}