package cn.iocoder.yudao.module.hrm.controller.admin.attendance; 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.attendance.vo.HrmAttendanceExceptionPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceExceptionRespVO; import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceExceptionSaveReqVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmAttendanceExceptionDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.service.attendance.HrmAttendanceExceptionService; import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService; 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/attendance/exception") @Validated public class HrmAttendanceExceptionController { @Resource private HrmAttendanceExceptionService attendanceExceptionService; @Resource private HrmEmployeeService employeeService; @Resource private DeptApi deptApi; @PostMapping("/create") @Operation(summary = "新增考勤异常申请") @PreAuthorize("@ss.hasPermission('hrm:attendance:create')") public CommonResult createAttendanceException(@Valid @RequestBody HrmAttendanceExceptionSaveReqVO createReqVO) { return success(attendanceExceptionService.createAttendanceException(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新考勤异常申请") @PreAuthorize("@ss.hasPermission('hrm:attendance:update')") public CommonResult updateAttendanceException(@Valid @RequestBody HrmAttendanceExceptionSaveReqVO updateReqVO) { attendanceExceptionService.updateAttendanceException(updateReqVO); return success(true); } @DeleteMapping("/delete") @Operation(summary = "删除考勤异常申请") @Parameter(name = "id", description = "申请ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:attendance:delete')") public CommonResult deleteAttendanceException(@RequestParam("id") Long id) { attendanceExceptionService.deleteAttendanceException(id); return success(true); } @GetMapping("/get") @Operation(summary = "获得考勤异常申请") @Parameter(name = "id", description = "申请ID", required = true) @PreAuthorize("@ss.hasPermission('hrm:attendance:query')") public CommonResult getAttendanceException(@RequestParam("id") Long id) { HrmAttendanceExceptionDO attendanceException = attendanceExceptionService.getAttendanceException(id); if (attendanceException == null) { return success(null); } HrmAttendanceExceptionRespVO respVO = BeanUtils.toBean(attendanceException, HrmAttendanceExceptionRespVO.class); // 填充员工和部门信息(userId 是员工ID) if (attendanceException.getUserId() != null) { HrmEmployeeDO employee = employeeService.getEmployee(attendanceException.getUserId()); if (employee != null) { respVO.setUserName(employee.getName()); } } if (attendanceException.getDeptId() != null) { DeptRespDTO dept = deptApi.getDept(attendanceException.getDeptId()); if (dept != null) { respVO.setDeptName(dept.getName()); } } return success(respVO); } @GetMapping("/page") @Operation(summary = "获得考勤异常申请分页") @PreAuthorize("@ss.hasPermission('hrm:attendance:query')") public CommonResult> getAttendanceExceptionPage(@Valid HrmAttendanceExceptionPageReqVO pageReqVO) { PageResult pageResult = attendanceExceptionService.getAttendanceExceptionPage(pageReqVO); List list = BeanUtils.toBean(pageResult.getList(), HrmAttendanceExceptionRespVO.class); // 填充员工和部门信息 if (CollUtil.isNotEmpty(list)) { fillUserInfo(list, pageResult.getList()); } return success(new PageResult<>(list, pageResult.getTotal())); } /** * 填充员工和部门信息(userId 是员工ID) */ private void fillUserInfo(List respList, List exceptionList) { // 批量获取员工信息 Set employeeIds = exceptionList.stream() .map(HrmAttendanceExceptionDO::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 = exceptionList.stream() .map(HrmAttendanceExceptionDO::getDeptId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds); // 填充信息 for (HrmAttendanceExceptionRespVO 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()); } } } @PostMapping("/submit") @Operation(summary = "提交考勤异常申请") @PreAuthorize("@ss.hasPermission('hrm:attendance:submit')") public CommonResult submitAttendanceException( @RequestParam("id") Long id, @RequestParam("processDefinitionKey") String processDefinitionKey) { // 获取当前登录用户ID Long userId = cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId(); attendanceExceptionService.submitAttendanceException(id, processDefinitionKey, userId); return success(true); } }