GWImplService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.malk.gewu.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.malk.gewu.service.GWService;
  4. import com.malk.server.aliwork.YDConf;
  5. import com.malk.server.aliwork.YDParam;
  6. import com.malk.server.dingtalk.DDConf;
  7. import com.malk.service.aliwork.YDClient;
  8. import com.malk.service.dingtalk.DDClient;
  9. import com.malk.service.dingtalk.DDClient_Contacts;
  10. import com.malk.service.dingtalk.DDClient_Personnel;
  11. import com.malk.service.dingtalk.DDClient_Schedule;
  12. import com.malk.utils.UtilDateTime;
  13. import com.malk.utils.UtilFile;
  14. import com.malk.utils.UtilMap;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.*;
  19. @Service
  20. @Slf4j
  21. public class GWImplService implements GWService {
  22. @Autowired
  23. private DDClient ddClient;
  24. @Autowired
  25. private DDConf ddConf;
  26. @Autowired
  27. private DDClient_Contacts ddClient_contacts;
  28. @Autowired
  29. private DDClient_Personnel ddClient_personnel;
  30. @Autowired
  31. private YDClient ydClient;
  32. /**
  33. * 同步花名册信息
  34. */
  35. @Override
  36. public void syncRoster() {
  37. // 花名册元数据
  38. List<Map> metaList = (List<Map>) UtilFile.readJsonObjectFromResource("static/json/personnel"); // 本地匹配了宜搭组件ID
  39. // List<Map> metaList = ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
  40. // 同步全量人员
  41. ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true).forEach(deptId -> {
  42. List<String> userIds = ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), deptId);
  43. log.info("dept, {}, userIds, {}", deptId, userIds.size());
  44. if (userIds.size() == 0) {
  45. return;
  46. }
  47. // 员工花名册信息
  48. ddClient_personnel.getEmployeeInfos(ddClient.getAccessToken(), userIds, ddConf.getAgentId(), null).forEach(employeeInfo -> {
  49. // 通过元数据字段code, 匹配员工花名册value
  50. List<Map> employeeField = (List<Map>) employeeInfo.get("field_data_list");
  51. // 宜搭表单数据
  52. Map formData = UtilMap.map("employeeField_limrznyp", Arrays.asList(employeeInfo.get("userid"))); // 成员权限
  53. metaList.forEach(meta -> {
  54. boolean isDetail = UtilMap.getBoolean(meta, "detail");
  55. List<Map> metaField = (List<Map>) meta.get("field_meta_info_list");
  56. Map detail = new HashMap(); // 明细行
  57. metaField.forEach(field -> {
  58. // 元数据内一些系统字段无 field_code, sys00 基本信息分组下 使用 field_name
  59. Optional optional = employeeField.stream().filter(employee -> field.get("field_code").equals(employee.get("field_code")) || employee.get("field_name").equals(field.get("field_name"))).findAny();
  60. if (optional.isPresent()) {
  61. // 数据组装
  62. Map employee = (Map) optional.get();
  63. String value = UtilMap.getString(((List<Map>) employee.get("field_value_list")).get(0), "label");
  64. log.info("分组 -> {}, 是否明细 -> {}; 字段 -> {}, 值 -> {}", meta.get("group_name"), meta.get("detail"), field.get("field_name"), value);
  65. // 值处理
  66. if (field.containsKey("comp_id")) {
  67. if (isDetail) {
  68. detail.put(field.get("comp_id"), value);
  69. } else {
  70. formData.put(field.get("comp_id"), value);
  71. }
  72. }
  73. }
  74. });
  75. // 明细表
  76. if (isDetail && meta.containsKey("comp_id")) {
  77. formData.put(meta.get("comp_id"), Arrays.asList(detail));
  78. }
  79. });
  80. // 宜搭更新
  81. YDParam ydParam = YDParam.builder()
  82. .searchFieldJson(JSON.toJSONString(UtilMap.map("employeeField_limrznyp", formData.get("employeeField_limrznyp"))))
  83. .formUuid("FORM-EA866E71M5ICA9TSABIFG9V1QMRN2PFL786KL8")
  84. .build();
  85. List<String> formInstIds = (List<String>) ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form_id).getData();
  86. if (formInstIds.size() > 0) {
  87. ydParam.setFormInstanceId(formInstIds.get(0));
  88. ydParam.setUpdateFormDataJson(JSON.toJSONString(formData));
  89. ydClient.operateData(ydParam, YDConf.FORM_OPERATION.update);
  90. } else {
  91. ydParam.setFormDataJson(JSON.toJSONString(formData));
  92. ydClient.operateData(ydParam, YDConf.FORM_OPERATION.create);
  93. }
  94. });
  95. });
  96. }
  97. @Autowired
  98. private DDClient_Schedule ddClient_schedule;
  99. /**
  100. * 创建钉钉日程
  101. */
  102. @Override
  103. public void createSchedule(String summary, String description, List<String> usrIds, Date sTime, Date eTIme, String organizer) {
  104. String startTime = UtilDateTime.format(sTime, UtilDateTime.DATE_TIME_ISO).replace("+0800", "+08:00");
  105. String endTime = UtilDateTime.format(eTIme, UtilDateTime.DATE_TIME_ISO).replace("+0800", "+08:00");
  106. // ppExt: start 与 end 不能使用同一个map对象, 会报错date不能为空.
  107. Map start = UtilMap.map("dateTime, timeZone", startTime, "Asia/Shanghai");
  108. Map end = UtilMap.map("dateTime, timeZone", endTime, "Asia/Shanghai");
  109. Map body = UtilMap.map("summary, description, start, end, userIds", summary, description, start, end, usrIds);
  110. ddClient_schedule.eventsSchedule(ddClient.getAccessToken(), organizer, body);
  111. }
  112. /// test
  113. @Override
  114. public void test() {
  115. ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
  116. }
  117. }