From cc5438a25896d19f02b652865781dc443b274354 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 28 十月 2026 14:26:23 +0800
Subject: [PATCH] feat(hrm): 员工档案与系统账号双向同步
---
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java | 92 ++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 84 insertions(+), 8 deletions(-)
diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
index 9f46412..1098246 100644
--- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
+++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
@@ -20,6 +20,9 @@
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserSyncReqDTO;
+import cn.iocoder.yudao.module.system.api.user.event.AdminUserDeletedEvent;
+import cn.iocoder.yudao.module.system.api.user.event.AdminUserUpdatedEvent;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
@@ -38,6 +41,7 @@
import jakarta.annotation.Resource;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@@ -55,7 +59,7 @@
/**
* 鍚庡彴鐢ㄦ埛 Service 瀹炵幇绫�
*
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
*/
@Service("adminUserService")
@Slf4j
@@ -91,6 +95,9 @@
@Resource
private AdminUserProducer adminUserProducer;
+
+ @Resource
+ private ApplicationEventPublisher eventPublisher;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -172,6 +179,11 @@
// 3. 璁板綍鎿嶄綔鏃ュ織涓婁笅鏂�
LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(oldUser, UserSaveReqVO.class));
LogRecordContext.putVariable("user", oldUser);
+
+ // 4. 閫氱煡 hrm 妯″潡鍚屾鍛樺伐妗f锛堟病鏈夊憳宸ユ。妗堢殑璐﹀彿浼氳鐩戝惉鍣ㄧ洿鎺ヨ烦杩囷級
+ eventPublisher.publishEvent(new AdminUserUpdatedEvent(updateReqVO.getId(),
+ updateReqVO.getNickname(), updateReqVO.getMobile(), updateReqVO.getDeptId(),
+ updateReqVO.getSex(), updateReqVO.getEmail()));
}
private void updateUserPost(UserSaveReqVO reqVO, AdminUserDO updateObj) {
@@ -197,6 +209,7 @@
}
@Override
+ @Transactional(rollbackFor = Exception.class)
public void updateUserProfile(Long id, UserProfileUpdateReqVO reqVO) {
// 1. 鏍¢獙姝g‘鎬�
AdminUserDO oldUser = validateUserExists(id);
@@ -208,6 +221,10 @@
// 3. 鏄电О / 澶村儚鍙樺寲鏃讹紝鍙戦�佹秷鎭緵涓嬫父璁㈤槄锛堝 IM 妯″潡鎺� FRIEND_INFO_UPDATED锛�
publishUserProfileUpdatedIfChanged(oldUser, reqVO.getNickname(), reqVO.getAvatar());
+
+ // 4. 閫氱煡 hrm 妯″潡鍚屾鍛樺伐妗f锛堟病鏈夊憳宸ユ。妗堢殑璐﹀彿浼氳鐩戝惉鍣ㄧ洿鎺ヨ烦杩囷級
+ eventPublisher.publishEvent(new AdminUserUpdatedEvent(id,
+ reqVO.getNickname(), reqVO.getMobile(), null, reqVO.getSex(), reqVO.getEmail()));
}
/**
@@ -273,6 +290,18 @@
@LogRecord(type = SYSTEM_USER_TYPE, subType = SYSTEM_USER_DELETE_SUB_TYPE, bizNo = "{{#id}}",
success = SYSTEM_USER_DELETE_SUCCESS)
public void deleteUser(Long id) {
+ doDeleteUser(id);
+ // 閫氱煡 hrm 妯″潡鑱斿姩鍒犻櫎鍛樺伐妗f锛堟病鏈夊憳宸ユ。妗堢殑璐﹀彿浼氳鐩戝惉鍣ㄧ洿鎺ヨ烦杩囷級
+ eventPublisher.publishEvent(new AdminUserDeletedEvent(id));
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void deleteUserSilently(Long id) {
+ doDeleteUser(id);
+ }
+
+ private void doDeleteUser(Long id) {
// 1. 鏍¢獙鐢ㄦ埛瀛樺湪
AdminUserDO user = validateUserExists(id);
@@ -298,6 +327,44 @@
permissionService.processUserDeleted(id);
userPostMapper.deleteByUserId(id);
});
+
+ // 3. 閫氱煡 hrm 妯″潡鑱斿姩鍒犻櫎鍛樺伐妗f
+ ids.forEach(id -> eventPublisher.publishEvent(new AdminUserDeletedEvent(id)));
+ }
+
+ @Override
+ public void updateUserForEmployee(AdminUserSyncReqDTO reqDTO) {
+ if (reqDTO == null || reqDTO.getUserId() == null) {
+ return;
+ }
+ // 1. 鏍¢獙鐢ㄦ埛瀛樺湪
+ AdminUserDO user = validateUserExists(reqDTO.getUserId());
+ // 2. 鏍¢獙鎵嬫満鍙� / 閭鍞竴锛堜笌鏃у�间笉鍚屾椂鎵嶆牎楠岋級
+ if (StrUtil.isNotBlank(reqDTO.getMobile()) && !ObjUtil.equal(user.getMobile(), reqDTO.getMobile())) {
+ validateMobileUnique(reqDTO.getUserId(), reqDTO.getMobile());
+ }
+ if (StrUtil.isNotBlank(reqDTO.getEmail()) && !ObjUtil.equal(user.getEmail(), reqDTO.getEmail())) {
+ validateEmailUnique(reqDTO.getUserId(), reqDTO.getEmail());
+ }
+ // 3. 鍙洿鏂伴潪绌哄瓧娈�
+ AdminUserDO updateObj = new AdminUserDO();
+ updateObj.setId(reqDTO.getUserId());
+ if (StrUtil.isNotBlank(reqDTO.getNickname())) {
+ updateObj.setNickname(reqDTO.getNickname());
+ }
+ if (StrUtil.isNotBlank(reqDTO.getMobile())) {
+ updateObj.setMobile(reqDTO.getMobile());
+ }
+ if (reqDTO.getDeptId() != null) {
+ updateObj.setDeptId(reqDTO.getDeptId());
+ }
+ if (reqDTO.getSex() != null) {
+ updateObj.setSex(reqDTO.getSex());
+ }
+ if (StrUtil.isNotBlank(reqDTO.getEmail())) {
+ updateObj.setEmail(reqDTO.getEmail());
+ }
+ userMapper.updateById(updateObj);
}
@Override
@@ -512,11 +579,8 @@
if (CollUtil.isEmpty(importUsers)) {
throw exception(USER_IMPORT_LIST_IS_EMPTY);
}
- // 1.2 鍒濆鍖栧瘑鐮佷笉鑳戒负绌�
+ // 1.2 绯荤粺鍙傛暟涓殑鍒濆瀵嗙爜锛屼綔涓� Excel 鏈~鍐欏瘑鐮佹椂鐨勫厹搴�
String initPassword = configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY);
- if (StrUtil.isEmpty(initPassword)) {
- throw exception(USER_IMPORT_INIT_PASSWORD);
- }
// 2. 閬嶅巻锛岄�愪釜鍒涘缓 or 鏇存柊
UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>())
@@ -524,11 +588,20 @@
AtomicInteger index = new AtomicInteger(1);
importUsers.forEach(importUser -> {
int currentIndex = index.getAndIncrement();
+ String key = StrUtil.blankToDefault(importUser.getUsername(), "绗� " + currentIndex + " 琛�");
+ // 2.1.0 瑙f瀽璇ヨ鍒濆瀵嗙爜锛欵xcel 濉啓浜嗗氨鐢ㄥ~鍐欑殑锛屽惁鍒欒鍙栫郴缁熷弬鏁�
+ String effectivePassword = StrUtil.isNotBlank(importUser.getPassword())
+ ? importUser.getPassword() : initPassword;
+ if (StrUtil.isBlank(effectivePassword)) {
+ respVO.getFailureUsernames().put(key,
+ "鏈~鍐欏垵濮嬪瘑鐮侊紝涓旂郴缁熷弬鏁� system.user.init-password 鏈厤缃��"
+ + "璇峰湪 Excel 涓~鍐欒琛屽垵濮嬪瘑鐮侊紝鎴栧厛鍦ㄥ弬鏁拌缃腑閰嶇疆 system.user.init-password");
+ return;
+ }
// 2.1.1 鏍¢獙瀛楁鏄惁绗﹀悎瑕佹眰
try {
- ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(initPassword));
+ ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(effectivePassword));
} catch (ConstraintViolationException ex) {
- String key = StrUtil.blankToDefault(importUser.getUsername(), "绗� " + currentIndex + " 琛�");
respVO.getFailureUsernames().put(key, ex.getMessage());
return;
}
@@ -545,7 +618,7 @@
AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername());
if (existUser == null) {
userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class)
- .setPassword(encodePassword(initPassword)).setPostIds(new HashSet<>())); // 璁剧疆榛樿瀵嗙爜鍙婄┖宀椾綅缂栧彿鏁扮粍
+ .setPassword(encodePassword(effectivePassword)).setPostIds(new HashSet<>())); // 璁剧疆鍒濆瀵嗙爜鍙婄┖宀椾綅缂栧彿鏁扮粍
respVO.getCreateUsernames().add(importUser.getUsername());
return;
}
@@ -556,6 +629,9 @@
}
AdminUserDO updateUser = BeanUtils.toBean(importUser, AdminUserDO.class);
updateUser.setId(existUser.getId());
+ // 瀵嗙爜鍗曠嫭澶勭悊锛欵xcel 濉啓浜嗘墠鏇存柊锛涙湭濉啓鍒欑疆绌猴紝淇濇寔鍘熷瘑鐮佷笉鍙橈紙閬垮厤鎶婃槑鏂囧啓杩涘簱锛�
+ updateUser.setPassword(StrUtil.isNotBlank(importUser.getPassword())
+ ? encodePassword(importUser.getPassword()) : null);
userMapper.updateById(updateUser);
respVO.getUpdateUsernames().add(importUser.getUsername());
});
--
Gitblit v1.9.3