liyong
9 天以前 88e384da863bb2f7324cb1e1474885df3b7cce91
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
package cn.iocoder.yudao.module.hrm.controller.admin.salary;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryCalculationPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryCalculationRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryCalculationSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryCalculationDO;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.hrm.service.salary.HrmSalaryCalculationService;
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.servlet.http.HttpServletResponse;
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.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
@Tag(name = "管理后台 - HRM 薪酬核算")
@RestController
@RequestMapping("/hrm/salary/calculation")
@Validated
public class HrmSalaryCalculationController {
 
    @Resource
    private HrmSalaryCalculationService salaryCalculationService;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private DeptApi deptApi;
 
    @GetMapping("/page")
    @Operation(summary = "获得薪酬核算分页")
    @PreAuthorize("@ss.hasPermission('hrm:salary:query')")
    public CommonResult<PageResult<HrmSalaryCalculationRespVO>> getSalaryCalculationPage(@Valid HrmSalaryCalculationPageReqVO pageReqVO) {
        PageResult<HrmSalaryCalculationDO> pageResult = salaryCalculationService.getSalaryCalculationPage(pageReqVO);
        // 转换为 VO
        List<HrmSalaryCalculationRespVO> list = BeanUtils.toBean(pageResult.getList(), HrmSalaryCalculationRespVO.class);
        // 填充员工和部门信息
        if (CollUtil.isNotEmpty(list)) {
            fillUserInfo(list, pageResult.getList());
        }
        return success(new PageResult<>(list, pageResult.getTotal()));
    }
 
    /**
     * 填充员工和部门信息(userId 是员工ID)
     */
    private void fillUserInfo(List<HrmSalaryCalculationRespVO> respList, List<HrmSalaryCalculationDO> calculationList) {
        // userId 是员工ID,批量获取员工信息
        Set<Long> employeeIds = calculationList.stream()
                .map(HrmSalaryCalculationDO::getUserId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, HrmEmployeeDO> employeeMap = new HashMap<>();
        for (Long employeeId : employeeIds) {
            HrmEmployeeDO employee = employeeService.getEmployee(employeeId);
            if (employee != null) {
                employeeMap.put(employeeId, employee);
            }
        }
 
        // 批量获取部门信息
        Set<Long> deptIds = calculationList.stream()
                .map(HrmSalaryCalculationDO::getDeptId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, DeptRespDTO> deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds);
 
        // 填充信息
        for (HrmSalaryCalculationRespVO respVO : respList) {
            HrmEmployeeDO employee = employeeMap.get(respVO.getUserId());
            if (employee != null) {
                respVO.setUserName(employee.getName());
            }
            DeptRespDTO dept = deptMap.get(respVO.getDeptId());
            if (dept != null) {
                respVO.setDeptName(dept.getName());
            }
        }
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得薪酬核算")
    @Parameter(name = "id", description = "核算ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:salary:query')")
    public CommonResult<HrmSalaryCalculationRespVO> getSalaryCalculation(@RequestParam("id") Long id) {
        HrmSalaryCalculationDO salaryCalculation = salaryCalculationService.getSalaryCalculation(id);
        if (salaryCalculation == null) {
            return success(null);
        }
        HrmSalaryCalculationRespVO respVO = BeanUtils.toBean(salaryCalculation, HrmSalaryCalculationRespVO.class);
        // 填充员工和部门信息(userId 是员工ID)
        if (salaryCalculation.getUserId() != null) {
            HrmEmployeeDO employee = employeeService.getEmployee(salaryCalculation.getUserId());
            if (employee != null) {
                respVO.setUserName(employee.getName());
            }
        }
        if (salaryCalculation.getDeptId() != null) {
            DeptRespDTO dept = deptApi.getDept(salaryCalculation.getDeptId());
            if (dept != null) {
                respVO.setDeptName(dept.getName());
            }
        }
        return success(respVO);
    }
 
    @PostMapping("/calculate")
    @Operation(summary = "执行薪酬核算")
    @Parameter(name = "period", description = "核算周期", required = true, example = "2024-01")
    @Parameter(name = "deptId", description = "部门ID,为空则核算所有部门", example = "1")
    @PreAuthorize("@ss.hasPermission('hrm:salary:calculate')")
    public CommonResult<Boolean> calculateSalary(@RequestParam("period") String period,
                                                  @RequestParam(value = "deptId", required = false) Long deptId) {
        salaryCalculationService.calculateSalary(period, deptId);
        return success(true);
    }
 
    @PostMapping("/confirm")
    @Operation(summary = "批量确认薪酬核算")
    @PreAuthorize("@ss.hasPermission('hrm:salary:confirm')")
    public CommonResult<Boolean> confirmSalaryCalculation(@RequestBody Map<String, List<Long>> request) {
        List<Long> ids = request.get("ids");
        salaryCalculationService.confirmSalaryCalculation(ids);
        return success(true);
    }
 
    @PostMapping("/revoke")
    @Operation(summary = "批量撤销薪酬核算确认")
    @PreAuthorize("@ss.hasPermission('hrm:salary:confirm')")
    public CommonResult<Boolean> revokeSalaryCalculation(@RequestBody Map<String, List<Long>> request) {
        List<Long> ids = request.get("ids");
        salaryCalculationService.revokeSalaryCalculation(ids);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除薪酬核算记录")
    @Parameter(name = "id", description = "核算ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:salary:delete')")
    public CommonResult<Boolean> deleteSalaryCalculation(@RequestParam("id") Long id) {
        salaryCalculationService.deleteSalaryCalculation(id);
        return success(true);
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出薪酬核算 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:salary:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSalaryCalculationExcel(@Valid HrmSalaryCalculationPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<HrmSalaryCalculationDO> pageResult = salaryCalculationService.getSalaryCalculationPage(pageReqVO);
        List<HrmSalaryCalculationDO> calculationList = pageResult.getList();
        // 转换为 VO
        List<HrmSalaryCalculationRespVO> list = BeanUtils.toBean(calculationList, HrmSalaryCalculationRespVO.class);
        // 填充员工和部门信息
        if (CollUtil.isNotEmpty(list)) {
            fillUserInfo(list, calculationList);
        }
        ExcelUtils.write(response, "薪酬核算.xls", "数据", HrmSalaryCalculationRespVO.class, list);
    }
 
    @GetMapping("/preview")
    @Operation(summary = "预览部门下所有员工的薪资核算信息")
    @Parameter(name = "period", description = "核算周期", required = true, example = "2024-01")
    @Parameter(name = "deptId", description = "部门ID,为空则查询全部", example = "1")
    @PreAuthorize("@ss.hasPermission('hrm:salary:query')")
    public CommonResult<List<HrmSalaryCalculationRespVO>> previewSalaryCalculation(
            @RequestParam("period") String period,
            @RequestParam(value = "deptId", required = false) Long deptId) {
        // 查询已计算好的薪酬核算记录
        List<HrmSalaryCalculationDO> list = salaryCalculationService.previewSalaryCalculation(period, deptId);
        // 转换为 VO
        List<HrmSalaryCalculationRespVO> respList = BeanUtils.toBean(list, HrmSalaryCalculationRespVO.class);
        // 填充员工和部门信息
        if (CollUtil.isNotEmpty(respList)) {
            fillUserInfo(respList, list);
        }
        return success(respList);
    }
 
    @PostMapping("/save")
    @Operation(summary = "批量保存薪酬核算记录")
    @PreAuthorize("@ss.hasPermission('hrm:salary:calculate')")
    public CommonResult<Boolean> saveSalaryCalculation(@Valid @RequestBody HrmSalaryCalculationSaveReqVO saveReqVO) {
        // 转换为 DO 列表
        List<HrmSalaryCalculationDO> calculationList = saveReqVO.getList().stream()
                .map(item -> {
                    HrmSalaryCalculationDO calculation = BeanUtils.toBean(item, HrmSalaryCalculationDO.class);
                    calculation.setPeriod(saveReqVO.getPeriod());
                    return calculation;
                })
                .collect(Collectors.toList());
        // 批量保存
        salaryCalculationService.saveSalaryCalculationList(calculationList);
        return success(true);
    }
 
}