9 天以前 8c14b50773a1e582b652c03f5a63bb2233d069b8
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cn.iocoder.yudao.module.hrm.controller.admin.salary;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.HrmEmployeeSocialSecurityPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecurityRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmEmployeeSocialSecuritySaveReqVO;
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.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.hrm.service.salary.HrmEmployeeSocialSecurityService;
import cn.iocoder.yudao.module.hrm.service.salary.HrmSocialSecuritySchemeService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
@Tag(name = "管理后台 - HRM 员工社保公积金档案")
@RestController
@RequestMapping("/hrm/salary/employee-social-security")
@Validated
public class HrmEmployeeSocialSecurityController {
 
    @Resource
    private HrmEmployeeSocialSecurityService employeeSocialSecurityService;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private DeptApi deptApi;
    @Resource
    private HrmSocialSecuritySchemeService schemeService;
 
    @PostMapping("/create")
    @Operation(summary = "创建员工社保公积金档案")
    @PreAuthorize("@ss.hasPermission('hrm:social-security:create')")
    public CommonResult<Long> createEmployeeSocialSecurity(@Valid @RequestBody HrmEmployeeSocialSecuritySaveReqVO createReqVO) {
        HrmEmployeeSocialSecurityDO socialSecurity = BeanUtils.toBean(createReqVO, HrmEmployeeSocialSecurityDO.class);
        return success(employeeSocialSecurityService.createEmployeeSocialSecurity(socialSecurity));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新员工社保公积金档案")
    @PreAuthorize("@ss.hasPermission('hrm:social-security:update')")
    public CommonResult<Boolean> updateEmployeeSocialSecurity(@Valid @RequestBody HrmEmployeeSocialSecuritySaveReqVO updateReqVO) {
        HrmEmployeeSocialSecurityDO socialSecurity = BeanUtils.toBean(updateReqVO, HrmEmployeeSocialSecurityDO.class);
        employeeSocialSecurityService.updateEmployeeSocialSecurity(socialSecurity);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除员工社保公积金档案")
    @Parameter(name = "id", description = "档案ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:social-security:delete')")
    public CommonResult<Boolean> deleteEmployeeSocialSecurity(@RequestParam("id") Long id) {
        employeeSocialSecurityService.deleteEmployeeSocialSecurity(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获取员工社保公积金档案")
    @Parameter(name = "id", description = "档案ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:social-security:query')")
    public CommonResult<HrmEmployeeSocialSecurityRespVO> getEmployeeSocialSecurity(@RequestParam("id") Long id) {
        HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityService.getEmployeeSocialSecurity(id);
        if (socialSecurity == null) {
            return success(null);
        }
        HrmEmployeeSocialSecurityRespVO respVO = BeanUtils.toBean(socialSecurity, HrmEmployeeSocialSecurityRespVO.class);
        // 填充员工和方案信息
        fillEmployeeInfo(respVO, socialSecurity.getUserId());
        fillSchemeInfo(respVO, socialSecurity.getSocialSecuritySchemeId());
        return success(respVO);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获取员工社保公积金档案分页")
    @PreAuthorize("@ss.hasPermission('hrm:social-security:query')")
    public CommonResult<PageResult<HrmEmployeeSocialSecurityRespVO>> getEmployeeSocialSecurityPage(
            @Valid HrmEmployeeSocialSecurityPageReqVO pageReqVO) {
        PageResult<HrmEmployeeSocialSecurityDO> pageResult = employeeSocialSecurityService.getEmployeeSocialSecurityPage(pageReqVO);
        List<HrmEmployeeSocialSecurityDO> recordList = pageResult.getList();
        if (CollUtil.isEmpty(recordList)) {
            return success(new PageResult<>(Collections.emptyList(), pageResult.getTotal()));
        }
        // userId 是员工ID,批量获取员工信息
        Set<Long> employeeIds = recordList.stream()
                .map(HrmEmployeeSocialSecurityDO::getUserId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, HrmEmployeeDO> employeeMap = new HashMap<>();
        Set<Long> deptIds = new HashSet<>();
        for (Long employeeId : employeeIds) {
            HrmEmployeeDO employee = employeeService.getEmployee(employeeId);
            if (employee != null) {
                employeeMap.put(employeeId, employee);
                if (employee.getDeptId() != null) {
                    deptIds.add(employee.getDeptId());
                }
            }
        }
        // 批量获取部门信息
        Map<Long, DeptRespDTO> deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds);
        // 批量获取方案信息
        Set<Long> schemeIds = recordList.stream()
                .map(HrmEmployeeSocialSecurityDO::getSocialSecuritySchemeId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, HrmSocialSecuritySchemeDO> schemeMap = new HashMap<>();
        for (Long schemeId : schemeIds) {
            HrmSocialSecuritySchemeDO scheme = schemeService.getScheme(schemeId);
            if (scheme != null) {
                schemeMap.put(schemeId, scheme);
            }
        }
        // 转换并填充信息
        List<HrmEmployeeSocialSecurityRespVO> list = recordList.stream()
                .map(socialSecurity -> {
                    HrmEmployeeSocialSecurityRespVO respVO = BeanUtils.toBean(socialSecurity, HrmEmployeeSocialSecurityRespVO.class);
                    // 填充员工姓名和部门名称(userId 是员工ID)
                    HrmEmployeeDO employee = employeeMap.get(socialSecurity.getUserId());
                    if (employee != null) {
                        respVO.setUserName(employee.getName());
                        if (employee.getDeptId() != null) {
                            DeptRespDTO dept = deptMap.get(employee.getDeptId());
                            if (dept != null) {
                                respVO.setDeptName(dept.getName());
                            }
                        }
                    }
                    // 填充方案名称
                    if (socialSecurity.getSocialSecuritySchemeId() != null) {
                        HrmSocialSecuritySchemeDO scheme = schemeMap.get(socialSecurity.getSocialSecuritySchemeId());
                        if (scheme != null) {
                            respVO.setSchemeName(scheme.getName());
                        }
                    }
                    return respVO;
                })
                .toList();
        return success(new PageResult<>(list, pageResult.getTotal()));
    }
 
    @GetMapping("/list-by-user")
    @Operation(summary = "获取员工社保公积金档案历史列表")
    @Parameter(name = "userId", description = "系统用户ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:social-security:query')")
    public CommonResult<List<HrmEmployeeSocialSecurityRespVO>> getEmployeeSocialSecurityList(
            @RequestParam("userId") Long userId) {
        List<HrmEmployeeSocialSecurityDO> list = employeeSocialSecurityService.getEmployeeSocialSecurityList(userId);
        List<HrmEmployeeSocialSecurityRespVO> result = list.stream()
                .map(socialSecurity -> {
                    HrmEmployeeSocialSecurityRespVO respVO = BeanUtils.toBean(socialSecurity, HrmEmployeeSocialSecurityRespVO.class);
                    fillSchemeInfo(respVO, socialSecurity.getSocialSecuritySchemeId());
                    return respVO;
                })
                .toList();
        return success(result);
    }
 
    @GetMapping("/get-latest")
    @Operation(summary = "获取员工最新生效的社保公积金档案")
    @Parameter(name = "userId", description = "系统用户ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:social-security:query')")
    public CommonResult<HrmEmployeeSocialSecurityRespVO> getLatestEmployeeSocialSecurity(
            @RequestParam("userId") Long userId) {
        HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityService.getLatestEmployeeSocialSecurity(userId);
        if (socialSecurity == null) {
            return success(null);
        }
        HrmEmployeeSocialSecurityRespVO respVO = BeanUtils.toBean(socialSecurity, HrmEmployeeSocialSecurityRespVO.class);
        // 填充员工和方案信息
        fillEmployeeInfo(respVO, socialSecurity.getUserId());
        fillSchemeInfo(respVO, socialSecurity.getSocialSecuritySchemeId());
        return success(respVO);
    }
 
    /**
     * 填充员工信息(通过员工ID)
     */
    private void fillEmployeeInfo(HrmEmployeeSocialSecurityRespVO respVO, Long userId) {
        // userId 是员工ID,通过员工表获取姓名和部门
        HrmEmployeeDO employee = employeeService.getEmployee(userId);
        if (employee != null) {
            respVO.setUserName(employee.getName());
            if (employee.getDeptId() != null) {
                DeptRespDTO dept = deptApi.getDept(employee.getDeptId());
                if (dept != null) {
                    respVO.setDeptName(dept.getName());
                }
            }
        }
    }
 
    /**
     * 填充方案信息
     */
    private void fillSchemeInfo(HrmEmployeeSocialSecurityRespVO respVO, Long schemeId) {
        if (schemeId != null) {
            HrmSocialSecuritySchemeDO scheme = schemeService.getScheme(schemeId);
            if (scheme != null) {
                respVO.setSchemeName(scheme.getName());
            }
        }
    }
 
}