gaoluyang
2026-10-16 7081c41bc3beb0ab49a22974ea4b010ae6854afe
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityImportExcelVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityImportReqVO;
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.employee.HrmEmployeeMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmEmployeeSocialSecurityMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSocialSecuritySchemeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
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 HrmEmployeeSocialSecurityServiceImpl implements HrmEmployeeSocialSecurityService {
 
    @Resource
    private HrmEmployeeSocialSecurityMapper employeeSocialSecurityMapper;
    @Resource
    private HrmEmployeeMapper employeeMapper;
    @Resource
    private HrmSocialSecuritySchemeMapper schemeMapper;
 
    @Override
    public Long createEmployeeSocialSecurity(HrmEmployeeSocialSecurityDO socialSecurity) {
        employeeSocialSecurityMapper.insert(socialSecurity);
        return socialSecurity.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchCreateEmployeeSocialSecurity(List<HrmEmployeeSocialSecurityDO> 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<HrmEmployeeSocialSecurityDO> getEmployeeSocialSecurityPage(HrmEmployeeSocialSecurityPageReqVO pageReqVO) {
        return employeeSocialSecurityMapper.selectPage(pageReqVO);
    }
 
    @Override
    public HrmEmployeeSocialSecurityDO getLatestEmployeeSocialSecurity(Long userId) {
        return employeeSocialSecurityMapper.selectLatestByUserId(userId);
    }
 
    @Override
    public List<HrmEmployeeSocialSecurityDO> getEmployeeSocialSecurityList(Long userId) {
        return employeeSocialSecurityMapper.selectListByUserId(userId);
    }
 
    private void validateEmployeeSocialSecurityExists(Long id) {
        if (employeeSocialSecurityMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_SOCIAL_SECURITY_NOT_EXISTS);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HrmEmployeeSocialSecurityImportRespVO importEmployeeSocialSecurityList(
            List<HrmEmployeeSocialSecurityImportExcelVO> importList, HrmEmployeeSocialSecurityImportReqVO importReqVO) {
        // 1. 校验非空
        importList = filterList(importList, item -> StrUtil.isNotBlank(item.getEmployeeName()));
        if (CollUtil.isEmpty(importList)) {
            throw exception(EMPLOYEE_SOCIAL_SECURITY_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        HrmEmployeeSocialSecurityImportRespVO respVO = HrmEmployeeSocialSecurityImportRespVO.builder()
                .createNames(new ArrayList<>())
                .updateNames(new ArrayList<>())
                .failureNames(new LinkedHashMap<>())
                .build();
        importList.forEach(importSocialSecurity -> {
            try {
                // 2.1 解析员工
                HrmEmployeeDO employee = employeeMapper.selectByName(importSocialSecurity.getEmployeeName());
                if (employee == null) {
                    throw exception(EMPLOYEE_SOCIAL_SECURITY_EMPLOYEE_NOT_EXISTS, importSocialSecurity.getEmployeeName());
                }
                // 2.2 解析方案
                Long schemeId = null;
                if (StrUtil.isNotBlank(importSocialSecurity.getSchemeName())) {
                    HrmSocialSecuritySchemeDO scheme = schemeMapper.selectByName(importSocialSecurity.getSchemeName());
                    if (scheme == null) {
                        throw exception(EMPLOYEE_SOCIAL_SECURITY_SCHEME_NOT_EXISTS, importSocialSecurity.getSchemeName());
                    }
                    schemeId = scheme.getId();
                }
                // 2.3 生效日期必填
                if (importSocialSecurity.getEffectiveDate() == null) {
                    respVO.getFailureNames().put(importSocialSecurity.getEmployeeName(), "生效日期不能为空");
                    return;
                }
                // 2.4 判断是否存在(按员工+生效日期唯一)
                HrmEmployeeSocialSecurityDO existSocialSecurity = employeeSocialSecurityMapper
                        .selectByUserIdAndEffectiveDate(employee.getId(), importSocialSecurity.getEffectiveDate());
                if (existSocialSecurity == null) {
                    // 2.4.1 新增
                    HrmEmployeeSocialSecurityDO socialSecurity = BeanUtils.toBean(importSocialSecurity, HrmEmployeeSocialSecurityDO.class)
                            .setUserId(employee.getId()).setSocialSecuritySchemeId(schemeId);
                    if (socialSecurity.getStatus() == null) {
                        socialSecurity.setStatus(0);
                    }
                    employeeSocialSecurityMapper.insert(socialSecurity);
                    respVO.getCreateNames().add(importSocialSecurity.getEmployeeName());
                    return;
                }
                // 2.4.2 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureNames().put(importSocialSecurity.getEmployeeName(),
                            StrUtil.format(EMPLOYEE_SOCIAL_SECURITY_EXISTS.getMsg(), importSocialSecurity.getEmployeeName()));
                    return;
                }
                HrmEmployeeSocialSecurityDO updateSocialSecurity = BeanUtils.toBean(importSocialSecurity, HrmEmployeeSocialSecurityDO.class)
                        .setId(existSocialSecurity.getId()).setUserId(employee.getId()).setSocialSecuritySchemeId(schemeId);
                if (updateSocialSecurity.getStatus() == null) {
                    updateSocialSecurity.setStatus(0);
                }
                employeeSocialSecurityMapper.updateById(updateSocialSecurity);
                respVO.getUpdateNames().add(importSocialSecurity.getEmployeeName());
            } catch (ServiceException ex) {
                respVO.getFailureNames().put(importSocialSecurity.getEmployeeName(), ex.getMessage());
            }
        });
        return respVO;
    }
 
}