| | |
| | | package com.ruoyi.system.service.impl.staff; |
| | | |
| | | import com.ruoyi.system.api.domain.poji.sys.SysDept; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.system.domain.dto.MgtWxCPStaffDto; |
| | | import com.ruoyi.system.service.staff.SysWxCpService; |
| | | import com.ruoyi.system.service.sys.ISysDeptService; |
| | | import lombok.extern.log4j.Log4j2; |
| | | import me.chanjar.weixin.common.error.WxErrorException; |
| | | import me.chanjar.weixin.cp.api.WxCpDepartmentService; |
| | | import me.chanjar.weixin.cp.api.WxCpService; |
| | | import me.chanjar.weixin.cp.bean.WxCpDepart; |
| | | import me.chanjar.weixin.cp.bean.WxCpUser; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @ClassName WxCpServiceImpl |
| | |
| | | public class SysWxCpServiceImpl implements SysWxCpService { |
| | | |
| | | @Resource |
| | | @Qualifier("wxService") |
| | | private WxCpService wxCpService; |
| | | |
| | | @Resource |
| | | private ISysDeptService sysDeptService; |
| | | @Qualifier("adWxService") |
| | | private WxCpService adWxCpService; |
| | | |
| | | |
| | | /** |
| | | * @description |
| | | * @author jqs |
| | |
| | | return accessToken; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * @description 同步部门 |
| | | * @description 更新企业微信部门 |
| | | * @author jqs |
| | | * @date 2023/8/17 15:42 |
| | | * @param |
| | | * @date 2023/9/4 15:19 |
| | | * @param wxDeptId |
| | | * @param deptName |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public void syncDepartment(){ |
| | | WxCpDepartmentService wxCpDepartmentService = wxCpService.getDepartmentService(); |
| | | public void updateWxCpDept(Long wxDeptId,String deptName){ |
| | | WxCpDepart group = new WxCpDepart(); |
| | | group.setId(wxDeptId); |
| | | group.setName(deptName); |
| | | try { |
| | | //从企业微信拉去部门名单 |
| | | List<WxCpDepart> wxCpDepartList = wxCpDepartmentService.list(null); |
| | | if(wxCpDepartList!=null&&wxCpDepartList.size()>0){ |
| | | for(WxCpDepart wxCpDepart : wxCpDepartList){ |
| | | //获取系统内对应部门 |
| | | SysDept sysDept = sysDeptService.getByWxDeptId(wxCpDepart.getId()); |
| | | //判断是否有该部门,没有则新建 |
| | | if(sysDept!=null){ |
| | | //判断是否有变化 |
| | | if(!sysDept.getDeptName().equals(wxCpDepart.getName())||sysDept.getOrderNum()!=wxCpDepart.getOrder().intValue()){ |
| | | sysDept.setDeptName(wxCpDepart.getName()); |
| | | sysDept.setOrderNum(wxCpDepart.getOrder().intValue()); |
| | | sysDept.setUpdateBy("企业微信同步"); |
| | | sysDeptService.updateDept(sysDept); |
| | | log.info("企业微信同步更新部门:"+wxCpDepart.getName()); |
| | | } |
| | | }else{ |
| | | sysDept = new SysDept(); |
| | | sysDept.setDelFlag("0"); |
| | | sysDept.setStatus("0"); |
| | | sysDept.setParentId(100L); |
| | | sysDept.setOrderNum(wxCpDepart.getOrder().intValue()); |
| | | sysDept.setCreateBy("企业微信同步"); |
| | | sysDept.setDeptName(wxCpDepart.getName()); |
| | | sysDeptService.insertDept(sysDept); |
| | | log.info("企业微信同步新增部门:"+wxCpDepart.getName()); |
| | | } |
| | | } |
| | | adWxCpService.getDepartmentService().update(group); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 创建企业微信部门 |
| | | * @author jqs |
| | | * @date 2023/9/4 15:19 |
| | | * @param parentId |
| | | * @param deptName |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public Long addWxCpDept(Long parentId,String deptName){ |
| | | WxCpDepart depart = new WxCpDepart(); |
| | | depart.setParentId(parentId); |
| | | depart.setName(deptName); |
| | | Long departId = null; |
| | | try { |
| | | departId = adWxCpService.getDepartmentService().create(depart); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return departId; |
| | | } |
| | | |
| | | /** |
| | | * @description 删除企业微信部门 |
| | | * @author jqs |
| | | * @date 2023/9/4 15:19 |
| | | * @param wxDeptId |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public void deleteWxCpDept(Long wxDeptId){ |
| | | try { |
| | | adWxCpService.getDepartmentService().delete(wxDeptId); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 通过手机号获取wxUserId |
| | | * @author jqs |
| | | * @date 2023/9/4 15:44 |
| | | * @param mobile |
| | | * @return String |
| | | */ |
| | | private String getWxUserIdByMobile(String mobile){ |
| | | String wxUserId = null; |
| | | try { |
| | | wxUserId = adWxCpService.getUserService().getUserId(mobile); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return wxUserId; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @description 通过wxUserId获取用户 |
| | | * @author jqs |
| | | * @date 2023/9/4 16:35 |
| | | * @param wxUserId |
| | | * @return WxCpUser |
| | | */ |
| | | private WxCpUser getWxCpUser(String wxUserId){ |
| | | WxCpUser wxCpUser = null; |
| | | try { |
| | | wxCpUser = wxCpService.getUserService().getById(wxUserId); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return wxCpUser; |
| | | } |
| | | |
| | | /** |
| | | * @description 创建企业微信员工 |
| | | * @author jqs |
| | | * @date 2023/9/4 15:19 |
| | | * @param mgtWxCPStaffDto |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public String addWxCpStaff(MgtWxCPStaffDto mgtWxCPStaffDto){ |
| | | String wxUserId = mgtWxCPStaffDto.getWxUserId(); |
| | | String mobile = mgtWxCPStaffDto.getStaffMobile(); |
| | | String oldUserId = this.getWxUserIdByMobile(mobile); |
| | | WxCpUser oldWxCpUser = null; |
| | | Boolean sameUserId = false; |
| | | //判断手机号是否有对应企业微信用户 有则更新 无则新建 |
| | | if(StringUtils.isNotBlank(oldUserId)){ |
| | | oldWxCpUser = this.getWxCpUser(oldUserId); |
| | | }else{ |
| | | //当手机号无对应用户时判断wxUserId是否被占用 |
| | | oldWxCpUser = this.getWxCpUser(wxUserId); |
| | | if(oldWxCpUser!=null){ |
| | | sameUserId = true; |
| | | oldWxCpUser = null; |
| | | } |
| | | } |
| | | //获取企业微信用户是否存在 |
| | | if(oldWxCpUser!=null){ |
| | | oldWxCpUser.setUserId(mgtWxCPStaffDto.getWxUserId()); |
| | | oldWxCpUser.setName(mgtWxCPStaffDto.getStaffName()); |
| | | oldWxCpUser.setMobile(mgtWxCPStaffDto.getStaffMobile()); |
| | | if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffEmail())){ |
| | | oldWxCpUser.setEmail(mgtWxCPStaffDto.getStaffEmail()); |
| | | } |
| | | if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffPost())){ |
| | | oldWxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost()); |
| | | } |
| | | oldWxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost()); |
| | | Long[] departIds = new Long[1]; |
| | | departIds[0] = mgtWxCPStaffDto.getWxDeptId(); |
| | | oldWxCpUser.setDepartIds(departIds); |
| | | if(mgtWxCPStaffDto.getHeadFlag()!=null){ |
| | | Integer[] isLeaderInDept = new Integer[1]; |
| | | isLeaderInDept[0] = Integer.valueOf(mgtWxCPStaffDto.getHeadFlag()); |
| | | oldWxCpUser.setIsLeaderInDept(isLeaderInDept); |
| | | } |
| | | try { |
| | | adWxCpService.getUserService().update(oldWxCpUser); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | }else{ |
| | | WxCpUser wxCpUser = new WxCpUser(); |
| | | //wxUserId被占用重新生成 |
| | | if(sameUserId){ |
| | | wxUserId = wxUserId + "1"; |
| | | } |
| | | wxCpUser.setUserId(wxUserId); |
| | | wxCpUser.setName(mgtWxCPStaffDto.getStaffName()); |
| | | wxCpUser.setMobile(mgtWxCPStaffDto.getStaffMobile()); |
| | | if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffEmail())){ |
| | | wxCpUser.setEmail(mgtWxCPStaffDto.getStaffEmail()); |
| | | } |
| | | if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffPost())){ |
| | | wxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost()); |
| | | } |
| | | wxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost()); |
| | | Long[] departIds = new Long[1]; |
| | | departIds[0] = mgtWxCPStaffDto.getWxDeptId(); |
| | | wxCpUser.setDepartIds(departIds); |
| | | if(mgtWxCPStaffDto.getHeadFlag()!=null){ |
| | | Integer[] isLeaderInDept = new Integer[1]; |
| | | isLeaderInDept[0] = Integer.valueOf(mgtWxCPStaffDto.getHeadFlag()); |
| | | wxCpUser.setIsLeaderInDept(isLeaderInDept); |
| | | } |
| | | try { |
| | | adWxCpService.getUserService().create(wxCpUser); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | return wxUserId; |
| | | } |
| | | |
| | | /** |
| | | * @description 删除企业微信员工 |
| | | * @author jqs |
| | | * @date 2023/9/4 15:19 |
| | | * @param wxUserId |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public void deleteWxCpStaff(String wxUserId){ |
| | | try { |
| | | adWxCpService.getUserService().delete(wxUserId); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |