diff --git a/pom.xml b/pom.xml index 336d33ee..1dce0895 100644 --- a/pom.xml +++ b/pom.xml @@ -251,7 +251,7 @@ com.aliyun alibaba-dingtalk-service-sdk - 1.0.0 + 2.0.0 suibian diff --git a/src/main/java/com/lz/common/utils/DingTalkUtil.java b/src/main/java/com/lz/common/utils/DingTalkUtil.java index 6a4d5187..298f089f 100644 --- a/src/main/java/com/lz/common/utils/DingTalkUtil.java +++ b/src/main/java/com/lz/common/utils/DingTalkUtil.java @@ -121,6 +121,7 @@ public class DingTalkUtil { try { //下面获取所有部门的i就按单信息 Map 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 getDepartmentDetailsV2(String accessToken, String departmentIds) { + List list = new ArrayList<>(); + + try { + //下面获取所有部门的i就按单信息 + getDepartment(list, 1L, accessToken); + return list; + } catch (Exception e) { + logger.info("获取部门详情异常{}", e); + } + return null; + } + + private void getDepartment(List 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 getDepartmentStaffDetailsV2(String accessToken, String departmentId) { + try { + long page = 0L; + List 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; } } diff --git a/src/main/java/com/lz/modules/app/dao/StaffOccupationDao.java b/src/main/java/com/lz/modules/app/dao/StaffOccupationDao.java index f0ce615e..c13d106a 100644 --- a/src/main/java/com/lz/modules/app/dao/StaffOccupationDao.java +++ b/src/main/java/com/lz/modules/app/dao/StaffOccupationDao.java @@ -33,4 +33,6 @@ public interface StaffOccupationDao extends BaseMapper { void updateStatusByStaff(Long staffId, DepartmentStaffBo departmentStaffBo); List removeDimissionStaffByStaffIds(@Param("staffIds") List staffIds); + + List selectAll(); } diff --git a/src/main/java/com/lz/modules/app/entity/DepartmentsStaffRelateEntity.java b/src/main/java/com/lz/modules/app/entity/DepartmentsStaffRelateEntity.java index edc90a6d..de9c6f4b 100644 --- a/src/main/java/com/lz/modules/app/entity/DepartmentsStaffRelateEntity.java +++ b/src/main/java/com/lz/modules/app/entity/DepartmentsStaffRelateEntity.java @@ -39,6 +39,11 @@ public class DepartmentsStaffRelateEntity implements Serializable { * 是否为部门领导 */ private Integer isLeader; + //获取mapkey + public String getKey(){ + return departmentId + "_" + staffId; + } + /** * 自增主键 * @return diff --git a/src/main/java/com/lz/modules/app/entity/StaffOccupationEntity.java b/src/main/java/com/lz/modules/app/entity/StaffOccupationEntity.java index 1959c17a..26ed9a84 100644 --- a/src/main/java/com/lz/modules/app/entity/StaffOccupationEntity.java +++ b/src/main/java/com/lz/modules/app/entity/StaffOccupationEntity.java @@ -105,4 +105,8 @@ public class StaffOccupationEntity implements Serializable { */ private String resumeUrl; + public String getKey(){ + return staffId + "_" + position; + } + } diff --git a/src/main/java/com/lz/modules/app/service/StaffOccupationService.java b/src/main/java/com/lz/modules/app/service/StaffOccupationService.java index 61ab3b85..1ee2f43e 100644 --- a/src/main/java/com/lz/modules/app/service/StaffOccupationService.java +++ b/src/main/java/com/lz/modules/app/service/StaffOccupationService.java @@ -33,5 +33,7 @@ public interface StaffOccupationService extends IService void updateStatusByStaff(Long staffId, DepartmentStaffBo departmentStaffBo); List removeDimissionStaffByStaffIds(List staffIds); + + List selectAll(); } diff --git a/src/main/java/com/lz/modules/app/service/StaffService.java b/src/main/java/com/lz/modules/app/service/StaffService.java index 936f6f45..eddffc4d 100644 --- a/src/main/java/com/lz/modules/app/service/StaffService.java +++ b/src/main/java/com/lz/modules/app/service/StaffService.java @@ -105,5 +105,7 @@ public interface StaffService extends IService { List selectStaffsByGroupId(Long copyId); List findManger(Long recordId, int flowProcess); + + StaffEntity convertStaffEntity(DepartmentStaffBo staffBo); } diff --git a/src/main/java/com/lz/modules/app/service/impl/StaffOccupationServiceImpl.java b/src/main/java/com/lz/modules/app/service/impl/StaffOccupationServiceImpl.java index 214bde65..38a998e7 100644 --- a/src/main/java/com/lz/modules/app/service/impl/StaffOccupationServiceImpl.java +++ b/src/main/java/com/lz/modules/app/service/impl/StaffOccupationServiceImpl.java @@ -72,4 +72,9 @@ public class StaffOccupationServiceImpl extends ServiceImpl selectAll(){ + return staffOccupationDao.selectAll(); + } } diff --git a/src/main/java/com/lz/modules/app/service/impl/StaffServiceImpl.java b/src/main/java/com/lz/modules/app/service/impl/StaffServiceImpl.java index a15333aa..cd0f894a 100644 --- a/src/main/java/com/lz/modules/app/service/impl/StaffServiceImpl.java +++ b/src/main/java/com/lz/modules/app/service/impl/StaffServiceImpl.java @@ -374,7 +374,7 @@ public class StaffServiceImpl extends ServiceImpl impleme } } - private StaffEntity convertStaffEntity(DepartmentStaffBo staffBo) { + public StaffEntity convertStaffEntity(DepartmentStaffBo staffBo) { StaffEntity staffEntity = new StaffEntity(); staffEntity.setName(staffBo.getName());//员工姓名 staffEntity.setGender(staffBo.getGender()); diff --git a/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java b/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java index 235da916..68851670 100644 --- a/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java +++ b/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java @@ -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 addDeparts = new ArrayList<>();//需要新增的部门信息 + List updateDeparts = new ArrayList<>();//需要更新的部门信息 + + List addStaffs = new ArrayList<>();//需要新增的人员信息 + List updateStaffs = new ArrayList<>();//需要更新的人员 + + List addDepartStaff = new ArrayList<>();//需要新增的部门人员对应关系 + List updateDepartStaff = new ArrayList<>();//需要更新部门人员对应关系 + Map mapEmployeeIdDeparts = new HashedMap();//新员工对应的部门对应关系 + + List addStaffOcc = new ArrayList<>();//需要新增的职位信息 + List updateStaffOcc = new ArrayList<>();//需要更新的职位信息 + Map mapEmployeeIdStaffOccs = new HashedMap();//新员工对应的职位对应关系 + + List newDepartIds = new ArrayList<>();//新的部门或者新的人员对应信息 + List newPositions = new ArrayList<>();//新的职位或者新的人员对应信息 + //获取所有的部门详情 - Map departmentInfosBos = dingTalkUtil.getDepartmentDetails(token, "1"); + List departmentInfosBos = dingTalkUtil.getDepartmentDetailsV2(token, "1"); if(departmentInfosBos.size() > 0){ - //获取所有部门信息 + //获取原有部门信息 + Map mapDepartmentInfosBos + = departmentInfosBos.stream().collect(Collectors.toMap(DepartmentInfosBo::getId, Function.identity(), (e, r) -> e)); List departmentsEntities = departmentsService.selectAll(); - Map mapDeparts = - departmentsEntities.stream().collect(Collectors.toMap(DepartmentsEntity::getDepartmentId, Function.identity(), (e, r) -> e)); - //更新数据库中的部门相关信息 - //departmentsService.updateDepartmentInfos(departmentInfosBos); + Map mapDeparts = null; + if(departmentsEntities.size() > 0){ + mapDeparts = + departmentsEntities.stream().collect(Collectors.toMap(DepartmentsEntity::getDepartmentId, Function.identity(), (e, r) -> e)); + } + + //获取原有的部门人员对应关系 + List departmentsStaffRelateEntities = departmentsStaffRelateService.selectAll(); + + Map mapDepStaffs = null; + if(departmentsStaffRelateEntities.size() > 0){ + mapDepStaffs = + departmentsStaffRelateEntities.stream().collect( + Collectors.toMap(DepartmentsStaffRelateEntity::getKey, + Function.identity(), (e, r) -> e)); + } + + //获取所有人员信息 + List staffEntities = staffService.selectAll(); + Map mapStaffs = null; + if(staffEntities.size() > 0){ + mapStaffs = staffEntities.stream(). + collect(Collectors.toMap(StaffEntity::getEmployeeId, Function.identity(), (e, r)->e)); + } + Map mapMorDepartStaffs = new HashedMap();//跨部门人员 + List staffOccupationEntities = staffOccupationService.selectAll(); + Map mapStaffOccs = null; + if(staffOccupationEntities.size() > 0){ + mapStaffOccs = + staffOccupationEntities.stream().collect(Collectors.toMap(StaffOccupationEntity::getKey, Function.identity(), (e, r) -> e)); + } + Map 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 staffs = dingTalkUtil.getDepartmentStaffDetails(token, departmentInfo.getId()); + List 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("部门信息为空"); } diff --git a/src/main/java/com/lz/modules/job/model/responseBo/DepartmentStaffBo.java b/src/main/java/com/lz/modules/job/model/responseBo/DepartmentStaffBo.java index 990fe21b..4d55936d 100644 --- a/src/main/java/com/lz/modules/job/model/responseBo/DepartmentStaffBo.java +++ b/src/main/java/com/lz/modules/job/model/responseBo/DepartmentStaffBo.java @@ -82,6 +82,10 @@ public class DepartmentStaffBo implements Serializable { */ private Integer isLeader; + /*** + * 是否为老板 + */ + private Integer isBoss; diff --git a/src/main/resources/mapper/generator/StaffDao.xml b/src/main/resources/mapper/generator/StaffDao.xml index 102b4ab8..0c17f05a 100644 --- a/src/main/resources/mapper/generator/StaffDao.xml +++ b/src/main/resources/mapper/generator/StaffDao.xml @@ -256,7 +256,7 @@ ),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} 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 diff --git a/src/main/resources/mapper/generator/StaffOccupationDao.xml b/src/main/resources/mapper/generator/StaffOccupationDao.xml index c5e70cfc..7b0240ae 100644 --- a/src/main/resources/mapper/generator/StaffOccupationDao.xml +++ b/src/main/resources/mapper/generator/StaffOccupationDao.xml @@ -68,4 +68,8 @@ + +