优化钉钉通讯录同步方法,更新钉钉V2版本,修复部分其他bug

This commit is contained in:
wulin 2020-11-26 17:35:54 +08:00
parent b9f9a2d125
commit 719fd8086c
13 changed files with 572 additions and 49 deletions

View File

@ -251,7 +251,7 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.0</version>
<version>2.0.0</version>
<classifier>suibian</classifier>
</dependency>

View File

@ -121,6 +121,7 @@ public class DingTalkUtil {
try {
//下面获取所有部门的i就按单信息
Map<String, DepartmentInfosBo> list = new HashMap<>();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
OapiDepartmentListRequest req = new OapiDepartmentListRequest();
req.setFetchChild(true);
@ -144,6 +145,7 @@ public class DingTalkUtil {
deReq.setId(departmentInfosBo.getId());
deReq.setHttpMethod("GET");
OapiDepartmentGetResponse deRsp = client.execute(deReq, accessToken);
logger.info("钉钉部门信息返回{}", deRsp.getBody());
json = JSONObject.parseObject(deRsp.getBody());
departmentInfosBo.setChatId(json.getString("deptGroupChatId"));
departmentInfosBo.setLeaderEmployeeId(json.getString("deptManagerUseridList"));//部门主管,多个主管以|隔开
@ -162,6 +164,47 @@ public class DingTalkUtil {
return null;
}
public List<DepartmentInfosBo> getDepartmentDetailsV2(String accessToken, String departmentIds) {
List<DepartmentInfosBo> list = new ArrayList<>();
try {
//下面获取所有部门的i就按单信息
getDepartment(list, 1L, accessToken);
return list;
} catch (Exception e) {
logger.info("获取部门详情异常{}", e);
}
return null;
}
private void getDepartment(List<DepartmentInfosBo> list, Long depId, String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(depId);
req.setLanguage("zh_CN");
OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);
logger.info("钉钉请求返回{}", rsp.getBody());
JSONObject json = JSONObject.parseObject(rsp.getBody());
if(json.getInteger("errcode") == 0) {
JSONArray array = json.getJSONArray("result");
logger.info("子部门数量{}", array.size());
for (int i = 0; i < array.size(); i++) {
json = array.getJSONObject(i);
DepartmentInfosBo departmentInfosBo = new DepartmentInfosBo();
departmentInfosBo.setName(json.getString("name"));
departmentInfosBo.setId(json.getString("dept_id"));//自身ID
departmentInfosBo.setParentId(json.getString("parent_id"));//父级ID
list.add(departmentInfosBo);
logger.info("钉钉请求{}的子部门", departmentInfosBo.getName());
getDepartment(list, json.getLong("dept_id"), accessToken);
}
}
}
/**
* 获取部门用户详情
*
@ -183,7 +226,7 @@ public class DingTalkUtil {
for(long page = 0; isNext; page++){
req.setOffset(page);
OapiUserListbypageResponse rsp = client.execute(req, accessToken);
logger.info("钉钉请求返回{}", rsp.getBody());
logger.info("钉钉请求人员返回{}", rsp.getBody());
JSONObject json = JSONObject.parseObject(rsp.getBody());
if(json.getInteger("errcode") == 0){
JSONArray array = json.getJSONArray("userlist");
@ -219,7 +262,99 @@ public class DingTalkUtil {
}else{
logger.info("获取部门详情异常{}", rsp.getBody());
logger.info("获取人员详情异常{}", rsp.getBody());
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
logger.info("获取部门用户详情异常{}", e);
}
return null;
}
/**
* 获取部门用户详情
*
* @param accessToken
* @return
*/
public List<DepartmentStaffBo> getDepartmentStaffDetailsV2(String accessToken, String departmentId) {
try {
long page = 0L;
List<DepartmentStaffBo> list =new ArrayList<>();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
OapiV2UserListRequest req = new OapiV2UserListRequest();
req.setDeptId(Long.parseLong(departmentId));
req.setCursor(page);
req.setSize(100L);
req.setOrderField("modify_desc");
req.setContainAccessLimit(false);
req.setLanguage("zh_CN");
/*OapiV2UserListResponse rsp = client.execute(req, accessToken);
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/listbypage");
OapiUserListbypageRequest req = new OapiUserListbypageRequest();
req.setDepartmentId(Long.parseLong(departmentId));
req.setSize(100L);
req.setHttpMethod("GET");*/
boolean isNext = false;
for(; true; page++){
req.setCursor(page);
OapiV2UserListResponse rsp = client.execute(req, accessToken);
logger.info("钉钉请求人员返回{}", rsp.getBody());
JSONObject json = JSONObject.parseObject(rsp.getBody());
if(json.getInteger("errcode") == 0){
json = json.getJSONObject("result");
JSONArray array = json.getJSONArray("list");
isNext = json.getBoolean("has_more");//获取是否由更多数据
for (int i = 0; i < array.size(); i++) {
json = array.getJSONObject(i);
DepartmentStaffBo departmentStaffBo = new DepartmentStaffBo();
departmentStaffBo.setEmployeeId(json.getString("userid"));//用户在企业内部的唯一编号创建时可指定可代表一定的含义
departmentStaffBo.setName(json.getString("name"));
departmentStaffBo.setEmployeeNo(json.getString("job_number"));//工号
departmentStaffBo.setUnionId(json.getString("unionid"));//企业内部id,唯一
departmentStaffBo.setOpenId(departmentStaffBo.getUnionId());//和上面的值目前是一样的未找到说明
departmentStaffBo.setMobile(json.getString("mobile"));//手机需要单独授权手机权限
departmentStaffBo.setEmail(json.getString("email"));//邮箱钉钉的企业邮箱才可以需要单独授权手机权限
departmentStaffBo.setAvatar(json.getString("avatar"));//头像
departmentStaffBo.setPosition(json.getString("title"));
departmentStaffBo.setStatus(0);
/*if(json.getBoolean("active")){
//在职已激活
departmentStaffBo.setStatus(0);
}else{
//在职未激活
departmentStaffBo.setStatus(4);
}*/
if(json.getBoolean("leader")){
departmentStaffBo.setIsLeader(1);
}else{
departmentStaffBo.setIsLeader(0);
}
if(json.getBoolean("boss")){
departmentStaffBo.setIsBoss(1);
}else{
departmentStaffBo.setIsBoss(0);
}
list.add(departmentStaffBo);
}
if(!isNext){
break;
}
}else{
logger.info("获取人员详情异常{}", rsp.getBody());
break;
}
}

View File

@ -33,4 +33,6 @@ public interface StaffOccupationDao extends BaseMapper<StaffOccupationEntity> {
void updateStatusByStaff(Long staffId, DepartmentStaffBo departmentStaffBo);
List<String> removeDimissionStaffByStaffIds(@Param("staffIds") List<String> staffIds);
List<StaffOccupationEntity> selectAll();
}

View File

@ -39,6 +39,11 @@ public class DepartmentsStaffRelateEntity implements Serializable {
* 是否为部门领导
*/
private Integer isLeader;
//获取mapkey
public String getKey(){
return departmentId + "_" + staffId;
}
/**
* 自增主键
* @return

View File

@ -105,4 +105,8 @@ public class StaffOccupationEntity implements Serializable {
*/
private String resumeUrl;
public String getKey(){
return staffId + "_" + position;
}
}

View File

@ -33,5 +33,7 @@ public interface StaffOccupationService extends IService<StaffOccupationEntity>
void updateStatusByStaff(Long staffId, DepartmentStaffBo departmentStaffBo);
List<String> removeDimissionStaffByStaffIds(List<String> staffIds);
List<StaffOccupationEntity> selectAll();
}

View File

@ -105,5 +105,7 @@ public interface StaffService extends IService<StaffEntity> {
List<StaffEntity> selectStaffsByGroupId(Long copyId);
List<StaffEntity> findManger(Long recordId, int flowProcess);
StaffEntity convertStaffEntity(DepartmentStaffBo staffBo);
}

View File

@ -72,4 +72,9 @@ public class StaffOccupationServiceImpl extends ServiceImpl<StaffOccupationDao,
}
return staffOccupationDao.removeDimissionStaffByStaffIds(staffIds);
}
@Override
public List<StaffOccupationEntity> selectAll(){
return staffOccupationDao.selectAll();
}
}

View File

@ -374,7 +374,7 @@ public class StaffServiceImpl extends ServiceImpl<StaffDao, StaffEntity> impleme
}
}
private StaffEntity convertStaffEntity(DepartmentStaffBo staffBo) {
public StaffEntity convertStaffEntity(DepartmentStaffBo staffBo) {
StaffEntity staffEntity = new StaffEntity();
staffEntity.setName(staffBo.getName());//员工姓名
staffEntity.setGender(staffBo.getGender());

View File

@ -1,15 +1,20 @@
package com.lz.modules.job.business;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRoleListRequest;
import com.dingtalk.api.request.OapiRoleSimplelistRequest;
import com.dingtalk.api.response.OapiRoleListResponse;
import com.dingtalk.api.response.OapiRoleSimplelistResponse;
import com.google.common.collect.Lists;
import com.lz.common.emun.WorkMsgTypeEnum;
import com.lz.common.utils.DateUtils;
import com.lz.common.utils.DingTalkUtil;
import com.lz.common.utils.R;
import com.lz.modules.app.dao.StaffDao;
import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.entity.StaffOccupationEntity;
import com.lz.modules.app.entity.StaffSimpleInfo;
import com.lz.modules.app.entity.*;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.app.service.StaffOccupationService;
@ -31,8 +36,11 @@ import com.lz.modules.third.entity.ThirdMsgSendRecord;
import com.lz.modules.third.entity.WorkMsg;
import com.lz.modules.third.service.ThirdAppConfigService;
import com.lz.modules.third.service.ThirdMsgSendRecordService;
import com.taobao.api.ApiException;
import org.apache.commons.collections.map.CompositeMap;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -40,6 +48,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@ -131,69 +140,420 @@ public class DingtalkBusiness {
//获取Token
String token = dingTalkUtil.getAccessToken(appid);
if(token != null && token.length() > 0){
/*DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/list");
OapiRoleListRequest request = new OapiRoleListRequest();
request.setOffset(0L);
request.setSize(10L);
OapiRoleListResponse response = null;
try {
response = client.execute(request, token);
logger.info("角色返回{}", response.getBody());
JSONObject jsonObject = JSONObject.parseObject(response.getBody());
JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("list");
for (int i = 0; i < jsonArray.size(); i++
) {
JSONObject json = jsonArray.getJSONObject(i);
JSONArray array = json.getJSONArray("roles");
logger.info("角色组{}", json.getString("name"));
for(int j = 0; j < array.size(); j++){
json = array.getJSONObject(j);
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/simplelist");
OapiRoleSimplelistRequest request1 = new OapiRoleSimplelistRequest();
request1.setRoleId(json.getLong("id"));
request1.setOffset(0L);
request1.setSize(200L);
OapiRoleSimplelistResponse response1 = client.execute(request1, token);
logger.info("钉钉获取角色{},数据为{}", json.getString("name"), response1.getBody());
}
}
} catch (ApiException e) {
e.printStackTrace();
}*/
List<DepartmentsEntity> addDeparts = new ArrayList<>();//需要新增的部门信息
List<DepartmentsEntity> updateDeparts = new ArrayList<>();//需要更新的部门信息
List<StaffEntity> addStaffs = new ArrayList<>();//需要新增的人员信息
List<StaffEntity> updateStaffs = new ArrayList<>();//需要更新的人员
List<DepartmentsStaffRelateEntity> addDepartStaff = new ArrayList<>();//需要新增的部门人员对应关系
List<DepartmentsStaffRelateEntity> updateDepartStaff = new ArrayList<>();//需要更新部门人员对应关系
Map<String, DepartmentsStaffRelateEntity> mapEmployeeIdDeparts = new HashedMap();//新员工对应的部门对应关系
List<StaffOccupationEntity> addStaffOcc = new ArrayList<>();//需要新增的职位信息
List<StaffOccupationEntity> updateStaffOcc = new ArrayList<>();//需要更新的职位信息
Map<String, StaffOccupationEntity> mapEmployeeIdStaffOccs = new HashedMap();//新员工对应的职位对应关系
List<String> newDepartIds = new ArrayList<>();//新的部门或者新的人员对应信息
List<String> newPositions = new ArrayList<>();//新的职位或者新的人员对应信息
//获取所有的部门详情
Map<String, DepartmentInfosBo> departmentInfosBos = dingTalkUtil.getDepartmentDetails(token, "1");
List<DepartmentInfosBo> departmentInfosBos = dingTalkUtil.getDepartmentDetailsV2(token, "1");
if(departmentInfosBos.size() > 0){
//获取所有部门信息
//获取原有部门信息
Map<String, DepartmentInfosBo> mapDepartmentInfosBos
= departmentInfosBos.stream().collect(Collectors.toMap(DepartmentInfosBo::getId, Function.identity(), (e, r) -> e));
List<DepartmentsEntity> departmentsEntities = departmentsService.selectAll();
Map<String, DepartmentsEntity> mapDeparts =
departmentsEntities.stream().collect(Collectors.toMap(DepartmentsEntity::getDepartmentId, Function.identity(), (e, r) -> e));
//更新数据库中的部门相关信息
//departmentsService.updateDepartmentInfos(departmentInfosBos);
Map<String, DepartmentsEntity> mapDeparts = null;
if(departmentsEntities.size() > 0){
mapDeparts =
departmentsEntities.stream().collect(Collectors.toMap(DepartmentsEntity::getDepartmentId, Function.identity(), (e, r) -> e));
}
//获取原有的部门人员对应关系
List<DepartmentsStaffRelateEntity> departmentsStaffRelateEntities = departmentsStaffRelateService.selectAll();
Map<String, DepartmentsStaffRelateEntity> mapDepStaffs = null;
if(departmentsStaffRelateEntities.size() > 0){
mapDepStaffs =
departmentsStaffRelateEntities.stream().collect(
Collectors.toMap(DepartmentsStaffRelateEntity::getKey,
Function.identity(), (e, r) -> e));
}
//获取所有人员信息
List<StaffEntity> staffEntities = staffService.selectAll();
Map<String, StaffEntity> mapStaffs = null;
if(staffEntities.size() > 0){
mapStaffs = staffEntities.stream().
collect(Collectors.toMap(StaffEntity::getEmployeeId, Function.identity(), (e, r)->e));
}
Map<String, StaffEntity> mapMorDepartStaffs = new HashedMap();//跨部门人员
List<StaffOccupationEntity> staffOccupationEntities = staffOccupationService.selectAll();
Map<String, StaffOccupationEntity> mapStaffOccs = null;
if(staffOccupationEntities.size() > 0){
mapStaffOccs =
staffOccupationEntities.stream().collect(Collectors.toMap(StaffOccupationEntity::getKey, Function.identity(), (e, r) -> e));
}
Map<String, StaffOccupationEntity> mapHaveStaffOccs = new HashedMap();
//删除原有的对应关系下面在更新
departmentsStaffRelateService.deleteAllRelates();
//未在飞书组织架构里的成员置为离职(全部置为离职再把在职的恢复)
staffOccupationService.updateAllOccupation();
//获取飞书部门对应的用户详情
for (String key : departmentInfosBos.keySet()) {
if(mapDeparts.containsKey(key)){
for (DepartmentInfosBo departmentInfo : departmentInfosBos) {
//DepartmentInfosBo departmentInfo = departmentInfosBos.get(key);
String key = departmentInfo.getId();
DepartmentsEntity departmentsEntity = null;
if(mapDeparts != null && mapDeparts.containsKey(key)){
//部门存在
departmentsEntity = mapDeparts.get(key);
updateDeparts.add(departmentsEntity);
mapDeparts.remove(key);
}else{//新增部门信息
departmentsEntity = new DepartmentsEntity();
departmentsEntity.setDepartmentId(departmentInfo.getId());
addDeparts.add(departmentsEntity);
}
DepartmentInfosBo departmentInfo = departmentInfosBos.get(key);
//获取部门用户详情
List<DepartmentStaffBo> staffs = dingTalkUtil.getDepartmentStaffDetails(token, departmentInfo.getId());
List<DepartmentStaffBo> staffs = dingTalkUtil.getDepartmentStaffDetailsV2(token, departmentInfo.getId());
logger.info("=============================" + departmentInfo.getName() + "================================");
//departmentInfo.setMemberCount(staffs.size());//设置部门人数钉钉没有返回部门人数只能这样设置
//计算父类的
DepartmentInfosBo integer = departmentInfo;
while(!integer.getParentId().equals("1")){
integer = departmentInfosBos.get(integer.getParentId());
if(integer.getMemberCount() == null){
integer.setMemberCount(0);
}
integer.setMemberCount(integer.getMemberCount() + staffs.size());
}
//获取自己的
//设置本部门人数
if(departmentInfo.getMemberCount() == null){
departmentInfo.setMemberCount(0);
}
departmentInfo.setMemberCount(departmentInfo.getMemberCount() + staffs.size());
//计算父类的
DepartmentInfosBo integer = departmentInfo;
while(!integer.getParentId().equals("1")){
integer = mapDepartmentInfosBos.get(integer.getParentId());
if(integer.getMemberCount() == null){
integer.setMemberCount(0);
}
integer.setMemberCount(integer.getMemberCount() + staffs.size());
}
if(staffs.size() > 0){
//循环录入到员工信息表中
if(staffService.updateStaffsInfo(staffs)){
//加入到部门和员工关系表同时更新leader关系
departmentsStaffRelateService.addRelateInfos(departmentInfo.getId(), staffs);
//录入员工职业信息表
enterStaffOccupationInfos(staffs);
for (DepartmentStaffBo departmentStaffBo:staffs
) {
StaffEntity staff = staffService.convertStaffEntity(departmentStaffBo);
if(mapStaffs != null && mapStaffs.containsKey(departmentStaffBo.getEmployeeId())){//老员工
StaffEntity entity = mapStaffs.get(departmentStaffBo.getEmployeeId());
staff.setId(entity.getId());
staff.setCreateTime(entity.getCreateTime());
staff.setUpdateTime(new Date());
updateStaffs.add(staff);
if(!mapMorDepartStaffs.containsKey(departmentStaffBo.getEmployeeId())){
mapMorDepartStaffs.put(departmentStaffBo.getEmployeeId(), staff);
}
mapStaffs.remove(departmentStaffBo.getEmployeeId());
StaffOccupationEntity staffOccupation = new StaffOccupationEntity();
staffOccupation.setStaffId(staff.getId());
staffOccupation.setPosition(departmentStaffBo.getPosition());//职位信息
staffOccupation.setEmployeeNo(departmentStaffBo.getEmployeeNo());
staffOccupation.setStaffNo(departmentStaffBo.getEmployeeNo());//钉钉的员工工号
staffOccupation.setStaffType(departmentStaffBo.getEmployeeType());
staffOccupation.setStaffStatus(departmentStaffBo.getStatus());
String key1 = entity.getId() + "_" + staffOccupation.getPosition();
if(mapStaffOccs != null && mapStaffOccs.containsKey(key1)){//职位信息已存在更新
StaffOccupationEntity staffOccupation1 = mapStaffOccs.get(key1);
staffOccupation.setId(staffOccupation1.getId());
staffOccupation.setUpdateTime(new Date());
updateStaffOcc.add(staffOccupation);
mapStaffOccs.remove(key1);
mapHaveStaffOccs.put(key1, staffOccupation);
}else if(!mapHaveStaffOccs.containsKey(key1)){//职位信息不存在新建
addStaffOcc.add(staffOccupation);
mapHaveStaffOccs.put(key1, staffOccupation);
}
DepartmentsStaffRelateEntity departmentsStaffRelateEntity = new DepartmentsStaffRelateEntity();
departmentsStaffRelateEntity.setDepartmentId(departmentInfo.getId());
departmentsStaffRelateEntity.setStaffId(staff.getId());
departmentsStaffRelateEntity.setIsLeader(departmentStaffBo.getIsLeader());
if(mapDepStaffs != null && mapDepStaffs.containsKey(departmentsStaffRelateEntity.getKey())){//对应关系已经存在
DepartmentsStaffRelateEntity staffRelateEntityentity1 = mapDepStaffs.get(departmentsStaffRelateEntity.getKey());
if(staffRelateEntityentity1.getIsLeader().intValue() != departmentsStaffRelateEntity.getIsLeader().intValue()){
departmentsStaffRelateEntity.setId(staffRelateEntityentity1.getId());
departmentsStaffRelateEntity.setUpdateTime(new Date());
updateDepartStaff.add(departmentsStaffRelateEntity);
}
mapDepStaffs.remove(departmentsStaffRelateEntity.getKey());
}else{//不存在对应关系
addDepartStaff.add(departmentsStaffRelateEntity);
}
}else if(mapMorDepartStaffs.containsKey(departmentStaffBo.getEmployeeId())){
//跨部门人员
StaffEntity entity = mapMorDepartStaffs.get(departmentStaffBo.getEmployeeId());
staff.setId(entity.getId());
DepartmentsStaffRelateEntity departmentsStaffRelateEntity = new DepartmentsStaffRelateEntity();
departmentsStaffRelateEntity.setDepartmentId(departmentInfo.getId());
departmentsStaffRelateEntity.setIsLeader(departmentStaffBo.getIsLeader());
departmentsStaffRelateEntity.setStaffId(staff.getId());
StaffOccupationEntity staffOccupation = new StaffOccupationEntity();
staffOccupation.setStaffId(staff.getId());
staffOccupation.setPosition(departmentStaffBo.getPosition());//职位信息
staffOccupation.setEmployeeNo(departmentStaffBo.getEmployeeNo());
staffOccupation.setStaffNo(departmentStaffBo.getEmployeeNo());//钉钉的员工工号
staffOccupation.setStaffType(departmentStaffBo.getEmployeeType());
staffOccupation.setStaffStatus(departmentStaffBo.getStatus());
if(entity.getId() == null){//新用户
key = staff.getEmployeeId() + "_" + departmentsStaffRelateEntity.getDepartmentId();
if(!mapEmployeeIdDeparts.containsKey(key)){
departmentsStaffRelateEntity.setIsLeader(departmentStaffBo.getIsLeader());
mapEmployeeIdDeparts.put(key
, departmentsStaffRelateEntity);
newDepartIds.add(departmentsStaffRelateEntity.getDepartmentId());
}
key = staff.getEmployeeId() + "_" + staffOccupation.getPosition();
if(!mapEmployeeIdStaffOccs.containsKey(key)){
mapEmployeeIdStaffOccs.put(key
, staffOccupation);
newPositions.add(departmentStaffBo.getPosition());
}
}else{
String key1 = entity.getId() + "_" + staffOccupation.getPosition();
if(mapStaffOccs != null && mapStaffOccs.containsKey(key1)){//职位信息已存在更新
StaffOccupationEntity staffOccupation1 = mapStaffOccs.get(key1);
staffOccupation.setId(staffOccupation1.getId());//职位信息
staffOccupation.setUpdateTime(new Date());
updateStaffOcc.add(staffOccupation);
mapStaffOccs.remove(key1);
mapHaveStaffOccs.put(key1, staffOccupation);
}else if(!mapHaveStaffOccs.containsKey(key1)){//职位信息不存在新建
addStaffOcc.add(staffOccupation);
mapHaveStaffOccs.put(key1, staffOccupation);
}
if(mapDepStaffs != null && mapDepStaffs.containsKey(departmentsStaffRelateEntity.getKey())){//对应关系已经存在
DepartmentsStaffRelateEntity staffRelateEntityentity1 = mapDepStaffs.get(departmentsStaffRelateEntity.getKey());
if(staffRelateEntityentity1.getIsLeader().intValue() != departmentsStaffRelateEntity.getIsLeader().intValue()){
departmentsStaffRelateEntity.setId(staffRelateEntityentity1.getId());
departmentsStaffRelateEntity.setUpdateTime(new Date());
updateDepartStaff.add(departmentsStaffRelateEntity);
}
mapDepStaffs.remove(departmentsStaffRelateEntity.getKey());
}else{//不存在对应关系
addDepartStaff.add(departmentsStaffRelateEntity);
}
}
}else{//新的员工信息
addStaffs.add(staff);
if(!mapMorDepartStaffs.containsKey(departmentStaffBo.getEmployeeId())){
mapMorDepartStaffs.put(departmentStaffBo.getEmployeeId(), staff);
}
//新增部门人员对应关系新增人员后插入数据库
DepartmentsStaffRelateEntity departmentsStaffRelateEntity = new DepartmentsStaffRelateEntity();
departmentsStaffRelateEntity.setDepartmentId(departmentInfo.getId());
key = staff.getEmployeeId() + "_" + departmentsStaffRelateEntity.getDepartmentId();
if(!mapEmployeeIdDeparts.containsKey(key)){
departmentsStaffRelateEntity.setIsLeader(departmentStaffBo.getIsLeader());
mapEmployeeIdDeparts.put(key
, departmentsStaffRelateEntity);
newDepartIds.add(departmentsStaffRelateEntity.getDepartmentId());
}
//新增人员职位信息新增人员后插入数据库
StaffOccupationEntity staffOccupation = new StaffOccupationEntity();
staffOccupation.setPosition(departmentStaffBo.getPosition());//职位信息
key = staff.getEmployeeId() + "_" + staffOccupation.getPosition();
if(!mapEmployeeIdStaffOccs.containsKey(key)){
staffOccupation.setEmployeeNo(departmentStaffBo.getEmployeeNo());
staffOccupation.setStaffNo(departmentStaffBo.getEmployeeNo());//钉钉的员工工号
staffOccupation.setStaffType(departmentStaffBo.getEmployeeType());
staffOccupation.setStaffStatus(departmentStaffBo.getStatus());
mapEmployeeIdStaffOccs.put(key
, staffOccupation);
newPositions.add(departmentStaffBo.getPosition());
}
}
}
}
}
for (String key : departmentInfosBos.keySet()) {
DepartmentInfosBo departmentInfo = departmentInfosBos.get(key);
departmentsService.updateDepartmentInfo(departmentInfo);
}
if(mapDeparts.size() > 0){//有需要删除的部门信息
if(mapDeparts != null && mapDeparts.size() > 0){//有需要删除的部门信息
logger.info("删除部门信息个数{}", mapDeparts.size());
departmentsEntities = mapDeparts.values().stream().collect(Collectors.toList());
departmentsService.delDepartments(departmentsEntities);
for(DepartmentsEntity entity:departmentsEntities) {
entity.setIsDelete(1);
entity.setUpdateTime(new Date());
}
departmentsService.updateBatchById(departmentsEntities);
}
if(addDeparts.size() > 0){//有新增部门
logger.info("新增部门信息个数{}", addDeparts.size());
for (DepartmentsEntity entity:addDeparts
) {
DepartmentInfosBo departmentInfo = mapDepartmentInfosBos.get(entity.getDepartmentId());
entity.setDepartmentId(departmentInfo.getId());
entity.setDepartmentName(departmentInfo.getName());
entity.setDepartmentParentId(departmentInfo.getParentId());
entity.setMemberCount(departmentInfo.getMemberCount());
entity.setChatId(departmentInfo.getChatId());
entity.setLeaderEmployeeId(departmentInfo.getLeaderEmployeeId());
entity.setLeaderOpenId(departmentInfo.getLeaderOpenId());
entity.setStatus(1);
}
departmentsService.saveBatch(addDeparts);
}
if(updateDeparts.size() > 0){//有新增部门
logger.info("更新部门信息个数{}", updateDeparts.size());
for (DepartmentsEntity entity:updateDeparts
) {
DepartmentInfosBo departmentInfo = mapDepartmentInfosBos.get(entity.getDepartmentId());
entity.setDepartmentId(departmentInfo.getId());
entity.setDepartmentName(departmentInfo.getName());
entity.setDepartmentParentId(departmentInfo.getParentId());
entity.setMemberCount(departmentInfo.getMemberCount());
entity.setChatId(departmentInfo.getChatId());
entity.setLeaderEmployeeId(departmentInfo.getLeaderEmployeeId());
entity.setLeaderOpenId(departmentInfo.getLeaderOpenId());
entity.setStatus(1);
}
departmentsService.updateBatchById(updateDeparts);
}
if(addStaffs.size() > 0){//有新增部门
logger.info("新增人员信息个数{}", addStaffs.size());
staffService.saveBatch(addStaffs);
String key;
for (StaffEntity entity:addStaffs
) {
for (String departId:newDepartIds
) {
key = entity.getEmployeeId() + "_" + departId;
if(mapEmployeeIdDeparts.containsKey(key)){
DepartmentsStaffRelateEntity departmentsStaffRelateEntity =
mapEmployeeIdDeparts.get(key);
departmentsStaffRelateEntity.setStaffId(entity.getId());
addDepartStaff.add(departmentsStaffRelateEntity);
mapEmployeeIdDeparts.remove(key);
}
}
for (String position:newPositions
) {
key = entity.getEmployeeId() + "_" + position;
if(mapEmployeeIdStaffOccs.containsKey(key)){
StaffOccupationEntity staffOccupation = mapEmployeeIdStaffOccs.get(key);
staffOccupation.setStaffId(entity.getId());
addStaffOcc.add(staffOccupation);
mapEmployeeIdStaffOccs.remove(key);
}
}
}
}
if(updateStaffs.size() > 0){//有新增部门
logger.info("更新人员信息个数{}", updateStaffs.size());
staffService.updateBatchById(updateStaffs);
}
if(mapDepStaffs != null && mapDepStaffs.size() > 0){
logger.info("删除人员部门个数{}", mapDepStaffs.size());
departmentsStaffRelateEntities = mapDepStaffs.values().stream().collect(Collectors.toList());
for (DepartmentsStaffRelateEntity entity:departmentsStaffRelateEntities
) {
entity.setIsDelete(1);
}
departmentsStaffRelateService.updateBatchById(departmentsStaffRelateEntities);
}
if(addDepartStaff.size() > 0){//有新增部门
logger.info("新增部门人员对应关系个数{}", addDepartStaff.size());
departmentsStaffRelateService.saveBatch(addDepartStaff);
}
if(updateDepartStaff.size() > 0){//有新增部门
logger.info("更新部门人员对应关系个数{}", updateDepartStaff.size());
departmentsStaffRelateService.updateBatchById(updateDepartStaff);
}
if(addStaffOcc.size() > 0){//有新增部门
logger.info("新增人员职位对应关系个数{}", addStaffOcc.size());
staffOccupationService.saveBatch(addStaffOcc);
}
if(updateStaffOcc.size() > 0){//有新增部门
logger.info("更新人员对职位应关系个数{}", updateStaffOcc.size());
staffOccupationService.updateBatchById(updateStaffOcc);
}
if(mapStaffs != null && mapStaffs.size() > 0){//离职人员
logger.info("离职人员可能个数{}", mapStaffs.size());
if(mapStaffOccs != null && mapStaffOccs.size() > 0){
logger.info("离职人员对应的职位信息个数{}", mapStaffs.size());
staffEntities = mapStaffs.values().stream().collect(Collectors.toList());
staffOccupationEntities.clear();
for (StaffEntity entity:staffEntities
) {
if(mapStaffOccs.containsKey(entity.getId())){
StaffOccupationEntity staffOccupationEntity = mapStaffOccs.get(entity.getId());
if(staffOccupationEntity.getStaffStatus().intValue() == 0){
staffOccupationEntity.setStaffStatus(1);
staffOccupationEntity.setUpdateTime(new Date());
staffOccupationEntities.add(staffOccupationEntity);
}
}
}
logger.info("离职人员实际个数{}", staffOccupationEntities.size());
staffOccupationService.updateBatchById(staffOccupationEntities);
}
}
}else{
logger.info("部门信息为空");
}

View File

@ -82,6 +82,10 @@ public class DepartmentStaffBo implements Serializable {
*/
private Integer isLeader;
/***
* 是否为老板
*/
private Integer isBoss;

View File

@ -256,7 +256,7 @@
</if>
),1,0) department_leader
from lz_staff ls,lz_staff_occupation lso
where lso.staff_id=ls.id and ls.is_delete=0
where lso.staff_id=ls.id and ls.is_delete=0 and lso.is_delete=0
and lso.staff_status=#{staffStatus}
<if test="name != null and name != ''">
and ls.name like concat('%',#{name},'%')
@ -480,7 +480,7 @@
from (select staff.id as staff_id, staff.name as name, relate.is_leader as department_leader
from lz_departments_staff_relate relate join lz_staff staff on staff.id = relate.staff_id
where relate.department_id = #{departmentId} and relate.is_delete=0 and staff.is_delete=0 group by relate.staff_id) jj
join lz_staff_occupation occ on jj.staff_id=occ.staff_id where occ.staff_status=0 and occ.is_delete=0
join lz_staff_occupation occ on jj.staff_id=occ.staff_id where occ.staff_status=0 and occ.is_delete=0 GROUP BY jj.staff_id
</select>

View File

@ -68,4 +68,8 @@
</foreach>
</select>
<select id="selectAll" resultType="com.lz.modules.app.entity.StaffOccupationEntity">
select * from lz_staff_occupation where is_delete=0
</select>
</mapper>