钉钉同步系统
This commit is contained in:
parent
65bc8105e6
commit
7a910afc07
|
@ -2,16 +2,22 @@ package com.snow.web.controller.dingtalk;
|
|||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dingtalk.api.response.OapiDepartmentListResponse;
|
||||
import com.dingtalk.oapi.lib.aes.DingTalkEncryptor;
|
||||
import com.snow.common.enums.DingTalkListenerType;
|
||||
import com.snow.dingtalk.common.EventNameEnum;
|
||||
import com.snow.dingtalk.service.impl.DepartmentServiceImpl;
|
||||
import com.snow.dingtalk.sync.ISyncSysInfo;
|
||||
import com.snow.dingtalk.sync.SyncSysInfoFactory;
|
||||
import com.snow.system.domain.DingtalkCallBack;
|
||||
import com.snow.system.domain.SysDept;
|
||||
import com.snow.system.service.impl.DingtalkCallBackServiceImpl;
|
||||
import com.snow.system.service.impl.SysDeptServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -27,6 +33,11 @@ public class DingTalkCallBackController {
|
|||
|
||||
@Autowired
|
||||
private DingtalkCallBackServiceImpl dingtalkCallBackService;
|
||||
@Autowired
|
||||
private DepartmentServiceImpl departmentService;
|
||||
@Autowired
|
||||
private SysDeptServiceImpl sysDeptService;
|
||||
|
||||
|
||||
/**
|
||||
* 钉钉回调
|
||||
|
@ -63,8 +74,12 @@ public class DingTalkCallBackController {
|
|||
|
||||
// 根据回调事件类型做不同的业务处理
|
||||
String eventType = callBackContent.getString("EventType");
|
||||
SyncSysInfoFactory syncSysInfoFactory = new SyncSysInfoFactory();
|
||||
|
||||
if (EventNameEnum.org_dept_create.equals(eventType)) {
|
||||
|
||||
if (DingTalkListenerType.DEPARTMENT_CREATE.getInfo().equals(eventType)) {
|
||||
ISyncSysInfo iSyncSysInfo = syncSysInfoFactory.getSyncSysInfoService(DingTalkListenerType.DEPARTMENT_CREATE);
|
||||
iSyncSysInfo.SyncSysInfo(DingTalkListenerType.DEPARTMENT_CREATE, callBackContent);
|
||||
log.info("部门创建回调数据: " + callBackContent);
|
||||
} else if (EventNameEnum.org_dept_modify.equals(eventType)) {
|
||||
log.info("验证更新回调URL有效性: " + plainText);
|
||||
|
@ -85,4 +100,20 @@ public class DingTalkCallBackController {
|
|||
return "fail";
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/initDepartment")
|
||||
public void initDepartment(){
|
||||
List<OapiDepartmentListResponse.Department> dingTalkDepartmentList = departmentService.getDingTalkDepartmentList();
|
||||
dingTalkDepartmentList.stream().forEach(t->{
|
||||
SysDept sysDept=new SysDept();
|
||||
sysDept.setDeptId(t.getId());
|
||||
sysDept.setDeptName(t.getName());
|
||||
sysDept.setOrderNum(String.valueOf(t.getId()));
|
||||
sysDept.setParentId(t.getParentid());
|
||||
sysDept.setIsSyncDingTalk(false);
|
||||
sysDeptService.insertDept(sysDept);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,15 @@ package com.snow.common.enums;
|
|||
* @date 2020/9/18 10:18
|
||||
*/
|
||||
public enum DingTalkListenerType {
|
||||
DEPARTMENT_CREATE(1, 2,"部门创建"),
|
||||
DEPARTMENT_CREATE(1, 2,"org_dept_create"),
|
||||
|
||||
DEPARTMENT_UPDATE(2, 2,"部门更新"),
|
||||
DEPARTMENT_UPDATE(2, 2,"org_dept_modify"),
|
||||
|
||||
DEPARTMENT_DELETED(3,2,"部门删除"),
|
||||
DEPARTMENT_DELETED(3,2,"org_dept_remove"),
|
||||
|
||||
USER_CREATE(1,1,"用户创建"),
|
||||
USER_CREATE(1,1,"user_add_org"),
|
||||
|
||||
USER_DELETE(3,1,"用户删除"),
|
||||
USER_DELETE(3,1,"user_leave_org"),
|
||||
|
||||
CALL_BACK_REGISTER(1,10, "回调注册"),
|
||||
|
||||
|
|
|
@ -25,6 +25,11 @@ public class BaseConstantUrl {
|
|||
*/
|
||||
public static final String DEPARTMENT_CREATE="https://oapi.dingtalk.com/department/create";
|
||||
|
||||
/**
|
||||
* 根据ID获取部门信息
|
||||
*/
|
||||
public static final String GET_DEPARTMENT_BY_ID="https://oapi.dingtalk.com/topapi/v2/department/get";
|
||||
|
||||
/**
|
||||
* 获取钉钉部门信息
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.snow.dingtalk.service;
|
||||
|
||||
import com.dingtalk.api.response.OapiDepartmentListResponse;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||
import com.snow.dingtalk.model.DepartmentCreateRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -21,6 +22,12 @@ public interface DepartmentService {
|
|||
*/
|
||||
Long createDepartment(DepartmentCreateRequest departmentDTO);
|
||||
|
||||
/**
|
||||
* 根据ID获取钉钉部门详情
|
||||
* @param id
|
||||
*/
|
||||
OapiV2DepartmentGetResponse.DeptGetResponse getDepartmentDetail(long id);
|
||||
|
||||
/**
|
||||
* 获取部门详情
|
||||
* @return
|
||||
|
|
|
@ -5,8 +5,10 @@ import com.dingtalk.api.DefaultDingTalkClient;
|
|||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiDepartmentCreateRequest;
|
||||
import com.dingtalk.api.request.OapiDepartmentListRequest;
|
||||
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
|
||||
import com.dingtalk.api.response.OapiDepartmentCreateResponse;
|
||||
import com.dingtalk.api.response.OapiDepartmentListResponse;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||
import com.snow.common.annotation.DingTalkSyncLog;
|
||||
import com.snow.common.enums.DingTalkListenerType;
|
||||
import com.snow.common.enums.DingTalkSyncType;
|
||||
|
@ -61,6 +63,26 @@ public class DepartmentServiceImpl extends BaseService implements DepartmentServ
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OapiV2DepartmentGetResponse.DeptGetResponse getDepartmentDetail(long id) {
|
||||
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.GET_DEPARTMENT_BY_ID);
|
||||
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
|
||||
req.setDeptId(id);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2DepartmentGetResponse rsp = null;
|
||||
try {
|
||||
rsp = client.execute(req, getDingTalkToken());
|
||||
if(rsp.getErrcode()==0){
|
||||
return rsp.getResult();
|
||||
|
||||
}
|
||||
} catch (ApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(rsp.getBody());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<OapiDepartmentListResponse.Department> getDingTalkDepartmentList(){
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.snow.dingtalk.sync;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.snow.common.enums.DingTalkListenerType;
|
||||
|
||||
public interface ISyncSysInfo {
|
||||
/**
|
||||
* 同步系统数据
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
JSONObject SyncSysInfo(DingTalkListenerType dingTalkListenerType,JSONObject jsonObject);
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.snow.dingtalk.sync;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||
import com.snow.common.enums.DingTalkListenerType;
|
||||
import com.snow.common.utils.spring.SpringUtils;
|
||||
import com.snow.dingtalk.service.impl.DepartmentServiceImpl;
|
||||
import com.snow.system.domain.SysDept;
|
||||
import com.snow.system.service.impl.SysDeptServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @program: snow
|
||||
* @description
|
||||
* @author: 没用的阿吉
|
||||
* @create: 2020-11-15 11:25
|
||||
**/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SyncSysDepartmentService implements ISyncSysInfo {
|
||||
@Autowired
|
||||
private SysDeptServiceImpl sysDeptService=SpringUtils.getBean(SysDeptServiceImpl.class);
|
||||
@Autowired
|
||||
private DepartmentServiceImpl departmentService=SpringUtils.getBean(DepartmentServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public JSONObject SyncSysInfo(DingTalkListenerType dingTalkListenerType,JSONObject jsonObject) {
|
||||
log.info("返回数据:{}",jsonObject);
|
||||
Integer code = dingTalkListenerType.getCode();
|
||||
if(code==DingTalkListenerType.DEPARTMENT_CREATE.getCode()){
|
||||
List<Long> deptId = jsonObject.getJSONArray("DeptId").toJavaList(Long.class);
|
||||
//todo 查询所有上级部门ID
|
||||
for (int i=0;i<deptId.size();i++) {
|
||||
OapiV2DepartmentGetResponse.DeptGetResponse departmentDetail = departmentService.getDepartmentDetail(deptId.get(i));
|
||||
log.info("调用钉钉返回信息:{}",JSON.toJSONString(departmentDetail));
|
||||
SysDept sysDept=new SysDept();
|
||||
sysDept.setDeptId(departmentDetail.getDeptId());
|
||||
sysDept.setParentId(departmentDetail.getParentId());
|
||||
sysDept.setDeptName(departmentDetail.getName());
|
||||
sysDept.setIsSyncDingTalk(false);
|
||||
sysDept.setOrderNum(String.valueOf(departmentDetail.getOrder()));
|
||||
sysDeptService.insertDept(sysDept);
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.snow.dingtalk.sync;
|
||||
|
||||
import com.snow.common.enums.DingTalkListenerType;
|
||||
|
||||
/**
|
||||
* @program: snow
|
||||
* @description
|
||||
* @author: 没用的阿吉
|
||||
* @create: 2020-11-15 11:18
|
||||
**/
|
||||
public class SyncSysInfoFactory {
|
||||
|
||||
|
||||
public ISyncSysInfo getSyncSysInfoService(DingTalkListenerType dingTalkListenerType){
|
||||
Integer type = dingTalkListenerType.getType();
|
||||
if(type==DingTalkListenerType.DEPARTMENT_CREATE.getType()){
|
||||
return new SyncSysDepartmentService();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package com.snow.system.domain;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.snow.common.core.domain.BaseEntity;
|
||||
|
@ -10,6 +12,7 @@ import com.snow.common.core.domain.BaseEntity;
|
|||
*
|
||||
* @author snow
|
||||
*/
|
||||
@Data
|
||||
public class SysDept extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -47,6 +50,7 @@ public class SysDept extends BaseEntity
|
|||
/** 父部门名称 */
|
||||
private String parentName;
|
||||
|
||||
private Boolean isSyncDingTalk=true;
|
||||
public Long getDeptId()
|
||||
{
|
||||
return deptId;
|
||||
|
|
|
@ -207,14 +207,16 @@ public class SysDeptServiceImpl implements ISysDeptService
|
|||
{
|
||||
SysDept info = deptMapper.selectDeptById(dept.getParentId());
|
||||
// 如果父节点不为"正常"状态,则不允许新增子节点
|
||||
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
|
||||
{
|
||||
// if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
|
||||
/* {
|
||||
throw new BusinessException("部门停用,不允许新增");
|
||||
}
|
||||
}*/
|
||||
dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
|
||||
//同步钉钉数据
|
||||
SyncEvent syncEvent = new SyncEvent(dept, DingTalkListenerType.DEPARTMENT_CREATE);
|
||||
applicationContext.publishEvent(syncEvent);
|
||||
if(dept.getIsSyncDingTalk()){
|
||||
//同步钉钉数据
|
||||
SyncEvent syncEvent = new SyncEvent(dept, DingTalkListenerType.DEPARTMENT_CREATE);
|
||||
applicationContext.publishEvent(syncEvent);
|
||||
}
|
||||
return deptMapper.insertDept(dept);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue