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
package cn.iocoder.yudao.module.hrm.service.attendance;
 
import cn.hutool.core.util.StrUtil;
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.HrmAttendanceExceptionSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmAttendanceExceptionDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmAttendanceExceptionMapper;
import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import cn.iocoder.yudao.module.hrm.enums.HrmAttendanceExceptionTypeEnum;
import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.Set;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * 考勤异常申请 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@Slf4j
public class HrmAttendanceExceptionServiceImpl implements HrmAttendanceExceptionService {
 
    /**
     * 考勤异常申请审批的业务类型编码
     *
     * 与 system_approval_config.biz_type 对应,审批人在「系统管理 - 审批配置」中维护。
     */
    private static final String ATTENDANCE_APPROVE_BIZ_TYPE = "hrm_attendance_approve";
 
    @Resource
    private HrmAttendanceExceptionMapper attendanceExceptionMapper;
    @Resource
    private HrmNoRedisDAO noRedisDAO;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
    @Resource
    private ApprovalConfigApi approvalConfigApi;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private HrmAttendanceRecordService attendanceRecordService;
 
    @Override
    public Long createAttendanceException(HrmAttendanceExceptionSaveReqVO createReqVO) {
        HrmEmployeeDO employee = employeeService.getEmployeeByUserId(
                cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId());
        if (employee == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        HrmAttendanceExceptionDO attendanceException = BeanUtils.toBean(createReqVO, HrmAttendanceExceptionDO.class);
        attendanceException.setUserId(employee.getId()).setDeptId(employee.getDeptId());
        attendanceException.setNo(generateNo());
        attendanceException.setStatus(HrmAuditStatusEnum.DRAFT.getStatus());
        attendanceExceptionMapper.insert(attendanceException);
        storageAttachmentApi.bindAttachments("file", "hrm_attendance_exception",
                attendanceException.getId(), createReqVO.getBlobIds());
        return attendanceException.getId();
    }
 
    private String generateNo() {
        return noRedisDAO.generate(HrmNoRedisDAO.ATTENDANCE_EXCEPTION_NO_PREFIX, 4);
    }
 
    @Override
    public void updateAttendanceException(HrmAttendanceExceptionSaveReqVO updateReqVO) {
        HrmAttendanceExceptionDO attendanceException = validateAttendanceExceptionExists(updateReqVO.getId());
        if (!HrmAuditStatusEnum.DRAFT.getStatus().equals(attendanceException.getStatus())) {
            throw exception(ATTENDANCE_EXCEPTION_UPDATE_FAIL_NOT_DRAFT);
        }
        HrmAttendanceExceptionDO updateObj = BeanUtils.toBean(updateReqVO, HrmAttendanceExceptionDO.class);
        attendanceExceptionMapper.updateById(updateObj);
        storageAttachmentApi.updateAttachments("file", "hrm_attendance_exception", updateReqVO.getId(), updateReqVO.getBlobIds());
    }
 
    @Override
    public void deleteAttendanceException(Long id) {
        HrmAttendanceExceptionDO attendanceException = validateAttendanceExceptionExists(id);
        if (!HrmAuditStatusEnum.DRAFT.getStatus().equals(attendanceException.getStatus())) {
            throw exception(ATTENDANCE_EXCEPTION_DELETE_FAIL_NOT_DRAFT);
        }
        storageAttachmentApi.deleteAttachmentsByRecord("hrm_attendance_exception", id);
        attendanceExceptionMapper.deleteById(id);
    }
 
    private HrmAttendanceExceptionDO validateAttendanceExceptionExists(Long id) {
        HrmAttendanceExceptionDO attendanceException = attendanceExceptionMapper.selectById(id);
        if (attendanceException == null) {
            throw exception(ATTENDANCE_EXCEPTION_NOT_EXISTS);
        }
        return attendanceException;
    }
 
    @Override
    public PageResult<HrmAttendanceExceptionDO> getAttendanceExceptionPage(HrmAttendanceExceptionPageReqVO pageReqVO) {
        return attendanceExceptionMapper.selectPage(pageReqVO);
    }
 
    @Override
    public HrmAttendanceExceptionDO getAttendanceException(Long id) {
        return attendanceExceptionMapper.selectById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitAttendanceException(Long id) {
        // 1. 校验存在 + 可提交状态(草稿 / 审核不通过)
        HrmAttendanceExceptionDO exception = validateAttendanceExceptionExists(id);
        if (!HrmAuditStatusEnum.DRAFT.getStatus().equals(exception.getStatus())
                && !HrmAuditStatusEnum.REJECT.getStatus().equals(exception.getStatus())) {
            throw exception(ATTENDANCE_EXCEPTION_SUBMIT_FAIL_NOT_EDITABLE);
        }
 
        // 2. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常)
        approvalConfigApi.validateApprovalEnabledAndGetApprovers(ATTENDANCE_APPROVE_BIZ_TYPE);
 
        // 3. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因
        attendanceExceptionMapper.submitAndResetAuditInfo(id, HrmAuditStatusEnum.PROCESS.getStatus());
    }
 
    @Override
    public Set<Long> getAttendanceExceptionApproverUserIds() {
        return approvalConfigApi.getApproverUserIds(ATTENDANCE_APPROVE_BIZ_TYPE);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditAttendanceException(Long id, Boolean pass, String reviewRemark) {
        // 1. 校验存在
        HrmAttendanceExceptionDO exception = validateAttendanceExceptionExists(id);
        // 2. 校验处于审批中状态
        if (!HrmAuditStatusEnum.PROCESS.getStatus().equals(exception.getStatus())) {
            throw exception(ATTENDANCE_EXCEPTION_AUDIT_FAIL_NOT_PROCESS);
        }
        // 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核)
        Long userId = getLoginUserId();
        approvalConfigApi.validateApprover(ATTENDANCE_APPROVE_BIZ_TYPE, userId);
        // 4. 审核不通过:必须填写原因,状态置为审核不通过,退回提交人修改后可重新提交
        if (!Boolean.TRUE.equals(pass)) {
            if (StrUtil.isBlank(reviewRemark)) {
                throw exception(ATTENDANCE_EXCEPTION_AUDIT_REJECT_REASON_REQUIRED);
            }
            attendanceExceptionMapper.auditAttendanceException(id, HrmAuditStatusEnum.REJECT.getStatus(),
                    userId, getUserNickname(userId), reviewRemark);
            return;
        }
        // 5. 审核通过:记录审核人与审核意见,状态置为审核通过
        attendanceExceptionMapper.auditAttendanceException(id, HrmAuditStatusEnum.APPROVE.getStatus(),
                userId, getUserNickname(userId), reviewRemark);
 
        // 6. 审核通过后置业务逻辑:补卡回填上下班时间 / 加班回写加班时长
        if (HrmAttendanceExceptionTypeEnum.SUPPLEMENT.getType().equals(exception.getType())) {
            log.info("[auditAttendanceException] 补卡申请({}) 审批通过,开始更新考勤记录", id);
            attendanceRecordService.supplementClockIn(exception);
        } else if (HrmAttendanceExceptionTypeEnum.OVERTIME.getType().equals(exception.getType())) {
            log.info("[auditAttendanceException] 加班申请({}) 审批通过,开始回写加班时长", id);
            attendanceRecordService.recordOvertime(exception);
        }
    }
 
    /**
     * 获取用户昵称
     *
     * @param userId 用户编号
     * @return 昵称,用户不存在时返回 null
     */
    private String getUserNickname(Long userId) {
        if (userId == null) {
            return null;
        }
        AdminUserRespDTO user = adminUserApi.getUser(userId);
        return user == null ? null : user.getNickname();
    }
 
}