LMImplService.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.malk.lemeng.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.malk.lemeng.service.LMService;
  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.aliwork.YDService;
  9. import com.malk.service.dingtalk.DDClient;
  10. import com.malk.service.dingtalk.DDClient_Contacts;
  11. import com.malk.service.dingtalk.DDClient_Personnel;
  12. import com.malk.utils.UtilFile;
  13. import com.malk.utils.UtilMap;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.util.*;
  18. @Service
  19. @Slf4j
  20. public class LMImplService implements LMService {
  21. @Autowired
  22. private DDClient ddClient;
  23. @Autowired
  24. private DDConf ddConf;
  25. @Autowired
  26. private DDClient_Contacts ddClient_contacts;
  27. @Autowired
  28. private DDClient_Personnel ddClient_personnel;
  29. @Autowired
  30. private YDClient ydClient;
  31. /**
  32. * 同步花名册信息
  33. */
  34. @Override
  35. public void syncRoster() {
  36. // 花名册元数据
  37. List<Map> metaList = (List<Map>) UtilFile.readJsonObjectFromResource("static/json/personnel.json"); // 本地匹配了宜搭组件ID
  38. // List<Map> metaList = ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
  39. // 同步全量人员
  40. ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true).forEach(deptId -> {
  41. List<String> userIds = ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), deptId);
  42. log.info("dept, {}, userIds, {}", deptId, userIds.size());
  43. if (userIds.size() == 0) {
  44. return;
  45. }
  46. // 员工花名册信息
  47. ddClient_personnel.getEmployeeInfos(ddClient.getAccessToken(), userIds, ddConf.getAgentId(), null).forEach(employeeInfo -> {
  48. // 通过元数据字段code, 匹配员工花名册value
  49. List<Map> employeeField = (List<Map>) employeeInfo.get("field_data_list");
  50. // 宜搭表单数据
  51. Map formData = UtilMap.map("employeeField_limrznyp", Arrays.asList(employeeInfo.get("userid"))); // 成员权限
  52. metaList.forEach(meta -> {
  53. boolean isDetail = UtilMap.getBoolean(meta, "detail");
  54. List<Map> metaField = (List<Map>) meta.get("field_meta_info_list");
  55. Map detail = new HashMap(); // 明细行
  56. metaField.forEach(field -> {
  57. // 元数据内一些系统字段无 field_code, sys00 基本信息分组下 使用 field_name
  58. 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();
  59. if (optional.isPresent()) {
  60. // 数据组装
  61. Map employee = (Map) optional.get();
  62. String value = UtilMap.getString(((List<Map>) employee.get("field_value_list")).get(0), "label");
  63. log.info("分组 -> {}, 是否明细 -> {}; 字段 -> {}, 值 -> {}", meta.get("group_name"), meta.get("detail"), field.get("field_name"), value);
  64. // 值处理
  65. if (field.containsKey("comp_id")) {
  66. if (isDetail) {
  67. detail.put(field.get("comp_id"), value);
  68. } else {
  69. formData.put(field.get("comp_id"), value);
  70. }
  71. }
  72. }
  73. });
  74. // 明细表
  75. if (isDetail && meta.containsKey("comp_id")) {
  76. formData.put(meta.get("comp_id"), Arrays.asList(detail));
  77. }
  78. });
  79. // 宜搭更新 todo 查询同步数据, 代码内匹配, 而不是循环内查询
  80. YDParam ydParam = YDParam.builder()
  81. .searchFieldJson(JSON.toJSONString(UtilMap.map("employeeField_limrznyp", formData.get("employeeField_limrznyp"))))
  82. .formUuid("FORM-54C47C335C054FFBAECCA0B92100A341PGD2")
  83. .build();
  84. List<String> formInstIds = (List<String>) ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form_id).getData();
  85. if (formInstIds.size() > 0) {
  86. ydParam.setFormInstanceId(formInstIds.get(0));
  87. ydParam.setUpdateFormDataJson(JSON.toJSONString(formData));
  88. ydClient.operateData(ydParam, YDConf.FORM_OPERATION.update);
  89. } else {
  90. ydParam.setFormDataJson(JSON.toJSONString(formData));
  91. ydClient.operateData(ydParam, YDConf.FORM_OPERATION.create);
  92. }
  93. });
  94. });
  95. }
  96. @Autowired
  97. private YDService ydService;
  98. /**
  99. * 同步部门信息
  100. */
  101. @Override
  102. public void syncDepartment() {
  103. // 部门已同步数据
  104. List<Map> dataList = ydService.queryFormData_all(YDParam.builder()
  105. .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
  106. .build());
  107. // 匹配钉钉通讯录
  108. List<Long> deptList = ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true, DDConf.TOP_DEPARTMENT);
  109. for (long deptId : deptList) {
  110. List<String> userIds = ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), deptId);
  111. if (userIds.size() == 0) {
  112. continue;
  113. }
  114. Map info = ddClient_contacts.getDepartmentInfo(ddClient.getAccessToken(), deptId);
  115. Map formData = UtilMap.map("textField_lpcff8zu, numberField_lr70cqsl, numberField_lr70cqsk", info.get("name"), info.get("parent_id"), info.get("dept_id"));
  116. // todo 部门ID写入需要是string集合\数组
  117. formData.putAll(UtilMap.map("employeeField_lpmbmyaa, departmentSelectField_lpaks312", info.get("dept_manager_userid_list"), Arrays.asList(String.valueOf(info.get("dept_id")))));// float budget = userIds.size() * 200; // 部门人均
  118. float budget = userIds.size() * 200; // 部门人均
  119. formData.putAll(UtilMap.map("numberField_lr71bkuj, employeeField_lr71bkui, numberField_lr71mvbk", userIds.size(), userIds, budget));
  120. // Upsert操作, 记录当月预算
  121. Optional optional = dataList.stream().filter(item -> deptId == UtilMap.getLong(item, "numberField_lr70cqsk")).findAny();
  122. if (optional.isPresent()) {
  123. formData.put("employeeField_lr71bkui", UtilMap.getString((Map) optional.get(), "employeeField_lr71bkui_id")); // 非常规组件数据处理
  124. ydClient.operateData(YDParam.builder()
  125. .formInstanceId(UtilMap.getString((Map) optional.get(), "instanceId"))
  126. .updateFormDataJson(JSON.toJSONString(formData))
  127. .build(), YDConf.FORM_OPERATION.update);
  128. continue;
  129. }
  130. formData.putAll(UtilMap.map("numberField_lpaks318, numberField_lpayajrp, numberField_lpayajrn", 0, 0, 0));
  131. formData.putAll(UtilMap.map("radioField_lpaks316", "是")); // test
  132. ydClient.operateData(YDParam.builder()
  133. .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
  134. .formDataJson(JSON.toJSONString(formData))
  135. .build(), YDConf.FORM_OPERATION.create);
  136. }
  137. }
  138. /**
  139. * 部门预算统计
  140. */
  141. @Override
  142. public void calcBudget() {
  143. // 部门已同步数据
  144. List<Map> dataList = ydService.queryFormData_all(YDParam.builder()
  145. .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
  146. .build());
  147. // prd 非独立预算部门, 累计到父部门, 若父部门为空, 持续往上累加
  148. dataList.forEach(item -> {
  149. if ("是".equals(item.get("radioField_lpaks316"))) {
  150. int budget = UtilMap.getInt(item, "numberField_lr71mvbk");
  151. // 冗余修改记录, 保留修改前金额并重置budget, 避免重复调用
  152. item.putAll(UtilMap.map("numberField_lr7yq38c, numberField_lr71mvbk", UtilMap.getFloat(item, "numberField_lpaks318"), 0));
  153. item.put("numberField_lpaks318", UtilMap.getFloat(item, "numberField_lpaks318") + budget);
  154. item.put("numberField_lpayajrn", UtilMap.getFloat(item, "numberField_lpayajrn") + budget);
  155. item.putAll(UtilMap.map("employeeField_lr71bkui, departmentSelectField_lpaks312", item.get("employeeField_lr71bkui_id"), item.get("departmentSelectField_lpaks312_id"))); // 非常规组件数据处理
  156. ydClient.operateData(YDParam.builder()
  157. .formInstanceId(UtilMap.getString(item, "instanceId"))
  158. .updateFormDataJson(JSON.toJSONString(item))
  159. .build(), YDConf.FORM_OPERATION.update);
  160. }
  161. // todo 一人多部门情况
  162. });
  163. }
  164. /// test
  165. @Override
  166. public void test() {
  167. ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
  168. }
  169. }