JSD-6952 开源任务代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

186 lines
7.5 KiB

/*
* Copyright (C), 2018-2021
* Project: starter
* FileName: UserServiceKit
* Author: Louis
* Date: 2021/5/14 8:28
*/
package com.fr.plugin.mqh.dingtalksyn.kit;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.Post;
import com.fr.decision.authority.data.User;
import com.fr.decision.authority.data.personnel.DepRole;
import com.fr.decision.privilege.TransmissionTool;
import com.fr.decision.webservice.bean.user.DepartmentPostBean;
import com.fr.decision.webservice.bean.user.UserBean;
import com.fr.decision.webservice.utils.UserSourceFactory;
import com.fr.decision.webservice.utils.WebServiceUtils;
import com.fr.decision.webservice.v10.user.PositionService;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.stable.StringUtils;
import com.fr.stable.collections.CollectionUtils;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.restriction.RestrictionFactory;
import java.util.ArrayList;
import java.util.List;
/**
* <Function Description><br>
* <UserServiceKit>
*
* @author Louis
* @since 1.0.0
*/
public class UserServiceKit extends UserService {
private static volatile UserServiceKit userServiceKit = null;
public UserServiceKit() {
}
public static UserServiceKit getInstance() {
if (userServiceKit == null) {
userServiceKit = new UserServiceKit();
}
return userServiceKit;
}
public UserBean createUserBean(OapiV2UserGetResponse.UserGetResponse userGetResponse) throws Exception {
UserBean userBean = new UserBean();
userBean.setUsername(userGetResponse.getUserid());
userBean.setRealName(userGetResponse.getName());
userBean.setEmail(userGetResponse.getEmail());
userBean.setMobile(userGetResponse.getMobile());
userBean.setPassword(TransmissionTool.defaultEncrypt(userGetResponse.getUserid() + "123456"));
if (!CollectionUtils.isEmpty(userGetResponse.getRoleList())) {
userBean.setRoleIds(oapiUserRoles2Ids(userGetResponse.getRoleList()));
}
if (!CollectionUtils.isEmpty(userGetResponse.getDeptIdList())) {
List<String> departmentPostIds = createDepartmentPostIds(userGetResponse.getDeptIdList(), userGetResponse.getTitle());
userBean.setDepartmentPostIds(departmentPostIds);
}
return userBean;
}
/**
* 钉钉部门list转为部门职务组合list
*
* @param deptIdList
* @param title
* @return
* @throws Exception
*/
private List<String> createDepartmentPostIds(List<Long> deptIdList, String title) throws Exception {
List<String> departmentPostIds = new ArrayList<>();
String departmentPostId;
for (long currentDepId : deptIdList) {
departmentPostId = String.valueOf(currentDepId);
if (StringKit.isBlank(departmentPostId) || StringKit.equals(departmentPostId, "null")) {
continue;
}
// 职务处理
String positionId = positionSynOperation(title, departmentPostId);
if (StringKit.isNotBlank(positionId)) {
departmentPostId = departmentPostId + "@@@" + positionId;
}
departmentPostIds.add(departmentPostId);
}
return departmentPostIds;
}
/**
* 职务同步操作
*
* @param title
* @return
* @throws Exception
*/
private String positionSynOperation(String title, String departmentId) throws Exception {
String position = StringKit.isNotBlank(title) ? title : "职员";
Post post = AuthorityContext.getInstance().getPostController().findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("name", position)));
String positionId;
if (post == null) {
positionId = PositionService.getInstance().addPosition(position, position);
} else {
positionId = post.getId();
}
List<DepartmentPostBean> departmentPostBeanList = PositionService.getInstance().getPositionsUnderParentDepartment(getAdminUserId(), departmentId, position);
if (departmentPostBeanList == null || departmentPostBeanList.isEmpty()) {
try {
AuthorityContext.getInstance().getPostController().addPostToDepartment(positionId, departmentId);
} catch (Exception e) {
LogKit.info("dingtalksyn-UserServiceKit-positionSynOperation-addPostToDepartmentFailed-position:{}, departmentId:{}", positionId + position, departmentId);
LogKit.error(e.getMessage(), e);
}
}
return positionId;
}
/**
* 获取管理员id
*
* @return
* @throws Exception
*/
public String getAdminUserId() throws Exception {
List<String> adminUserIdList = UserService.getInstance().getAdminUserIdList();
if (adminUserIdList.isEmpty()) {
return "admin";
}
return StringKit.isNotBlank(adminUserIdList.get(0)) ? adminUserIdList.get(0) : "admin";
}
public UserBean updateUserBean(OapiV2UserGetResponse.UserGetResponse userGetResponse) throws Exception {
User user = UserService.getInstance().getUserByUserName(userGetResponse.getUserid());
if (user == null) {
return null;
}
UserBean userBean = new UserBean();
userBean.setId(user.getId());
userBean.setUsername(user.getUserName());
userBean.setRealName(userGetResponse.getName());
userBean.setEmail(userGetResponse.getEmail());
userBean.setMobile(userGetResponse.getMobile());
if (!CollectionUtils.isEmpty(userGetResponse.getRoleList())) {
userBean.setRoleIds(oapiUserRoles2Ids(userGetResponse.getRoleList()));
}
if (!CollectionUtils.isEmpty(userGetResponse.getDeptIdList())) {
List<String> departmentPostIds = createDepartmentPostIds(userGetResponse.getDeptIdList(), userGetResponse.getTitle());
userBean.setDepartmentPostIds(departmentPostIds);
}
return userBean;
}
/**
* 增加用户部门关联
*
* @param userBean
* @throws Exception
*/
public void addUserDepartment(UserBean userBean) throws Exception {
if (CollectionUtils.isEmpty(userBean.getDepartmentPostIds())) {
return;
}
for (String departmentPostId : userBean.getDepartmentPostIds()) {
if (StringUtils.isEmpty(departmentPostId)) {
continue;
}
User user = UserService.getInstance().getUserByUserName(userBean.getUsername());
DepRole depRole = WebServiceUtils.parseUniqueDepartmentPostId(departmentPostId);
UserSourceFactory.getInstance().checkSource(user, AuthorityContext.getInstance().getDepartmentController().getById(depRole.getDepartmentId()));
AuthorityContext.getInstance().getUserController().addUserToDepartmentAndPost(user.getId(), depRole.getDepartmentId(), depRole.getPostId());
}
}
private String[] oapiUserRoles2Ids(List<OapiV2UserGetResponse.UserRole> oapiUserRoleList) {
String[] roleIds = new String[oapiUserRoleList.size()];
for (int i = 0; i < oapiUserRoleList.size(); i++) {
roleIds[i] = String.valueOf(oapiUserRoleList.get(i).getId());
}
return roleIds;
}
}