|
@@ -1,16 +1,39 @@
|
|
|
package com.ruoyi.quartz.task;
|
|
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
+import com.dingtalk.api.DingTalkClient;
|
|
|
+import com.dingtalk.api.request.*;
|
|
|
+import com.dingtalk.api.response.*;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysDept;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.system.service.ISysDeptService;
|
|
|
+import com.ruoyi.system.service.ISysUserService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 定时任务调度测试
|
|
|
*
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
+
|
|
|
+//@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
+//@SpringBootTest(classes = RyTask.class)
|
|
|
@Component("ryTask")
|
|
|
public class RyTask
|
|
|
{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysDeptService iSysDeptService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService iSysUserService;
|
|
|
+
|
|
|
+
|
|
|
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
|
|
|
{
|
|
|
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
|
|
@@ -25,4 +48,281 @@ public class RyTask
|
|
|
{
|
|
|
System.out.println("执行无参方法");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private String appkey = "dingmidxbep08wynpmtx";
|
|
|
+ private String appSecret = "9_NL7MkSkxQJwlFp9wo2VX8oSxU55hxSOG2Xv8Vk6r_OcLATjMewPNRfPCIuXWNI";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String token="";
|
|
|
+ private Long tokentime=0L;
|
|
|
+ private Long exp = 0L;
|
|
|
+
|
|
|
+ private void getToken(){
|
|
|
+ try{
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
+ OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
+ request.setAppkey(appkey);
|
|
|
+ request.setAppsecret(appSecret);
|
|
|
+ request.setHttpMethod("GET");
|
|
|
+ OapiGettokenResponse response = client.execute(request);
|
|
|
+ token = response.getAccessToken();
|
|
|
+ tokentime = new Date().getTime()/1000;
|
|
|
+ exp = response.getExpiresIn();
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> getDept(Long deptid){
|
|
|
+ List<OapiV2DepartmentListsubResponse.DeptBaseResponse> departments =new ArrayList<>();
|
|
|
+ try{
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
+ OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
+ req.setDeptId(deptid);
|
|
|
+ req.setLanguage("zh_CN");
|
|
|
+ OapiV2DepartmentListsubResponse rsp = client.execute(req, token);
|
|
|
+ departments.addAll(rsp.getResult());
|
|
|
+ if(departments.size()>0){
|
|
|
+ for (OapiV2DepartmentListsubResponse.DeptBaseResponse d:rsp.getResult()
|
|
|
+ ) {
|
|
|
+ departments.addAll(getDept(d.getDeptId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return departments;
|
|
|
+ }catch (Exception e){}
|
|
|
+ return departments;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OapiV2UserListResponse.ListUserResponse> getUser(Long deptid,Long next){
|
|
|
+ List<OapiV2UserListResponse.ListUserResponse> users = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
|
|
|
+ OapiV2UserListRequest req = new OapiV2UserListRequest();
|
|
|
+ req.setDeptId(deptid);
|
|
|
+ req.setCursor(next);
|
|
|
+ req.setSize(100L);
|
|
|
+ OapiV2UserListResponse rsp = client.execute(req, token);
|
|
|
+ users.addAll(rsp.getResult().getList());
|
|
|
+ if(rsp.getResult().getList().size()==100){
|
|
|
+ users.addAll(getUser(deptid,next+1));
|
|
|
+ }
|
|
|
+ }catch (Exception e){}
|
|
|
+ return users;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sysncDept() {
|
|
|
+ if(token.equals("")||(new Date().getTime()/1000 - tokentime)>exp){
|
|
|
+ getToken();
|
|
|
+ }
|
|
|
+ List<OapiV2DepartmentListsubResponse.DeptBaseResponse> departments = getDept(1L);
|
|
|
+ List<SysDept> depts = iSysDeptService.selectDeptList(new SysDept());
|
|
|
+ Map<String,SysDept> deptMap = new HashMap<>();
|
|
|
+ Map<String,OapiV2DepartmentListsubResponse.DeptBaseResponse> departmentMap = new HashMap<>();
|
|
|
+ for (OapiV2DepartmentListsubResponse.DeptBaseResponse d:departments
|
|
|
+ ) {
|
|
|
+ departmentMap.put(Long.toString(d.getDeptId()),d);
|
|
|
+ }
|
|
|
+ //需要删除的
|
|
|
+ List<SysDept> delList = new ArrayList<>();
|
|
|
+ //需要修改的
|
|
|
+ List<SysDept> updateList = new ArrayList<>();
|
|
|
+ //需要新增的
|
|
|
+ List<SysDept> addList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (SysDept d:depts
|
|
|
+ ) {
|
|
|
+ //根部门
|
|
|
+ if(d.getDeptId().equals(100L)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //用户系统新增
|
|
|
+ if(d.getOtherId().equals("")){
|
|
|
+ delList.add(d);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //钉钉已删除
|
|
|
+ if(departmentMap.get(d.getOtherId())==null){
|
|
|
+ delList.add(d);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ deptMap.put(d.getOtherId(),d);
|
|
|
+ }
|
|
|
+ //新建部门
|
|
|
+ for (OapiV2DepartmentListsubResponse.DeptBaseResponse p:departments
|
|
|
+ ) {
|
|
|
+ //跟新
|
|
|
+ if(deptMap.get(Long.toString(p.getDeptId())) !=null){
|
|
|
+ SysDept dd = deptMap.get( Long.toString(p.getDeptId()));
|
|
|
+ dd.setDeptName(p.getName());
|
|
|
+ dd.setUpdateBy("同步");
|
|
|
+ updateList.add(dd);
|
|
|
+ }else{
|
|
|
+ //新增
|
|
|
+ SysDept dd = new SysDept();
|
|
|
+ dd.setParentId(100L);
|
|
|
+ dd.setDeptName(p.getName());
|
|
|
+ dd.setUpdateBy("同步");
|
|
|
+ dd.setOtherId(Long.toString(p.getDeptId()));
|
|
|
+ addList.add(dd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (SysDept d:delList
|
|
|
+ ) {
|
|
|
+ iSysDeptService.deleteDeptById(d.getDeptId());
|
|
|
+ }
|
|
|
+ for (SysDept d:addList
|
|
|
+ ) {
|
|
|
+ iSysDeptService.insertDept(d);
|
|
|
+ }
|
|
|
+ for (SysDept d:updateList
|
|
|
+ ) {
|
|
|
+ iSysDeptService.updateDept(d);
|
|
|
+ }
|
|
|
+
|
|
|
+ //跟新部门关系
|
|
|
+ depts = iSysDeptService.selectDeptList(new SysDept());
|
|
|
+ deptMap = new HashMap<>();
|
|
|
+ for (SysDept d:depts
|
|
|
+ ) {
|
|
|
+ deptMap.put(d.getOtherId(),d);
|
|
|
+ }
|
|
|
+ for (OapiV2DepartmentListsubResponse.DeptBaseResponse p:departments
|
|
|
+ ){
|
|
|
+ if(deptMap.get(Long.toString(p.getDeptId())) !=null){
|
|
|
+ SysDept dd = deptMap.get( Long.toString(p.getDeptId()));
|
|
|
+ dd.setDeptName(p.getName());
|
|
|
+ SysDept pd = deptMap.get(Long.toString(p.getParentId()));
|
|
|
+ dd.setParentId(pd == null?100L:pd.getDeptId());
|
|
|
+ iSysDeptService.updateDept(dd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sysncUser(){
|
|
|
+ if(token.equals("")||(new Date().getTime()/1000 - tokentime)>exp){
|
|
|
+ getToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<OapiV2UserListResponse.ListUserResponse> users = new ArrayList<>();
|
|
|
+ Map<String,SysUser> userMap = new HashMap<>();
|
|
|
+ Map<String,OapiV2UserListResponse.ListUserResponse> userResponseMap = new HashMap<>();
|
|
|
+ List<SysDept> depts = iSysDeptService.selectDeptList(new SysDept());
|
|
|
+ Map<String,SysDept> deptMap = new HashMap<>();
|
|
|
+ for (SysDept d:depts){
|
|
|
+ deptMap.put(d.getOtherId(),d);
|
|
|
+ }
|
|
|
+ for (SysDept d:depts
|
|
|
+ ) {
|
|
|
+ if(!d.getOtherId().equals("")){
|
|
|
+ users.addAll(getUser(Long.parseLong(d.getOtherId()),0L));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (OapiV2UserListResponse.ListUserResponse u:users
|
|
|
+ ) {
|
|
|
+ userResponseMap.put(u.getUnionid(),u);
|
|
|
+ }
|
|
|
+ List<SysUser> userList = iSysUserService.selectUserList(new SysUser());
|
|
|
+ //需要删除的
|
|
|
+ List<SysUser> delList = new ArrayList<>();
|
|
|
+ //需要修改的
|
|
|
+ List<SysUser> updateList = new ArrayList<>();
|
|
|
+ //需要新增的
|
|
|
+ List<SysUser> addList = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for (SysUser u:userList
|
|
|
+ ) {
|
|
|
+ // 管理员
|
|
|
+ if(u.getUserId().equals(1L)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //用户系统新增
|
|
|
+ if(u.getRemark().equals("")){
|
|
|
+ delList.add(u);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //钉钉已删除
|
|
|
+ if(userResponseMap.get(u.getRemark())==null){
|
|
|
+ delList.add(u);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ userMap.put(u.getRemark(),u);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (OapiV2UserListResponse.ListUserResponse u:userResponseMap.values()
|
|
|
+ ) {
|
|
|
+ if(userMap.get(u.getUnionid())==null){
|
|
|
+ //新增
|
|
|
+ SysUser user = new SysUser();
|
|
|
+ user.setCreateBy("同步");
|
|
|
+ user.setUserName(u.getUserid());
|
|
|
+ user.setNickName(u.getName());
|
|
|
+ user.setRemark(u.getUnionid());
|
|
|
+ user.setAvatar(u.getAvatar());
|
|
|
+ user.setEmail(u.getEmail());
|
|
|
+ user.setPhonenumber(u.getMobile());
|
|
|
+
|
|
|
+ SysDept dept = deptMap.get("-1");
|
|
|
+ if(u.getDeptIdList()!=null && u.getDeptIdList().size()>0){
|
|
|
+ dept = deptMap.get(Long.toString(u.getDeptIdList().get(0)));
|
|
|
+ }
|
|
|
+ if(dept==null){
|
|
|
+ dept = deptMap.get("-1");
|
|
|
+ }
|
|
|
+ user.setDeptId(dept.getDeptId());
|
|
|
+ user.setDept(dept);
|
|
|
+ addList.add(user);
|
|
|
+ }else{
|
|
|
+ SysUser user = userMap.get(u.getUnionid());
|
|
|
+ user.setUpdateBy("同步");
|
|
|
+ user.setUserName(u.getUserid());
|
|
|
+ user.setNickName(u.getName());
|
|
|
+ user.setRemark(u.getUnionid());
|
|
|
+ user.setAvatar(u.getAvatar());
|
|
|
+ user.setEmail(u.getEmail());
|
|
|
+ user.setPhonenumber(u.getMobile());
|
|
|
+ SysDept dept = deptMap.get("-1");
|
|
|
+ if(u.getDeptIdList()!=null && u.getDeptIdList().size()>0){
|
|
|
+ dept = deptMap.get(Long.toString(u.getDeptIdList().get(0)));
|
|
|
+ }
|
|
|
+ if(dept==null){
|
|
|
+ dept = deptMap.get("-1");
|
|
|
+ }
|
|
|
+ user.setDeptId(dept.getDeptId());
|
|
|
+ user.setDept(dept);
|
|
|
+ updateList.add(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (SysUser d:delList
|
|
|
+ ) {
|
|
|
+ iSysUserService.deleteUserById(d.getUserId());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (SysUser d:addList
|
|
|
+ ) {
|
|
|
+ iSysUserService.insertUser(d);
|
|
|
+ }
|
|
|
+ for (SysUser d:updateList
|
|
|
+ ) {
|
|
|
+ iSysUserService.updateUser(d);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ RyTask ryTask =new RyTask();
|
|
|
+ ryTask.sysncDept();
|
|
|
+ }
|
|
|
}
|