package com.malk.service.dingtalk.impl; import cn.hutool.core.util.ObjectUtil; import com.malk.server.dingtalk.DDConf; import com.malk.service.dingtalk.DDClient_Attendance; import com.malk.service.dingtalk.DDClient_Contacts; import com.malk.service.dingtalk.DDClient_Workflow; import com.malk.service.dingtalk.DDService; import com.malk.utils.UtilMap; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @Service @Slf4j public class DDImplService implements DDService { @Autowired private DDClient_Workflow ddClient_workflow; @Autowired private DDClient_Attendance ddClient_attendance; @Autowired private DDClient_Contacts ddClient_contacts; /** * 新发起审批15s内不允许撤销, 异步执行 [审批同意/拒绝只能通过节点操作, 系统无法直接介入] -- 异步需要中转一层进行触发, client为原子接口 */ @Async @Override @SneakyThrows public void terminateRunningApprove_async(String access_token, String processInstanceId, boolean isSystem, String subject, String description, String operatingUserId, String noteUserId) { ddClient_workflow.createTBTask(access_token, noteUserId, subject, description, new Date().getTime() + 16000, Arrays.asList(noteUserId), null, null, false, 20, null); Thread.sleep(16000); ddClient_workflow.terminateRunningApprove(access_token, processInstanceId, isSystem, description, operatingUserId); } /** * 钉钉查询假期余额返回是记录, 按照消失/天单位计算该假期类型下的余额 */ @Override public Map queryVacationQuota_balance(String access_token, String op_userid, String leave_code, String userids, int offset, int size) { List records = ddClient_attendance.queryVacationQuota_all(access_token, op_userid, leave_code, userids, offset, size); if (ObjectUtil.isNull(records)) { return null; } AtomicInteger balance = new AtomicInteger(); AtomicReference unit = new AtomicReference<>(""); records.forEach(item -> { if (ObjectUtil.isNotNull(item.get("quota_num_per_hour"))) { balance.set(balance.get() + Integer.valueOf(String.valueOf(item.get("quota_num_per_hour"))) - Integer.valueOf(String.valueOf(item.get("used_num_per_hour")))); unit.set("小时"); } else { balance.set(balance.get() + (Integer.valueOf(String.valueOf(item.get("quota_num_per_day"))) - Integer.valueOf(String.valueOf(item.get("used_num_per_day"))))); unit.set("天"); } }); Map result = new HashMap(); result.put("balance", balance.get() / 100f); result.put("unit", unit); return result; } /** * 判断员工是否在指定部门 */ @Override public boolean matchDepartment(String access_token, String userId, List deptIds) { List deptIdList = (List) ddClient_contacts.getUserInfoById(access_token, userId).get("dept_id_list"); boolean isMatch = false; // 兼容多部门场景 for (Number deptId : deptIdList) { // ppExt: 不要直接使用 Number 作为类型, 不同基本类型比较值时会有偏差 [可以作为父类接受数据, 避免直接强制类型转换错误, 再进行基本类型处理] isMatch = _matchDepartment(access_token, deptId.longValue(), deptIds); if (isMatch) { break; } } return isMatch; } /// 递归: 判断员工是否在指定部门 boolean _matchDepartment(String access_token, long dept_id, List deptIds) { // 判断入参 if (dept_id == DDConf.TOP_DEPARTMENT) { return false; } if (deptIds.contains(dept_id)) { return true; } Map deptInfo = ddClient_contacts.getDepartmentInfo(access_token, dept_id); long parentId = UtilMap.getLong(deptInfo, "parent_id"); long deptId = UtilMap.getLong(deptInfo, "dept_id"); _matchDepartment(access_token, parentId, deptIds); // 判断递归 if (deptId == DDConf.TOP_DEPARTMENT) { return false; } if (deptIds.contains(parentId)) { return true; } return false; } }