package com.ruzhoukj.sms.controller; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest; import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse; import com.google.common.collect.Lists; import com.ruzhoukj.sms.ding.PermissionConfig; import com.ruzhoukj.sms.model.vo.DingDeptVO; import com.taobao.api.ApiException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.checkerframework.checker.nullness.qual.Nullable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; /** * @author : wujipeng * @date : 2025/2/28 10:00 * @Version: 1.0 * @Desc : 钉钉考勤管理 */ @Api(tags = "钉钉考勤管理") @RestController @RequestMapping("/ding/attendance") public class DingAttendanceController { @Resource private PermissionConfig permissionConfig; @ApiOperation(value = "获取子部门ID列表") @GetMapping("/child/{deptId}") public List getChildIdList(@PathVariable Long deptId) { String accessToken = permissionConfig.getAccessToken(); try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid"); OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest(); req.setDeptId(deptId); req.setHttpMethod("POST"); OapiV2DepartmentListsubidResponse rsp = client.execute(req, accessToken); OapiV2DepartmentListsubidResponse.DeptListSubIdResponse result = rsp.getResult(); return result.getDeptIdList(); } catch (ApiException e) { throw new RuntimeException(e); } } @ApiOperation(value = "获取部门树状图") @GetMapping("/tree") public DingDeptVO getDeptTree() { // 创建根节点 DingDeptVO root = new DingDeptVO(); root.setId(1L); // 构建子节点树 List childNodes = buildChildTree(1L); // 设置子节点到根节点 root.setChild(childNodes); return root; } /** * 递归构建子节点树 */ private List buildChildTree(Long parentId) { // 获取当前父节点的所有子节点ID List childIds = getChildIdList(parentId); // 如果没有子节点,返回空列表 if (childIds == null || childIds.isEmpty()) { return Collections.emptyList(); } // 构建子节点列表 List children = new ArrayList<>(); for (Long childId : childIds) { DingDeptVO child = DingDeptVO.builder() .id(childId) .build(); // 递归构建子节点的子树 List grandchildren = buildChildTree(childId); if (!grandchildren.isEmpty()) { child.setChild(grandchildren); } children.add(child); } return children; } }