云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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.util.object.BeanUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmTaxRateConfigImportExcelVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmTaxRateConfigImportReqVO;
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 jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDate;
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 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class HrmTaxRateConfigServiceImpl implements HrmTaxRateConfigService {
 
    @Resource
    private HrmTaxRateConfigMapper taxRateConfigMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTaxRateConfigList(List<HrmTaxRateConfigDO> 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<HrmTaxRateConfigDO> getTaxRateConfigList() {
        return taxRateConfigMapper.selectListByStatus(null);
    }
 
    @Override
    public List<HrmTaxRateConfigDO> getEffectiveTaxRateConfigList(LocalDate effectiveDate) {
        return taxRateConfigMapper.selectListByEffectiveDate(effectiveDate);
    }
 
    private void validateTaxRateConfigExists(Long id) {
        if (taxRateConfigMapper.selectById(id) == null) {
            throw exception(TAX_RATE_CONFIG_NOT_EXISTS);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HrmTaxRateConfigImportRespVO importTaxRateConfigList(
            List<HrmTaxRateConfigImportExcelVO> importList, HrmTaxRateConfigImportReqVO importReqVO) {
        // 1. 校验非空
        importList = filterList(importList, item -> item.getLevel() != null);
        if (CollUtil.isEmpty(importList)) {
            throw exception(TAX_RATE_CONFIG_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        HrmTaxRateConfigImportRespVO respVO = HrmTaxRateConfigImportRespVO.builder()
                .createNames(new ArrayList<>())
                .updateNames(new ArrayList<>())
                .failureNames(new LinkedHashMap<>())
                .build();
        importList.forEach(importConfig -> {
            String configKey = StrUtil.isNotBlank(importConfig.getName()) ? importConfig.getName()
                    : (importConfig.getEffectiveDate() + "-" + importConfig.getLevel());
            try {
                // 2.1 生效日期与级数必填
                if (importConfig.getEffectiveDate() == null) {
                    respVO.getFailureNames().put(configKey, "生效日期不能为空");
                    return;
                }
                // 2.2 判断是否存在(按生效日期+级数唯一)
                HrmTaxRateConfigDO existConfig = taxRateConfigMapper.selectByEffectiveDateAndLevel(
                        importConfig.getEffectiveDate(), importConfig.getLevel());
                if (existConfig == null) {
                    // 2.2.1 新增
                    HrmTaxRateConfigDO config = BeanUtils.toBean(importConfig, HrmTaxRateConfigDO.class);
                    if (config.getStatus() == null) {
                        config.setStatus(0);
                    }
                    taxRateConfigMapper.insert(config);
                    respVO.getCreateNames().add(configKey);
                    return;
                }
                // 2.2.2 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureNames().put(configKey,
                            StrUtil.format(TAX_RATE_CONFIG_EXISTS.getMsg(), configKey));
                    return;
                }
                HrmTaxRateConfigDO updateConfig = BeanUtils.toBean(importConfig, HrmTaxRateConfigDO.class)
                        .setId(existConfig.getId());
                if (updateConfig.getStatus() == null) {
                    updateConfig.setStatus(0);
                }
                taxRateConfigMapper.updateById(updateConfig);
                respVO.getUpdateNames().add(configKey);
            } catch (ServiceException ex) {
                respVO.getFailureNames().put(configKey, ex.getMessage());
            }
        });
        return respVO;
    }
 
}