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
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.HrmAttendanceExceptionAuditReqVO;
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 cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
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;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @PostMapping("/create")
    @Operation(summary = "新增考勤异常申请")
    @PreAuthorize("@ss.hasPermission('hrm:attendance:create')")
    public CommonResult<Long> createAttendanceException(@Valid @RequestBody HrmAttendanceExceptionSaveReqVO createReqVO) {
        return success(attendanceExceptionService.createAttendanceException(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新考勤异常申请")
    @PreAuthorize("@ss.hasPermission('hrm:attendance:update')")
    public CommonResult<Boolean> 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<Boolean> 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<HrmAttendanceExceptionRespVO> 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());
            }
        }
        respVO.setAttachmentList(storageAttachmentApi.listAttachments("hrm_attendance_exception", id));
        return success(respVO);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得考勤异常申请分页")
    @PreAuthorize("@ss.hasPermission('hrm:attendance:query')")
    public CommonResult<PageResult<HrmAttendanceExceptionRespVO>> getAttendanceExceptionPage(@Valid HrmAttendanceExceptionPageReqVO pageReqVO) {
        PageResult<HrmAttendanceExceptionDO> pageResult = attendanceExceptionService.getAttendanceExceptionPage(pageReqVO);
        List<HrmAttendanceExceptionRespVO> 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<HrmAttendanceExceptionRespVO> respList, List<HrmAttendanceExceptionDO> exceptionList) {
        // 批量获取员工信息
        Set<Long> employeeIds = exceptionList.stream()
                .map(HrmAttendanceExceptionDO::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 = exceptionList.stream()
                .map(HrmAttendanceExceptionDO::getDeptId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, DeptRespDTO> 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());
            }
        }
    }
 
    @PutMapping("/submit")
    @Operation(summary = "提交审批(草稿/审核不通过→审批中),审批人由审批配置统一指定")
    @Parameter(name = "id", description = "申请ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:attendance-exception:submit')")
    public CommonResult<Boolean> submitAttendanceException(@RequestParam("id") Long id) {
        attendanceExceptionService.submitAttendanceException(id);
        return success(true);
    }
 
    @PutMapping("/audit")
    @Operation(summary = "审核(审批中→审核通过/审核不通过),仅审批人本人可操作")
    @PreAuthorize("@ss.hasPermission('hrm:attendance-exception:audit')")
    public CommonResult<Boolean> auditAttendanceException(@Valid @RequestBody HrmAttendanceExceptionAuditReqVO auditReqVO) {
        attendanceExceptionService.auditAttendanceException(auditReqVO.getId(), auditReqVO.getPass(), auditReqVO.getReviewRemark());
        return success(true);
    }
 
    @GetMapping("/approver-ids")
    @Operation(summary = "获得审批人编号集合,供前端判断是否展示审核按钮")
    @PreAuthorize("@ss.hasPermission('hrm:attendance:query')")
    public CommonResult<Set<Long>> getApproverUserIds() {
        return success(attendanceExceptionService.getAttendanceExceptionApproverUserIds());
    }
 
}