From 836a063ba1f98360f3f424ba7db10943e80b6232 Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@local>
Date: 星期五, 06 十一月 2026 14:38:57 +0800
Subject: [PATCH] fix(hrm): 调岗申请人/员工档案查询兼容 user_id 双口径

---
 yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java |  295 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 254 insertions(+), 41 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 8279b4e..eeee784 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
@@ -5,6 +5,9 @@
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 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;
@@ -13,7 +16,11 @@
 import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDetailDO;
 import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDO;
 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;
 import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmEmployeeSocialSecurityMapper;
@@ -22,8 +29,13 @@
 import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryPaymentDetailMapper;
 import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryPaymentMapper;
 import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSocialSecuritySchemeMapper;
+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;
 import org.springframework.transaction.annotation.Transactional;
@@ -34,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;
@@ -48,6 +64,7 @@
  */
 @Service
 @Validated
+@Slf4j
 public class HrmSalaryCalculationServiceImpl implements HrmSalaryCalculationService {
 
     /**
@@ -78,19 +95,50 @@
     @Resource
     private DeptApi deptApi;
     @Resource
+    private NotifyMessageSendApi notifyMessageSendApi;
+    @Resource
     private HrmDeductionService deductionService;
     @Resource
     private HrmLeaveApplicationMapper leaveApplicationMapper;
+    @Resource
+    private HrmOvertimeApplicationMapper overtimeApplicationMapper;
+    @Resource
+    private HrmSalaryStructureMapper salaryStructureMapper;
     @Resource
     private HrmLeaveTypeConfigMapper leaveTypeConfigMapper;
     @Resource
     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
@@ -121,27 +169,37 @@
 
         // 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) {
                     throw exception(SALARY_CALCULATION_DUPLICATE, employee.getName(), period);
                 }
-                continue; // 宸插瓨鍦ㄥ垯璺宠繃
+                // 宸茬‘璁ゆ垨宸插彂鏀剧殑璁板綍涓嶅彲鑷姩鏀瑰啓锛涘緟纭璁板綍鍏佽閲嶇畻锛屼互鍚告敹鍚庢潵瀹℃壒閫氳繃鐨勫姞鐝��
+                if (existCalculations.get(0).getStatus() != 0) {
+                    continue;
+                }
             }
 
-            // 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) {
@@ -149,17 +207,22 @@
             }
 
             // 2.5 鏋勫缓鏍哥畻璁板綍锛堝寘鍚墸闄よ绠楋紝閫氳繃鏂规鑾峰彇姣斾緥锛�
-            HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, employeeId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
+            HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, userId, employee, employeeSalary, socialSecurity, scheme, effectiveDate);
 
-            // 2.6 鐢熸垚鏍哥畻鍗曞彿
-            String no = noRedisDAO.generateSalaryCalculationNo();
-            while (salaryCalculationMapper.selectByNo(no) != null) {
-                no = noRedisDAO.generateSalaryCalculationNo();
+            // 2.6 淇濆瓨锛氶娆$敓鎴愬崟鍙凤紱宸叉湁寰呯‘璁よ褰曞垯淇濈暀鍗曞彿骞惰鐩栭噸绠楃粨鏋�
+            if (CollUtil.isNotEmpty(existCalculations)) {
+                HrmSalaryCalculationDO existing = existCalculations.get(0);
+                calculation.setId(existing.getId());
+                calculation.setNo(existing.getNo());
+                salaryCalculationMapper.updateById(calculation);
+            } else {
+                String no = noRedisDAO.generateSalaryCalculationNo();
+                while (salaryCalculationMapper.selectByNo(no) != null) {
+                    no = noRedisDAO.generateSalaryCalculationNo();
+                }
+                calculation.setNo(no);
+                salaryCalculationMapper.insert(calculation);
             }
-            calculation.setNo(no);
-
-            // 2.7 淇濆瓨鏍哥畻璁板綍
-            salaryCalculationMapper.insert(calculation);
         }
     }
 
@@ -174,13 +237,16 @@
     }
 
     /**
-     * 鏋勫缓榛樿鐨勫憳宸ヨ柂閰。妗堬紙鍩轰簬鍛樺伐琛ㄧ殑鍩烘湰宸ヨ祫锛�
+     * 鏋勫缓榛樿鐨勫憳宸ヨ柂閰。妗堛�傚熀鏈伐璧勭粺涓�鍙栧憳宸ユ。妗堬紝缁╂晥宸ヨ祫榛樿涓嶈嚜鍔ㄧ敓鎴愩��
+     *
+     * @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(employee.getBaseSalary() != null ? employee.getBaseSalary() : BigDecimal.ZERO)
+                .baseSalary(baseSalary)
                 .performanceSalary(BigDecimal.ZERO)
                 .mealAllowance(BigDecimal.ZERO)
                 .transportAllowance(BigDecimal.ZERO)
@@ -192,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,
@@ -204,17 +272,24 @@
         BigDecimal mealAllowance = employeeSalary.getMealAllowance() != null ? employeeSalary.getMealAllowance() : BigDecimal.ZERO;
         BigDecimal transportAllowance = employeeSalary.getTransportAllowance() != null ? employeeSalary.getTransportAllowance() : BigDecimal.ZERO;
         BigDecimal otherAllowance = employeeSalary.getOtherAllowance() != null ? employeeSalary.getOtherAllowance() : BigDecimal.ZERO;
-        BigDecimal overtimePay = 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)
                 .add(transportAllowance).add(otherAllowance).add(overtimePay);
 
-        // 鑾峰彇绀句繚鍩烘暟锛堝憳宸ユ。妗堜腑鐨勪釜鎬у寲鍩烘暟锛�
+        // 鑾峰彇绀句繚鍩烘暟锛氬憳宸ョぞ淇濇。妗� > 鍛樺伐鍩烘湰宸ヨ祫
         BigDecimal socialSecurityBase = (socialSecurity != null && socialSecurity.getSocialSecurityBase() != null)
                 ? socialSecurity.getSocialSecurityBase() : baseSalary;
 
-        // 鑾峰彇鍏Н閲戝熀鏁帮紙鍛樺伐妗f涓殑涓�у寲鍩烘暟锛�
+        // 鑾峰彇鍏Н閲戝熀鏁帮細鍛樺伐绀句繚妗f > 鍛樺伐鍩烘湰宸ヨ祫
         BigDecimal housingFundBase = (socialSecurity != null && socialSecurity.getHousingFundBase() != null)
                 ? socialSecurity.getHousingFundBase() : baseSalary;
 
@@ -262,7 +337,7 @@
 
         return HrmSalaryCalculationDO.builder()
                 .period(period)
-                .userId(employeeId) // 浣跨敤鍛樺伐ID
+                .userId(userId)
                 .deptId(employee.getDeptId())
                 .baseSalary(baseSalary)
                 .performanceSalary(performanceSalary)
@@ -276,8 +351,9 @@
                 .otherDeduction(otherDeduction)
                 .leaveDeduction(leaveDeduction)
                 .actualSalary(actualSalary)
-                .workDays(22) // 榛樿宸ヤ綔鏃�
-                .overtimeHours(BigDecimal.ZERO)
+                .workDays(workDays) // 鎸夋帓鐝疮璁″簲鍑哄嫟澶╂暟锛屾棤鎺掔彮鏃跺洖閫� 22
+                .overtimeHours(overtimeSalary.hours())
+                .totalWorkHours(totalWorkHours)
                 .status(0) // 寰呯‘璁�
                 .build();
     }
@@ -334,6 +410,85 @@
         return totalLeaveDeduction;
     }
 
+    /**
+     * 姹囨�诲綋鏈堝鎵归�氳繃鐨勫姞鐝敵璇枫�傚皬鏃跺伐璧勬寜鍩烘湰宸ヨ祫 / 21.75 / 8 璁$畻锛�
+     * 宸ヤ綔鏃ラ粯璁� 1.5 鍊嶃�佷紤鎭棩榛樿 2 鍊嶃�佹硶瀹氳妭鍋囨棩榛樿 3 鍊嶏紝鍧囧彲鐢辫柂閰粨鏋勮鐩栥��
+     */
+    private OvertimeSalary calculateOvertimeSalary(HrmEmployeeDO employee, HrmEmployeeSalaryDO employeeSalary,
+                                                    String period, BigDecimal baseSalary) {
+        if (employee.getUserId() == null) {
+            return new OvertimeSalary(BigDecimal.ZERO, BigDecimal.ZERO);
+        }
+        YearMonth month = YearMonth.parse(period);
+        List<HrmOvertimeApplicationDO> applications = overtimeApplicationMapper.selectApprovedByUserAndDateRange(
+                employee.getUserId(), month.atDay(1), month.atEndOfMonth());
+        if (CollUtil.isEmpty(applications)) {
+            return new OvertimeSalary(BigDecimal.ZERO, BigDecimal.ZERO);
+        }
+        Long structureId = employeeSalary.getSalaryStructureId() != null
+                ? employeeSalary.getSalaryStructureId() : employee.getSalaryStructureId();
+        HrmSalaryStructureDO structure = structureId == null ? null : salaryStructureMapper.selectById(structureId);
+        BigDecimal workdayMultiplier = structure != null && structure.getOvertimePayRatio() != null
+                ? structure.getOvertimePayRatio() : HrmOvertimeTypeEnum.WORKDAY.getSalaryMultiplier();
+        BigDecimal restDayMultiplier = structure != null && structure.getRestDayOvertimePayRatio() != null
+                ? structure.getRestDayOvertimePayRatio() : HrmOvertimeTypeEnum.REST_DAY.getSalaryMultiplier();
+        BigDecimal legalHolidayMultiplier = structure != null && structure.getLegalHolidayOvertimePayRatio() != null
+                ? structure.getLegalHolidayOvertimePayRatio() : HrmOvertimeTypeEnum.LEGAL_HOLIDAY.getSalaryMultiplier();
+        BigDecimal hourlySalary = baseSalary.divide(new BigDecimal("21.75"), 8, RoundingMode.HALF_UP)
+                .divide(new BigDecimal("8"), 8, RoundingMode.HALF_UP);
+        BigDecimal totalHours = BigDecimal.ZERO;
+        BigDecimal totalPay = BigDecimal.ZERO;
+        for (HrmOvertimeApplicationDO application : applications) {
+            BigDecimal hours = application.getDuration() == null ? BigDecimal.ZERO : application.getDuration();
+            HrmOvertimeTypeEnum type = HrmOvertimeTypeEnum.valueOfType(application.getOvertimeType());
+            if (type == null || hours.signum() <= 0) {
+                continue;
+            }
+            BigDecimal multiplier = switch (type) {
+                case WORKDAY -> workdayMultiplier;
+                case REST_DAY -> restDayMultiplier;
+                case LEGAL_HOLIDAY -> legalHolidayMultiplier;
+            };
+            totalHours = totalHours.add(hours);
+            totalPay = totalPay.add(hourlySalary.multiply(hours).multiply(multiplier));
+        }
+        return new OvertimeSalary(totalHours.setScale(1, RoundingMode.HALF_UP),
+                totalPay.setScale(2, RoundingMode.HALF_UP));
+    }
+
+    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) {
@@ -355,6 +510,8 @@
             salaryCalculationMapper.updateById(salaryCalculation);
             // 璁板綍鍛ㄦ湡
             period = salaryCalculation.getPeriod();
+            // 纭鍚庣粰璇ュ憳宸ュ彂閫佸伐璧勬潯閫氱煡锛堝け璐ヤ笉褰卞搷纭涓绘祦绋嬶級
+            sendPayslipNotice(salaryCalculation);
         }
 
         // 妫�鏌ヨ鍛ㄦ湡鏄惁鎵�鏈夋牳绠楄褰曢兘宸茬‘璁わ紝鑷姩鐢熸垚鍙戞斁鍙拌处
@@ -366,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);
@@ -417,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) {
@@ -435,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;
@@ -446,7 +642,24 @@
     public void saveSalaryCalculationList(List<HrmSalaryCalculationDO> calculationList) {
         for (HrmSalaryCalculationDO calculation : calculationList) {
             if (calculation.getId() == null) {
-                // 鏂板璁板綍锛岀敓鎴愭牳绠楀崟鍙�
+                // 鏂板鍓嶅厛鏌ラ噸锛氬悓涓�鍛樺伐锛堢郴缁熻处鍙� ID锛�+ 鍚屼竴鍛ㄦ湡 鍙厑璁稿瓨鍦ㄤ竴鏉¤褰曘��
+                // 鍓嶇銆岄瑙堟暟鎹� 鈫� 淇濆瓨鏍哥畻銆嶈蛋鐨勫氨鏄繖閲岋紝涓嶆煡閲嶄細閲嶅鎻掑叆銆�
+                List<HrmSalaryCalculationDO> existList = salaryCalculationMapper
+                        .selectListByUserIdAndPeriod(calculation.getUserId(), calculation.getPeriod());
+                if (CollUtil.isNotEmpty(existList)) {
+                    HrmSalaryCalculationDO existing = existList.get(0);
+                    // 宸茬‘璁�/宸插彂鏀剧殑璁板綍涓嶅厑璁歌瑕嗙洊锛岀洿鎺ヨ烦杩�
+                    if (existing.getStatus() != null && existing.getStatus() != 0) {
+                        continue;
+                    }
+                    // 寰呯‘璁ょ殑璁板綍锛氳鐩栭噸绠楃粨鏋滐紝淇濈暀鍘熷崟鍙�
+                    calculation.setId(existing.getId());
+                    calculation.setNo(existing.getNo());
+                    calculation.setStatus(0);
+                    salaryCalculationMapper.updateById(calculation);
+                    continue;
+                }
+                // 纭疄鏄柊澧烇紝鐢熸垚鏍哥畻鍗曞彿
                 String no = noRedisDAO.generateSalaryCalculationNo();
                 while (salaryCalculationMapper.selectByNo(no) != null) {
                     no = noRedisDAO.generateSalaryCalculationNo();
@@ -577,4 +790,4 @@
         salaryCalculationMapper.deleteById(id);
     }
 
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3