From 061cac2d75b9abe60cd29a5e9abae0e8e630739f Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@local>
Date: 星期五, 06 十一月 2026 15:46:10 +0800
Subject: [PATCH] fix(hrm): 调岗提交不再强制要求 processDefinitionKey,并在审批通过后自动同步员工档案
---
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 161 insertions(+), 19 deletions(-)
diff --git a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java
index 71d74fe..b5b1c74 100644
--- a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java
+++ b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java
@@ -2,6 +2,8 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
@@ -12,11 +14,14 @@
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationSaveReqVO;
+import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmTransferApplicationDO;
+import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmTransferApplicationMapper;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum;
import cn.iocoder.yudao.module.hrm.enums.HrmTransferTypeEnum;
+import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.PostApi;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
@@ -33,6 +38,7 @@
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -48,7 +54,7 @@
/**
* 璋冨矖鐢宠 Service 瀹炵幇绫�
*
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
*/
@Service
@Validated
@@ -66,6 +72,8 @@
@Resource
private AdminUserApi adminUserApi;
@Resource
+ private HrmEmployeeService employeeService;
+ @Resource
private BpmProcessInstanceApi bpmProcessInstanceApi;
@Resource
private BpmCategoryService bpmCategoryService;
@@ -77,25 +85,60 @@
@Override
@Transactional(rollbackFor = Exception.class)
public Long createTransferApplication(HrmTransferApplicationSaveReqVO createReqVO) {
- // 1. 鏍¢獙鐩爣閮ㄩ棬涓庡師閮ㄩ棬涓嶈兘鐩稿悓
+ // 1. 瑙f瀽鐢宠浜� & 琛ュ叏鍘熼儴闂�/鍘熷矖浣�
+ // 鍓嶇琛ㄥ崟鍙鐢ㄦ埛閫夋嫨銆岃璋冨矖鍛樺伐銆嶏紝涓嶇洿鎺ユ彁浜� currentDeptId/currentPostId锛�
+ // 鑰岃繖涓や釜瀛楁鍦ㄥ簱閲屾槸 NOT NULL锛岀己鐪佷細鐩存帴鎶� SQL 寮傚父锛團ield 'current_dept_id' doesn't have a default value锛�
+ Long userId = createReqVO.getUserId();
+ Long employeeId = createReqVO.getEmployeeId();
+ HrmEmployeeDO employee = null;
+ if (employeeId != null) {
+ employee = employeeService.getEmployee(employeeId);
+ }
+ if (employee == null && userId != null) {
+ // 绾﹀畾 user_id 瀛� system_users.id锛涗絾鍘嗗彶涓婂墠绔篃鏈夋妸銆屽憳宸ヤ富閿�嶅杩� userId 鐨勬儏鍐碉紝涓ょ閮藉吋瀹�
+ employee = employeeService.getEmployeeByUserId(userId);
+ if (employee == null) {
+ employee = employeeService.getEmployee(userId);
+ }
+ }
+ // 浠ュ憳宸ユ。妗堜笂鐨� user_id 涓哄噯锛堝墠绔彲鑳芥妸鍛樺伐涓婚敭濉炲湪 userId 瀛楁閲岋紝鐩存帴鐢ㄤ細鍐欓敊鐢宠浜猴級
+ if (employee != null && employee.getUserId() != null) {
+ userId = employee.getUserId();
+ }
+ if (userId == null && employee != null) {
+ userId = employee.getUserId();
+ }
+ if (userId == null) {
+ userId = getCurrentUserId();
+ if (employee == null) {
+ employee = employeeService.getEmployeeByUserId(userId);
+ }
+ }
+ if (adminUserApi.getUser(userId) == null) {
+ userId = getCurrentUserId();
+ }
+ if (createReqVO.getCurrentDeptId() == null && employee != null) {
+ createReqVO.setCurrentDeptId(employee.getDeptId());
+ }
+ if (createReqVO.getCurrentPostId() == null && employee != null) {
+ createReqVO.setCurrentPostId(parseFirstPostId(employee.getPostIds()));
+ }
+ if (createReqVO.getCurrentDeptId() == null) {
+ throw exception(TRANSFER_APPLICATION_CURRENT_DEPT_REQUIRED);
+ }
+ if (createReqVO.getCurrentPostId() == null) {
+ throw exception(TRANSFER_APPLICATION_CURRENT_POST_REQUIRED);
+ }
+ // 2. 鏍¢獙鐩爣閮ㄩ棬涓庡師閮ㄩ棬涓嶈兘鐩稿悓锛堟斁鍦ㄨˉ鍏ㄤ箣鍚庯紝鍚﹀垯琛ュ叏鍑烘潵鐨勫師閮ㄩ棬缁曡繃浜嗘牎楠岋級
if (Objects.equals(createReqVO.getTargetDeptId(), createReqVO.getCurrentDeptId())) {
throw exception(TRANSFER_APPLICATION_SAME_DEPT);
}
- // 2. 鏍¢獙鐩爣宀椾綅涓庡師宀椾綅涓嶈兘鐩稿悓
+ // 3. 鏍¢獙鐩爣宀椾綅涓庡師宀椾綅涓嶈兘鐩稿悓
if (Objects.equals(createReqVO.getTargetPostId(), createReqVO.getCurrentPostId())) {
throw exception(TRANSFER_APPLICATION_SAME_POST);
}
- // 3. 鐢熸垚鐢宠鍗曞彿
+ // 4. 鐢熸垚鐢宠鍗曞彿
String no = generateUniqueNo();
- // 4. 璁剧疆鐢宠浜轰俊鎭�
- Long userId = createReqVO.getUserId();
- if (userId == null) {
- userId = getCurrentUserId();
- }
- AdminUserRespDTO user = adminUserApi.getUser(userId);
- if (user == null) {
- userId = getCurrentUserId();
- }
// 5. 鎻掑叆
HrmTransferApplicationDO application = BeanUtils.toBean(createReqVO, HrmTransferApplicationDO.class);
application.setNo(no);
@@ -105,6 +148,27 @@
storageAttachmentApi.bindAttachments("file", "hrm_transfer_application",
application.getId(), createReqVO.getBlobIds());
return application.getId();
+ }
+
+ /**
+ * 鍙栧憳宸ュ矖浣嶅垪琛ㄤ腑鐨勭涓�涓綔涓恒�屽師宀椾綅銆�
+ *
+ * <p>hrm_employee.post_ids 浠� JSON 鏁扮粍瀛楃涓插瓨鍌紝渚嬪 {@code [20,21]}
+ *
+ * @param postIds 宀椾綅缂栧彿 JSON 瀛楃涓�
+ * @return 绗竴涓矖浣嶇紪鍙凤紱瑙f瀽涓嶅嚭鏉ユ椂杩斿洖 {@code null}
+ */
+ private Long parseFirstPostId(String postIds) {
+ if (StrUtil.isBlank(postIds)) {
+ return null;
+ }
+ try {
+ List<Long> ids = JSONUtil.toList(postIds, Long.class);
+ return CollUtil.isEmpty(ids) ? null : ids.get(0);
+ } catch (Exception ex) {
+ log.warn("[parseFirstPostId][postIds({}) 瑙f瀽澶辫触]", postIds, ex);
+ return null;
+ }
}
private Long getCurrentUserId() {
@@ -168,9 +232,14 @@
}
HrmTransferApplicationRespVO respVO = BeanUtils.toBean(application, HrmTransferApplicationRespVO.class);
if (application.getUserId() != null) {
- AdminUserRespDTO user = adminUserApi.getUser(application.getUserId());
- if (user != null) {
- respVO.setUserName(user.getNickname());
+ // user_id 姝e父瀛� system_users.id锛涘巻鍙叉暟鎹篃鏈夊瓨銆屽憳宸ヤ富閿�嶇殑锛屼袱绉嶉兘鍏煎
+ HrmEmployeeDO employee = employeeService.getEmployeeByUserId(application.getUserId());
+ if (employee == null) {
+ employee = employeeService.getEmployee(application.getUserId());
+ }
+ if (employee != null) {
+ respVO.setUserName(employee.getName());
+ respVO.setEmployeeId(employee.getId());
}
}
fillDeptNames(respVO, application);
@@ -215,7 +284,17 @@
.map(HrmTransferApplicationDO::getUserId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
+ // user_id 姝e父瀛� system_users.id锛涘巻鍙叉暟鎹篃鏈夊瓨銆屽憳宸ヤ富閿�嶇殑锛屼袱绉嶉兘鍏煎
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
+ Map<Long, String> employeeNameMap = new HashMap<>();
+ for (Long userId : userIds) {
+ if (!userMap.containsKey(userId)) {
+ HrmEmployeeDO employee = employeeService.getEmployee(userId);
+ if (employee != null) {
+ employeeNameMap.put(userId, employee.getName());
+ }
+ }
+ }
Set<Long> deptIds = pageResult.getList().stream()
.flatMap(app -> Stream.of(app.getCurrentDeptId(), app.getTargetDeptId()))
.filter(Objects::nonNull)
@@ -228,8 +307,12 @@
Map<Long, PostRespDTO> postMap = postApi.getPostMap(postIds);
List<HrmTransferApplicationRespVO> list = pageResult.getList().stream().map(app -> {
HrmTransferApplicationRespVO vo = BeanUtils.toBean(app, HrmTransferApplicationRespVO.class);
- if (app.getUserId() != null && userMap.containsKey(app.getUserId())) {
- vo.setUserName(userMap.get(app.getUserId()).getNickname());
+ if (app.getUserId() != null) {
+ if (userMap.containsKey(app.getUserId())) {
+ vo.setUserName(userMap.get(app.getUserId()).getNickname());
+ } else if (employeeNameMap.containsKey(app.getUserId())) {
+ vo.setUserName(employeeNameMap.get(app.getUserId()));
+ }
}
if (app.getCurrentDeptId() != null && deptMap.containsKey(app.getCurrentDeptId())) {
vo.setCurrentDeptName(deptMap.get(app.getCurrentDeptId()).getName());
@@ -258,17 +341,40 @@
throw exception(TRANSFER_APPLICATION_SUBMIT_FAIL_NOT_DRAFT);
}
validateApproveCategoryAndProcessDefinition();
+ // 鍓嶇涓嶄紶娴佺▼瀹氫箟 Key 鏃讹紝鑷姩鍙栬瀹℃壒鍒嗙被涓嬫渶鏂扮増鏈殑娲昏穬娴佺▼锛�
+ // 閬垮厤鐩存帴鎶� MissingServletRequestParameterException 鎶涚粰鐢ㄦ埛
+ if (StrUtil.isBlank(processDefinitionKey)) {
+ processDefinitionKey = resolveActiveProcessDefinitionKey();
+ }
ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey);
if (processDefinition == null) {
throw exception(TRANSFER_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
}
String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO()
- .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id)));
+ .setProcessDefinitionKey(processDefinition.getKey()).setBusinessKey(String.valueOf(id)));
transferApplicationMapper.updateById(HrmTransferApplicationDO.builder()
.id(id)
.processInstanceId(processInstanceId)
.status(HrmAuditStatusEnum.PROCESS.getStatus())
.build());
+ }
+
+ /**
+ * 鍙栬皟宀楀鎵瑰垎绫讳笅鏈�鏂扮増鏈殑娲昏穬娴佺▼瀹氫箟 Key
+ */
+ private String resolveActiveProcessDefinitionKey() {
+ List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
+ .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE);
+ if (CollUtil.isEmpty(definitionInfoList)) {
+ throw exception(TRANSFER_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
+ }
+ Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId);
+ List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds);
+ return processDefinitions.stream()
+ .filter(pd -> !pd.isSuspended())
+ .max(Comparator.comparingInt(ProcessDefinition::getVersion))
+ .map(ProcessDefinition::getKey)
+ .orElseThrow(() -> exception(TRANSFER_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS));
}
private void validateApproveCategoryAndProcessDefinition() {
@@ -329,6 +435,42 @@
.status(status)
.approveTime(java.time.LocalDateTime.now())
.build());
+ // 瀹℃壒閫氳繃锛氭妸鍛樺伐妗f閲岀殑閮ㄩ棬/宀椾綅鍚屾鎴愯皟宀楀悗鐨勭洰鏍囧��
+ if (HrmAuditStatusEnum.APPROVE.getStatus().equals(status)) {
+ syncEmployeePosition(application);
+ }
+ }
+
+ /**
+ * 璋冨矖瀹℃壒閫氳繃鍚庯紝鎶婂憳宸ユ。妗堢殑閮ㄩ棬/宀椾綅鏇存柊涓虹洰鏍囬儴闂�/宀椾綅
+ *
+ * <p>璧� {@link HrmEmployeeService#updateEmployee} 鑰屼笉鏄洿鎺ユ敼琛紝
+ * 杩欐牱 system_users 涓婄殑閮ㄩ棬/宀椾綅鍏宠仈涔熶細涓�骞跺悓姝ワ紝鍛樺伐绠$悊鍒楄〃鑳界珛鍒荤湅鍒版柊宀椾綅銆�
+ */
+ private void syncEmployeePosition(HrmTransferApplicationDO application) {
+ if (application.getTargetDeptId() == null || application.getTargetPostId() == null) {
+ return;
+ }
+ try {
+ HrmEmployeeDO employee = employeeService.getEmployeeByUserId(application.getUserId());
+ if (employee == null) {
+ employee = employeeService.getEmployee(application.getUserId());
+ }
+ if (employee == null) {
+ log.warn("[syncEmployeePosition][璋冨矖鐢宠({}) 鎵句笉鍒板憳宸ユ。妗堬紝璺宠繃妗f鍚屾]", application.getId());
+ return;
+ }
+ HrmEmployeeSaveReqVO updateReqVO = BeanUtils.toBean(employee, HrmEmployeeSaveReqVO.class);
+ updateReqVO.setDeptId(application.getTargetDeptId());
+ // 璋冨矖鎸夊崟宀椾綅澶勭悊锛岀洿鎺ヨ惤鐩爣宀椾綅
+ updateReqVO.setPostIds(Collections.singletonList(application.getTargetPostId()));
+ employeeService.updateEmployee(updateReqVO);
+ log.info("[syncEmployeePosition][璋冨矖鐢宠({}) 宸插皢鍛樺伐({}) 閮ㄩ棬鏀逛负 {}銆佸矖浣嶆敼涓� {}]",
+ application.getId(), employee.getName(), application.getTargetDeptId(), application.getTargetPostId());
+ } catch (Exception ex) {
+ // 妗f鍚屾澶辫触锛堜緥濡傜洰鏍囬儴闂�/宀椾綅宸茶鍒犻櫎锛変笉搴旇闃绘柇瀹℃壒鐘舵�佽惤搴擄紝鍙褰曟棩蹇�
+ log.error("[syncEmployeePosition][璋冨矖鐢宠({}) 鍚屾鍛樺伐妗f澶辫触]", application.getId(), ex);
+ }
}
@Override
--
Gitblit v1.9.3