|
@@ -0,0 +1,263 @@
|
|
|
+package com.jeeplus.modules.workstaff.utils;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
+import com.dingtalk.api.DingTalkClient;
|
|
|
+import com.dingtalk.api.request.*;
|
|
|
+import com.dingtalk.api.response.*;
|
|
|
+import com.jeeplus.common.config.Global;
|
|
|
+import com.jeeplus.common.utils.StringUtils;
|
|
|
+import com.jeeplus.modules.workcalendar.service.WorkCalendarTaskService;
|
|
|
+import com.jeeplus.modules.workstaff.dao.WorkStaffBasicInfoDao;
|
|
|
+import com.jeeplus.modules.workstaff.entity.DingTalkUser;
|
|
|
+import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
|
|
|
+import com.taobao.api.ApiException;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class DingTalkUtils {
|
|
|
+
|
|
|
+ //会计钉钉配置
|
|
|
+ private String appKey = Global.getConfig("appKey");
|
|
|
+
|
|
|
+ private String appSecret = Global.getConfig("appSecret");
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WorkStaffBasicInfoDao workStaffBasicInfoDao;
|
|
|
+ private Logger logger = LoggerFactory.getLogger(DingTalkUtils.class);
|
|
|
+ private final static String dingFlag = Global.getConfig("dingFlag");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取钉钉token
|
|
|
+ */
|
|
|
+ public String getToken() throws ApiException {
|
|
|
+ // 创建钉钉客户端
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
+ // 配置请求参数
|
|
|
+ OapiGettokenRequest req = new OapiGettokenRequest();
|
|
|
+ req.setAppkey( appKey); // 确保 appKey 已正确初始化
|
|
|
+ req.setAppsecret(appSecret); // 确保 appSecret 已正确初始化
|
|
|
+ req.setHttpMethod("GET");
|
|
|
+ // 执行请求,获取响应
|
|
|
+ OapiGettokenResponse rsp = client.execute(req);
|
|
|
+ String accessToken = rsp.getAccessToken();
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取部门列表
|
|
|
+ */
|
|
|
+ public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> deptList() throws ApiException {
|
|
|
+ List<OapiV2DepartmentListsubResponse.DeptBaseResponse> allDepartments = new ArrayList<>();
|
|
|
+ Long rootDeptId = 1L;
|
|
|
+ String token = getToken();
|
|
|
+ fetchAllDepartments(rootDeptId, allDepartments,token);
|
|
|
+ return allDepartments;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 假设有一个方法可以调用 API 获取子部门列表
|
|
|
+ public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getSubDepartments(Long parentDeptId,String token) {
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
+ OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
+ // 检查 deptId 是否为空
|
|
|
+ if (parentDeptId != null) {
|
|
|
+ req.setDeptId(parentDeptId);
|
|
|
+ }
|
|
|
+ OapiV2DepartmentListsubResponse rsp = client.execute(req, token);
|
|
|
+ List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rsp.getResult();
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 打印错误日志(开发调试用)
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 递归获取所有子部门
|
|
|
+ public void fetchAllDepartments(Long parentDeptId, List<OapiV2DepartmentListsubResponse.DeptBaseResponse> allDepts,String token) throws ApiException {
|
|
|
+ List<OapiV2DepartmentListsubResponse.DeptBaseResponse> subDepts = getSubDepartments(parentDeptId,token);
|
|
|
+ for (OapiV2DepartmentListsubResponse.DeptBaseResponse dept : subDepts) {
|
|
|
+ allDepts.add(dept); // 先添加当前部门
|
|
|
+ fetchAllDepartments(dept.getDeptId(), allDepts,token); // 递归获取子部门
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建用户
|
|
|
+ */
|
|
|
+ public OapiV2UserCreateResponse addUser(DingTalkUser addUserDTO) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
|
|
|
+ OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
|
|
|
+ String name = addUserDTO.getName();
|
|
|
+ String mobile = addUserDTO.getMobile();
|
|
|
+ String deptIdList = addUserDTO.getDeptIdList();
|
|
|
+ String userid = addUserDTO.getUserid();
|
|
|
+ req.setName(name);
|
|
|
+ req.setDeptIdList(deptIdList);
|
|
|
+ req.setHideMobile(false);
|
|
|
+ req.setMobile(mobile);
|
|
|
+ req.setUserid(userid);
|
|
|
+ OapiV2UserCreateResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return rsp;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public OapiV2UserUpdateResponse updateUser(DingTalkUser addUserDTO) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update");
|
|
|
+ OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
|
|
|
+ String name = addUserDTO.getName();
|
|
|
+ String mobile = addUserDTO.getMobile();
|
|
|
+ String deptIdList = addUserDTO.getDeptIdList();
|
|
|
+ String userId = addUserDTO.getUserid();
|
|
|
+ req.setUserid(userId);
|
|
|
+ req.setName(name);
|
|
|
+ req.setDeptIdList(deptIdList);
|
|
|
+ req.setHideMobile(false);
|
|
|
+ req.setMobile(mobile);
|
|
|
+ OapiV2UserUpdateResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return rsp;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public OapiV2UserGetResponse.UserGetResponse selectUserById(String id) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
+ OapiV2UserGetRequest req = new OapiV2UserGetRequest();
|
|
|
+ req.setUserid(id);
|
|
|
+ req.setLanguage("zh_CN");
|
|
|
+ OapiV2UserGetResponse rsp = client.execute(req, token);
|
|
|
+ OapiV2UserGetResponse.UserGetResponse result = rsp.getResult();
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public OapiV2UserDeleteResponse deleteUser(DingTalkUser dingTalkUser) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
|
|
|
+ OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
|
|
|
+ req.setUserid(dingTalkUser.getUserid());
|
|
|
+ OapiV2UserDeleteResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return rsp;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据手机号获取用户信息
|
|
|
+ * @param mobile
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public OapiV2UserGetbymobileResponse selectUserByMobile(String mobile) {
|
|
|
+ try {
|
|
|
+ String token = getToken();
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
|
|
|
+ OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
|
|
|
+ req.setMobile(mobile);
|
|
|
+ String body = client.execute(req, token).getBody();
|
|
|
+ OapiV2UserGetbymobileResponse rsp = client.execute(req, token);
|
|
|
+ // 返回 JSON 响应
|
|
|
+ return rsp;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步钉钉数据,定时任务
|
|
|
+ *
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ //@Scheduled(cron = "0 */1 * * * ?")//一分钟一次
|
|
|
+ //@Scheduled(cron = "0 0 3 * * ?")//每天凌晨3点执行
|
|
|
+ public void syncDingTalkData() throws ApiException {
|
|
|
+ logger.info("钉钉数据同步开始");
|
|
|
+ if ("true".equals(dingFlag)) {
|
|
|
+ List<WorkStaffBasicInfo> allList = workStaffBasicInfoDao.findAllUserList();
|
|
|
+ // 创建线程池,最多允许3个钉钉并发请求
|
|
|
+ ExecutorService executorService = Executors.newFixedThreadPool(3);
|
|
|
+ CompletionService<WorkStaffBasicInfo> completionService = new ExecutorCompletionService<>(executorService);
|
|
|
+
|
|
|
+ // 提交任务
|
|
|
+ for (WorkStaffBasicInfo staff : allList) {
|
|
|
+ if (StringUtils.isBlank(staff.getMobile()) || StringUtils.isNotBlank(staff.getDdId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ completionService.submit(() -> {
|
|
|
+ OapiV2UserGetbymobileResponse response = selectUserByMobile(staff.getMobile());
|
|
|
+ String body = response.getBody();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
+
|
|
|
+ String ddUserId = "";
|
|
|
+ WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
|
|
|
+ workStaffBasicInfo.setId(staff.getId());
|
|
|
+ if ("0".equals(jsonObject.getStr("errcode"))) {
|
|
|
+ ddUserId = jsonObject.getJSONObject("result").getStr("userid");
|
|
|
+ workStaffBasicInfo.setDdId(ddUserId);
|
|
|
+ workStaffBasicInfo.setDdType("1");
|
|
|
+ } else {
|
|
|
+ workStaffBasicInfo.setDdType("0");
|
|
|
+ }
|
|
|
+
|
|
|
+ return workStaffBasicInfo;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理任务结果
|
|
|
+ int taskCount = allList.size();
|
|
|
+ for (int i = 0; i < taskCount; i++) {
|
|
|
+ try {
|
|
|
+ Future<WorkStaffBasicInfo> future = completionService.take();
|
|
|
+ WorkStaffBasicInfo result = future.get();
|
|
|
+ if (result != null) {
|
|
|
+ workStaffBasicInfoDao.updateDdInfo(result.getId(),result.getDdId(),result.getDdType());
|
|
|
+ }
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("-------------钉钉同步---解析信息发生异常-------------");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭线程池
|
|
|
+ executorService.shutdown();
|
|
|
+ logger.info("钉钉数据同步结束");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|