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.HrmSalaryPaymentDetailPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryPaymentDetailRespVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryPaymentPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryPaymentRespVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDetailDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDO; import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService; import cn.iocoder.yudao.module.hrm.service.salary.HrmSalaryPaymentService; 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/payment") @Validated public class HrmSalaryPaymentController { @Resource private HrmSalaryPaymentService salaryPaymentService; @Resource private HrmEmployeeService employeeService; @Resource private DeptApi deptApi; @GetMapping("/page") @Operation(summary = "获得薪资发放分页") @PreAuthorize("@ss.hasPermission('hrm:salary:query')") public CommonResult> getSalaryPaymentPage(@Valid HrmSalaryPaymentPageReqVO pageReqVO) { PageResult pageResult = salaryPaymentService.getSalaryPaymentPage(pageReqVO); return success(BeanUtils.toBean(pageResult, HrmSalaryPaymentRespVO.class)); } @GetMapping("/get") @Operation(summary = "获得薪资发放") @Parameter(name = "id", description = "发放ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:salary:query')") public CommonResult getSalaryPayment(@RequestParam("id") Long id) { HrmSalaryPaymentDO salaryPayment = salaryPaymentService.getSalaryPayment(id); return success(BeanUtils.toBean(salaryPayment, HrmSalaryPaymentRespVO.class)); } @PostMapping("/generate") @Operation(summary = "生成薪资发放台账") @Parameter(name = "period", description = "核算周期", required = true, example = "2024-01") @PreAuthorize("@ss.hasPermission('hrm:salary:generate')") public CommonResult generatePayment(@RequestParam("period") String period) { return success(salaryPaymentService.generatePayment(period)); } @PostMapping("/pay") @Operation(summary = "发放薪资(整个台账)") @Parameter(name = "id", description = "发放台账ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:salary:pay')") public CommonResult paySalary(@RequestParam("id") Long id) { salaryPaymentService.paySalary(id); return success(true); } @PostMapping("/pay-detail") @Operation(summary = "批量发放薪资明细") @PreAuthorize("@ss.hasPermission('hrm:salary:pay')") public CommonResult paySalaryDetail(@RequestBody Map> request) { List detailIds = request.get("detailIds"); salaryPaymentService.paySalaryDetail(detailIds); return success(true); } @PostMapping("/revoke-detail") @Operation(summary = "批量撤销薪资发放明细") @PreAuthorize("@ss.hasPermission('hrm:salary:pay')") public CommonResult revokeSalaryPaymentDetail(@RequestBody Map> request) { List detailIds = request.get("detailIds"); salaryPaymentService.revokeSalaryPaymentDetail(detailIds); return success(true); } @DeleteMapping("/delete") @Operation(summary = "删除薪资发放台账") @Parameter(name = "id", description = "发放台账ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:salary:delete')") public CommonResult deleteSalaryPayment(@RequestParam("id") Long id) { salaryPaymentService.deleteSalaryPayment(id); return success(true); } @GetMapping("/detail/page") @Operation(summary = "获得薪资发放明细分页") @PreAuthorize("@ss.hasPermission('hrm:salary:query')") public CommonResult> getSalaryPaymentDetailPage(@Valid HrmSalaryPaymentDetailPageReqVO pageReqVO) { PageResult pageResult = salaryPaymentService.getSalaryPaymentDetailPage(pageReqVO); List list = BeanUtils.toBean(pageResult.getList(), HrmSalaryPaymentDetailRespVO.class); // 填充员工和部门信息 if (CollUtil.isNotEmpty(list)) { fillUserInfo(list, pageResult.getList()); } return success(new PageResult<>(list, pageResult.getTotal())); } @GetMapping("/detail/list") @Operation(summary = "获得薪资发放明细列表") @Parameter(name = "paymentId", description = "发放台账ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:salary:query')") public CommonResult> getSalaryPaymentDetailList(@RequestParam("paymentId") Long paymentId) { List detailList = salaryPaymentService.getSalaryPaymentDetailListByPaymentId(paymentId); List list = BeanUtils.toBean(detailList, HrmSalaryPaymentDetailRespVO.class); // 填充员工和部门信息 if (CollUtil.isNotEmpty(list)) { fillUserInfo(list, detailList); } return success(list); } /** * 填充员工和部门信息 */ private void fillUserInfo(List respList, List detailList) { // 批量获取员工信息 Set employeeIds = detailList.stream() .map(HrmSalaryPaymentDetailDO::getUserId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map employeeMap = new HashMap<>(); for (Long employeeId : employeeIds) { HrmEmployeeDO employee = employeeService.getEmployee(employeeId); if (employee != null) { employeeMap.put(employeeId, employee); } } // 批量获取部门信息 Set deptIds = detailList.stream() .map(HrmSalaryPaymentDetailDO::getDeptId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds); // 填充信息 for (HrmSalaryPaymentDetailRespVO 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()); } } } }