From ba100c651a81162a9a9a4533ff4d072f2889171a Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@tuqin.cn>
Date: 星期五, 06 十一月 2026 09:38:46 +0800
Subject: [PATCH] fix(hrm): 考勤统计支持员工姓名搜索;缺卡统计补全(与考勤记录联动)

---
 yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/salary/HrmSalaryCalculationServiceImpl.java |  292 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 219 insertions(+), 73 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 ed13d18..aa54240 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,7 @@
 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.HrmAttendanceSummaryDO;
 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;
@@ -18,6 +19,7 @@
 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.HrmAttendanceSummaryMapper;
 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;
@@ -114,6 +116,8 @@
     private HrmEmployeeScheduleMapper employeeScheduleMapper;
     @Resource
     private HrmShiftMapper shiftMapper;
+    @Resource
+    private HrmAttendanceSummaryMapper attendanceSummaryMapper;
 
     @Override
     public PageResult<HrmSalaryCalculationDO> getSalaryCalculationPage(HrmSalaryCalculationPageReqVO pageReqVO) {
@@ -248,45 +252,90 @@
         OvertimeSalary overtimeSalary = calculateOvertimeSalary(employee, employeeSalary, period, baseSalary);
         BigDecimal overtimePay = overtimeSalary.pay();
 
-        // 鎸夋帓鐝疮璁″嚭鍕ゅぉ鏁颁笌鎺掔彮鎬诲伐鏃讹紙鏃犳帓鐝椂鍑哄嫟澶╂暟鍥為��榛樿 22锛�
+        // 搴斿嚭澶╂暟锛氫紭鍏堝彇鑰冨嫟姹囨�婚噷鐨勩�屽簲鍑哄嫟澶╂暟銆嶏紙瀹㈡埛鎸夊疄闄呮湀浠界淮鎶わ紝濡� 30 澶╋級锛�
+        // 娌℃湁姹囨�绘椂閫�鍥炴帓鐝疮璁″ぉ鏁帮紝鍐嶉��鍥為粯璁� 22
         ScheduleStat scheduleStat = calculateScheduleStat(employeeId, period);
         Integer workDays = scheduleStat.scheduledDays() > 0 ? scheduleStat.scheduledDays() : 22;
         BigDecimal totalWorkHours = scheduleStat.totalWorkHours();
 
-        // 搴斿彂宸ヨ祫鎬婚
+        // 瀹炲嚭澶╂暟锛氬彇鑷�冨嫟姹囨�汇��
+        // 娉ㄦ剰锛歯ull 琛ㄧず璇ユ湀灏氭湭鐢熸垚鑰冨嫟姹囨�伙紝姝ゆ椂涓嶅仛銆岀己鍕ゆ墸娆俱�嶏紙瑙嗕负婊″嫟锛屽彧鎸夎鍋囧崟鎵o級锛�
+        //       鍚﹀垯涓�閲嶇畻灏变細鎶婃墍鏈夋病褰曡�冨嫟鐨勫憳宸ュ伐璧勬墸鍏夈��
+        Integer actualDays = null;
+        HrmAttendanceSummaryDO attendanceSummary =
+                attendanceSummaryMapper.selectByUserIdAndYearMonth(employeeId, period);
+        if (attendanceSummary != null) {
+            if (attendanceSummary.getActualDays() != null) {
+                actualDays = attendanceSummary.getActualDays();
+            }
+            if (attendanceSummary.getWorkDays() != null && attendanceSummary.getWorkDays() > 0) {
+                workDays = attendanceSummary.getWorkDays();
+            }
+        }
+
+        // 鍏ㄥ嫟濂栵細涓庡叾浠栨触璐翠竴鑷达紝鐢变汉宸ュ湪鏍哥畻鍗曚腑缁存姢锛屾柊寤烘椂榛樿 0
+        BigDecimal fullAttendanceBonus = BigDecimal.ZERO;
+
+        // 搴斿彂宸ヨ祫鎬婚锛堟敹鍏ラ」鍙e緞蹇呴』涓� calcActualSalary 淇濇寔涓�鑷达級
         BigDecimal totalIncome = baseSalary.add(performanceSalary).add(postSalary).add(senioritySalary)
+                .add(fullAttendanceBonus)
                 .add(mealAllowance).add(transportAllowance).add(otherAllowance).add(overtimePay);
 
-        // 鑾峰彇绀句繚鍩烘暟锛氬憳宸ョぞ淇濇。妗� > 鍛樺伐鍩烘湰宸ヨ祫
+        // 绀句繚鍩烘暟锛氬彧鍙栧憳宸ョぞ淇濇。妗堥噷閰嶇疆鐨勭即绾冲熀鏁帮紱鏈厤缃椂鐢ㄥ熀鏈伐璧�
         BigDecimal socialSecurityBase = (socialSecurity != null && socialSecurity.getSocialSecurityBase() != null)
                 ? socialSecurity.getSocialSecurityBase() : baseSalary;
 
-        // 鑾峰彇鍏Н閲戝熀鏁帮細鍛樺伐绀句繚妗f > 鍛樺伐鍩烘湰宸ヨ祫
-        BigDecimal housingFundBase = (socialSecurity != null && socialSecurity.getHousingFundBase() != null)
-                ? socialSecurity.getHousingFundBase() : baseSalary;
-
-        // 浠庢柟妗堜腑鑾峰彇缂寸撼姣斾緥锛堜紭鍏堜娇鐢ㄦ柟妗堜腑鐨勬瘮渚嬶紝鍚﹀垯浣跨敤榛樿姣斾緥锛�
+        // 浠庢柟妗堜腑鑾峰彇绀句繚缂寸撼姣斾緥锛堟柟妗堟湭閰嶇疆鏃跺洖閫�榛樿姣斾緥锛�
         BigDecimal pensionPersonalRatio = (scheme != null && scheme.getPensionPersonalRatio() != null)
                 ? scheme.getPensionPersonalRatio() : DEFAULT_PENSION_PERSONAL_RATIO;
         BigDecimal medicalPersonalRatio = (scheme != null && scheme.getMedicalPersonalRatio() != null)
                 ? scheme.getMedicalPersonalRatio() : DEFAULT_MEDICAL_PERSONAL_RATIO;
         BigDecimal unemploymentPersonalRatio = (scheme != null && scheme.getUnemploymentPersonalRatio() != null)
                 ? scheme.getUnemploymentPersonalRatio() : DEFAULT_UNEMPLOYMENT_PERSONAL_RATIO;
-        BigDecimal housingFundPersonalRatio = (scheme != null && scheme.getHousingFundPersonalRatio() != null)
-                ? scheme.getHousingFundPersonalRatio() : DEFAULT_HOUSING_FUND_PERSONAL_RATIO;
+        // 澶х梾鍖荤枟 / 闀挎湡鎶ょ悊鐨勪釜浜烘瘮渚嬶細鏂规閲屽彲鑳戒负绌猴紝涓虹┖瑙嗕负 0锛堜笉鎵o級
+        BigDecimal seriousIllnessPersonalRatio = (scheme != null && scheme.getSeriousIllnessPersonalRatio() != null)
+                ? scheme.getSeriousIllnessPersonalRatio() : BigDecimal.ZERO;
+        BigDecimal longTermCarePersonalRatio = (scheme != null && scheme.getLongTermCarePersonalRatio() != null)
+                ? scheme.getLongTermCarePersonalRatio() : BigDecimal.ZERO;
 
-        // 绀句繚鎵i櫎 = 鍩烘暟 脳 (鍏昏�佷釜浜烘瘮渚� + 鍖荤枟涓汉姣斾緥 + 澶变笟涓汉姣斾緥)
-        BigDecimal socialSecurityDeduction = socialSecurityBase
-                .multiply(pensionPersonalRatio)
-                .add(socialSecurityBase.multiply(medicalPersonalRatio))
-                .add(socialSecurityBase.multiply(unemploymentPersonalRatio))
-                .setScale(2, RoundingMode.HALF_UP);
+        // 鍏Н閲戜釜浜烘瘮渚嬶細鍙彇鏂规閰嶇疆鍊硷紝涓嶅啀鍥為��榛樿姣斾緥銆�
+        // 鍚﹀垯銆屽叕鍙告病閰嶅叕绉噾鏂规銆嶆椂浼氳鎸夐粯璁� 12% 鎵f锛堣繖灏辨槸瀹㈡埛鍙嶉鐨勯棶棰橈級銆�
+        BigDecimal housingFundPersonalRatio = scheme != null ? scheme.getHousingFundPersonalRatio() : null;
 
-        // 鍏Н閲戞墸闄� = 鍩烘暟 脳 鍏Н閲戜釜浜烘瘮渚�
-        BigDecimal housingFundDeduction = housingFundBase.multiply(housingFundPersonalRatio).setScale(2, RoundingMode.HALF_UP);
+        // 绀句繚鎵i櫎锛氬繀椤汇�屽憳宸ラ厤缃簡绀句繚妗f銆嶆墠鎵f銆�
+        // 涔嬪墠鏃犺鏈夋病鏈夌ぞ淇濇。妗堥兘鎸� 鍩烘暟 脳 榛樿姣斾緥(0.08+0.02+0.005=10.5%) 鎵f锛�
+        // 瀵艰嚧 102 涓病鍔犵ぞ淇濈殑鍛樺伐琚墸浜� 204.75锛�= 鍩烘湰宸ヨ祫 1950 脳 10.5%锛夈��
+        // 涓汉搴旂即 = 鍏昏�� + 鍖荤枟 + 澶变笟 + 澶х梾鍖荤枟 + 闀挎湡鎶ょ悊銆�
+        // 娉ㄦ剰锛氬師鏉ユ紡绠椾簡鍚庝袱椤癸紝浣垮緱銆屽叺鍥簲闄┿�嶄笌銆屽湴鏂逛簲闄┿�嶄袱涓柟妗堢畻鍑烘潵鐨勬墸娆惧畬鍏ㄤ竴鏍枫��
+        BigDecimal socialSecurityDeduction = BigDecimal.ZERO;
+        if (socialSecurity != null) {
+            BigDecimal personalTotalRatio = pensionPersonalRatio
+                    .add(medicalPersonalRatio)
+                    .add(unemploymentPersonalRatio)
+                    .add(seriousIllnessPersonalRatio)
+                    .add(longTermCarePersonalRatio);
+            socialSecurityDeduction = socialSecurityBase.multiply(personalTotalRatio)
+                    .setScale(2, RoundingMode.HALF_UP);
+        }
 
-        // 璁$畻璇峰亣鎵f
-        BigDecimal leaveDeduction = calculateLeaveDeduction(employee, period, baseSalary);
+        // 鍏Н閲戞墸闄わ細蹇呴』鍚屾椂婊¤冻銆屾柟妗堥厤缃簡鍏Н閲戞瘮渚� > 0銆嶄笖銆屽憳宸ユ。妗堥厤缃簡鍏Н閲戝熀鏁� > 0銆嶆墠鎵f锛�
+        // 浠讳竴涓嶆弧瓒抽兘瑙嗕负鍏徃鏈即绾冲叕绉噾锛屾墸娆句负 0銆�
+        BigDecimal housingFundDeduction = BigDecimal.ZERO;
+        BigDecimal housingFundBase = socialSecurity != null ? socialSecurity.getHousingFundBase() : null;
+        if (housingFundPersonalRatio != null && housingFundPersonalRatio.signum() > 0
+                && housingFundBase != null && housingFundBase.signum() > 0) {
+            housingFundDeduction = housingFundBase.multiply(housingFundPersonalRatio)
+                    .setScale(2, RoundingMode.HALF_UP);
+        }
+
+        // 缂哄嫟鎵f锛堣鍋� + 鏃峰伐锛�
+        // 璁¤柂鍩烘暟 = 鍩烘湰宸ヨ祫 + 宀椾綅宸ヨ祫 + 宸ラ緞宸ヨ祫 + 缁╂晥宸ヨ祫锛堣繖浜涗笌鍑哄嫟澶╂暟鐩存帴鐩稿叧锛夛紱
+        // 涓嶅惈鍔犵彮璐�/鍚勯」琛ヨ创/鍏ㄥ嫟濂栵紙杩欎簺涓嶉殢鍑哄嫟澶╂暟鎶樼畻锛夈��
+        // 娉ㄦ剰锛氫笉鑳界敤"鍩烘湰宸ヨ祫"褰撳熀鏁扳�斺�斿緢澶氬憳宸ュ伐璧勪富浣撴槸宀椾綅宸ヨ祫锛屽惁鍒欏疄鍑� 0 澶╂椂鍙墸鎺夊熀鏈伐璧勶紝宀椾綅宸ヨ祫鐓у彂銆�
+        BigDecimal attendanceBaseSalary = baseSalary
+                .add(postSalary).add(senioritySalary).add(performanceSalary);
+        BigDecimal leaveDeduction = calculateAttendanceDeduction(
+                employee, period, attendanceBaseSalary, workDays, actualDays);
 
         // 璁$畻涓◣鎵i櫎
         // 搴旂撼绋庢墍寰楅 = 搴斿彂宸ヨ祫 - 绀句繚鎵i櫎 - 鍏Н閲戞墸闄� - 璧峰緛鐐�(5000)
@@ -302,12 +351,10 @@
 
         BigDecimal otherDeduction = BigDecimal.ZERO;
 
-        // 璁$畻瀹炲彂宸ヨ祫 = 搴斿彂 - 鎵i櫎
-        BigDecimal totalDeduction = socialSecurityDeduction.add(housingFundDeduction)
-                .add(taxDeduction).add(otherDeduction).add(leaveDeduction);
-        BigDecimal actualSalary = totalIncome.subtract(totalDeduction);
+        // 璇存槑锛氬疄鍙戝伐璧勭粺涓�璧� calcActualSalary 璁$畻锛堟敹鍏� - 鎵i櫎锛屼笖涓嶅厑璁镐负璐燂級锛�
+        // 涓庛�屼慨鏀规牳绠楀崟 / 鎵归噺淇濆瓨銆嶄繚鎸佸悓涓�鍙e緞锛岄伩鍏嶅嚭鐜拌礋鏁板伐璧�
 
-        return HrmSalaryCalculationDO.builder()
+        HrmSalaryCalculationDO calculation = HrmSalaryCalculationDO.builder()
                 .period(period)
                 .userId(employeeId) // 浣跨敤鍛樺伐ID
                 .deptId(employee.getDeptId())
@@ -319,69 +366,89 @@
                 .mealAllowance(mealAllowance)
                 .transportAllowance(transportAllowance)
                 .otherAllowance(otherAllowance)
+                .fullAttendanceBonus(fullAttendanceBonus)
                 .socialSecurityDeduction(socialSecurityDeduction)
                 .housingFundDeduction(housingFundDeduction)
                 .taxDeduction(taxDeduction)
                 .otherDeduction(otherDeduction)
                 .leaveDeduction(leaveDeduction)
-                .actualSalary(actualSalary)
-                .workDays(workDays) // 鎸夋帓鐝疮璁″簲鍑哄嫟澶╂暟锛屾棤鎺掔彮鏃跺洖閫� 22
+                .workDays(workDays) // 搴斿嚭澶╂暟锛氫紭鍏堣�冨嫟姹囨�伙紝鍏舵鎺掔彮绱锛屾渶鍚庡洖閫� 22
+                .actualDays(actualDays) // 瀹炲嚭澶╂暟锛氬彇鑷�冨嫟姹囨��
                 .overtimeHours(overtimeSalary.hours())
-                .totalWorkHours(totalWorkHours)
+                .totalWorkHours(totalWorkHours) // 淇濈暀鍘嗗彶瀛楁锛屼笟鍔″凡鏀圭敤 actualDays
                 .status(0) // 寰呯‘璁�
                 .build();
+        calculation.setActualSalary(calcActualSalary(calculation));
+        return calculation;
     }
 
     /**
-     * 璁$畻璇峰亣鎵f
+     * 璁$畻銆岀己鍕ゆ墸娆俱��= 璇峰亣鎵f + 鏃峰伐鎵f
      *
-     * @param employee   鍛樺伐淇℃伅
-     * @param period     鏍哥畻鍛ㄦ湡锛堝2026-02锛�
-     * @param baseSalary 鍩烘湰宸ヨ祫
-     * @return 璇峰亣鎵f閲戦
+     * <p>瑙勫垯锛�
+     * <ol>
+     *     <li>鏃ュ伐璧� = 鍩烘湰宸ヨ祫 / 搴斿嚭澶╂暟锛堜笌搴斿嚭澶╂暟鍙e緞涓�鑷达紝閬垮厤缂哄嫟澶╂暟瓒呰繃璁¤柂澶╂暟鏃舵墸瓒咃級锛�</li>
+     *     <li>璇峰亣澶╂暟鎸夈�岃鍋囨墸娆鹃厤缃�嶇殑姣斾緥鎵o細璋冧紤绛夋湭閰嶇疆鐨勭被鍨嬫瘮渚嬭涓� 0锛堜笉鎵o級锛岀梾鍋囨寜閰嶇疆姣斾緥鎵o紱</li>
+     *     <li>鑻ヨ鏈堝凡鐢熸垚鑰冨嫟姹囨�伙紙actualDays 涓嶄负 null锛夛細
+     *         鏃峰伐澶╂暟 = 搴斿嚭澶╂暟 鈭� 瀹炲嚭澶╂暟 鈭� 璇峰亣澶╂暟锛堜笉涓鸿礋锛夛紝鎸� 100% 鎵o紱</li>
+     *     <li>鑻ユ湭鐢熸垚鑰冨嫟姹囨�伙紙actualDays 涓� null锛夛紝瑙嗕负婊″嫟锛屼笉鍋氭椃宸ユ墸娆撅紝鍙寜璇峰亣鍗曟墸锛�</li>
+     *     <li>鎵f鎬婚灏侀《涓哄熀鏈伐璧勶紝閬垮厤鍑虹幇璐熷伐璧勩��</li>
+     * </ol>
+     *
+     * @param employee       鍛樺伐淇℃伅
+     * @param period         鏍哥畻鍛ㄦ湡锛堝 2026-02锛�
+     * @param attendanceBase 璁¤柂鍩烘暟 = 鍩烘湰 + 宀椾綅 + 宸ラ緞 + 缁╂晥锛堜笌鍑哄嫟鐩存帴鐩稿叧鐨勫浐瀹氬伐璧勶級
+     * @param workDays       搴斿嚭澶╂暟锛堟帓鐝疮璁★紝鏃犳帓鐝椂浼� 22锛�
+     * @param actualDays     瀹炲嚭澶╂暟锛堝彇鑷�冨嫟姹囨�伙紱null 琛ㄧず璇ユ湀鏈敓鎴愭眹鎬伙級
+     * @return 缂哄嫟鎵f閲戦锛堝皝椤朵负璁¤柂鍩烘暟锛�
      */
-    private BigDecimal calculateLeaveDeduction(HrmEmployeeDO employee, String period, BigDecimal baseSalary) {
-        // 瑙f瀽鍛ㄦ湡鑾峰彇鏈堜唤寮�濮嬪拰缁撴潫鏃堕棿
-        YearMonth yearMonth = YearMonth.parse(period);
-        LocalDateTime monthStart = yearMonth.atDay(1).atStartOfDay();
-        LocalDateTime monthEnd = yearMonth.atEndOfMonth().atTime(23, 59, 59);
+    private BigDecimal calculateAttendanceDeduction(HrmEmployeeDO employee, String period,
+                                                    BigDecimal attendanceBase, Integer workDays, Integer actualDays) {
+        int shouldDays = (workDays != null && workDays > 0) ? workDays : 22;
+        // 鏃ュ伐璧� = 璁¤柂鍩烘暟 / 搴斿嚭澶╂暟锛堜繚鐣� 4 浣嶏紝鏈�鍚庣粺涓�淇濈暀 2 浣嶏級
+        BigDecimal dailySalary = attendanceBase.divide(new BigDecimal(shouldDays), 4, RoundingMode.HALF_UP);
 
-        // 鏌ヨ鍛樺伐鍦ㄨ鏈堝凡瀹℃壒閫氳繃鐨勮鍋囪褰�
-        // 娉ㄦ剰锛氳鍋囩敵璇风殑 userId 鏄郴缁熺敤鎴稩D锛岄渶瑕侀�氳繃鍛樺伐琛ㄨ幏鍙�
+        BigDecimal totalDeduction = BigDecimal.ZERO;
+        BigDecimal leaveDays = BigDecimal.ZERO;
+
+        // 1. 璇峰亣鎵f锛氭寜璇峰亣绫诲瀷鐨勯厤缃瘮渚�
         Long systemUserId = employee.getUserId();
-        if (systemUserId == null) {
-            return BigDecimal.ZERO;
-        }
-        List<HrmLeaveApplicationDO> leaveApplications = leaveApplicationMapper
-                .selectApprovedListByUserIdAndTimeRange(systemUserId, monthStart, monthEnd);
-
-        if (leaveApplications.isEmpty()) {
-            return BigDecimal.ZERO;
+        if (systemUserId != null) {
+            YearMonth yearMonth = YearMonth.parse(period);
+            LocalDateTime monthStart = yearMonth.atDay(1).atStartOfDay();
+            LocalDateTime monthEnd = yearMonth.atEndOfMonth().atTime(23, 59, 59);
+            List<HrmLeaveApplicationDO> leaveApplications = leaveApplicationMapper
+                    .selectApprovedListByUserIdAndTimeRange(systemUserId, monthStart, monthEnd);
+            if (CollUtil.isNotEmpty(leaveApplications)) {
+                List<HrmLeaveTypeConfigDO> configList = leaveTypeConfigMapper.selectList();
+                Map<Integer, BigDecimal> configMap = configList.stream()
+                        .filter(c -> c.getStatus() != null && c.getStatus() == 0) // 鍙彇鍚敤鐨勯厤缃�
+                        .collect(Collectors.toMap(HrmLeaveTypeConfigDO::getLeaveType,
+                                HrmLeaveTypeConfigDO::getDeductionRatio));
+                for (HrmLeaveApplicationDO application : leaveApplications) {
+                    BigDecimal duration = application.getDuration() != null
+                            ? application.getDuration() : BigDecimal.ZERO;
+                    // 鏈厤缃殑绫诲瀷锛堝璋冧紤锛夋瘮渚嬫寜 0 澶勭悊 = 涓嶆墸娆�
+                    BigDecimal deductionRatio = configMap.getOrDefault(application.getLeaveType(), BigDecimal.ZERO);
+                    totalDeduction = totalDeduction.add(
+                            dailySalary.multiply(duration).multiply(deductionRatio));
+                    leaveDays = leaveDays.add(duration);
+                }
+            }
         }
 
-        // 鑾峰彇鎵�鏈夎鍋囩被鍨嬮厤缃�
-        List<HrmLeaveTypeConfigDO> configList = leaveTypeConfigMapper.selectList();
-        Map<Integer, BigDecimal> configMap = configList.stream()
-                .filter(c -> c.getStatus() == 0) // 鍙彇鍚敤鐨勯厤缃�
-                .collect(Collectors.toMap(HrmLeaveTypeConfigDO::getLeaveType, HrmLeaveTypeConfigDO::getDeductionRatio));
-
-        // 璁$畻鏃ュ伐璧勶紙鍩烘湰宸ヨ祫 / 21.75锛屾寜鍥藉瑙勫畾鐨勫伐浣滄棩璁$畻锛�
-        BigDecimal dailySalary = baseSalary.divide(new BigDecimal("21.75"), 2, RoundingMode.HALF_UP);
-
-        // 绱璇峰亣鎵f
-        BigDecimal totalLeaveDeduction = BigDecimal.ZERO;
-        for (HrmLeaveApplicationDO application : leaveApplications) {
-            Integer leaveType = application.getLeaveType();
-            BigDecimal duration = application.getDuration() != null ? application.getDuration() : BigDecimal.ZERO;
-            BigDecimal deductionRatio = configMap.getOrDefault(leaveType, BigDecimal.ZERO);
-
-            // 璇峰亣鎵f = 鏃ュ伐璧� 脳 璇峰亣澶╂暟 脳 鎵f姣斾緥
-            BigDecimal deduction = dailySalary.multiply(duration).multiply(deductionRatio)
-                    .setScale(2, RoundingMode.HALF_UP);
-            totalLeaveDeduction = totalLeaveDeduction.add(deduction);
+        // 2. 鏃峰伐鎵f锛氱己鍕ゅぉ鏁颁腑鏈璇峰亣鍗曡鐩栫殑閮ㄥ垎锛屾寜 100% 鎵o紙浠呭綋鏈夎�冨嫟姹囨�绘椂锛�
+        if (actualDays != null) {
+            BigDecimal absentDays = new BigDecimal(shouldDays - actualDays).subtract(leaveDays);
+            if (absentDays.signum() > 0) {
+                totalDeduction = totalDeduction.add(dailySalary.multiply(absentDays));
+            }
         }
 
-        return totalLeaveDeduction;
+        // 3. 灏侀《锛氭墸娆句笉瓒呰繃鍩烘湰宸ヨ祫
+        BigDecimal result = totalDeduction.setScale(2, RoundingMode.HALF_UP);
+        BigDecimal cap = attendanceBase.setScale(2, RoundingMode.HALF_UP);
+        return result.compareTo(cap) > 0 ? cap : result;
     }
 
     /**
@@ -654,6 +721,9 @@
     @Transactional(rollbackFor = Exception.class)
     public void saveSalaryCalculationList(List<HrmSalaryCalculationDO> calculationList) {
         for (HrmSalaryCalculationDO calculation : calculationList) {
+            // 缂哄嫟鎵f锛堣鍋� + 鏃峰伐锛変笌瀹炲彂宸ヨ祫缁熶竴鐢卞悗绔噸绠楋紝
+            // 淇濊瘉鍒楄〃閲屾敼銆屽簲鍑哄ぉ鏁� / 瀹炲嚭澶╂暟銆嶅悗閲戦璺熺潃鍙�
+            recalcAttendanceDeduction(calculation);
             if (calculation.getId() == null) {
                 // 鏂板璁板綍锛岀敓鎴愭牳绠楀崟鍙�
                 String no = noRedisDAO.generateSalaryCalculationNo();
@@ -700,22 +770,61 @@
         calculation.setMealAllowance(nvl(calculation.getMealAllowance()));
         calculation.setTransportAllowance(nvl(calculation.getTransportAllowance()));
         calculation.setOtherAllowance(nvl(calculation.getOtherAllowance()));
+        calculation.setFullAttendanceBonus(nvl(calculation.getFullAttendanceBonus()));
         calculation.setSocialSecurityDeduction(nvl(calculation.getSocialSecurityDeduction()));
         calculation.setHousingFundDeduction(nvl(calculation.getHousingFundDeduction()));
         calculation.setTaxDeduction(nvl(calculation.getTaxDeduction()));
         calculation.setOtherDeduction(nvl(calculation.getOtherDeduction()));
-        calculation.setLeaveDeduction(nvl(calculation.getLeaveDeduction()));
-        // 瀹炲彂宸ヨ祫鐢卞悗绔噸绠楋紝閬垮厤鍓嶇浼犲叆鍊间笌鏄庣粏涓嶄竴鑷�
-        calculation.setActualSalary(calcActualSalary(calculation));
+        // 缂哄嫟鎵f锛堣鍋� + 鏃峰伐锛変笌瀹炲彂宸ヨ祫缁熶竴鐢卞悗绔噸绠楋紝
+        // 淇濊瘉鍒楄〃閲屾敼銆屽簲鍑哄ぉ鏁� / 瀹炲嚭澶╂暟銆嶅悗閲戦璺熺潃鍙橈紝涓斾笉鍙楀墠绔紶鍏ョ殑 leaveDeduction 褰卞搷
+        recalcAttendanceDeduction(calculation);
         salaryCalculationMapper.updateById(calculation);
     }
 
     /**
+     * 閲嶇畻銆岀己鍕ゆ墸娆俱�嶄笌銆屽疄鍙戝伐璧勩��
+     *
+     * <p>鏍哥畻鍗� userId 瀛樼殑鏄憳宸D锛坔rm_employee.id锛夈�傛寜銆屽簲鍑哄ぉ鏁� / 瀹炲嚭澶╂暟 + 璇峰亣鍗曘�嶉噸绠�
+     * 缂哄嫟鎵f锛堣鍋囨寜閰嶇疆姣斾緥鎵c�佹椃宸ユ寜 100% 鎵c�佸皝椤惰钖熀鏁帮級锛屽啀閲嶇畻瀹炲彂宸ヨ祫锛堜笉鍙负璐燂級銆�
+     *
+     * <p>璁¤柂鍩烘暟 = 鍩烘湰 + 宀椾綅 + 宸ラ緞 + 缁╂晥锛堜笌鍑哄嫟鐩存帴鐩稿叧鐨勫浐瀹氬伐璧勯儴鍒嗭級锛�
+     * 涓嶈兘鐢ㄥ熀鏈伐璧勫崟鐙皝椤讹紝鍚﹀垯宸ヨ祫涓讳綋涓哄矖浣嶅伐璧勭殑鍛樺伐瀹炲嚭 0 澶╂椂浠嶄細鍙戞斁澶ч儴鍒嗗伐璧勩��
+     */
+    private void recalcAttendanceDeduction(HrmSalaryCalculationDO calculation) {
+        HrmEmployeeDO employee = calculation.getUserId() != null
+                ? employeeMapper.selectById(calculation.getUserId()) : null;
+        if (employee != null && calculation.getPeriod() != null) {
+            BigDecimal attendanceBaseSalary = nvl(calculation.getBaseSalary())
+                    .add(nvl(calculation.getPostSalary()))
+                    .add(nvl(calculation.getSenioritySalary()))
+                    .add(nvl(calculation.getPerformanceSalary()));
+            BigDecimal deduction = calculateAttendanceDeduction(
+                    employee, calculation.getPeriod(),
+                    attendanceBaseSalary,
+                    calculation.getWorkDays(),
+                    calculation.getActualDays());
+            calculation.setLeaveDeduction(deduction);
+        } else {
+            calculation.setLeaveDeduction(nvl(calculation.getLeaveDeduction()));
+        }
+        calculation.setActualSalary(calcActualSalary(calculation));
+    }
+
+    /**
      * 璁$畻瀹炲彂宸ヨ祫 = 鏀跺叆椤瑰悎璁� - 鎵i櫎椤瑰悎璁★紝淇濈暀涓や綅灏忔暟
+     *
+     * 鏀跺叆椤瑰繀椤讳笌 buildSalaryCalculation 涓� totalIncome 鐨勫彛寰勫畬鍏ㄤ竴鑷达紝
+     * 鍚﹀垯浼氬嚭鐜般�屾柊寤烘牳绠楃畻寰楀銆佷慨鏀逛竴娆℃牳绠楀崟瀹炲彂灏卞彉灏戙�嶇殑涓嶄竴鑷淬��
+     *
+     * 瀹炲彂宸ヨ祫涓嶅厑璁镐负璐燂細缂哄嫟鎵f绛夊凡灏侀《涓哄熀鏈伐璧勶紝浣嗙ぞ淇�/鍏Н閲�/涓◣鍙犲姞鍚庝粛鍙兘涓鸿礋锛�
+     * 涓氬姟涓婃寜銆屾湰鏈堟棤宸ヨ祫銆嶅鐞嗭紙缃� 0锛夛紝閬垮厤鍑虹幇鍛樺伐鍊掓瑺鍏徃鐨勬儏鍐点��
      */
     private static BigDecimal calcActualSalary(HrmSalaryCalculationDO c) {
         BigDecimal income = nvl(c.getBaseSalary())
+                .add(nvl(c.getPostSalary()))
+                .add(nvl(c.getSenioritySalary()))
                 .add(nvl(c.getPerformanceSalary()))
+                .add(nvl(c.getFullAttendanceBonus()))
                 .add(nvl(c.getOvertimePay()))
                 .add(nvl(c.getMealAllowance()))
                 .add(nvl(c.getTransportAllowance()))
@@ -725,7 +834,8 @@
                 .add(nvl(c.getTaxDeduction()))
                 .add(nvl(c.getOtherDeduction()))
                 .add(nvl(c.getLeaveDeduction()));
-        return income.subtract(deduction).setScale(2, RoundingMode.HALF_UP);
+        BigDecimal actual = income.subtract(deduction).setScale(2, RoundingMode.HALF_UP);
+        return actual.signum() < 0 ? BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP) : actual;
     }
 
     private static BigDecimal nvl(BigDecimal v) {
@@ -848,4 +958,40 @@
         salaryCalculationMapper.deleteById(id);
     }
 
+    /**
+     * 鎵归噺鍒犻櫎鏍哥畻璁板綍锛堝叏閫夊悗涓�閿垹闄わ級
+     *
+     * 璇存槑锛�
+     * 1. 鎸変笟鍔¤姹傦紝鎵归噺鍒犻櫎涓嶉檺鍒舵牳绠楀崟鐘舵�侊紝鍕鹃�夊嵆鍒狅紱
+     * 2. 涓嶅瓨鍦ㄧ殑 ID 鐩存帴璺宠繃锛屼笉鎶ラ敊锛堥伩鍏嶅墠绔嬀閫夊垪琛ㄤ笌搴撲笉涓�鑷存椂鏁翠綋澶辫触锛夛紱
+     * 3. 宸茶銆屽伐璧勫彂鏀炬槑缁嗐�嶅紩鐢ㄧ殑璁板綍浼氳烦杩囦笉鍒狅紝閬垮厤鍙戞斁鏄庣粏鍙樻垚瀛ゅ効鏁版嵁銆�
+     *
+     * @return 瀹為檯鍒犻櫎鐨勬潯鏁帮紙鍙兘灏忎簬浼犲叆鏉℃暟锛�
+     */
+    @Override
+    public Integer deleteSalaryCalculationList(List<Long> ids) {
+        if (CollUtil.isEmpty(ids)) {
+            return 0;
+        }
+        List<Long> existIds = salaryCalculationMapper.selectBatchIds(ids).stream()
+                .map(HrmSalaryCalculationDO::getId)
+                .collect(Collectors.toList());
+        if (CollUtil.isEmpty(existIds)) {
+            return 0;
+        }
+        // 寮曠敤淇濇姢锛氬凡杩涘叆宸ヨ祫鍙戞斁娴佺▼鐨勬牳绠楀崟涓嶅厑璁稿垹闄�
+        Set<Long> referencedIds = salaryPaymentDetailMapper.selectListByCalculationIds(existIds).stream()
+                .map(HrmSalaryPaymentDetailDO::getCalculationId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+        List<Long> deletableIds = existIds.stream()
+                .filter(id -> !referencedIds.contains(id))
+                .collect(Collectors.toList());
+        if (CollUtil.isEmpty(deletableIds)) {
+            return 0;
+        }
+        salaryCalculationMapper.deleteByIds(deletableIds);
+        return deletableIds.size();
+    }
+
 }

--
Gitblit v1.9.3