salesforcexiaowu
102 分钟以前 8a0af8f743fc1e60a7fee653ae7e524bbf78d077
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package other.xsy.jinpanPhaseII.utli;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.XsyHttpException;
import com.rkhd.platform.sdk.http.*;
import com.rkhd.platform.sdk.http.handler.ResponseBodyHandlers;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.OperateResult;
import com.rkhd.platform.sdk.model.QueryResult;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.service.XObjectService;
import org.apache.commons.lang.StringUtils;
 
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class NeoCrmRkhdService {
    private static final Logger logger = LoggerFactory.getLogger();
 
    /**
    * @Description: 获取角色用户
    * @Param: [client, apikey]
    * @return: com.alibaba.fastjson.JSONObject
    * @Author: 武于伦
    * @email: 2717718875@qq.com
    * @Date: 2025/7/31
    */
    public static JSONArray roleUsers(RkhdHttpClient client, String apikey) throws IOException {
        JSONArray records = new JSONArray();
        try {
            if (client==null){
                client = RkhdHttpClient.instance();
            }
            RkhdHttpData data = new RkhdHttpData();
            data.setCallString("/rest/metadata/v2.0/privileges/roles/"+apikey+"/actions/users");
            data.setCall_type("GET");
            String responseStr = client.execute(data, ResponseBodyHandlers.ofString());
            if (StringUtils.isNotBlank(responseStr)) {
                logger.info("查询列表结果:" + responseStr);
                JSONObject responseObject = JSONObject.parseObject(responseStr);
                Integer responseCode = responseObject.getIntValue("code");
                if (responseCode.equals(0)) {
                    String resultStr = responseObject.getString("data");
                    JSONObject dataJson = JSONObject.parseObject(resultStr);
                    records = dataJson.getJSONArray("records");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return records;
    }
    /***
    * @Description: 获取指定职能用户
    * @Param: [client, apikey]
    * @return: com.alibaba.fastjson.JSONArray
    * @Author: 武于伦
    * @email: 2717718875@qq.com
    * @Date: 2025/9/4
    */
    public static JSONArray functionsUsers(RkhdHttpClient client, String apikey) throws IOException {
        JSONArray records = new JSONArray();
        try {
            if (client==null){
                client = RkhdHttpClient.instance();
            }
            RkhdHttpData data = new RkhdHttpData();
            data.setCallString("/rest/metadata/v2.0/privileges/responsibility/"+apikey+"/actions/users");
            data.setCall_type("GET");
            String responseStr = client.execute(data, ResponseBodyHandlers.ofString());
            if (StringUtils.isNotBlank(responseStr)) {
                logger.info("查询列表结果:" + responseStr);
                JSONObject responseObject = JSONObject.parseObject(responseStr);
                Integer responseCode = responseObject.getIntValue("code");
                if (responseCode.equals(0)) {
                    String resultStr = responseObject.getString("data");
                    JSONObject dataJson = JSONObject.parseObject(resultStr);
                    records = dataJson.getJSONArray("records");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return records;
    }
    /**
    * @Description: 发布工作圈
    * @Param: [client, objectId, content]
    * @return: com.alibaba.fastjson.JSONObject
    * @Author: 武于伦
    * @email: 2717718875@qq.com
    * @Date: 2025/7/31
    */
    public static JSONObject workingCircle(RkhdHttpClient client, Long objectId, String content) throws IOException {
        JSONObject jsonData = new JSONObject();
        jsonData.put("objectId",objectId);
        jsonData.put("content",content);
        JSONObject dataJson = new JSONObject();
        try {
            if (client == null) {
                client = RkhdHttpClient.instance();
            }
            RkhdHttpData data = new RkhdHttpData();
            data.setCallString("/rest/data/v2.0/social/story");
            data.setCall_type("POST");
            data.setBody(jsonData.toJSONString());
            String responseStr = client.execute(data, ResponseBodyHandlers.ofString());
            if (StringUtils.isNotBlank(responseStr)) {
                logger.info("请求返回结果:" + responseStr);
                JSONObject responseObject = JSONObject.parseObject(responseStr);
                Integer responseCode = responseObject.getIntValue("code");
                if (responseCode.equals(200)) {
                    String resultStr = responseObject.getString("result");
                    dataJson = JSONObject.parseObject(resultStr);
                }
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return dataJson;
    }
 
   /***
   * @Description: 更新或者新增
   * @Param: [client, xObject, onlyValue, partialSuccess, admin]
   * @return: com.rkhd.platform.sdk.model.OperateResult
   * @Author: 武于伦
   * @email: 2717718875@qq.com
   * @Date: 2025/9/1
   */
    public static OperateResult upsert(RkhdHttpClient client, XObject xObject,String onlyValue, boolean partialSuccess, boolean admin) throws IOException {
        try {
            if (client == null) {
                client = RkhdHttpClient.instance();
            }
            String apiKey = xObject.getApiKey();
            // 获取xObject中onlyValue字段对应的值
            Object fieldValue = xObject.getAttribute(onlyValue);
            if (fieldValue == null) {
                throw new RuntimeException("字段 " + onlyValue + " 的值为空,无法执行upsert操作");
            }
            
            // 通过唯一字段查询
            //1、拼写sql - 使用字符串拼接,因为XObjectService.query不支持参数绑定
            String sql = "select id,"+onlyValue+" from "+apiKey+" where "+onlyValue+"='" + fieldValue + "'";
            // 执行查询
            QueryResult<XObject> query = XObjectService.instance().query(sql, partialSuccess, admin);
            
            // 如果查询到记录,执行更新操作
            if (query != null && query.getRecords() != null && !query.getRecords().isEmpty()) {
                // 获取第一条记录的ID
                XObject existingRecord = query.getRecords().get(0);
                Long recordId = existingRecord.getId();
                
                // 设置ID并执行更新
                xObject.setId(recordId);
                logger.info("执行更新操作,记录ID: " + recordId);
                return XObjectService.instance().update(xObject,admin);
            } else {
                // 如果没有查询到记录,执行插入操作
                logger.info("执行插入操作,字段 " + onlyValue + " 的值: " + fieldValue);
                return XObjectService.instance().insert(xObject,admin);
            }
            
        }catch (Exception e){
            logger.error("upsert操作失败: " + e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
    public static <T extends XObject> OperateResult upsert(RkhdHttpClient client, List<T> xObjectList, String onlyValue, boolean partialSuccess, boolean admin) throws IOException {
        try {
            if (client == null) {
                client = RkhdHttpClient.instance();
            }
            
            if (xObjectList == null || xObjectList.isEmpty()) {
                throw new RuntimeException("XObject列表为空,无法执行upsert操作");
            }
            
            // 获取第一个对象的apiKey,假设所有对象都是同一类型
            String apiKey = xObjectList.get(0).getApiKey();
            
            // 批量处理每个XObject对象
            OperateResult finalResult = null;
            int successCount = 0;
            int totalCount = xObjectList.size();
            
            for (T xObject : xObjectList) {
                try {
                    // 获取xObject中onlyValue字段对应的值
                    Object fieldValue = xObject.getAttribute(onlyValue);
                    if (fieldValue == null) {
                        logger.warn("对象 " + xObject + " 的字段 " + onlyValue + " 的值为空,跳过此对象");
                        continue;
                    }
                    
                    // 通过唯一字段查询
                    String sql = "select id," + onlyValue + " from " + apiKey + " where " + onlyValue + "='" + fieldValue + "'";
                    
                    // 执行查询
                    QueryResult<XObject> query = XObjectService.instance().query(sql, partialSuccess, admin);
                    
                    // 如果查询到记录,执行更新操作
                    if (query != null && query.getRecords() != null && !query.getRecords().isEmpty()) {
                        // 获取第一条记录的ID
                        XObject existingRecord = query.getRecords().get(0);
                        Long recordId = existingRecord.getId();
                        
                        // 设置ID并执行更新
                        xObject.setId(recordId);
                        logger.info("执行更新操作,记录ID: " + recordId + ", 字段值: " + fieldValue);
                        OperateResult updateResult = XObjectService.instance().update(xObject,admin);
                        if (updateResult != null && updateResult.getSuccess()) {
                            successCount++;
                        }
                        finalResult = updateResult; // 保存最后一个结果
                    } else {
                        // 如果没有查询到记录,执行插入操作
                        logger.info("执行插入操作,字段 " + onlyValue + " 的值: " + fieldValue);
                        OperateResult insertResult = XObjectService.instance().insert(xObject,admin);
                        if (insertResult != null && insertResult.getSuccess()) {
                            successCount++;
                        }
                        finalResult = insertResult; // 保存最后一个结果
                    }
                    
                } catch (Exception e) {
                    logger.error("处理对象时发生错误: " + e.getMessage(), e);
                    if (!partialSuccess) {
                        throw new RuntimeException("处理对象失败: " + e.getMessage(), e);
                    }
                    // 如果允许部分成功,继续处理下一个对象
                }
            }
            
            logger.info("批量upsert操作完成,成功处理 " + successCount + "/" + totalCount + " 个对象");
            
            // 返回最后一个操作结果,或者创建一个汇总结果
            return finalResult;
            
        } catch (Exception e) {
            logger.error("批量upsert操作失败: " + e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
 
    public static OperateResult campaignUpsert(RkhdHttpClient client, XObject xObject,String onlyValue, boolean partialSuccess, boolean admin) throws IOException {
        try {
            if (client == null) {
                client = RkhdHttpClient.instance();
            }
            String apiKey = xObject.getApiKey();
            // 获取xObject中onlyValue字段对应的值
            Object fieldValue = xObject.getAttribute(onlyValue);
            if (fieldValue == null) {
                throw new RuntimeException("字段 " + onlyValue + " 的值为空,无法执行upsert操作");
            }
 
            // 通过唯一字段查询
            //1、拼写sql - 使用字符串拼接,因为XObjectService.query不支持参数绑定
            String sql = "select id,status,finishReasonDescription__c,finishReason__c,"+onlyValue+" from "+apiKey+" where "+onlyValue+"='" + fieldValue + "'";
            // 执行查询
            QueryResult<XObject> query = XObjectService.instance().query(sql, partialSuccess, admin);
 
            // 如果查询到记录,执行更新操作
            if (query != null && query.getRecords() != null && !query.getRecords().isEmpty()) {
                // 获取第一条记录的ID
                XObject existingRecord = query.getRecords().get(0);
                Long recordId = existingRecord.getId();
                Integer status = existingRecord.getAttribute("status");
                String finishReasonDescription__c = existingRecord.getAttribute("finishReasonDescription__c");
                String finishReason__c = existingRecord.getAttribute("finishReason__c");
                if (status == 4 && (finishReasonDescription__c == null || finishReason__c.isEmpty())) {
                    throw new RemoteException("请填写中止原因以及详细说明");
                }
                // 设置ID并执行更新
                xObject.setId(recordId);
                logger.info("执行更新操作,记录ID: " + recordId);
                return XObjectService.instance().update(xObject,admin);
            } else {
                // 如果没有查询到记录,执行插入操作
                logger.info("执行插入操作,字段 " + onlyValue + " 的值: " + fieldValue);
                return XObjectService.instance().insert(xObject,admin);
            }
 
        }catch (Exception e){
            logger.error("upsert操作失败: " + e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws ApiEntityServiceException, IOException, XsyHttpException {
        RkhdHttpClient client = RkhdHttpClient.instance();
        RkhdHttpData data = new RkhdHttpData();
        data.setCallString("/rest/template/v2.0/reconciliation_Json__c/4034941595027543/4043190410528873/1");
        data.setCall_type("GET");
        String responseStr = client.execute(data, ResponseBodyHandlers.ofString());
        JSONObject responseObject = JSONObject.parseObject(responseStr);
        System.out.println(responseObject.getJSONObject("data").getString("data"));
        byte[] fileBytes = Base64.getDecoder().decode(responseObject.getJSONObject("data").getString("data"));
        RkhdBlob rkhdBlob = new RkhdBlob();
        rkhdBlob.setFileName("客户对账单贷款汇总全部.pdf");
        rkhdBlob.setBytes(fileBytes);
        Map<String, Object> map = new HashMap<>(16);
        rkhdBlob.setBytes(Base64.getDecoder().decode(responseObject.getJSONObject("data").getString("data")));
        map.put("files", rkhdBlob);
//        map.put("belongId", 001L);
//        map.put("dataId", 001L);
        RkhdHttpData  rkhdHttpData = RkhdHttpData.newBuilder().callString("/rest/file/v2.0/document/batchUploadAndBindObject")
                .callType("POST").formData("files",rkhdBlob).build();
//        rkhdHttpData.putHeader("Content-Type", "form-data");
        JSONObject execute = RkhdHttpClient.instance().execute(rkhdHttpData, JSONObject::parseObject);
        System.out.println(execute.toJSONString());
    }
}