123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.malk.lemeng.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.malk.lemeng.service.LMService;
- import com.malk.server.aliwork.YDConf;
- import com.malk.server.aliwork.YDParam;
- import com.malk.server.dingtalk.DDConf;
- import com.malk.service.aliwork.YDClient;
- import com.malk.service.aliwork.YDService;
- import com.malk.service.dingtalk.DDClient;
- import com.malk.service.dingtalk.DDClient_Contacts;
- import com.malk.service.dingtalk.DDClient_Personnel;
- import com.malk.utils.UtilFile;
- import com.malk.utils.UtilMap;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- @Service
- @Slf4j
- public class LMImplService implements LMService {
- @Autowired
- private DDClient ddClient;
- @Autowired
- private DDConf ddConf;
- @Autowired
- private DDClient_Contacts ddClient_contacts;
- @Autowired
- private DDClient_Personnel ddClient_personnel;
- @Autowired
- private YDClient ydClient;
- /**
- * 同步花名册信息
- */
- @Override
- public void syncRoster() {
- // 花名册元数据
- List<Map> metaList = (List<Map>) UtilFile.readJsonObjectFromResource("static/json/personnel.json"); // 本地匹配了宜搭组件ID
- // List<Map> metaList = ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
- // 同步全量人员
- ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true).forEach(deptId -> {
- List<String> userIds = ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), deptId);
- log.info("dept, {}, userIds, {}", deptId, userIds.size());
- if (userIds.size() == 0) {
- return;
- }
- // 员工花名册信息
- ddClient_personnel.getEmployeeInfos(ddClient.getAccessToken(), userIds, ddConf.getAgentId(), null).forEach(employeeInfo -> {
- // 通过元数据字段code, 匹配员工花名册value
- List<Map> employeeField = (List<Map>) employeeInfo.get("field_data_list");
- // 宜搭表单数据
- Map formData = UtilMap.map("employeeField_limrznyp", Arrays.asList(employeeInfo.get("userid"))); // 成员权限
- metaList.forEach(meta -> {
- boolean isDetail = UtilMap.getBoolean(meta, "detail");
- List<Map> metaField = (List<Map>) meta.get("field_meta_info_list");
- Map detail = new HashMap(); // 明细行
- metaField.forEach(field -> {
- // 元数据内一些系统字段无 field_code, sys00 基本信息分组下 使用 field_name
- 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();
- if (optional.isPresent()) {
- // 数据组装
- Map employee = (Map) optional.get();
- String value = UtilMap.getString(((List<Map>) employee.get("field_value_list")).get(0), "label");
- log.info("分组 -> {}, 是否明细 -> {}; 字段 -> {}, 值 -> {}", meta.get("group_name"), meta.get("detail"), field.get("field_name"), value);
- // 值处理
- if (field.containsKey("comp_id")) {
- if (isDetail) {
- detail.put(field.get("comp_id"), value);
- } else {
- formData.put(field.get("comp_id"), value);
- }
- }
- }
- });
- // 明细表
- if (isDetail && meta.containsKey("comp_id")) {
- formData.put(meta.get("comp_id"), Arrays.asList(detail));
- }
- });
- // 宜搭更新 todo 查询同步数据, 代码内匹配, 而不是循环内查询
- YDParam ydParam = YDParam.builder()
- .searchFieldJson(JSON.toJSONString(UtilMap.map("employeeField_limrznyp", formData.get("employeeField_limrznyp"))))
- .formUuid("FORM-54C47C335C054FFBAECCA0B92100A341PGD2")
- .build();
- List<String> formInstIds = (List<String>) ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form_id).getData();
- if (formInstIds.size() > 0) {
- ydParam.setFormInstanceId(formInstIds.get(0));
- ydParam.setUpdateFormDataJson(JSON.toJSONString(formData));
- ydClient.operateData(ydParam, YDConf.FORM_OPERATION.update);
- } else {
- ydParam.setFormDataJson(JSON.toJSONString(formData));
- ydClient.operateData(ydParam, YDConf.FORM_OPERATION.create);
- }
- });
- });
- }
- @Autowired
- private YDService ydService;
- /**
- * 同步部门信息
- */
- @Override
- public void syncDepartment() {
- // 部门已同步数据
- List<Map> dataList = ydService.queryFormData_all(YDParam.builder()
- .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
- .build());
- // 匹配钉钉通讯录
- List<Long> deptList = ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true, DDConf.TOP_DEPARTMENT);
- for (long deptId : deptList) {
- List<String> userIds = ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), deptId);
- if (userIds.size() == 0) {
- continue;
- }
- Map info = ddClient_contacts.getDepartmentInfo(ddClient.getAccessToken(), deptId);
- Map formData = UtilMap.map("textField_lpcff8zu, numberField_lr70cqsl, numberField_lr70cqsk", info.get("name"), info.get("parent_id"), info.get("dept_id"));
- // todo 部门ID写入需要是string集合\数组
- 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; // 部门人均
- float budget = userIds.size() * 200; // 部门人均
- formData.putAll(UtilMap.map("numberField_lr71bkuj, employeeField_lr71bkui, numberField_lr71mvbk", userIds.size(), userIds, budget));
- // Upsert操作, 记录当月预算
- Optional optional = dataList.stream().filter(item -> deptId == UtilMap.getLong(item, "numberField_lr70cqsk")).findAny();
- if (optional.isPresent()) {
- formData.put("employeeField_lr71bkui", UtilMap.getString((Map) optional.get(), "employeeField_lr71bkui_id")); // 非常规组件数据处理
- ydClient.operateData(YDParam.builder()
- .formInstanceId(UtilMap.getString((Map) optional.get(), "instanceId"))
- .updateFormDataJson(JSON.toJSONString(formData))
- .build(), YDConf.FORM_OPERATION.update);
- continue;
- }
- formData.putAll(UtilMap.map("numberField_lpaks318, numberField_lpayajrp, numberField_lpayajrn", 0, 0, 0));
- formData.putAll(UtilMap.map("radioField_lpaks316", "是")); // test
- ydClient.operateData(YDParam.builder()
- .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
- .formDataJson(JSON.toJSONString(formData))
- .build(), YDConf.FORM_OPERATION.create);
- }
- }
- /**
- * 部门预算统计
- */
- @Override
- public void calcBudget() {
- // 部门已同步数据
- List<Map> dataList = ydService.queryFormData_all(YDParam.builder()
- .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH")
- .build());
- // prd 非独立预算部门, 累计到父部门, 若父部门为空, 持续往上累加
- dataList.forEach(item -> {
- if ("是".equals(item.get("radioField_lpaks316"))) {
- int budget = UtilMap.getInt(item, "numberField_lr71mvbk");
- // 冗余修改记录, 保留修改前金额并重置budget, 避免重复调用
- item.putAll(UtilMap.map("numberField_lr7yq38c, numberField_lr71mvbk", UtilMap.getFloat(item, "numberField_lpaks318"), 0));
- item.put("numberField_lpaks318", UtilMap.getFloat(item, "numberField_lpaks318") + budget);
- item.put("numberField_lpayajrn", UtilMap.getFloat(item, "numberField_lpayajrn") + budget);
- item.putAll(UtilMap.map("employeeField_lr71bkui, departmentSelectField_lpaks312", item.get("employeeField_lr71bkui_id"), item.get("departmentSelectField_lpaks312_id"))); // 非常规组件数据处理
- ydClient.operateData(YDParam.builder()
- .formInstanceId(UtilMap.getString(item, "instanceId"))
- .updateFormDataJson(JSON.toJSONString(item))
- .build(), YDConf.FORM_OPERATION.update);
- }
- // todo 一人多部门情况
- });
- }
- /// test
- @Override
- public void test() {
- ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId());
- }
- }
|