From 9494075e6d9d2a4b2b884b4ed740dee0c434734c Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@local>
Date: 星期二, 03 十一月 2026 11:48:44 +0800
Subject: [PATCH] fix(hrm): 修复薪酬核算写错 user_id 导致员工查不到工资条
---
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 153 insertions(+), 24 deletions(-)
diff --git a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java
index 8be05ab..7f1d49e 100644
--- a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java
+++ b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java
@@ -6,6 +6,8 @@
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryCalculationPageReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmLeaveApplicationDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmOvertimeApplicationDO;
+import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmEmployeeScheduleDO;
+import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmShiftDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmEmployeeSalaryDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmEmployeeSocialSecurityDO;
@@ -16,6 +18,8 @@
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSocialSecuritySchemeDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryStructureDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmLeaveApplicationMapper;
+import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmEmployeeScheduleMapper;
+import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmShiftMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmOvertimeApplicationMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmEmployeeSalaryMapper;
@@ -28,6 +32,9 @@
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryStructureMapper;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
+import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
+import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
+import lombok.extern.slf4j.Slf4j;
import cn.iocoder.yudao.module.hrm.enums.HrmOvertimeTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@@ -39,8 +46,12 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -53,6 +64,7 @@
*/
@Service
@Validated
+@Slf4j
public class HrmSalaryCalculationServiceImpl implements HrmSalaryCalculationService {
/**
@@ -83,6 +95,8 @@
@Resource
private DeptApi deptApi;
@Resource
+ private NotifyMessageSendApi notifyMessageSendApi;
+ @Resource
private HrmDeductionService deductionService;
@Resource
private HrmLeaveApplicationMapper leaveApplicationMapper;
@@ -96,10 +110,35 @@
private HrmSalaryPaymentMapper salaryPaymentMapper;
@Resource
private HrmSalaryPaymentDetailMapper salaryPaymentDetailMapper;
+ @Resource
+ private HrmEmployeeScheduleMapper employeeScheduleMapper;
+ @Resource
+ private HrmShiftMapper shiftMapper;
@Override
public PageResult<HrmSalaryCalculationDO> getSalaryCalculationPage(HrmSalaryCalculationPageReqVO pageReqVO) {
return salaryCalculationMapper.selectPage(pageReqVO);
+ }
+
+ @Override
+ public PageResult<HrmSalaryCalculationDO> getMySalaryCalculationPage(Long userId, HrmSalaryCalculationPageReqVO pageReqVO) {
+ // 寮哄埗闄愬畾涓哄綋鍓嶇櫥褰曚汉锛氬拷鐣ュ墠绔紶鍏ョ殑 userId / deptId锛岄槻姝㈣秺鏉冩煡鐪嬩粬浜哄伐璧�
+ pageReqVO.setUserId(userId);
+ pageReqVO.setDeptId(null);
+ return salaryCalculationMapper.selectPage(pageReqVO);
+ }
+
+ @Override
+ public HrmSalaryCalculationDO getMySalaryCalculation(Long id, Long userId) {
+ HrmSalaryCalculationDO calculation = salaryCalculationMapper.selectById(id);
+ if (calculation == null) {
+ return null;
+ }
+ // 涓嶅睘浜庢湰浜虹殑璁板綍涓�寰嬭涓轰笉瀛樺湪锛岄伩鍏嶉�氳繃鐚� id 瓒婃潈鏌ョ湅浠栦汉宸ヨ祫
+ if (!java.util.Objects.equals(calculation.getUserId(), userId)) {
+ return null;
+ }
+ return calculation;
}
@Override
@@ -130,10 +169,17 @@
// 2. 閬嶅巻鍛樺伐杩涜钖叕鏍哥畻
for (HrmEmployeeDO employee : employees) {
- // employeeId 鏄憳宸D锛岀敤浜庡叧鑱旇柂閰。妗堛�佺ぞ淇濇。妗堛�佹牳绠楄褰�
+ // employeeId 鏄憳宸ユ。妗堜富閿紙hrm_employee.id锛夛紝浠呯敤浜庡叧鑱旀帓鐝�
+ // userId 鏄郴缁熻处鍙� ID锛坰ystem_users.id锛夛紝鏍哥畻璁板綍 / 钖叕妗f / 绀句繚妗f
+ // 鍏ㄩ儴鎸夊畠鍏宠仈 鈥斺�� 鍛樺伐鐧诲綍鍚庢煡銆屾垜鐨勮柂璧勩�嶇敤鐨勪篃鏄繖涓� ID
Long employeeId = employee.getId();
- // 2.1 妫�鏌ヨ鍛樺伐褰撴湀鏄惁宸插瓨鍦ㄦ牳绠楄褰曪紙鎸夊憳宸D鏌ヨ锛�
- List<HrmSalaryCalculationDO> existCalculations = salaryCalculationMapper.selectListByUserIdAndPeriod(employeeId, period);
+ Long userId = employee.getUserId();
+ if (userId == null) {
+ log.warn("[calculateSalary] 鍛樺伐({}) 鏈叧鑱旂郴缁熻处鍙凤紝璺宠繃鏍哥畻", employee.getName());
+ continue;
+ }
+ // 2.1 妫�鏌ヨ鍛樺伐褰撴湀鏄惁宸插瓨鍦ㄦ牳绠楄褰曪紙鎸夌郴缁熻处鍙� ID 鏌ヨ锛�
+ List<HrmSalaryCalculationDO> existCalculations = salaryCalculationMapper.selectListByUserIdAndPeriod(userId, period);
if (CollUtil.isNotEmpty(existCalculations)) {
// 濡傛灉瀛樺湪澶氭潯璁板綍锛屾彁绀哄紓甯告暟鎹�
if (existCalculations.size() > 1) {
@@ -145,15 +191,15 @@
}
}
- // 2.2 鑾峰彇鍛樺伐钖叕妗f锛堟寜鍛樺伐ID鏌ヨ锛�
- HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(employeeId);
+ // 2.2 鑾峰彇鍛樺伐钖叕妗f锛堟寜绯荤粺璐﹀彿 ID 鏌ヨ锛�
+ HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(userId);
if (employeeSalary == null) {
// 濡傛灉娌℃湁钖叕妗f锛屼娇鐢ㄥ憳宸ヨ〃涓殑鍩烘湰宸ヨ祫鍒涘缓榛樿鏁版嵁
- employeeSalary = buildDefaultEmployeeSalary(employeeId, employee);
+ employeeSalary = buildDefaultEmployeeSalary(userId, employee);
}
- // 2.3 鑾峰彇鍛樺伐绀句繚鍏Н閲戞。妗堬紙鎸夊憳宸D鏌ヨ锛屽彇鏈�鏂扮敓鏁堢殑锛�
- HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(employeeId);
+ // 2.3 鑾峰彇鍛樺伐绀句繚鍏Н閲戞。妗堬紙鎸夌郴缁熻处鍙� ID 鏌ヨ锛屽彇鏈�鏂扮敓鏁堢殑锛�
+ HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(userId);
// 2.4 濡傛灉鏈夌ぞ淇濇。妗堬紝鑾峰彇缁戝畾鐨勬柟妗�
HrmSocialSecuritySchemeDO scheme = null;
if (socialSecurity != null && socialSecurity.getSocialSecuritySchemeId() != null) {
@@ -161,7 +207,7 @@
}
// 2.5 鏋勫缓鏍哥畻璁板綍锛堝寘鍚墸闄よ绠楋紝閫氳繃鏂规鑾峰彇姣斾緥锛�
- HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, employeeId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
+ HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, userId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
// 2.6 淇濆瓨锛氶娆$敓鎴愬崟鍙凤紱宸叉湁寰呯‘璁よ褰曞垯淇濈暀鍗曞彿骞惰鐩栭噸绠楃粨鏋�
if (CollUtil.isNotEmpty(existCalculations)) {
@@ -192,11 +238,13 @@
/**
* 鏋勫缓榛樿鐨勫憳宸ヨ柂閰。妗堛�傚熀鏈伐璧勭粺涓�鍙栧憳宸ユ。妗堬紝缁╂晥宸ヨ祫榛樿涓嶈嚜鍔ㄧ敓鎴愩��
+ *
+ * @param userId 绯荤粺璐﹀彿 ID锛坰ystem_users.id锛夛紝涓嶆槸 hrm_employee.id
*/
- private HrmEmployeeSalaryDO buildDefaultEmployeeSalary(Long employeeId, HrmEmployeeDO employee) {
+ private HrmEmployeeSalaryDO buildDefaultEmployeeSalary(Long userId, HrmEmployeeDO employee) {
BigDecimal baseSalary = employee.getBaseSalary() != null ? employee.getBaseSalary() : BigDecimal.ZERO;
return HrmEmployeeSalaryDO.builder()
- .userId(employeeId) // 浣跨敤鍛樺伐ID
+ .userId(userId)
.salaryStructureId(employee.getSalaryStructureId())
.baseSalary(baseSalary)
.performanceSalary(BigDecimal.ZERO)
@@ -210,8 +258,10 @@
/**
* 鏋勫缓钖叕鏍哥畻璁板綍锛堝寘鍚ぞ淇濄�佸叕绉噾銆佷釜绋庛�佽鍋囨墸闄よ绠楋級
* 閫氳繃鍛樺伐绀句繚妗f缁戝畾鐨勬柟妗堣幏鍙栫即绾虫瘮渚�
+ *
+ * @param userId 绯荤粺璐﹀彿 ID锛坰ystem_users.id锛夛紝鍐欏叆鏍哥畻璁板綍鐨� user_id
*/
- private HrmSalaryCalculationDO buildSalaryCalculation(String period, Long employeeId, HrmEmployeeDO employee,
+ private HrmSalaryCalculationDO buildSalaryCalculation(String period, Long userId, HrmEmployeeDO employee,
HrmEmployeeSalaryDO employeeSalary,
HrmEmployeeSocialSecurityDO socialSecurity,
HrmSocialSecuritySchemeDO scheme,
@@ -224,6 +274,12 @@
BigDecimal otherAllowance = employeeSalary.getOtherAllowance() != null ? employeeSalary.getOtherAllowance() : BigDecimal.ZERO;
OvertimeSalary overtimeSalary = calculateOvertimeSalary(employee, employeeSalary, period, baseSalary);
BigDecimal overtimePay = overtimeSalary.pay();
+
+ // 鎸夋帓鐝疮璁″嚭鍕ゅぉ鏁颁笌鎺掔彮鎬诲伐鏃讹紙鏃犳帓鐝椂鍑哄嫟澶╂暟鍥為��榛樿 22锛�
+ // 娉ㄦ剰锛氭帓鐝〃鎸夊憳宸ユ。妗� ID锛坔rm_employee.id锛夊叧鑱旓紝涓庢牳绠楄褰曠殑 userId 涓嶅悓
+ ScheduleStat scheduleStat = calculateScheduleStat(employee.getId(), period);
+ Integer workDays = scheduleStat.scheduledDays() > 0 ? scheduleStat.scheduledDays() : 22;
+ BigDecimal totalWorkHours = scheduleStat.totalWorkHours();
// 搴斿彂宸ヨ祫鎬婚
BigDecimal totalIncome = baseSalary.add(performanceSalary).add(mealAllowance)
@@ -281,7 +337,7 @@
return HrmSalaryCalculationDO.builder()
.period(period)
- .userId(employeeId) // 浣跨敤鍛樺伐ID
+ .userId(userId)
.deptId(employee.getDeptId())
.baseSalary(baseSalary)
.performanceSalary(performanceSalary)
@@ -295,8 +351,9 @@
.otherDeduction(otherDeduction)
.leaveDeduction(leaveDeduction)
.actualSalary(actualSalary)
- .workDays(22) // 榛樿宸ヤ綔鏃�
+ .workDays(workDays) // 鎸夋帓鐝疮璁″簲鍑哄嫟澶╂暟锛屾棤鎺掔彮鏃跺洖閫� 22
.overtimeHours(overtimeSalary.hours())
+ .totalWorkHours(totalWorkHours)
.status(0) // 寰呯‘璁�
.build();
}
@@ -401,6 +458,37 @@
private record OvertimeSalary(BigDecimal hours, BigDecimal pay) {}
+ private record ScheduleStat(int scheduledDays, BigDecimal totalWorkHours) {}
+
+ /**
+ * 鎸夋帓鐝〃绱鍛樺伐鐨勫嚭鍕ゅぉ鏁颁笌鎺掔彮鎬诲伐鏃躲��
+ * 浠呯粺璁℃湁鐝鐨勬帓鐝褰曪紝鐝鏍囧噯宸ユ椂涔嬪拰鍗充负鎺掔彮宸ユ椂銆�
+ */
+ private ScheduleStat calculateScheduleStat(Long employeeId, String period) {
+ YearMonth yearMonth = YearMonth.parse(period);
+ List<HrmEmployeeScheduleDO> schedules = employeeScheduleMapper.selectListByEmployeeIdAndPeriod(
+ employeeId, yearMonth.atDay(1), yearMonth.atEndOfMonth());
+ if (CollUtil.isEmpty(schedules)) {
+ return new ScheduleStat(0, BigDecimal.ZERO);
+ }
+ Set<Long> shiftIds = schedules.stream().map(HrmEmployeeScheduleDO::getShiftId)
+ .filter(Objects::nonNull).collect(Collectors.toSet());
+ Map<Long, HrmShiftDO> shiftMap = CollUtil.isEmpty(shiftIds) ? Collections.emptyMap()
+ : shiftMapper.selectBatchIds(shiftIds).stream()
+ .collect(Collectors.toMap(HrmShiftDO::getId, shift -> shift, (a, b) -> a));
+ int scheduledDays = 0;
+ BigDecimal totalWorkHours = BigDecimal.ZERO;
+ for (HrmEmployeeScheduleDO schedule : schedules) {
+ HrmShiftDO shift = schedule.getShiftId() == null ? null : shiftMap.get(schedule.getShiftId());
+ if (shift == null) {
+ continue;
+ }
+ scheduledDays++;
+ totalWorkHours = totalWorkHours.add(shift.getWorkHours() != null ? shift.getWorkHours() : BigDecimal.ZERO);
+ }
+ return new ScheduleStat(scheduledDays, totalWorkHours.setScale(2, RoundingMode.HALF_UP));
+ }
+
@Override
@Transactional(rollbackFor = Exception.class)
public void confirmSalaryCalculation(List<Long> ids) {
@@ -422,6 +510,8 @@
salaryCalculationMapper.updateById(salaryCalculation);
// 璁板綍鍛ㄦ湡
period = salaryCalculation.getPeriod();
+ // 纭鍚庣粰璇ュ憳宸ュ彂閫佸伐璧勬潯閫氱煡锛堝け璐ヤ笉褰卞搷纭涓绘祦绋嬶級
+ sendPayslipNotice(salaryCalculation);
}
// 妫�鏌ヨ鍛ㄦ湡鏄惁鎵�鏈夋牳绠楄褰曢兘宸茬‘璁わ紝鑷姩鐢熸垚鍙戞斁鍙拌处
@@ -433,6 +523,39 @@
/**
* 妫�鏌ヨ鍛ㄦ湡鏄惁鎵�鏈夋牳绠楄褰曢兘宸茬‘璁わ紝濡傛灉鍏ㄩ儴纭鍒欒嚜鍔ㄧ敓鎴愬彂鏀惧彴璐�
*/
+ /**
+ * 宸ヨ祫鏉¢�氱煡鐨勭珯鍐呬俊妯℃澘缂栫爜
+ *
+ * 瀵瑰簲 system_notify_template.code = salary_payslip_notice
+ */
+ private static final String TEMPLATE_CODE_PAYSLIP = "salary_payslip_notice";
+
+ /**
+ * 缁欏憳宸ュ彂閫佸伐璧勬潯閫氱煡锛圓PP 绔欏唴淇★級
+ *
+ * 閫氱煡澶辫触鍙鏃ュ織锛屼笉褰卞搷鏍哥畻纭涓绘祦绋�
+ */
+ private void sendPayslipNotice(HrmSalaryCalculationDO calculation) {
+ if (calculation == null || calculation.getUserId() == null) {
+ return;
+ }
+ try {
+ NotifySendSingleToUserReqDTO reqDTO = new NotifySendSingleToUserReqDTO();
+ reqDTO.setUserId(calculation.getUserId());
+ reqDTO.setTemplateCode(TEMPLATE_CODE_PAYSLIP);
+ Map<String, Object> params = new HashMap<>();
+ params.put("period", calculation.getPeriod());
+ params.put("actualSalary", calculation.getActualSalary());
+ reqDTO.setTemplateParams(params);
+ notifyMessageSendApi.sendSingleMessageToAdmin(reqDTO);
+ log.info("[sendPayslipNotice] 宸ヨ祫鏉¢�氱煡宸插彂閫侊紝userId={}, period={}",
+ calculation.getUserId(), calculation.getPeriod());
+ } catch (Exception e) {
+ log.warn("[sendPayslipNotice] 宸ヨ祫鏉¢�氱煡鍙戦�佸け璐ワ紝userId={}, period={}",
+ calculation.getUserId(), calculation.getPeriod(), e);
+ }
+ }
+
private void autoGeneratePaymentIfAllConfirmed(String period) {
// 鏌ヨ璇ュ懆鏈熸墍鏈夋牳绠楄褰�
List<HrmSalaryCalculationDO> allCalculations = salaryCalculationMapper.selectListByPeriod(period);
@@ -484,17 +607,23 @@
// 2. 閬嶅巻鍛樺伐瀹炴椂璁$畻钖叕
List<HrmSalaryCalculationDO> resultList = new java.util.ArrayList<>();
for (HrmEmployeeDO employee : employees) {
- Long employeeId = employee.getId();
-
- // 2.1 鑾峰彇鍛樺伐钖叕妗f锛堟寜鍛樺伐ID鏌ヨ锛�
- HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(employeeId);
- if (employeeSalary == null) {
- // 濡傛灉娌℃湁钖叕妗f锛屼娇鐢ㄥ憳宸ヨ〃涓殑鍩烘湰宸ヨ祫鍒涘缓榛樿鏁版嵁
- employeeSalary = buildDefaultEmployeeSalary(employeeId, employee);
+ // userId 鏄郴缁熻处鍙� ID锛坰ystem_users.id锛夛紝钖叕妗f / 绀句繚妗f鎸夊畠鍏宠仈锛�
+ // 鎺掔彮琛ㄥ垯鎸� hrm_employee.id 鍏宠仈锛屼袱鑰呬笉鍚�
+ Long userId = employee.getUserId();
+ if (userId == null) {
+ log.warn("[previewSalaryCalculation] 鍛樺伐({}) 鏈叧鑱旂郴缁熻处鍙凤紝璺宠繃", employee.getName());
+ continue;
}
- // 2.2 鑾峰彇鍛樺伐绀句繚鍏Н閲戞。妗堬紙鎸夊憳宸D鏌ヨ锛屽彇鏈�鏂扮敓鏁堢殑锛�
- HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(employeeId);
+ // 2.1 鑾峰彇鍛樺伐钖叕妗f锛堟寜绯荤粺璐﹀彿 ID 鏌ヨ锛�
+ HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(userId);
+ if (employeeSalary == null) {
+ // 濡傛灉娌℃湁钖叕妗f锛屼娇鐢ㄥ憳宸ヨ〃涓殑鍩烘湰宸ヨ祫鍒涘缓榛樿鏁版嵁
+ employeeSalary = buildDefaultEmployeeSalary(userId, employee);
+ }
+
+ // 2.2 鑾峰彇鍛樺伐绀句繚鍏Н閲戞。妗堬紙鎸夌郴缁熻处鍙� ID 鏌ヨ锛屽彇鏈�鏂扮敓鏁堢殑锛�
+ HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(userId);
// 2.3 濡傛灉鏈夌ぞ淇濇。妗堬紝鑾峰彇缁戝畾鐨勬柟妗�
HrmSocialSecuritySchemeDO scheme = null;
if (socialSecurity != null && socialSecurity.getSocialSecuritySchemeId() != null) {
@@ -502,7 +631,7 @@
}
// 2.4 鏋勫缓鏍哥畻璁板綍锛堝疄鏃惰绠楋紝涓嶅瓨鍌級
- HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, employeeId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
+ HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, userId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
resultList.add(calculation);
}
return resultList;
--
Gitblit v1.9.3