Search in sources :

Example 1 with Department

use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.

the class ThirdAppDingtalkServiceImpl method syncDepartmentToLocalRecursion.

public void syncDepartmentToLocalRecursion(List<JdtDepartmentTreeVo> departmentTreeList, String sysParentId, String username, SyncInfoVo syncInfo, String accessToken) {
    if (departmentTreeList != null && departmentTreeList.size() != 0) {
        for (JdtDepartmentTreeVo departmentTree : departmentTreeList) {
            LambdaQueryWrapper<SysDepart> queryWrapper = new LambdaQueryWrapper<>();
            // 根据 source_identifier 字段查询
            queryWrapper.eq(SysDepart::getId, departmentTree.getSource_identifier());
            SysDepart sysDepart = sysDepartService.getOne(queryWrapper);
            if (sysDepart != null) {
                // 执行更新操作
                SysDepart updateSysDepart = this.dtDepartmentToSysDepart(departmentTree, sysDepart);
                if (sysParentId != null) {
                    updateSysDepart.setParentId(sysParentId);
                }
                try {
                    sysDepartService.updateDepartDataById(updateSysDepart, username);
                    String str = String.format("部门 %s 更新成功!", updateSysDepart.getDepartName());
                    syncInfo.addSuccessInfo(str);
                } catch (Exception e) {
                    this.syncDepartCollectErrInfo(e, departmentTree, syncInfo);
                }
                if (departmentTree.hasChildren()) {
                    // 紧接着同步子级
                    this.syncDepartmentToLocalRecursion(departmentTree.getChildren(), updateSysDepart.getId(), username, syncInfo, accessToken);
                }
            } else {
                // 执行新增操作
                SysDepart newSysDepart = this.dtDepartmentToSysDepart(departmentTree, null);
                if (sysParentId != null) {
                    newSysDepart.setParentId(sysParentId);
                    // 2 = 组织机构
                    newSysDepart.setOrgCategory("2");
                } else {
                    // 1 = 公司
                    newSysDepart.setOrgCategory("1");
                }
                try {
                    sysDepartService.saveDepartData(newSysDepart, username);
                    // 更新钉钉 source_identifier
                    Department updateDtDepart = new Department();
                    updateDtDepart.setDept_id(departmentTree.getDept_id());
                    updateDtDepart.setSource_identifier(newSysDepart.getId());
                    Response response = JdtDepartmentAPI.update(updateDtDepart, accessToken);
                    if (!response.isSuccess()) {
                        throw new RuntimeException(response.getErrmsg());
                    }
                    String str = String.format("部门 %s 创建成功!", newSysDepart.getDepartName());
                    syncInfo.addSuccessInfo(str);
                } catch (Exception e) {
                    this.syncDepartCollectErrInfo(e, departmentTree, syncInfo);
                }
                // 紧接着同步子级
                if (departmentTree.hasChildren()) {
                    this.syncDepartmentToLocalRecursion(departmentTree.getChildren(), newSysDepart.getId(), username, syncInfo, accessToken);
                }
            }
        }
    }
}
Also used : Response(com.jeecg.dingtalk.api.core.response.Response) Department(com.jeecg.dingtalk.api.department.vo.Department) JdtDepartmentTreeVo(org.jeecg.modules.system.vo.thirdapp.JdtDepartmentTreeVo) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Example 2 with Department

use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.

the class ThirdAppDingtalkServiceImpl method syncThirdAppUserToLocal.

@Override
public SyncInfoVo syncThirdAppUserToLocal() {
    SyncInfoVo syncInfo = new SyncInfoVo();
    String accessToken = this.getAccessToken();
    if (accessToken == null) {
        syncInfo.addFailInfo("accessToken获取失败!");
        return syncInfo;
    }
    // 获取本地用户
    List<SysUser> sysUsersList = sysUserService.list();
    // 查询钉钉所有的部门,用于同步用户和部门的关系
    List<Department> allDepartment = JdtDepartmentAPI.listAll(accessToken);
    // 根据钉钉部门查询所有钉钉用户,用于反向同步到本地
    List<User> ddUserList = this.getDtAllUserByDepartment(allDepartment, accessToken);
    for (User dtUserInfo : ddUserList) {
        SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneByThirdUserId(dtUserInfo.getUserid(), THIRD_TYPE);
        List<SysUser> collect = sysUsersList.stream().filter(user -> (dtUserInfo.getMobile().equals(user.getPhone()) || dtUserInfo.getUserid().equals(user.getUsername()))).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            SysUser sysUserTemp = collect.get(0);
            // 循环到此说明用户匹配成功,进行更新操作
            SysUser updateSysUser = this.dtUserToSysUser(dtUserInfo, sysUserTemp);
            try {
                sysUserService.updateById(updateSysUser);
                String str = String.format("用户 %s(%s) 更新成功!", updateSysUser.getRealname(), updateSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, dtUserInfo, syncInfo);
            }
            // 第三方账号关系表
            this.thirdAccountSaveOrUpdate(sysThirdAccount, updateSysUser.getId(), dtUserInfo.getUserid());
        } else {
            // 如果没有匹配到用户,则走创建逻辑
            SysUser newSysUser = this.dtUserToSysUser(dtUserInfo);
            try {
                sysUserService.save(newSysUser);
                String str = String.format("用户 %s(%s) 创建成功!", newSysUser.getRealname(), newSysUser.getUsername());
                syncInfo.addSuccessInfo(str);
            } catch (Exception e) {
                this.syncUserCollectErrInfo(e, dtUserInfo, syncInfo);
            }
            // 第三方账号关系表
            this.thirdAccountSaveOrUpdate(null, newSysUser.getId(), dtUserInfo.getUserid());
        }
    }
    return syncInfo;
}
Also used : SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) ThirdLoginModel(org.jeecg.modules.system.model.ThirdLoginModel) ActionCardMessage(com.jeecg.dingtalk.api.message.vo.ActionCardMessage) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) Department(com.jeecg.dingtalk.api.department.vo.Department) JdtDepartmentTreeVo(org.jeecg.modules.system.vo.thirdapp.JdtDepartmentTreeVo) org.jeecg.modules.system.entity(org.jeecg.modules.system.entity) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ContactUser(com.jeecg.dingtalk.api.oauth2.vo.ContactUser) ArrayList(java.util.ArrayList) ThirdAppConfig(org.jeecg.config.thirdapp.ThirdAppConfig) AccessToken(com.jeecg.dingtalk.api.core.vo.AccessToken) JdtMessageAPI(com.jeecg.dingtalk.api.message.JdtMessageAPI) Message(com.jeecg.dingtalk.api.message.vo.Message) Service(org.springframework.stereotype.Service) User(com.jeecg.dingtalk.api.user.vo.User) org.jeecg.common.util.oConvertUtils(org.jeecg.common.util.oConvertUtils) GetUserListBody(com.jeecg.dingtalk.api.user.body.GetUserListBody) JdtBaseAPI(com.jeecg.dingtalk.api.base.JdtBaseAPI) org.jeecg.modules.system.service(org.jeecg.modules.system.service) JdtDepartmentAPI(com.jeecg.dingtalk.api.department.JdtDepartmentAPI) ThirdAppTypeItemVo(org.jeecg.config.thirdapp.ThirdAppTypeItemVo) JdtUserAPI(com.jeecg.dingtalk.api.user.JdtUserAPI) JwtUtil(org.jeecg.common.system.util.JwtUtil) PasswordUtil(org.jeecg.common.util.PasswordUtil) SysAnnouncementSendMapper(org.jeecg.modules.system.mapper.SysAnnouncementSendMapper) Collectors(java.util.stream.Collectors) RestUtil(org.jeecg.common.util.RestUtil) JdtOauth2API(com.jeecg.dingtalk.api.oauth2.JdtOauth2API) Slf4j(lombok.extern.slf4j.Slf4j) SpringContextUtils(org.jeecg.common.util.SpringContextUtils) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Response(com.jeecg.dingtalk.api.core.response.Response) JSONObject(com.alibaba.fastjson.JSONObject) SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) TextMessage(com.jeecg.dingtalk.api.message.vo.TextMessage) CommonConstant(org.jeecg.common.constant.CommonConstant) MessageDTO(org.jeecg.common.api.dto.message.MessageDTO) PageResult(com.jeecg.dingtalk.api.core.vo.PageResult) SysDepartTreeModel(org.jeecg.modules.system.model.SysDepartTreeModel) BeanUtils(org.springframework.beans.BeanUtils) Department(com.jeecg.dingtalk.api.department.vo.Department) ContactUser(com.jeecg.dingtalk.api.oauth2.vo.ContactUser) User(com.jeecg.dingtalk.api.user.vo.User) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 3 with Department

use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.

the class ThirdAppDingtalkServiceImpl method sysDepartToDtDepartment.

/**
 * 【同步部门】将SysDepartTreeModel转为【钉钉】的Department对象(创建新部门)
 */
private Department sysDepartToDtDepartment(SysDepartTreeModel departTree, Integer parentId) {
    Department department = new Department();
    department.setSource_identifier(departTree.getId());
    return this.sysDepartToDtDepartment(departTree, department, parentId);
}
Also used : Department(com.jeecg.dingtalk.api.department.vo.Department)

Example 4 with Department

use of com.jeecg.dingtalk.api.department.vo.Department in project kms by mahonelau.

the class ThirdAppDingtalkServiceImpl method sysUserToDtUser.

/**
 * 【同步用户】将SysUser转为【钉钉】的User对象(更新旧用户)
 */
private User sysUserToDtUser(SysUser sysUser, User user, List<Department> allDepartment) {
    user.setName(sysUser.getRealname());
    user.setMobile(sysUser.getPhone());
    user.setTelephone(sysUser.getTelephone());
    user.setJob_number(sysUser.getWorkNo());
    // 职务翻译
    if (oConvertUtils.isNotEmpty(sysUser.getPost())) {
        SysPosition position = sysPositionService.getByCode(sysUser.getPost());
        if (position != null) {
            user.setTitle(position.getName());
        }
    }
    user.setEmail(sysUser.getEmail());
    // 查询并同步用户部门关系
    List<SysDepart> departList = this.getUserDepart(sysUser);
    if (departList != null) {
        List<Integer> departmentIdList = new ArrayList<>();
        for (SysDepart sysDepart : departList) {
            // 企业微信的部门id
            Department department = this.getDepartmentByDepartId(sysDepart.getId(), allDepartment);
            if (department != null) {
                departmentIdList.add(department.getDept_id());
            }
        }
        user.setDept_id_list(departmentIdList.toArray(new Integer[] {}));
        user.setDept_order_list(null);
    }
    if (oConvertUtils.isEmpty(user.getDept_id_list())) {
        // 没有找到匹配部门,同步到根部门下
        user.setDept_id_list(1);
        user.setDept_order_list(null);
    }
    // sysUser.getStatus()
    return user;
}
Also used : Department(com.jeecg.dingtalk.api.department.vo.Department) ArrayList(java.util.ArrayList)

Example 5 with Department

use of com.jeecg.dingtalk.api.department.vo.Department in project kms by mahonelau.

the class ThirdAppDingtalkServiceImpl method syncLocalUserToThirdApp.

@Override
public SyncInfoVo syncLocalUserToThirdApp(String ids) {
    SyncInfoVo syncInfo = new SyncInfoVo();
    String accessToken = this.getAccessToken();
    if (accessToken == null) {
        syncInfo.addFailInfo("accessToken获取失败!");
        return syncInfo;
    }
    List<SysUser> sysUsers;
    if (StringUtils.isNotBlank(ids)) {
        String[] idList = ids.split(",");
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUser::getId, (Object[]) idList);
        // 获取本地指定用户
        sysUsers = sysUserService.list(queryWrapper);
    } else {
        // 获取本地所有用户
        sysUsers = sysUserService.list();
    }
    // 查询钉钉所有的部门,用于同步用户和部门的关系
    List<Department> allDepartment = JdtDepartmentAPI.listAll(accessToken);
    for (SysUser sysUser : sysUsers) {
        // 外部模拟登陆临时账号,不同步
        if ("_reserve_user_external".equals(sysUser.getUsername())) {
            continue;
        }
        // 钉钉用户信息,不为null代表已同步过
        Response<User> dtUserInfo;
        /*
             * 判断是否同步过的逻辑:
             * 1. 查询 sys_third_account(第三方账号表)是否有数据,如果有代表已同步
             * 2. 本地表里没有,就先用手机号判断,不通过再用username判断。
             */
        SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneBySysUserId(sysUser.getId(), ThirdAppConfig.DINGTALK.toLowerCase());
        if (sysThirdAccount != null && oConvertUtils.isNotEmpty(sysThirdAccount.getThirdUserId())) {
            // sys_third_account 表匹配成功,通过第三方userId查询出第三方userInfo
            dtUserInfo = JdtUserAPI.getUserById(sysThirdAccount.getThirdUserId(), accessToken);
        } else {
            // 手机号匹配
            Response<String> thirdUserId = JdtUserAPI.getUseridByMobile(sysUser.getPhone(), accessToken);
            // 手机号匹配成功
            if (thirdUserId.isSuccess() && oConvertUtils.isNotEmpty(thirdUserId.getResult())) {
                // 通过查询到的userId查询用户详情
                dtUserInfo = JdtUserAPI.getUserById(thirdUserId.getResult(), accessToken);
            } else {
                // 手机号匹配失败,尝试使用username匹配
                dtUserInfo = JdtUserAPI.getUserById(sysUser.getUsername(), accessToken);
            }
        }
        String dtUserId;
        // api 接口是否执行成功
        boolean apiSuccess;
        // 已同步就更新,否则就创建
        if (dtUserInfo != null && dtUserInfo.isSuccess() && dtUserInfo.getResult() != null) {
            User dtUser = dtUserInfo.getResult();
            dtUserId = dtUser.getUserid();
            User updateQwUser = this.sysUserToDtUser(sysUser, dtUser, allDepartment);
            Response<JSONObject> updateRes = JdtUserAPI.update(updateQwUser, accessToken);
            // 收集成功/失败信息
            apiSuccess = this.syncUserCollectErrInfo(updateRes, sysUser, syncInfo);
        } else {
            User newQwUser = this.sysUserToDtUser(sysUser, allDepartment);
            Response<String> createRes = JdtUserAPI.create(newQwUser, accessToken);
            dtUserId = createRes.getResult();
            // 收集成功/失败信息
            apiSuccess = this.syncUserCollectErrInfo(createRes, sysUser, syncInfo);
        }
        // api 接口执行成功,并且 sys_third_account 表匹配失败,就向 sys_third_account 里插入一条数据
        if (apiSuccess && (sysThirdAccount == null || oConvertUtils.isEmpty(sysThirdAccount.getThirdUserId()))) {
            if (sysThirdAccount == null) {
                sysThirdAccount = new SysThirdAccount();
                sysThirdAccount.setSysUserId(sysUser.getId());
                sysThirdAccount.setStatus(1);
                sysThirdAccount.setDelFlag(0);
                sysThirdAccount.setThirdType(ThirdAppConfig.DINGTALK.toLowerCase());
            }
            // 设置第三方app用户ID
            sysThirdAccount.setThirdUserId(dtUserId);
            sysThirdAccountService.saveOrUpdate(sysThirdAccount);
        }
    }
    return syncInfo;
}
Also used : SyncInfoVo(org.jeecg.modules.system.vo.thirdapp.SyncInfoVo) User(com.jeecg.dingtalk.api.user.vo.User) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Department(com.jeecg.dingtalk.api.department.vo.Department) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

Department (com.jeecg.dingtalk.api.department.vo.Department)21 SyncInfoVo (org.jeecg.modules.system.vo.thirdapp.SyncInfoVo)10 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)9 Response (com.jeecg.dingtalk.api.core.response.Response)9 User (com.jeecg.dingtalk.api.user.vo.User)9 JdtDepartmentTreeVo (org.jeecg.modules.system.vo.thirdapp.JdtDepartmentTreeVo)9 JSONObject (com.alibaba.fastjson.JSONObject)6 ArrayList (java.util.ArrayList)6 SysDepartTreeModel (org.jeecg.modules.system.model.SysDepartTreeModel)6 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)6 JdtBaseAPI (com.jeecg.dingtalk.api.base.JdtBaseAPI)3 AccessToken (com.jeecg.dingtalk.api.core.vo.AccessToken)3 PageResult (com.jeecg.dingtalk.api.core.vo.PageResult)3 JdtDepartmentAPI (com.jeecg.dingtalk.api.department.JdtDepartmentAPI)3 JdtMessageAPI (com.jeecg.dingtalk.api.message.JdtMessageAPI)3 ActionCardMessage (com.jeecg.dingtalk.api.message.vo.ActionCardMessage)3 Message (com.jeecg.dingtalk.api.message.vo.Message)3 TextMessage (com.jeecg.dingtalk.api.message.vo.TextMessage)3 ContactUser (com.jeecg.dingtalk.api.oauth2.vo.ContactUser)3 JdtUserAPI (com.jeecg.dingtalk.api.user.JdtUserAPI)3