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.apache.commons.lang3.StringUtils; 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 metaList = (List) UtilFile.readJsonObjectFromResource("static/json/personnel.json"); // 本地匹配了宜搭组件ID // List metaList = ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId()); // 同步全量人员 ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true).forEach(deptId -> { List 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 employeeField = (List) 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 metaField = (List) 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) 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 formInstIds = (List) 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; private static final int BUDGET_PER = 200; /** * 同步部门信息 */ @Override public void syncDepartment() { // 部门已同步数据 List dataList = ydService.queryFormData_all(YDParam.builder() .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH") .build()); // 匹配钉钉通讯录 List deptList = ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true, DDConf.TOP_DEPARTMENT); for (long deptId : deptList) { List 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() * BUDGET_PER; // 部门人均 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); } } /// 涉及行政归属, 拆分规则 private void updateBalance(Map item, int budget, float quota) { item.put("numberField_lpaks318", UtilMap.getFloat(item, "numberField_lpaks318") + budget); // 总经费 item.put("numberField_lpayajrn", UtilMap.getFloat(item, "numberField_lpayajrn") + budget); // 可用余额 item.put("numberField_lr7yq38c", UtilMap.getFloat(item, "numberField_lr7yq38c") + budget); // 修改前总金额 item.put("numberField_lr71mvbk", UtilMap.getFloat(item, "numberField_lr71mvbk") + budget); // 当月预计预算 item.put("numberField_lr71bkuj", UtilMap.getInt(item, "numberField_lr71bkuj") + quota); // 部门人数 } /** * 部门预算统计 */ @Override public void calcBudget() { // 部门已同步数据 List dataList = ydService.queryFormData_all(YDParam.builder() .formUuid("FORM-B6F662B18C3F4D7D855CFE50243394AFQPOH") .build()); // prd 不存在一人多部门情况, 若是涉及行政归属则手动维护 List manualList = ydService.queryFormData_all(YDParam.builder() .formUuid("FORM-A192B0134C184B1A84A791F322AA4D60N4F2") .build()); for (Map data : manualList) { String userId = String.valueOf(UtilMap.getList(data, "employeeField_lroz3y2w_id").get(0)); Map srcDpet = dataList.stream().filter(item -> UtilMap.getList(data, "employeeField_lr71bkui_id").contains(userId)).findAny().get(); updateBalance(srcDpet, -BUDGET_PER, -1); // 去除原部门个人金额 boolean multi = StringUtils.isNotBlank(UtilMap.getString(data, "numberField_lroz3y37")); if (multi) { Map curDpet = dataList.stream().filter(item -> UtilMap.getList(data, "numberField_lroz3y37").contains(item.get("numberField_lr70cqsk"))).findAny().get(); updateBalance(curDpet, BUDGET_PER / 2, 0.5f); // 累加部门2个人金额一半 } Map curDpet = dataList.stream().filter(item -> UtilMap.getList(data, "numberField_lr70cqsk").contains(item.get("numberField_lr70cqsk"))).findAny().get(); updateBalance(curDpet, multi ? BUDGET_PER / 2 : BUDGET_PER, multi ? 0.5f : 1f); // 累加部门1个人金额 } // prd todo 非独立预算部门, 累计到父部门, 若父部门为空, 持续往上累加 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); } }); } /// test @Override public void test() { ddClient_personnel.getPersonnelMeta(ddClient.getAccessToken(), ddConf.getAgentId()); } }