zhanghua
2023-12-26 04186253c1f67753e5ef2200f2ef42bfaaab82ea
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package com.ycl.common.dingding;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziOrganizationListOrganizationsByCodesRequest;
import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziOrganizationPageOrganizationEmployeePositionsRequest;
import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziOrganizationPageSubOrganizationCodesRequest;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziOrganizationListOrganizationsByCodesResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziOrganizationPageOrganizationEmployeePositionsResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziOrganizationPageSubOrganizationCodesResponse;
import com.alibaba.xxpt.gateway.shared.client.http.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ycl.config.DingConfig;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.dingding.DingUserInfo;
import com.ycl.entity.user.UmsRole;
import com.ycl.exception.ApiException;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.user.UmsRoleService;
import com.ycl.util.DingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
import static com.ycl.common.constant.DingConst.*;
 
 
/**
 * Ding 获取接口api相关
 *
 * @author cjx
 */
@Component
public class DingCommon {
 
    private static final Log log = Log.get();
 
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private ExecutableClient executableClient;
    @Autowired
    private UmsDepartService umsDepartService;
 
    @Autowired
    private DingUtil dingUtil;
 
    /**
     * 获取用户信息
     *
     * @param code 临时授权码 前台获取
     * @return
     */
    public DingUserInfo getDingUserInfo(String code) {
//        判断ding 信息是否存在
        boolean hasDing = redisTemplate.hasKey("ding");
        String getToken = "";
        if (hasDing) {
            log.info("redis正常有消息");
            String dingStr = redisTemplate.opsForValue().get("ding");
            JSONObject dingObj = JSONObject.parseObject(dingStr);
            getToken = dingObj.getString("token");
 
        } else {
            getToken = dingUtil.getToken();
        }
        if (ObjectUtil.isEmpty(code) || ObjectUtil.isEmpty(getToken)) {
            throw new ApiException("登录异常");
        }
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(GET_USER_INFO);
        postClient.addParameter("access_token", getToken); //token
        postClient.addParameter("auth_code", code);//临时授权码
        String apiResult = postClient.post();
        JSONObject resObj = parsingJson(apiResult);
        log.info("resObj--------------->{}", resObj);
        if (null != resObj) {
            resObj = resObj.getJSONObject("data");
            redisTemplate.opsForValue().set("realmId", resObj.getString("realmId"));
            JSONObject empCodeObj = getEmpByCode(resObj.getString("employeeCode"), resObj.getString("realmId"));
            if (ObjectUtil.isNotEmpty(empCodeObj)) {
                resObj.putAll(empCodeObj);
            }
            DingUserInfo dingUserInfo = BeanUtil.toBean(resObj, DingUserInfo.class);
            String accId = DingEncodeUtil.getMD5(resObj.getString("employeeCode").getBytes());
            dingUserInfo.setUserId(Long.parseLong(accId));
            return dingUserInfo;
        }
        return null;
    }
 
    /**
     * 根据员工Code查询详情
     *
     * @param employeeCode 员工code列表
     * @param tenantId     租户id
     * @return
     */
    public JSONObject getEmpByCode(String employeeCode, String tenantId) {
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(GET_EMP_BY_CODE);
        postClient.addParameter("employeeCode", employeeCode);
        postClient.addParameter("tenantId", tenantId);
        String apiResult = postClient.post();
        JSONObject resObj = parsingJson(apiResult);
        if (null != resObj) {
            resObj = resObj.getJSONObject("data");
        } else {
            resObj = null;
        }
        return resObj;
    }
 
    /**
     * 发送文本消息
     *
     * @param receiverIds 接收人用户ID(accountId), 多个人时使用半角逗号分隔,
     * @param content     消息,最好500字以内
     * @return
     */
    public JSONObject sendDingMsgStr(String receiverIds, String content) {
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(POST_SEND_NOTIFY);
        postClient.addParameter("receiverIds", receiverIds);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("msg", getJsonStringText(content));
        String apiResult = postClient.post();
        log.info("apiResult------>{}", apiResult);
        JSONObject resObj = parsingJson(apiResult);
        if (null != resObj) {
            resObj = resObj.getJSONObject("data");
        }
        return resObj;
    }
 
    /**
     * 发送markdown消息
     *
     * @param receiverIds 接收人用户ID(accountId), 多个人时使用半角逗号分隔,
     * @param text        消息,最好500字以内
     * @return
     */
    public JSONObject sendDingMsgMarkDown(String receiverIds, String title, String text) {
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(POST_SEND_NOTIFY);
        postClient.addParameter("receiverIds", receiverIds);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("msg", getJsonMarkDown(title, text));
        String apiResult = postClient.post();
        log.info("apiResult------>{}", apiResult);
        JSONObject resObj = parsingJson(apiResult);
        if (null != resObj) {
            resObj = resObj.getJSONObject("data");
        }
        return resObj;
    }
 
    /**
     * 发送链接消息
     *
     * @param receiverIds 接收人用户ID(accountId), 多个人时使用半角逗号分隔,
     * @param content     消息,最好500字以内
     * @param title       消息标题
     * @param picUrl      图片地址
     * @param urlLink     链接跳转路径
     * @return
     */
    public JSONObject sendDingMsgLink(String receiverIds, String title, String urlLink, String content, String picUrl) {
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(POST_SEND_NOTIFY);
        postClient.addParameter("receiverIds", receiverIds);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("msg", getJsonStringLink(urlLink, picUrl, title, content));
        String apiResult = postClient.post();
        JSONObject resObj = parsingJson(apiResult);
        return resObj;
    }
 
    public JSONObject sendDingMsgCardLink(String receiverIds, String title, String urlLink, String pcUrlLink, String content) {
        //获取用户信息
        PostClient postClient = executableClient.newPostClient(POST_SEND_NOTIFY);
        postClient.addParameter("receiverIds", receiverIds);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("bizMsgId", StrUtil.uuid());
        postClient.addParameter("msg", getJsonStringCardLink(urlLink, pcUrlLink, title, content));
 
        String apiResult = postClient.post();
        JSONObject resObj = parsingJson(apiResult);
        return resObj;
    }
 
    @Resource
    private DingConfig dingConfig;
 
    /**
     * 获取通讯录权限范围
     */
    public JSONObject getBookAuthority() {
        //获取用户信息
        GetClient postClient = executableClient.newGetClient(GET_BOOK_AUTH);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        String apiResult = postClient.get();
        JSONObject resObj = parsingJson(apiResult);
        return resObj;
    }
 
    /**
     * 查询全部部门信息
     *
     * @return
     */
    public List<UmsDepart> getOrgInfo(JSONArray deptTopArr) {
        List<UmsDepart> orgList = new ArrayList<>();
        if (CollectionUtils.isEmpty(deptTopArr)) {
            return null;
        }
        //获取全部部门code
        //JSONObject topObj = getBookAuthority();
        //顶级部门id
        // JSONArray deptTopArr = topObj.getJSONArray("deptVisibleScopes");
        //根据分页获取下⼀级组织 Code 列表 一层一层获取组织 Code 列表
        String topCode = deptTopArr.getString(0);
        orgNameList.add(topCode);
        getPageOrgCode(topCode);
        //根据code查询组织详情
        JSONArray resDept = getOrgDetailByCodeList(orgNameList);
        //解析为系统组织结构实体类
        for (int i = 0; i < resDept.size(); i++) {
            JSONObject orgObj = resDept.getJSONObject(i);
            UmsDepart depart = new UmsDepart();
            //本级code编码
            String code = orgObj.getString("organizationCode");
            //父级code编码
            String pcode = orgObj.getString("parentCode");
            //本级名称
            String name = orgObj.getString("organizationName");
            //排序
            Integer sort = orgObj.getInteger("displayOrder");
            code = DingEncodeUtil.getMD5(code.getBytes());
            pcode = DingEncodeUtil.getMD5(pcode.getBytes());
            depart.setParentId(Long.parseLong(pcode));
            depart.setId(Long.parseLong(code));
            depart.setCode(orgObj.getString("organizationCode"));
            depart.setDepartName(name);
            depart.setSort(sort);
            orgList.add(depart);
        }
        orgNameList = new ArrayList<>();
        return orgList;
    }
 
    /**
     * 根据组织code 查询 通讯录人员信息
     * <p>
     * //     * @return
     * //
     */
    public Set<DingUserInfo> getPersonInfoByOrgCode() {
        LambdaQueryWrapper<UmsDepart> orgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orgLambdaQueryWrapper.select(UmsDepart::getCode);
        orgLambdaQueryWrapper.likeRight(UmsDepart::getCode, "GO_");
        List<UmsDepart> orgList = umsDepartService.list(orgLambdaQueryWrapper);
        Set<DingUserInfo> dingUserInfoList = new HashSet<>();
        List<String> employeeCodes = new ArrayList();
        for (UmsDepart org : orgList) {
            if (ObjectUtil.isNotEmpty(org.getCode())) {
                IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(POST_PERSON_INFO_BY_ORG_CODE);
                OapiMoziOrganizationPageOrganizationEmployeePositionsRequest orgOptions = new OapiMoziOrganizationPageOrganizationEmployeePositionsRequest();
                //是否请求总数,默认是false
//                orgOptions.setReturnTotalSize(true);
                //分页大小,默认是20,最大100
                orgOptions.setPageSize(100);
                //员工状态,A为有效,F为无效,默认是所有
                orgOptions.setEmployeeStatus("A");
                //组织code
                orgOptions.setOrganizationCode(org.getCode());
                //请求起始页,默认是1
//                orgOptions.setPageNo(1);
                //租户id
                orgOptions.setTenantId(Long.valueOf(dingConfig.getTenantId()));
                //page页
                Integer page = 0;
                //是否有下一页
                boolean isHasPage;
                do {
                    isHasPage = false;
                    page++;
//                    postClient.addParameter("pageNo", page.toString());
//                    String apiResult = postClient.post();
                    orgOptions.setPageNo(page);
                    OapiMoziOrganizationPageOrganizationEmployeePositionsResponse apiResult = intelligentGetClient.get(orgOptions);
                    JSONObject resObj = parsingJson(JSON.toJSONString(apiResult));
                    if (null != resObj) {
                        if (resObj.containsKey("data")) {
                            JSONArray dataArr = resObj.getJSONArray("data");
                            for (int i = 0; i < dataArr.size(); i++) {
                                //人员信息
                                JSONObject data = dataArr.getJSONObject(i);
//                                String empName = data.getString("employeeName");
                                String employeeCode = data.getString("employeeCode");
                                employeeCodes.add(employeeCode);
                                //根据员工code 获取员工详情
                                DingUserInfo dingUserInfo = getEmpInfoByCode(employeeCode);
                                if (ObjectUtil.isNotEmpty(dingUserInfo)) {
                                    dingUserInfoList.add(dingUserInfo);
                                }
                            }
                            isHasPage = true;
                        }
                    }
                    //解析数据,判断是否继续查询下一页
                } while (isHasPage);
            }
        }
        //补充accountId
        if (CollectionUtils.isNotEmpty(employeeCodes)) {
            log.info("开始补充accountId");
            JSONArray users = getAccountId(employeeCodes);
            if (CollectionUtils.isNotEmpty(users)) {
                for (int i = 0; i < users.size(); i++) {
                    JSONObject jsonObject = users.getJSONObject(i);
                    String employeeCode = jsonObject.getString("employeeCode");
                    String accountId = jsonObject.getString("accountId");
                    Optional<DingUserInfo> first = dingUserInfoList.stream().filter(dingUserInfo -> dingUserInfo.getEmployeeCode().equals(employeeCode))
                            .findFirst();
                    if (first.isPresent()) {
                        DingUserInfo dingUserInfo = first.get();
                        dingUserInfo.setAccountId(Long.parseLong(accountId));
                    }
                }
            }
        }
 
        JSONArray arr = new JSONArray(Collections.singletonList(dingUserInfoList));
        redisTemplate.opsForValue().set("userJsonList", arr.toJSONString());
        return dingUserInfoList;
    }
 
    /**
     * 根据员工code 获取员工accountId
     *
     * @param employeeCodes 员工code
     * @return 钉钉用户体系实体对象
     */
    private JSONArray getAccountId(List<String> employeeCodes) {
        log.info("Code参数集合------>{}", employeeCodes);
        PostClient postClient = executableClient.newPostClient(POST_ACCOUNTID_BY_EMPLOYEECODE);
        for (String code : employeeCodes) {
            postClient.addParameter("employeeCodes", code);
        }
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        String apiResult = postClient.post();
        JSONObject resObj = parsingJson(apiResult);
        if (null != resObj) {
            return resObj.getJSONArray("data");
        }
        return null;
    }
 
    @Resource
    UmsRoleService userRoleService;
 
    /**
     * 根据员工code 获取员工详情
     *
     * @param employeeCode 员工code
     * @return 钉钉用户体系实体对象
     */
    public DingUserInfo getEmpInfoByCode(String employeeCode) {
        if (ObjectUtil.isNotEmpty(employeeCode)) {
            //根据员工code获取员工详细信息
            JSONObject empCodeObj = getEmpByCode(employeeCode, dingConfig.getTenantId());
            if (ObjectUtil.isNotEmpty(empCodeObj)) {
                empCodeObj.put("userId", DingEncodeUtil.getMD5(employeeCode.getBytes()));
                //查询任职
                JSONArray jobArr = getStaffAppointment(employeeCode);
                assert jobArr != null;
                //临时保存任职的ids(系统的用户体系)
                List<Long> posIds = new ArrayList<>();
                //用户角色code
                List<String> roleCodeList = new ArrayList<>();
                for (int j = 0; j < jobArr.size(); j++) {
                    JSONObject jobPos = jobArr.getJSONObject(j);
                    //是否主职
                    Boolean isMain = jobPos.getBoolean("mainJob");
                    if (isMain) {
                        JSONObject holdapost = jobArr.getJSONObject(0);
                        String orgCode = holdapost.getString("organizationCode");
                        if (StrUtil.isNotBlank(orgCode)) {
//                            JSONObject orgDetail = getOrgDetailByCode(orgCode);
//                            if (null != orgDetail) {
//                                String orgId = orgDetail.getString("organizationCode");
//                                String orgName = orgDetail.getString("organizationName");
                            Long orgId = Long.parseLong(DingEncodeUtil.getMD5(orgCode.getBytes()));
                            empCodeObj.put("orgId", orgId);
//                                empCodeObj.put("orgName", orgName);
//                            }
                        }
                    }
                    //职位信息
                    String posName = jobPos.getString("govEmpPosJob");
                    if (StrUtil.isBlank(posName)) {
                        posName = "未填写职位名称";
                    }
                    //角色
                    String roleCode = jobPos.getString("empPosEmployeeRoleCode");
                    roleCodeList.add(roleCode);
                    String code = DingEncodeUtil.getMD5(posName.getBytes());
                    //查询是否有重复的
                    LambdaQueryWrapper<UmsRole> posLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    // posLambdaQueryWrapper.eq(UmsRole::getCode, code);
                    posLambdaQueryWrapper.eq(UmsRole::getName, posName);
                    posLambdaQueryWrapper.last("LIMIT 1");
                    UmsRole posOne = userRoleService.getOne(posLambdaQueryWrapper);
                    //任职名称不存在,自动添加
                    if (ObjectUtil.isEmpty(posOne)) {
                        posOne = new UmsRole();
                        posOne.setSort(100);
                        posOne.setName(posName);
                        posOne.setCode(code);
                        posOne.setStatus(1);
                        userRoleService.save(posOne);
                    } else {
                        posOne.setCode(code);
                        userRoleService.updateById(posOne);
                    }
                    posIds.add(posOne.getId());
                }
                //转换为实体类
                DingUserInfo dingUserInfo = BeanUtil.toBean(empCodeObj, DingUserInfo.class);
                Long orgId = dingUserInfo.getOrgId();
                if (ObjectUtil.isNotEmpty(orgId)) {
                    dingUserInfo.setPosIdList(posIds);
                    dingUserInfo.setRoleCodeList(roleCodeList);
                }
                return dingUserInfo;
            }
        }
        return null;
    }
 
 
    /**
     * 根据员工Code获取员工的任职
     */
    private JSONArray getStaffAppointment(String empCode) {
        PostClient postClient = executableClient.newPostClient(POST_STAFF_APPOINTMENT);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("employeeCode", empCode);
        String apiResult = postClient.post();
        JSONObject dataObj = parsingJson(apiResult);
        if (null != dataObj) {
            return dataObj.getJSONArray("data");
        }
        return null;
    }
 
    /**
     * 根据组织code获取组织详情
     */
    private JSONObject getOrgDetailByCode(String orgCode) {
        PostClient postClient = executableClient.newPostClient(POST_ORG_BY_CODE);
        postClient.addParameter("tenantId", dingConfig.getTenantId());
        postClient.addParameter("organizationCode", orgCode);
        String apiResult = postClient.post();
        JSONObject dataObj = parsingJson(apiResult);
        if (null != dataObj) {
            return dataObj.getJSONObject("data");
        }
        return dataObj;
    }
 
 
    /**
     * 批量根据组织Code查询详情
     */
    private JSONArray getOrgDetailByCodeList(List<String> codeList) {
        JSONArray resultArr = new JSONArray();
        if (ObjectUtil.isNotEmpty(codeList)) {
            int codeIndexSize = codeList.size() % 100 == 0 ? codeList.size() / 100 : codeList.size() / 100 + 1;
            List<String> result;
            int start = 0;
            int end = 0;
            for (int i = 1; i <= codeIndexSize; i++) {
                if (codeList.size() >= 100) {
                    end += 100;
                    if (i == codeIndexSize) {
                        end = codeList.size();
                    }
                    result = codeList.subList(start, end);
                    start = end;
                } else {
                    result = codeList;
                }
                //executableClient保证单例
                IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient(POST_ORG_DETIAL_CODE_BATTH);
                OapiMoziOrganizationListOrganizationsByCodesRequest oapiMoziOrganizationListOrganizationsByCodesRequest = new OapiMoziOrganizationListOrganizationsByCodesRequest();
                oapiMoziOrganizationListOrganizationsByCodesRequest.setOrganizationCodes(result);
                oapiMoziOrganizationListOrganizationsByCodesRequest.setTenantId(Long.parseLong(dingConfig.getTenantId()));
                //获取结果
                OapiMoziOrganizationListOrganizationsByCodesResponse apiResult = intelligentPostClient.post(oapiMoziOrganizationListOrganizationsByCodesRequest);
                if (apiResult.getSuccess()) {
                    JSONArray obj = JSONArray.parseArray(apiResult.getContent().getData());
                    resultArr.addAll(obj);
                }
            }
        }
        return resultArr;
    }
 
    /**
     * 根据分页获取下⼀级组织 Code 列表
     *
     * @return
     */
    private List<String> orgNameList = new ArrayList<>();
 
    /**
     * 分页获取下⼀级组织 Code 列表
     *
     * @param partCode
     */
    private void getPageOrgCode(String partCode) {
        //临时存放当前组织
        List<String> currTempOrgList = new ArrayList<>();
//        PostClient postClient = executableClient.newPostClient(POST_ORG_PAGE_CODE_LIST);
//        postClient.addParameter("tenantId", TENANT_ID);
//        postClient.addParameter("organizationCode", partCode);
        //只查询有效数据
//        postClient.addParameter("status", "A");
//        postClient.addParameter("pageSize", "100");
        IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(POST_ORG_PAGE_CODE_LIST);
        OapiMoziOrganizationPageSubOrganizationCodesRequest options = new OapiMoziOrganizationPageSubOrganizationCodesRequest();
//        options.setReturnTotalSize(true);
        options.setOrganizationCode(partCode);
        options.setPageSize(100);
        options.setStatus("A");
        options.setTenantId(Long.valueOf(dingConfig.getTenantId()));
        //page页
        Integer page = 0;
        //是否有下一页
        boolean isHasPage;
        do {
            isHasPage = false;
            page++;
//            postClient.addParameter("pageNo", page.toString());
//            String apiResult = postClient.post();
            options.setPageNo(page);
            OapiMoziOrganizationPageSubOrganizationCodesResponse apiResult = intelligentGetClient.get(options);
            JSONObject resObj = parsingJson(JSON.toJSONString(apiResult));
            if (null != resObj) {
                if (resObj.containsKey("data")) {
                    JSONArray dataArr = resObj.getJSONArray("data");
                    for (int i = 0; i < dataArr.size(); i++) {
                        String deptCode = dataArr.getString(i);
                        currTempOrgList.add(deptCode);
                        orgNameList.add(deptCode);
                    }
                    isHasPage = true;
                }
            }
            //解析数据,判断是否继续查询下一页
        } while (isHasPage);
        for (String str : currTempOrgList) {
            getPageOrgCode(str);
        }
    }
 
 
    /**
     * 解析返回值
     *
     * @param apiResult
     */
    private JSONObject parsingJson(String apiResult) {
        try {
            if (ObjectUtil.isNotNull(apiResult)) {
                JSONObject resJson = JSONObject.parseObject(apiResult);
                if (resJson.getBoolean("success")) {
                    JSONObject data = resJson.getJSONObject("content");
                    if (ObjectUtil.isNotEmpty(data)) {
                        return data;
                    }
                } else if (!ObjectUtil.equals("OPF-B001-05-16-0006", resJson.getString("bizErrorCode"))) {
                    log.error("DING_ERROR:" + resJson.toJSONString());
                    return resJson;
                }
            }
        } catch (Exception e) {
            redisTemplate.opsForValue().set("error_" + System.currentTimeMillis(), apiResult, 10, TimeUnit.MINUTES);
            return null;
        }
        return null;
    }
 
 
    /**
     * 拼接要发送的消息  文本消息
     * json对象 必须 {"msgtype":"text","text":{"content":"消息内容"}}
     * 消息内容,目前支持:文本消息:text, 链接消息:link, OA消息:oa, 卡片消息:action_card。最长不超过2048个字节
     *
     * @param content
     */
    private String getJsonStringText(String content) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype", "text");//消息类型,此时固定为:text
        JSONObject text = new JSONObject();
        text.put("content", content);//消息内容,建议500字符以内
        jsonObject.put("text", text);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        return jsonStr;
    }
 
    /**
     * 拼接要发送的消息  markdown消息
     * json对象 必须 {"msgtype":"markdown","markdown":{"title":"首屏会话透出的展示内容","text":"消息内容"}}
     *
     * @param title 首屏会话透出的展示内容
     */
    private String getJsonMarkDown(String title, String text) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype", "markdown");//消息类型,此时固定为:markdown
        JSONObject markdown = new JSONObject();
        markdown.put("title", title);//首屏会话透出的展示内容
        markdown.put("text", text);//消息内容,建议500字符以内
        jsonObject.put("markdown", markdown);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        return jsonStr;
    }
 
    /**
     * 拼接要发送的消息   链接消息
     *
     * @param messageUrl 消息点击链接地址,当发送消息为小程序时支持小程序跳转链接
     * @param picUrl     图片地址
     * @param title      消息标题,建议100字符以内
     * @param text       消息描述,建议500字符以内
     */
    private String getJsonStringLink(String messageUrl, String picUrl, String title, String text) {
        JSONObject jsonObject = new JSONObject();
        //消息类型,此时固定为:text
        jsonObject.put("msgtype", "link");
        JSONObject link = new JSONObject();
        link.put("messageUrl", messageUrl);
        link.put("picUrl", picUrl);
        link.put("title", title);
        link.put("text", text);
        jsonObject.put("link", link);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        return jsonStr;
    }
 
    /**
     * 拼接要发送的消息   卡片链接消息
     *
     * @param messageUrl 消息点击链接地址,当发送消息为小程序时支持小程序跳转链接
     * @param title      消息标题,建议100字符以内
     * @param text       消息描述,建议500字符以内
     */
    private String getJsonStringCardLink(String messageUrl, String messagePcUrl, String title, String text) {
        JSONObject jsonObject = new JSONObject();
        //消息类型,此时固定为:text
        jsonObject.put("msgtype", "action_card");
        JSONObject link = new JSONObject();
        link.put("title", title);
        link.put("markdown", text);
        link.put("single_title", "查看详情");
        link.put("single_url", messageUrl);
        link.put("single_pc_url", messagePcUrl);
        jsonObject.put("action_card", link);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        return jsonStr;
    }
//
//    /**
//     * 下载员工头像
//     *
//     * @param mediaId 媒体头像id
//     */
//    public void downloadEmployeeAvatar(String mediaId) {
//        try {
//            String api = "/media/download";
//            GetClient getClient = executableClient.newGetClient(api);
//            //设置参数
//            String dingStr = redisTemplate.opsForValue().get("ding");
//            JSONObject dingObj = JSONObject.parseObject(dingStr);
//            String getToken = dingObj.getString("token");
//            getClient.addParameter("access_token", getToken);
//            getClient.addParameter("media_id", mediaId.replace("$", ""));
//            CloseableHttpResponse response = getClient.getB();
//            //调用API
//            String fileName = mediaId + ".jpg";
//            String path = ConstantContextHolder.getSysConfig("SNOWY_FILE_UPLOAD_PATH_FOR_WINDOWS", String.class, null) + "/Avatar/";
//            File voice = new File(path + fileName);
//            FileUtil.writeFromStream(response.getEntity().getContent(), voice);
//        } catch (Exception e) {
//            log.error("DING_ERROR:" + e.getMessage());
//            System.out.println(e);
//        }
//    }
 
}