Commit 312a144a authored by wujipeng's avatar wujipeng
Browse files

优化

parent 49d86301
package com.ruzhoukj.sms.controller;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
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.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;
/**
* @author : wujipeng
* @date : 2025/2/28 10:00
......@@ -32,95 +15,6 @@ import java.util.List;
@RequestMapping("/ding/attendance")
public class DingAttendanceController {
@Resource
private PermissionConfig permissionConfig;
@ApiOperation(value = "获取子部门ID列表")
@GetMapping("/child/{deptId}")
public List<Long> 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();
OapiV2DepartmentGetResponse.DeptGetResponse deptDetail = getDeptDetail(1L);
root.setName(deptDetail.getName());
root.setId(1L);
// 构建子节点树
List<DingDeptVO> childNodes = buildChildTree(1L);
// 设置子节点到根节点
root.setChild(childNodes);
return root;
}
/**
* 递归构建子节点树
*/
private List<DingDeptVO> buildChildTree(Long parentId) {
// 获取当前父节点的所有子节点ID
List<Long> childIds = getChildIdList(parentId);
// 如果没有子节点,返回空列表
if (childIds == null || childIds.isEmpty()) {
return Collections.emptyList();
}
// 构建子节点列表
List<DingDeptVO> children = new ArrayList<>();
for (Long childId : childIds) {
OapiV2DepartmentGetResponse.DeptGetResponse deptDetail = getDeptDetail(childId);
DingDeptVO child = DingDeptVO.builder()
.id(childId)
.name(deptDetail.getName())
.build();
// 递归构建子节点的子树
List<DingDeptVO> grandchildren = buildChildTree(childId);
if (!grandchildren.isEmpty()) {
child.setChild(grandchildren);
}
children.add(child);
}
return children;
}
@ApiOperation(value = "查询部门详情")
@GetMapping("/detail/{deptId}")
public OapiV2DepartmentGetResponse.DeptGetResponse getDeptDetail(@PathVariable Long deptId) {
String accessToken = permissionConfig.getAccessToken();
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
req.setDeptId(deptId);
req.setLanguage("zh_CN");
req.setHttpMethod("POST");
OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
return rsp.getResult();
} catch (ApiException e) {
throw new RuntimeException(e);
}
}
}
package com.ruzhoukj.sms.controller;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
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.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;
/**
* @author : wujipeng
* @date : 2025/2/28 10:34
......@@ -15,6 +32,95 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/ding/dept")
public class DingDeptController {
@Resource
private PermissionConfig permissionConfig;
@ApiOperation(value = "获取子部门ID列表")
@GetMapping("/child/{deptId}")
public List<Long> 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();
OapiV2DepartmentGetResponse.DeptGetResponse deptDetail = getDeptDetail(1L);
root.setName(deptDetail.getName());
root.setId(1L);
// 构建子节点树
List<DingDeptVO> childNodes = buildChildTree(1L);
// 设置子节点到根节点
root.setChild(childNodes);
return root;
}
/**
* 递归构建子节点树
*/
private List<DingDeptVO> buildChildTree(Long parentId) {
// 获取当前父节点的所有子节点ID
List<Long> childIds = getChildIdList(parentId);
// 如果没有子节点,返回空列表
if (childIds == null || childIds.isEmpty()) {
return Collections.emptyList();
}
// 构建子节点列表
List<DingDeptVO> children = new ArrayList<>();
for (Long childId : childIds) {
OapiV2DepartmentGetResponse.DeptGetResponse deptDetail = getDeptDetail(childId);
DingDeptVO child = DingDeptVO.builder()
.id(childId)
.name(deptDetail.getName())
.build();
// 递归构建子节点的子树
List<DingDeptVO> grandchildren = buildChildTree(childId);
if (!grandchildren.isEmpty()) {
child.setChild(grandchildren);
}
children.add(child);
}
return children;
}
@ApiOperation(value = "查询部门详情")
@GetMapping("/detail/{deptId}")
public OapiV2DepartmentGetResponse.DeptGetResponse getDeptDetail(@PathVariable Long deptId) {
String accessToken = permissionConfig.getAccessToken();
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
req.setDeptId(deptId);
req.setLanguage("zh_CN");
req.setHttpMethod("POST");
OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
return rsp.getResult();
} catch (ApiException e) {
throw new RuntimeException(e);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment