From 6be17495cd29ba8b28af35da7ba171cbc0d61c21 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 12 六月 2026 14:18:49 +0800
Subject: [PATCH] 根据单个员工计算工资

---
 src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java |  233 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 138 insertions(+), 95 deletions(-)

diff --git a/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java b/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java
index 73d01a6..bf886a2 100644
--- a/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java
+++ b/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java
@@ -164,91 +164,84 @@
             return; // 鎴栬繑鍥炵┖鍒楄〃锛屾牴鎹笟鍔¢渶姹傝皟鏁�
         }
         Long staffId = id.longValue();
-        // 绀句繚閲戦
-        BigDecimal socialPersonal = new BigDecimal("0.00");
-        // 鍏Н閲戦噾棰�
-        BigDecimal fundPersonal = new BigDecimal("0.00");
-        // 鍩烘湰宸ヨ祫
-        BigDecimal basicSalary = new BigDecimal("0.00");
-        map.put("fundPersonal", fundPersonal); // 鍏Н閲�
-        map.put("socialPersonal", socialPersonal); // 绀句繚閲戦
-        map.put("socialSupplementAmount", BigDecimal.ZERO); // 绀句繚琛ョ即閲戦
-        map.put("basicSalary", basicSalary); // 鍩烘湰宸ヨ祫
-        // 涓◣閲戦
-        BigDecimal salaryTax = new BigDecimal("0.00");
-        map.put("salaryTax", salaryTax);
-        // 璁′欢宸ヨ祫
-        BigDecimal pieceSalary = new BigDecimal("0.00");
-        map.put("pieceSalary", pieceSalary);
-        // 璁℃椂宸ヨ祫
-        BigDecimal hourlySalary = new BigDecimal("0.00");
-        map.put("hourlySalary", hourlySalary);
-        // 鍏朵粬鏀跺叆
-        BigDecimal otherIncome = new BigDecimal("0.00");
-        map.put("otherIncome", otherIncome);
-        // 鍏朵粬鏀嚭
-        BigDecimal otherDeduct = new BigDecimal("0.00");
-        map.put("otherDeduct", otherDeduct);
-        // 搴斿彂宸ヨ祫
-        BigDecimal grossSalary = new BigDecimal("0.00");
-        map.put("grossSalary", grossSalary);
-        // 搴旀墸宸ヨ祫
-        BigDecimal deductSalary = new BigDecimal("0.00");
-        map.put("deductSalary", deductSalary);
-        // 瀹炲彂宸ヨ祫
-        BigDecimal netSalary = new BigDecimal("0.00");
-        map.put("netSalary", netSalary);
-        // 鏍规嵁鎺掔彮鏁版嵁璁$畻鐧界彮澶╂暟鍜屽鐝ぉ鏁�
-        BigDecimal dayDays = new BigDecimal("0.00");
-        BigDecimal nightDays = new BigDecimal("0.00");
+        // 鍓嶇浼犲叆鐨勫彲缂栬緫鍊间紭鍏堬紝鍏朵綑瀛楁鐢卞悗绔粺涓�璁$畻
+        boolean hasBasicSalaryInput = hasValue(map, "basicSalary");
+        boolean hasDayDaysInput = hasValue(map, "dayDays");
+        boolean hasNightDaysInput = hasValue(map, "nightDays");
+        boolean hasPieceSalaryInput = hasValue(map, "pieceSalary");
+        boolean hasHourlySalaryInput = hasValue(map, "hourlySalary");
+        boolean hasSocialPersonalInput = hasValue(map, "socialPersonal");
+        boolean hasFundPersonalInput = hasValue(map, "fundPersonal");
+
+        BigDecimal basicSalary = getBigDecimal(map, "basicSalary");
+        BigDecimal dayDays = getBigDecimal(map, "dayDays");
+        BigDecimal nightDays = getBigDecimal(map, "nightDays");
+        BigDecimal pieceSalary = getBigDecimal(map, "pieceSalary");
+        BigDecimal hourlySalary = getBigDecimal(map, "hourlySalary");
+        BigDecimal otherIncome = getBigDecimal(map, "otherIncome");
+        BigDecimal otherDeduct = getBigDecimal(map, "otherDeduct");
+        BigDecimal socialPersonalInput = getBigDecimal(map, "socialPersonal");
+        BigDecimal fundPersonalInput = getBigDecimal(map, "fundPersonal");
+        BigDecimal socialPersonal = BigDecimal.ZERO;
+        BigDecimal fundPersonal = BigDecimal.ZERO;
+        BigDecimal salaryTax = BigDecimal.ZERO;
+        BigDecimal grossSalary = BigDecimal.ZERO;
+        BigDecimal deductSalary = BigDecimal.ZERO;
+        BigDecimal netSalary = BigDecimal.ZERO;
+        BigDecimal dayDaysInput = dayDays;
+        BigDecimal nightDaysInput = nightDays;
+        otherIncome = defaultIfNull(otherIncome);
+        otherDeduct = defaultIfNull(otherDeduct);
+        map.put("socialSupplementAmount", BigDecimal.ZERO);
 
         // 鏌ヨ褰撴湀鎺掔彮璁板綍
-        List<PersonalShift> shiftList = personalShiftMapper.selectList(new LambdaQueryWrapper<PersonalShift>()
-                .eq(PersonalShift::getStaffOnJobId, staffId)
-                .like(PersonalShift::getWorkTime, date));
+        BigDecimal computedDayDays = BigDecimal.ZERO;
+        BigDecimal computedNightDays = BigDecimal.ZERO;
+        if (!hasDayDaysInput || !hasNightDaysInput) {
+            List<PersonalShift> shiftList = personalShiftMapper.selectList(new LambdaQueryWrapper<PersonalShift>()
+                    .eq(PersonalShift::getStaffOnJobId, staffId)
+                    .like(PersonalShift::getWorkTime, date));
 
-        if(!CollectionUtils.isEmpty(shiftList)){
-            // 鏀堕泦鎵�鏈夌彮娆¢厤缃甀D
-            List<Integer> configIds = shiftList.stream()
-                    .map(PersonalShift::getPersonalAttendanceLocationConfigId)
-                    .filter(Objects::nonNull)
-                    .collect(Collectors.toList());
+            if(!CollectionUtils.isEmpty(shiftList)){
+                // 鏀堕泦鎵�鏈夌彮娆¢厤缃甀D
+                List<Integer> configIds = shiftList.stream()
+                        .map(PersonalShift::getPersonalAttendanceLocationConfigId)
+                        .filter(Objects::nonNull)
+                        .collect(Collectors.toList());
 
-            if(!CollectionUtils.isEmpty(configIds)){
-                // 鏌ヨ鐝閰嶇疆淇℃伅
-                List<PersonalAttendanceLocationConfig> configList = personalAttendanceLocationConfigMapper.selectList(
-                        new LambdaQueryWrapper<PersonalAttendanceLocationConfig>()
-                                .in(PersonalAttendanceLocationConfig::getId, configIds));
+                if(!CollectionUtils.isEmpty(configIds)){
+                    // 鏌ヨ鐝閰嶇疆淇℃伅
+                    List<PersonalAttendanceLocationConfig> configList = personalAttendanceLocationConfigMapper.selectList(
+                            new LambdaQueryWrapper<PersonalAttendanceLocationConfig>()
+                                    .in(PersonalAttendanceLocationConfig::getId, configIds));
 
-                // 鏋勫缓鐝閰嶇疆鏄犲皠
-                Map<Integer, String> configMap = configList.stream()
-                        .collect(Collectors.toMap(
-                                PersonalAttendanceLocationConfig::getId,
-                                PersonalAttendanceLocationConfig::getShift,
-                                (a, b) -> a));
+                    // 鏋勫缓鐝閰嶇疆鏄犲皠
+                    Map<Integer, String> configMap = configList.stream()
+                            .collect(Collectors.toMap(
+                                    PersonalAttendanceLocationConfig::getId,
+                                    PersonalAttendanceLocationConfig::getShift,
+                                    (a, b) -> a));
 
-                // 缁熻鐧界彮鍜屽鐝ぉ鏁�
-                for (PersonalShift shift : shiftList) {
-                    Integer configId = shift.getPersonalAttendanceLocationConfigId();
-                    String shiftName = configMap.get(configId);
-                    if(shiftName != null){
-                        // 鏍规嵁鐝鍚嶇О鍒ゆ柇锛氬寘鍚�"鐧�"鎴�"鏃�"涓虹櫧鐝紝鍖呭惈"澶�"涓哄鐝�
-                        if(shiftName.contains("鐧�") || shiftName.contains("鏃�")){
-                            dayDays = dayDays.add(BigDecimal.ONE);
-                        } else if(shiftName.contains("澶�")){
-                            nightDays = nightDays.add(BigDecimal.ONE);
+                    // 缁熻鐧界彮鍜屽鐝ぉ鏁�
+                    for (PersonalShift shift : shiftList) {
+                        Integer configId = shift.getPersonalAttendanceLocationConfigId();
+                        String shiftName = configMap.get(configId);
+                        if(shiftName != null){
+                            // 鏍规嵁鐝鍚嶇О鍒ゆ柇锛氬寘鍚�"鐧�"鎴�"鏃�"涓虹櫧鐝紝鍖呭惈"澶�"涓哄鐝�
+                            if(shiftName.contains("鐧�") || shiftName.contains("鏃�")){
+                                computedDayDays = computedDayDays.add(BigDecimal.ONE);
+                            } else if(shiftName.contains("澶�")){
+                                computedNightDays = computedNightDays.add(BigDecimal.ONE);
+                            }
                         }
                     }
                 }
             }
         }
+        dayDays = hasDayDaysInput ? defaultIfNull(dayDaysInput) : computedDayDays;
+        nightDays = hasNightDaysInput ? defaultIfNull(nightDaysInput) : computedNightDays;
         map.put("dayDays", dayDays); // 鐧界彮澶╂暟
         map.put("nightDays", nightDays); // 澶滅彮澶╂暟
-
-        // 鏌ヨ椁愯ˉ鍜屽鐝ˉ璐�
-        // 璁$畻椁愯ˉ锛堝洖鏃忕壒鏈夛級鍜屽鐝ˉ鍔�
-        BigDecimal mealAmount = new BigDecimal("0.00"); // 椁愯ˉ
-        BigDecimal nightAmount = new BigDecimal("0.00"); // 澶滅彮琛ュ姪
 
         // 鑾峰彇椁愯ˉ鍜屽鐝ˉ璐存爣鍑嗭紙浠庤ˉ璐撮厤缃腑鑾峰彇锛�
         SubsidyConfiguration subsidyConfig = subsidyConfigurationMapper.selectOne(null);
@@ -265,7 +258,17 @@
             return;
         }
 
+        if (!hasBasicSalaryInput) {
+            basicSalary = defaultIfNull(staffOnJobDto.getBasicSalary());
+        } else {
+            basicSalary = defaultIfNull(basicSalary);
+        }
+
         String nation = staffOnJobDto.getNation();
+        // 鏌ヨ椁愯ˉ鍜屽鐝ˉ璐�
+        // 璁$畻椁愯ˉ锛堝洖鏃忕壒鏈夛級鍜屽鐝ˉ鍔�
+        BigDecimal mealAmount = new BigDecimal("0.00"); // 椁愯ˉ
+        BigDecimal nightAmount = new BigDecimal("0.00"); // 澶滅彮琛ュ姪
         if("鍥炴棌".equals(nation)){
             mealAmount = dayDays.add(nightDays).multiply(mealStandard);
         }
@@ -277,28 +280,37 @@
         map.put("nightAmount", nightAmount); // 澶滅彮琛ュ姪
 
         // 璋冪敤鍩烘湰宸ヨ祫
-        basicSalary = staffOnJobDto.getBasicSalary();
         map.put("basicSalary", basicSalary);
-        // 搴斿彂宸ヨ祫锛堝熀鏈伐璧�+椁愯ˉ+澶滅彮琛ュ姪+鍏朵粬鏀跺叆锛�
-        grossSalary = basicSalary.add(mealAmount).add(nightAmount).add(otherIncome);
+        map.put("otherIncome", otherIncome);
+        map.put("otherDeduct", otherDeduct);
+        // 璁℃椂宸ヨ祫 璁′欢宸ヨ祫
+        if (!hasPieceSalaryInput || !hasHourlySalaryInput) {
+            UserProductionAccountingDto userProductionAccountingDto = new UserProductionAccountingDto();
+            userProductionAccountingDto.setUserId(getUidByStaffId(staffId));
+            userProductionAccountingDto.setDate(date);
+            UserAccountDto byUserId = salesLedgerProductionAccountingService.getByUserId(userProductionAccountingDto);
+            if(byUserId != null){
+                if (!hasPieceSalaryInput) {
+                    pieceSalary = byUserId.getAccountBalance();
+                }
+                if (!hasHourlySalaryInput) {
+                    hourlySalary = byUserId.getAccount();
+                }
+            }
+        }
+        map.put("pieceSalary", defaultIfNull(pieceSalary));
+        map.put("hourlySalary", defaultIfNull(hourlySalary));
+        // 搴斿彂宸ヨ祫锛堝熀鏈伐璧�+椁愯ˉ+澶滅彮琛ュ姪+璁′欢宸ヨ祫+璁℃椂宸ヨ祫+鍏朵粬鏀跺叆锛�
+        grossSalary = basicSalary
+                .add(mealAmount)
+                .add(nightAmount)
+                .add(defaultIfNull(pieceSalary))
+                .add(defaultIfNull(hourlySalary))
+                .add(otherIncome);
         map.put("grossSalary", grossSalary);
         // 瀹炲彂宸ヨ祫鍒濆鍊�
         netSalary = grossSalary;
         map.put("netSalary", netSalary);
-        // 璁℃椂宸ヨ祫 璁′欢宸ヨ祫
-        UserProductionAccountingDto userProductionAccountingDto = new UserProductionAccountingDto();
-        userProductionAccountingDto.setUserId(getUidByStaffId(staffId));
-        userProductionAccountingDto.setDate(date);
-        UserAccountDto byUserId = salesLedgerProductionAccountingService.getByUserId(userProductionAccountingDto);
-        if(byUserId != null){
-            map.put("pieceSalary", byUserId.getAccountBalance());
-            map.put("hourlySalary", byUserId.getAccount());
-            // 搴斿彂 瀹炲彂澧炲姞
-            grossSalary = grossSalary.add(byUserId.getAccountBalance()).add(byUserId.getAccount());
-            map.put("grossSalary", grossSalary);
-            netSalary = netSalary.add(byUserId.getAccountBalance()).add(byUserId.getAccount());
-            map.put("netSalary", netSalary);
-        }
         // 2. 鏌ヨ璇ヤ汉鍛樺搴旂殑绀句繚鏂规
         List<SchemeApplicableStaff> schemeList = schemeApplicableStaffMapper.selectSchemeByStaffId(staffId);
         if (CollectionUtils.isEmpty(schemeList)) {
@@ -320,25 +332,30 @@
                 socialPersonal = socialPersonal.add(amount);
             }
         }
-        BigDecimal currentMonthSocialPersonal = socialPersonal;
+        BigDecimal currentMonthSocialPersonal = hasSocialPersonalInput ? defaultIfNull(socialPersonalInput) : socialPersonal;
+        if (!hasSocialPersonalInput) {
+            map.put("socialPersonal", currentMonthSocialPersonal);
+        }
+        BigDecimal resolvedFundPersonal = hasFundPersonalInput ? defaultIfNull(fundPersonalInput) : fundPersonal;
+        if (!hasFundPersonalInput) {
+            map.put("fundPersonal", resolvedFundPersonal);
+        }
         BigDecimal socialSupplementAmount = calculateSocialSupplementAmount(
                 staffId,
                 staffOnJobDto,
                 targetMonth,
                 currentScheme,
                 currentDetailList,
-                socialPersonal
+                currentMonthSocialPersonal
         );
         BigDecimal totalSocialPersonal = currentMonthSocialPersonal.add(socialSupplementAmount);
-        map.put("socialPersonal", currentMonthSocialPersonal);
-        map.put("fundPersonal", fundPersonal);
         map.put("socialSupplementAmount", socialSupplementAmount);
         // 涓◣閲戦锛堢ぞ淇濈増锛�
-        BigDecimal bigDecimal = TaxCalculator.calculateMonthlyTax(grossSalary, totalSocialPersonal, fundPersonal);
+        BigDecimal bigDecimal = TaxCalculator.calculateMonthlyTax(grossSalary, totalSocialPersonal, resolvedFundPersonal);
         map.put("salaryTax", bigDecimal);
 
         // 搴旀墸宸ヨ祫 = 涓◣ + 鍏Н閲戜釜浜� + 绀句繚涓汉 + 绀句繚琛ョ即 + 鍏朵粬鏀嚭
-        deductSalary = bigDecimal.add(fundPersonal).add(totalSocialPersonal).add(otherDeduct);
+        deductSalary = bigDecimal.add(resolvedFundPersonal).add(totalSocialPersonal).add(otherDeduct);
         map.put("deductSalary", deductSalary);
 
         // 瀹炲彂宸ヨ祫 = 搴斿彂宸ヨ祫 - 搴旀墸宸ヨ祫
@@ -347,6 +364,32 @@
 
     }
 
+    private BigDecimal getBigDecimal(Map<String, Object> map, String key) {
+        if (map == null || !map.containsKey(key) || map.get(key) == null) {
+            return null;
+        }
+        Object value = map.get(key);
+        if (value instanceof BigDecimal) {
+            return (BigDecimal) value;
+        }
+        if (value instanceof Number) {
+            return new BigDecimal(value.toString());
+        }
+        String text = String.valueOf(value).trim();
+        if (text.isEmpty()) {
+            return null;
+        }
+        return new BigDecimal(text);
+    }
+
+    private boolean hasValue(Map<String, Object> map, String key) {
+        return map != null && map.containsKey(key) && map.get(key) != null;
+    }
+
+    private BigDecimal defaultIfNull(BigDecimal value) {
+        return value == null ? BigDecimal.ZERO : value;
+    }
+
     /**
      * 閫氳繃鍛樺伐Id鑾峰彇鐢ㄦ埛id
      * @param staffId

--
Gitblit v1.9.3