ExtK3CloudApi.java
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.huaheng.api.erp;
import com.google.gson.Gson;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.bean.BeanUtils;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
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;
/**
* 新增处理erp数据字段 方法
*/
public class ExtK3CloudApi extends K3CloudApi {
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;
}
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());
}
}
@Override
protected <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(",");
for (int i = 0; i < fields.length; i++) {
String field = fields[i];
/* 去掉F */
field = BeanUtils.cutHeader(field);
/* 去掉_CH_ */
field = BeanUtils.convertReplace(field, QuantityConstant._CH_,"");
/* 去掉_PKMX_ */
field = BeanUtils.convertReplace(field, QuantityConstant._PKMX_,"");
/* 去掉.替换为 _ */
fields[i] = BeanUtils.convertToFormat(field);
}
Method[] setPes = this.getMethodsByFields(pes, "set", fields);
Method[] getPes = this.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 = this.convertToDest(getPes[i].getReturnType(), darray.get(i));
if (v != null) {
setPes[i].invoke(ret, v);
}
}
}
rets.add(ret);
}
return rets;
}
}