From f688d1656a6542f1756f62e2f10a11ff4489674a Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 12 六月 2026 15:28:54 +0800
Subject: [PATCH] 修改社保工资计算

---
 src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java |  246 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 216 insertions(+), 30 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 f06704d..be8abe5 100644
--- a/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java
+++ b/src/main/java/com/ruoyi/staff/service/impl/SchemeApplicableStaffServiceImpl.java
@@ -2,26 +2,33 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.production.bean.dto.UserAccountDto;
+import com.ruoyi.production.bean.dto.UserProductionAccountingDto;
+import com.ruoyi.production.service.SalesLedgerProductionAccountingService;
 import com.ruoyi.project.system.domain.SysDept;
 import com.ruoyi.project.system.domain.SysUser;
-import com.ruoyi.project.system.domain.SysUserDept;
 import com.ruoyi.project.system.mapper.SysDeptMapper;
-import com.ruoyi.project.system.mapper.SysUserDeptMapper;
 import com.ruoyi.project.system.mapper.SysUserMapper;
-import com.ruoyi.staff.mapper.SchemeInsuranceDetailMapper;
-import com.ruoyi.staff.pojo.SchemeApplicableStaff;
+import com.ruoyi.staff.controller.TaxCalculator;
 import com.ruoyi.staff.mapper.SchemeApplicableStaffMapper;
+import com.ruoyi.staff.mapper.SchemeInsuranceDetailMapper;
+import com.ruoyi.staff.mapper.StaffOnJobMapper;
+import com.ruoyi.staff.pojo.SchemeApplicableStaff;
 import com.ruoyi.staff.pojo.SchemeInsuranceDetail;
+import com.ruoyi.staff.pojo.StaffOnJob;
 import com.ruoyi.staff.service.SchemeApplicableStaffService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import org.springframework.beans.factory.annotation.Autowired;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
@@ -34,22 +41,15 @@
  * @since 2026-03-05 11:50:17
  */
 @Service
+@RequiredArgsConstructor
 public class SchemeApplicableStaffServiceImpl extends ServiceImpl<SchemeApplicableStaffMapper, SchemeApplicableStaff> implements SchemeApplicableStaffService {
 
-    @Autowired
-    private SchemeApplicableStaffMapper schemeApplicableStaffMapper;
-
-    @Autowired
-    private SchemeInsuranceDetailMapper schemeInsuranceDetailMapper;
-
-    @Autowired
-    private SysUserDeptMapper sysUserDeptMapper;
-
-    @Autowired
-    private SysUserMapper sysUserMapper;
-
-    @Autowired
-    private SysDeptMapper sysDeptMapper;
+    private final SchemeApplicableStaffMapper schemeApplicableStaffMapper;
+    private final SchemeInsuranceDetailMapper schemeInsuranceDetailMapper;
+    private final SysUserMapper sysUserMapper;
+    private final SysDeptMapper sysDeptMapper;
+    private final StaffOnJobMapper staffOnJobMapper;
+    private final SalesLedgerProductionAccountingService salesLedgerProductionAccountingService;
 
 
     @Override
@@ -60,6 +60,7 @@
                 schemeApplicableStaffLambdaQueryWrapper.like(SchemeApplicableStaff::getTitle, schemeApplicableStaff.getTitle());
             }
         }
+        schemeApplicableStaffLambdaQueryWrapper.orderByDesc(SchemeApplicableStaff::getId);
         Page<SchemeApplicableStaff> page1 = schemeApplicableStaffMapper.selectPage(page, schemeApplicableStaffLambdaQueryWrapper);
         List<Long> collect = page1.getRecords().stream().map(SchemeApplicableStaff::getId).collect(Collectors.toList());
         if(CollectionUtils.isEmpty(collect)){
@@ -88,22 +89,19 @@
 
     public void setSchemeApplicableStaffUserInfo(SchemeApplicableStaff schemeApplicableStaff) {
         // 閫氳繃閮ㄩ棬鑾峰彇浜哄憳id
-        List<SysUserDept> sysUserDepts = sysUserDeptMapper.selectList(new LambdaQueryWrapper<SysUserDept>()
-                .in(SysUserDept::getDeptId, schemeApplicableStaff.getDeptIds()));
-        if(CollectionUtils.isEmpty(sysUserDepts)){
+        String[] split = schemeApplicableStaff.getDeptIds().split(",");
+        List<StaffOnJob> staffOnJobs = staffOnJobMapper.selectList(new LambdaQueryWrapper<StaffOnJob>()
+                .in(StaffOnJob::getSysDeptId, Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList())));
+        if(CollectionUtils.isEmpty(staffOnJobs)){
             throw new IllegalArgumentException("閮ㄩ棬涓嬫棤鍛樺伐");
         }
-        List<SysUser> sysUsers = sysUserMapper.selectUserByIds(sysUserDepts.stream().map(SysUserDept::getUserId).collect(Collectors.toList()));
-        if(CollectionUtils.isEmpty(sysUsers)){
-            throw new IllegalArgumentException("閮ㄩ棬涓嬫棤鍛樺伐");
-        }
-        schemeApplicableStaff.setStaffIds(sysUsers
+        schemeApplicableStaff.setStaffIds(staffOnJobs
                 .stream()
-                .map(SysUser::getUserId)
+                .map(StaffOnJob::getId)
                 .filter(Objects::nonNull)         // 杩囨护鎺� null 鍊�
                 .map(String::valueOf)
                 .collect(Collectors.joining( ",")));
-        schemeApplicableStaff.setStaffNames(sysUsers.stream().map(SysUser::getNickName).collect(Collectors.joining(",")));
+        schemeApplicableStaff.setStaffNames(staffOnJobs.stream().map(StaffOnJob::getStaffName).collect(Collectors.joining(",")));
     }
 
     @Override
@@ -150,4 +148,192 @@
                 .in(SchemeInsuranceDetail::getSchemeId, ids));
         return AjaxResult.success(delete);
     }
+
+    /**
+     * 閫氳繃鍛樺伐id璁$畻绀句繚鏂规
+     * @param id
+     */
+    public void calculateByEmployeeId(Integer id,Map<String, Object> map,String date) {
+        if (id == null || map == null) {
+            return;
+        }
+        Long staffId = id.longValue();
+        StaffOnJob staffOnJobDto = staffOnJobMapper.selectById(staffId);
+        if(staffOnJobDto == null){
+            return;
+        }
+        BigDecimal basicSalary = hasValue(map, "basicSalary")
+                ? normalizeMoney(getBigDecimal(map, "basicSalary"))
+                : normalizeMoney(staffOnJobDto.getBasicSalary());
+        BigDecimal otherIncome = normalizeMoney(getBigDecimal(map, "otherIncome"));
+        BigDecimal otherDeduct = normalizeMoney(getBigDecimal(map, "otherDeduct"));
+        BigDecimal socialSupplementAmount = hasValue(map, "socialSupplementAmount")
+                ? normalizeMoney(getBigDecimal(map, "socialSupplementAmount"))
+                : calculateLatestDeptSocialSupplementAmount(staffOnJobDto);
+        BigDecimal pieceSalary = normalizeMoney(getBigDecimal(map, "pieceSalary"));
+        BigDecimal hourlySalary = normalizeMoney(getBigDecimal(map, "hourlySalary"));
+        BigDecimal socialPersonal = normalizeMoney(getBigDecimal(map, "socialPersonal"));
+        BigDecimal fundPersonal = normalizeMoney(getBigDecimal(map, "fundPersonal"));
+
+        UserProductionAccountingDto userProductionAccountingDto = new UserProductionAccountingDto();
+        userProductionAccountingDto.setUserId(getUidByStaffId(staffId));
+        userProductionAccountingDto.setDate(date);
+        UserAccountDto byUserId = salesLedgerProductionAccountingService.getByUserId(userProductionAccountingDto);
+        if(byUserId != null){
+            pieceSalary = normalizeMoney(byUserId.getAccountBalance());
+            hourlySalary = normalizeMoney(byUserId.getAccount());
+        }
+
+        // 鏌ヨ璇ヤ汉鍛樺搴旂殑绀句繚鏂规
+        List<SchemeApplicableStaff> schemeList = schemeApplicableStaffMapper.selectSchemeByStaffId(staffId);
+        if (!CollectionUtils.isEmpty(schemeList)) {
+            for (SchemeApplicableStaff scheme : schemeList) {
+                List<SchemeInsuranceDetail> detailList = schemeApplicableStaffMapper.selectDetailBySchemeId(scheme.getId());
+                if(CollectionUtils.isEmpty(detailList)){
+                    continue;
+                }
+                for (SchemeInsuranceDetail detail : detailList) {
+                    if("鍏Н閲�".equals(detail.getInsuranceType())){
+                        fundPersonal = fundPersonal.add(calculateByEmployeeIdType(detail.getInsuranceType(), fundPersonal, staffOnJobDto, detail));
+                    }else{
+                        socialPersonal = socialPersonal.add(calculateByEmployeeIdType(detail.getInsuranceType(), socialPersonal, staffOnJobDto, detail));
+                    }
+                }
+            }
+        }
+
+        BigDecimal grossSalary = basicSalary.add(pieceSalary).add(hourlySalary).add(otherIncome);
+        BigDecimal salaryTax = TaxCalculator.calculateMonthlyTax(
+                grossSalary,
+                socialPersonal.add(socialSupplementAmount),
+                fundPersonal
+        );
+        BigDecimal deductSalary = salaryTax
+                .add(fundPersonal)
+                .add(socialPersonal)
+                .add(socialSupplementAmount)
+                .add(otherDeduct);
+        BigDecimal netSalary = grossSalary.subtract(deductSalary);
+
+        if (!hasText(map, "staffName")) {
+            map.put("staffName", staffOnJobDto.getStaffName());
+        }
+        if (!hasText(map, "nation")) {
+            map.put("nation", staffOnJobDto.getNation());
+        }
+        map.put("basicSalary", basicSalary);
+        map.put("pieceSalary", pieceSalary);
+        map.put("hourlySalary", hourlySalary);
+        map.put("otherIncome", otherIncome);
+        map.put("socialPersonal", socialPersonal);
+        map.put("fundPersonal", fundPersonal);
+        map.put("otherDeduct", otherDeduct);
+        map.put("socialSupplementAmount", socialSupplementAmount);
+        map.put("grossSalary", grossSalary);
+        map.put("salaryTax", salaryTax);
+        map.put("deductSalary", deductSalary);
+        map.put("netSalary", netSalary);
+    }
+
+    /**
+     * 閫氳繃鍛樺伐Id鑾峰彇鐢ㄦ埛id
+     * @param staffId
+     * @return
+     */
+    public Long getUidByStaffId(Long staffId){
+        StaffOnJob staffOnJob = staffOnJobMapper.selectById(staffId);
+        if(staffOnJob == null){
+            return -1L; // 杩斿洖涓嶅瓨鍦↖d
+        }
+        SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>()
+                .eq(SysUser::getUserName, staffOnJob.getStaffNo())
+                .eq(SysUser::getDelFlag, "0")
+                .last("limit 1"));
+        if(sysUser == null){
+            return -1L; // 杩斿洖涓嶅瓨鍦↖d
+        }
+        return sysUser.getUserId();
+    }
+
+    /**
+     * 璁$畻
+     * @param type
+     * @param bigDecimal
+     * @param staffOnJobDto
+     * @param detail
+     * @return
+     */
+    public BigDecimal calculateByEmployeeIdType(String type,BigDecimal bigDecimal, StaffOnJob staffOnJobDto,SchemeInsuranceDetail detail) {
+        // 鍒ゆ柇鏄惁璋冪敤鍩烘湰宸ヨ祫
+        if (detail.getUseBasicSalary() == 1) {
+            BigDecimal divide = detail.getPaymentBase().multiply(detail.getPersonalRatio()).divide(new BigDecimal("100"), 2);
+            bigDecimal = bigDecimal.add(divide);
+        }else{
+            // 璋冪敤鍩烘湰宸ヨ祫
+            BigDecimal multiply = staffOnJobDto.getBasicSalary().multiply(detail.getPersonalRatio().divide(new BigDecimal("100"), 2));
+            bigDecimal = bigDecimal.add(multiply);
+        }
+        bigDecimal = bigDecimal.add(detail.getPersonalFixed());
+        return bigDecimal;
+    }
+
+    private BigDecimal calculateLatestDeptSocialSupplementAmount(StaffOnJob staffOnJobDto) {
+        if (staffOnJobDto == null || staffOnJobDto.getSysDeptId() == null) {
+            return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
+        }
+        SchemeApplicableStaff latestScheme = schemeApplicableStaffMapper.selectLatestSchemeByDeptId(staffOnJobDto.getSysDeptId());
+        if (latestScheme == null) {
+            return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
+        }
+        List<SchemeInsuranceDetail> detailList = schemeApplicableStaffMapper.selectDetailBySchemeId(latestScheme.getId());
+        if (CollectionUtils.isEmpty(detailList)) {
+            return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
+        }
+        BigDecimal socialSupplementAmount = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
+        for (SchemeInsuranceDetail detail : detailList) {
+            if (!"鍏Н閲�".equals(detail.getInsuranceType())) {
+                socialSupplementAmount = socialSupplementAmount.add(
+                        calculateByEmployeeIdType(detail.getInsuranceType(), socialSupplementAmount, staffOnJobDto, detail)
+                );
+            }
+        }
+        return socialSupplementAmount.setScale(2, RoundingMode.HALF_UP);
+    }
+
+    private boolean hasValue(Map<String, Object> map, String key) {
+        return map != null && map.get(key) != null;
+    }
+
+    private boolean hasText(Map<String, Object> map, String key) {
+        if (map == null) {
+            return false;
+        }
+        Object value = map.get(key);
+        return value != null && !String.valueOf(value).trim().isEmpty();
+    }
+
+    private BigDecimal getBigDecimal(Map<String, Object> map, String key) {
+        if (map == null) {
+            return null;
+        }
+        Object value = map.get(key);
+        if (value == null) {
+            return null;
+        }
+        if (value instanceof BigDecimal) {
+            return (BigDecimal) value;
+        }
+        if (value instanceof Number) {
+            return new BigDecimal(String.valueOf(value));
+        }
+        String text = String.valueOf(value).trim();
+        if (text.isEmpty()) {
+            return null;
+        }
+        return new BigDecimal(text);
+    }
+
+    private BigDecimal normalizeMoney(BigDecimal value) {
+        return value == null ? BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP) : value.setScale(2, RoundingMode.HALF_UP);
+    }
 }

--
Gitblit v1.9.3