package cn.iocoder.yudao.module.hrm.service.salary; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityImportExcelVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityImportRespVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityPageReqVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmEmployeeSocialSecurityDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSocialSecuritySchemeDO; import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmEmployeeSocialSecurityMapper; import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService; import cn.iocoder.yudao.module.hrm.util.HrmImportUtils; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.TreeSet; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; /** * 员工社保公积金档案 Service 实现类 * * @author 超级管理员 */ @Service @Validated public class HrmEmployeeSocialSecurityServiceImpl implements HrmEmployeeSocialSecurityService { @Resource private HrmEmployeeSocialSecurityMapper employeeSocialSecurityMapper; @Resource private HrmEmployeeService employeeService; @Resource private HrmSocialSecuritySchemeService schemeService; @Override public Long createEmployeeSocialSecurity(HrmEmployeeSocialSecurityDO socialSecurity) { employeeSocialSecurityMapper.insert(socialSecurity); return socialSecurity.getId(); } @Override @Transactional(rollbackFor = Exception.class) public void batchCreateEmployeeSocialSecurity(List socialSecurityList) { for (HrmEmployeeSocialSecurityDO socialSecurity : socialSecurityList) { employeeSocialSecurityMapper.insert(socialSecurity); } } @Override public void updateEmployeeSocialSecurity(HrmEmployeeSocialSecurityDO socialSecurity) { validateEmployeeSocialSecurityExists(socialSecurity.getId()); employeeSocialSecurityMapper.updateById(socialSecurity); } @Override public void deleteEmployeeSocialSecurity(Long id) { validateEmployeeSocialSecurityExists(id); employeeSocialSecurityMapper.deleteById(id); } @Override public HrmEmployeeSocialSecurityDO getEmployeeSocialSecurity(Long id) { return employeeSocialSecurityMapper.selectById(id); } @Override public PageResult getEmployeeSocialSecurityPage(HrmEmployeeSocialSecurityPageReqVO pageReqVO) { return employeeSocialSecurityMapper.selectPage(pageReqVO); } @Override public HrmEmployeeSocialSecurityDO getLatestEmployeeSocialSecurity(Long userId) { return employeeSocialSecurityMapper.selectLatestByUserId(userId); } @Override public List getEmployeeSocialSecurityList(Long userId) { return employeeSocialSecurityMapper.selectListByUserId(userId); } @Override @Transactional(rollbackFor = Exception.class) public HrmEmployeeSocialSecurityImportRespVO importEmployeeSocialSecurityList( List importList, Boolean updateSupport) { // 1. 校验非空(姓名和手机号都为空的视为空行,空行在下面循环里跳过,但行号仍按表格实际行号计算) boolean hasDataRow = importList.stream().anyMatch( item -> StrUtil.isNotBlank(item.getUserName()) || StrUtil.isNotBlank(item.getPhone())); if (!hasDataRow) { throw exception(HRM_IMPORT_LIST_IS_EMPTY); } // 2. 预加载方案名称索引 Map> schemeIdMap = schemeService.getSchemeList(null).stream() .collect(Collectors.groupingBy(HrmSocialSecuritySchemeDO::getName, Collectors.mapping(HrmSocialSecuritySchemeDO::getId, Collectors.toList()))); // 3. 逐行导入 HrmEmployeeSocialSecurityImportRespVO respVO = HrmEmployeeSocialSecurityImportRespVO.builder() .createNames(new ArrayList<>()) .updateNames(new ArrayList<>()) .failureNames(new LinkedHashMap<>()) .build(); for (int i = 0; i < importList.size(); i++) { HrmEmployeeSocialSecurityImportExcelVO row = importList.get(i); if (StrUtil.isBlank(row.getUserName()) && StrUtil.isBlank(row.getPhone())) { continue; // 空行跳过;i 是表格里的原始行下标,提示行号不会错位 } String rowKey = "第" + (i + 2) + "行"; try { importEmployeeSocialSecurityRow(row, schemeIdMap, Boolean.TRUE.equals(updateSupport), respVO); } catch (ServiceException ex) { respVO.getFailureNames().put(rowKey, ex.getMessage()); } catch (Exception ex) { respVO.getFailureNames().put(rowKey, "导入失败:" + ex.getMessage()); } } return respVO; } private void importEmployeeSocialSecurityRow(HrmEmployeeSocialSecurityImportExcelVO row, Map> schemeIdMap, boolean updateSupport, HrmEmployeeSocialSecurityImportRespVO respVO) { HrmEmployeeDO employee = HrmImportUtils.resolveEmployee(employeeService, row.getUserName(), row.getPhone()); Long schemeId = resolveSchemeId(schemeIdMap, row.getSchemeName()); BigDecimal socialSecurityBase = requireBase(row.getSocialSecurityBase(), "社保缴纳基数"); BigDecimal housingFundBase = requireBase(row.getHousingFundBase(), "公积金缴纳基数"); if (row.getEffectiveDate() == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, "生效日期"); } Integer status = StrUtil.isBlank(row.getStatus()) ? 0 : HrmImportUtils.matchEnum(row.getStatus(), "状态", new String[]{"启用", "禁用"}, new Integer[]{0, 1}); // 同一员工、同一生效日期视为同一条档案 HrmEmployeeSocialSecurityDO existing = employeeSocialSecurityMapper .selectListByUserId(employee.getId()).stream() .filter(item -> Objects.equals(item.getEffectiveDate(), row.getEffectiveDate())) .findFirst().orElse(null); String display = employee.getName() + " " + row.getEffectiveDate(); if (existing != null) { if (!updateSupport) { throw new ServiceException(HRM_IMPORT_DATA_EXISTS.getCode(), "员工【" + employee.getName() + "】在生效日期【" + row.getEffectiveDate() + "】已有社保公积金档案,如需覆盖请勾选「已存在时更新」"); } employeeSocialSecurityMapper.updateById(HrmEmployeeSocialSecurityDO.builder() .id(existing.getId()) .socialSecuritySchemeId(schemeId) .socialSecurityBase(socialSecurityBase) .housingFundBase(housingFundBase) .status(status) .remark(row.getRemark()) .build()); respVO.getUpdateNames().add(display); return; } employeeSocialSecurityMapper.insert(HrmEmployeeSocialSecurityDO.builder() .userId(employee.getId()) .socialSecuritySchemeId(schemeId) .socialSecurityBase(socialSecurityBase) .housingFundBase(housingFundBase) .effectiveDate(row.getEffectiveDate()) .status(status) .remark(row.getRemark()) .build()); respVO.getCreateNames().add(display); } private Long resolveSchemeId(Map> schemeIdMap, String schemeName) { String cleanName = HrmImportUtils.requireText(schemeName, "社保公积金方案"); List ids = schemeIdMap.get(cleanName); if (CollUtil.isEmpty(ids)) { // 把系统现有的方案名一并告诉用户,避免只报「不存在」还得自己去猜该填什么 String existing = schemeIdMap.isEmpty() ? "系统里还没有任何方案,请先在「社保公积金方案」中新增" : "系统现有方案:" + String.join("、", new TreeSet<>(schemeIdMap.keySet())) + "。请把表格里「社保公积金方案」列改成其中之一,或先在「社保公积金方案」中新增该方案"; throw new ServiceException(HRM_IMPORT_SCHEME_NOT_EXISTS.getCode(), "表格填写的方案【" + cleanName + "】不存在," + existing); } if (ids.size() > 1) { throw exception(HRM_IMPORT_FIELD_INVALID, "社保公积金方案", cleanName, "系统中有多个同名方案,请先在「社保公积金方案」中改名区分"); } return ids.get(0); } private BigDecimal requireBase(BigDecimal base, String label) { if (base == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, label); } if (base.compareTo(BigDecimal.ZERO) < 0) { throw exception(HRM_IMPORT_FIELD_INVALID, label, base.toPlainString(), "不小于 0 的金额,如 5000"); } return base; } private void validateEmployeeSocialSecurityExists(Long id) { if (employeeSocialSecurityMapper.selectById(id) == null) { throw exception(EMPLOYEE_SOCIAL_SECURITY_NOT_EXISTS); } } }