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.module.hrm.controller.admin.salary.vo.HrmSalaryStructureImportExcelVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryStructureImportRespVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryStructureDO; 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.HrmSalaryStructureMapper; 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 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 @Validated public class HrmSalaryStructureServiceImpl implements HrmSalaryStructureService { @Resource private HrmSalaryStructureMapper salaryStructureMapper; @Resource private HrmEmployeeMapper employeeMapper; @Resource private HrmEmployeeSalaryMapper employeeSalaryMapper; @Override public Long createSalaryStructure(HrmSalaryStructureDO structure) { validateNameUnique(null, structure.getName()); // 基本工资由员工档案维护。数据库历史字段非空,新建结构统一写入占位值 0。 structure.setBaseSalary(BigDecimal.ZERO); salaryStructureMapper.insert(structure); return structure.getId(); } @Override public void updateSalaryStructure(HrmSalaryStructureDO structure) { validateExists(structure.getId()); validateNameUnique(structure.getId(), structure.getName()); salaryStructureMapper.updateById(structure); } @Override public void deleteSalaryStructure(Long id) { validateExists(id); if (employeeMapper.selectCountBySalaryStructureId(id) > 0 || employeeSalaryMapper.selectCountBySalaryStructureId(id) > 0) { throw exception(SALARY_STRUCTURE_IN_USE); } salaryStructureMapper.deleteById(id); } @Override public HrmSalaryStructureDO getSalaryStructure(Long id) { return salaryStructureMapper.selectById(id); } @Override public List getSalaryStructureList(String name, Integer status) { return salaryStructureMapper.selectListByCondition(name, status); } @Override @Transactional(rollbackFor = Exception.class) public HrmSalaryStructureImportRespVO importSalaryStructureList( List importList, Boolean updateSupport) { // 1. 过滤空行(结构名称为空视为空行) importList = filterList(importList, item -> StrUtil.isNotBlank(item.getName())); if (CollUtil.isEmpty(importList)) { throw exception(HRM_IMPORT_LIST_IS_EMPTY); } // 2. 逐行导入 HrmSalaryStructureImportRespVO respVO = HrmSalaryStructureImportRespVO.builder() .createNames(new ArrayList<>()) .updateNames(new ArrayList<>()) .failureNames(new LinkedHashMap<>()) .build(); for (int i = 0; i < importList.size(); i++) { HrmSalaryStructureImportExcelVO row = importList.get(i); String rowKey = "第" + (i + 2) + "行"; try { String name = HrmImportUtils.requireText(row.getName(), "结构名称"); validateRatio("工作日加班倍率", row.getOvertimePayRatio()); validateRatio("休息日加班倍率", row.getRestDayOvertimePayRatio()); validateRatio("法定节假日加班倍率", row.getLegalHolidayOvertimePayRatio()); Integer status = StrUtil.isBlank(row.getStatus()) ? 0 : HrmImportUtils.matchEnum(row.getStatus(), "状态", new String[]{"启用", "禁用"}, new Integer[]{0, 1}); HrmSalaryStructureDO structure = HrmSalaryStructureDO.builder() .name(name) .overtimePayRatio(row.getOvertimePayRatio()) .restDayOvertimePayRatio(row.getRestDayOvertimePayRatio()) .legalHolidayOvertimePayRatio(row.getLegalHolidayOvertimePayRatio()) .status(status) .remark(row.getRemark()) .build(); HrmSalaryStructureDO existing = salaryStructureMapper.selectByName(name); if (existing != null) { if (!Boolean.TRUE.equals(updateSupport)) { throw new ServiceException(HRM_IMPORT_DATA_EXISTS.getCode(), "结构名称【" + name + "】已存在,如需覆盖请勾选「已存在时更新」"); } structure.setId(existing.getId()); updateSalaryStructure(structure); respVO.getUpdateNames().add(name); continue; } createSalaryStructure(structure); respVO.getCreateNames().add(name); } catch (ServiceException ex) { respVO.getFailureNames().put(rowKey, ex.getMessage()); } catch (Exception ex) { respVO.getFailureNames().put(rowKey, "导入失败:" + ex.getMessage()); } } return respVO; } private void validateRatio(String label, BigDecimal value) { if (value == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, label); } if (value.compareTo(BigDecimal.ZERO) < 0 || value.compareTo(new BigDecimal("10")) > 0) { throw exception(HRM_IMPORT_FIELD_INVALID, label, value.toPlainString(), "0 到 10 之间的倍数(如 1.5、2、3),0 表示该类加班不计工资"); } } private void validateExists(Long id) { if (id == null || salaryStructureMapper.selectById(id) == null) { throw exception(SALARY_STRUCTURE_NOT_EXISTS); } } private void validateNameUnique(Long id, String name) { HrmSalaryStructureDO structure = salaryStructureMapper.selectByName(name); if (structure != null && !structure.getId().equals(id)) { throw exception(SALARY_STRUCTURE_NAME_EXISTS); } } }