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.HrmTaxRateConfigImportExcelVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmTaxRateConfigImportRespVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmTaxRateConfigDO; import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmTaxRateConfigMapper; 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.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; 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 HrmTaxRateConfigServiceImpl implements HrmTaxRateConfigService { @Resource private HrmTaxRateConfigMapper taxRateConfigMapper; @Override @Transactional(rollbackFor = Exception.class) public void createTaxRateConfigList(List configList) { for (HrmTaxRateConfigDO config : configList) { taxRateConfigMapper.insert(config); } } @Override public void updateTaxRateConfig(HrmTaxRateConfigDO config) { validateTaxRateConfigExists(config.getId()); taxRateConfigMapper.updateById(config); } @Override public void deleteTaxRateConfig(Long id) { validateTaxRateConfigExists(id); taxRateConfigMapper.deleteById(id); } @Override public HrmTaxRateConfigDO getTaxRateConfig(Long id) { return taxRateConfigMapper.selectById(id); } @Override public List getTaxRateConfigList() { return taxRateConfigMapper.selectListByStatus(null); } @Override public List getEffectiveTaxRateConfigList(LocalDate effectiveDate) { return taxRateConfigMapper.selectListByEffectiveDate(effectiveDate); } @Override @Transactional(rollbackFor = Exception.class) public HrmTaxRateConfigImportRespVO importTaxRateConfigList( 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. 逐行导入(同名 + 同生效日期 + 同级数视为同一条) Map existingMap = new HashMap<>(); for (HrmTaxRateConfigDO config : getTaxRateConfigList()) { existingMap.put(buildUniqueKey(config), config); } HrmTaxRateConfigImportRespVO respVO = HrmTaxRateConfigImportRespVO.builder() .createNames(new ArrayList<>()) .updateNames(new ArrayList<>()) .failureNames(new LinkedHashMap<>()) .build(); for (int i = 0; i < importList.size(); i++) { HrmTaxRateConfigImportExcelVO row = importList.get(i); String rowKey = "第" + (i + 2) + "行"; try { String name = HrmImportUtils.requireText(row.getName(), "配置名称"); if (row.getEffectiveDate() == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, "生效日期"); } if (row.getLevel() == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, "税率级数"); } if (row.getLevel() < 1 || row.getLevel() > 7) { throw exception(HRM_IMPORT_FIELD_INVALID, "税率级数", String.valueOf(row.getLevel()), "1 到 7 的整数"); } requireAmount(row.getMinAmount(), "应纳税所得额下限"); requireAmount(row.getMaxAmount(), "应纳税所得额上限"); requireAmount(row.getTaxRate(), "税率"); requireAmount(row.getQuickDeduction(), "速算扣除数"); Integer status = StrUtil.isBlank(row.getStatus()) ? 0 : HrmImportUtils.matchEnum(row.getStatus(), "状态", new String[]{"启用", "禁用"}, new Integer[]{0, 1}); HrmTaxRateConfigDO config = HrmTaxRateConfigDO.builder() .name(name) .effectiveDate(row.getEffectiveDate()) .level(row.getLevel()) .minAmount(row.getMinAmount()) .maxAmount(row.getMaxAmount()) .taxRate(row.getTaxRate()) .quickDeduction(row.getQuickDeduction()) .status(status) .remark(row.getRemark()) .build(); String display = name + " 第" + row.getLevel() + "级"; HrmTaxRateConfigDO existing = existingMap.get(buildUniqueKey(config)); if (existing != null) { if (!Boolean.TRUE.equals(updateSupport)) { throw new ServiceException(HRM_IMPORT_DATA_EXISTS.getCode(), "税率配置【" + display + "】(生效日期 " + row.getEffectiveDate() + ")已存在,如需覆盖请勾选「已存在时更新」"); } config.setId(existing.getId()); updateTaxRateConfig(config); respVO.getUpdateNames().add(display); continue; } taxRateConfigMapper.insert(config); respVO.getCreateNames().add(display); } catch (ServiceException ex) { respVO.getFailureNames().put(rowKey, ex.getMessage()); } catch (Exception ex) { respVO.getFailureNames().put(rowKey, "导入失败:" + ex.getMessage()); } } return respVO; } private String buildUniqueKey(HrmTaxRateConfigDO config) { return config.getName() + "|" + config.getEffectiveDate() + "|" + config.getLevel(); } private void requireAmount(BigDecimal value, String label) { if (value == null) { throw exception(HRM_IMPORT_FIELD_REQUIRED, label); } if (value.compareTo(BigDecimal.ZERO) < 0) { throw exception(HRM_IMPORT_FIELD_INVALID, label, value.toPlainString(), "不小于 0 的金额"); } } private void validateTaxRateConfigExists(Long id) { if (taxRateConfigMapper.selectById(id) == null) { throw exception(TAX_RATE_CONFIG_NOT_EXISTS); } } }