gaoluyang
昨天 578f84ece732a710278e7914396e127d8a27ed8a
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package cn.iocoder.yudao.module.hrm.service.attendance;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleBatchSaveReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleBatchSaveRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleLeaveRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleListReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmEmployeeScheduleSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmLeaveApplicationDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmAttendanceHolidayDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmEmployeeScheduleDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmShiftDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmLeaveApplicationMapper;
import cn.iocoder.yudao.module.hrm.enums.HrmAttendanceHolidayTypeEnum;
import cn.iocoder.yudao.module.hrm.enums.HrmLeaveTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmEmployeeScheduleMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmShiftMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * 员工排班 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class HrmEmployeeScheduleServiceImpl implements HrmEmployeeScheduleService {
 
    @Resource
    private HrmEmployeeScheduleMapper employeeScheduleMapper;
    @Resource
    private HrmEmployeeMapper employeeMapper;
    @Resource
    private HrmShiftMapper shiftMapper;
    @Resource
    private DeptApi deptApi;
    @Resource
    private HrmAttendanceHolidayService attendanceHolidayService;
    @Resource
    private HrmLeaveApplicationMapper leaveApplicationMapper;
 
    @Override
    public List<HrmEmployeeScheduleRespVO> getScheduleList(HrmEmployeeScheduleListReqVO reqVO) {
        YearMonth yearMonth = YearMonth.parse(reqVO.getYearMonth());
        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();
 
        // 1. 按条件查询排班记录
        List<HrmEmployeeScheduleDO> list;
        if (reqVO.getEmployeeId() != null) {
            list = employeeScheduleMapper.selectListByEmployeeIdAndPeriod(reqVO.getEmployeeId(), startDate, endDate);
        } else if (reqVO.getDeptId() != null) {
            // 与员工分页保持一致:选中部门时连带查询所有下级部门的员工
            Set<Long> deptIds = getSelfAndChildDeptIds(reqVO.getDeptId());
            List<HrmEmployeeDO> employees = employeeMapper.selectListByDeptIds(deptIds);
            Set<Long> employeeIds = employees.stream().map(HrmEmployeeDO::getId).collect(Collectors.toSet());
            list = CollUtil.isEmpty(employeeIds) ? Collections.emptyList()
                    : employeeScheduleMapper.selectListByEmployeeIdsAndPeriod(employeeIds, startDate, endDate);
        } else {
            list = employeeScheduleMapper.selectListByPeriod(startDate, endDate);
        }
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
 
        // 2. 拼接员工信息与班次信息
        Set<Long> employeeIds = list.stream().map(HrmEmployeeScheduleDO::getEmployeeId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Set<Long> shiftIds = list.stream().map(HrmEmployeeScheduleDO::getShiftId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, HrmEmployeeDO> employeeMap = CollUtil.isEmpty(employeeIds) ? Collections.emptyMap()
                : employeeMapper.selectBatchIds(employeeIds).stream()
                        .collect(Collectors.toMap(HrmEmployeeDO::getId, Function.identity(), (a, b) -> a));
        Map<Long, HrmShiftDO> shiftMap = CollUtil.isEmpty(shiftIds) ? Collections.emptyMap()
                : shiftMapper.selectBatchIds(shiftIds).stream()
                        .collect(Collectors.toMap(HrmShiftDO::getId, Function.identity(), (a, b) -> a));
 
        return list.stream().map(schedule -> {
            HrmEmployeeScheduleRespVO respVO = new HrmEmployeeScheduleRespVO();
            respVO.setId(schedule.getId());
            respVO.setEmployeeId(schedule.getEmployeeId());
            HrmEmployeeDO employee = schedule.getEmployeeId() == null ? null : employeeMap.get(schedule.getEmployeeId());
            if (employee != null) {
                respVO.setEmployeeName(employee.getName());
                respVO.setEmployeeNo(employee.getEmployeeNo());
            }
            respVO.setDate(schedule.getDate());
            respVO.setShiftId(schedule.getShiftId());
            HrmShiftDO shift = schedule.getShiftId() == null ? null : shiftMap.get(schedule.getShiftId());
            if (shift != null) {
                respVO.setShiftName(shift.getName());
                respVO.setShiftColor(shift.getColor());
            }
            respVO.setRemark(schedule.getRemark());
            return respVO;
        }).collect(Collectors.toList());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSchedule(List<HrmEmployeeScheduleSaveReqVO> saveReqVOList) {
        if (CollUtil.isEmpty(saveReqVOList)) {
            throw exception(SCHEDULE_ITEM_LIST_EMPTY);
        }
        // 1. 校验员工存在
        Set<Long> employeeIds = saveReqVOList.stream().map(HrmEmployeeScheduleSaveReqVO::getEmployeeId)
                .collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(employeeIds)) {
            List<HrmEmployeeDO> employees = employeeMapper.selectBatchIds(employeeIds);
            if (employees.size() != employeeIds.size()) {
                throw exception(SCHEDULE_EMPLOYEE_NOT_EXISTS);
            }
        }
        // 2. 校验班次存在
        Set<Long> shiftIds = saveReqVOList.stream().map(HrmEmployeeScheduleSaveReqVO::getShiftId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(shiftIds)) {
            List<HrmShiftDO> shifts = shiftMapper.selectBatchIds(shiftIds);
            if (shifts.size() != shiftIds.size()) {
                throw exception(SCHEDULE_SHIFT_NOT_EXISTS);
            }
        }
        // 3. 逐条 upsert:有班次则保存,无班次则删除
        for (HrmEmployeeScheduleSaveReqVO reqVO : saveReqVOList) {
            HrmEmployeeScheduleDO existing = employeeScheduleMapper.selectByEmployeeIdAndDate(
                    reqVO.getEmployeeId(), reqVO.getDate());
            if (reqVO.getShiftId() == null) {
                if (existing != null) {
                    employeeScheduleMapper.deleteById(existing.getId());
                }
            } else {
                if (existing == null) {
                    employeeScheduleMapper.insert(BeanUtils.toBean(reqVO, HrmEmployeeScheduleDO.class));
                } else {
                    existing.setShiftId(reqVO.getShiftId());
                    existing.setRemark(reqVO.getRemark());
                    employeeScheduleMapper.updateById(existing);
                }
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HrmEmployeeScheduleBatchSaveRespVO batchSaveSchedule(HrmEmployeeScheduleBatchSaveReqVO reqVO) {
        // 1. 校验日期范围和班次
        if (reqVO.getEndDate().isBefore(reqVO.getStartDate())) {
            throw exception(SCHEDULE_BATCH_DATE_RANGE_INVALID);
        }
        if (reqVO.getShiftId() != null && shiftMapper.selectById(reqVO.getShiftId()) == null) {
            throw exception(SCHEDULE_SHIFT_NOT_EXISTS);
        }
        // 2. 按部门(含下级)+ 岗位 + 员工状态筛选目标员工
        Set<Long> deptIds = getSelfAndChildDeptIds(reqVO.getDeptId());
        List<HrmEmployeeDO> employees = employeeMapper.selectListByDeptIds(deptIds).stream()
                .filter(employee -> matchEmployeeStatus(employee, reqVO.getEmployeeStatus()))
                .filter(employee -> matchPost(employee, reqVO.getPostId()))
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(employees)) {
            DeptRespDTO dept = deptApi.getDept(reqVO.getDeptId());
            throw exception(SCHEDULE_BATCH_EMPLOYEE_EMPTY, dept == null ? String.valueOf(reqVO.getDeptId()) : dept.getName());
        }
 
        // 3. 预加载范围内已有排班,避免逐条查询
        Set<Long> employeeIds = employees.stream().map(HrmEmployeeDO::getId).collect(Collectors.toSet());
        Map<String, HrmEmployeeScheduleDO> existingMap = employeeScheduleMapper
                .selectListByEmployeeIdsAndPeriod(employeeIds, reqVO.getStartDate(), reqVO.getEndDate())
                .stream()
                .collect(Collectors.toMap(item -> scheduleKey(item.getEmployeeId(), item.getDate()),
                        Function.identity(), (a, b) -> a));
 
        // 4. 预加载区间内节假日配置:配置为休息日的当天强制休息,配置为工作日的当天强制排班(可覆盖周末)
        Map<LocalDate, Integer> holidayTypeMap = attendanceHolidayService
                .getAttendanceHolidayListByDateRange(reqVO.getStartDate(), reqVO.getEndDate())
                .stream()
                .collect(Collectors.toMap(HrmAttendanceHolidayDO::getDate, HrmAttendanceHolidayDO::getType,
                        (a, b) -> a));
 
        // 5. 预加载目标员工的已审批请假:请假当天跳过,不排班也不清除已有排班
        Map<Long, Set<LocalDate>> leaveDateMap = buildLeaveDateMap(employees,
                reqVO.getStartDate(), reqVO.getEndDate());
 
        // 6. 按日期逐天生成排班:休息日删除原班次,工作日 upsert
        int itemCount = 0;
        for (LocalDate date = reqVO.getStartDate(); !date.isAfter(reqVO.getEndDate()); date = date.plusDays(1)) {
            boolean rest = isRestDay(date, reqVO.getRestMode(), holidayTypeMap.get(date))
                    || reqVO.getShiftId() == null;
            for (HrmEmployeeDO employee : employees) {
                // 员工当天在请假期内,保持原排班不动
                if (leaveDateMap.getOrDefault(employee.getId(), Collections.emptySet()).contains(date)) {
                    continue;
                }
                HrmEmployeeScheduleDO existing = existingMap.get(scheduleKey(employee.getId(), date));
                if (rest) {
                    if (existing != null) {
                        employeeScheduleMapper.deleteById(existing.getId());
                        itemCount++;
                    }
                    continue;
                }
                if (existing == null) {
                    HrmEmployeeScheduleDO schedule = new HrmEmployeeScheduleDO();
                    schedule.setEmployeeId(employee.getId());
                    schedule.setDate(date);
                    schedule.setShiftId(reqVO.getShiftId());
                    employeeScheduleMapper.insert(schedule);
                    itemCount++;
                } else if (!Objects.equals(existing.getShiftId(), reqVO.getShiftId())) {
                    existing.setShiftId(reqVO.getShiftId());
                    employeeScheduleMapper.updateById(existing);
                    itemCount++;
                }
            }
        }
 
        HrmEmployeeScheduleBatchSaveRespVO respVO = new HrmEmployeeScheduleBatchSaveRespVO();
        respVO.setEmployeeCount(employees.size());
        respVO.setItemCount(itemCount);
        return respVO;
    }
 
    @Override
    public List<HrmEmployeeScheduleLeaveRespVO> getApprovedLeaveList(LocalDate startDate, LocalDate endDate) {
        if (startDate == null || endDate == null || endDate.isBefore(startDate)) {
            return Collections.emptyList();
        }
        List<HrmLeaveApplicationDO> leaves = leaveApplicationMapper.selectApprovedListByTimeRange(
                startDate.atStartOfDay(), endDate.atTime(LocalTime.MAX), null);
        if (CollUtil.isEmpty(leaves)) {
            return Collections.emptyList();
        }
        Set<Long> userIds = leaves.stream().map(HrmLeaveApplicationDO::getUserId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, HrmEmployeeDO> userEmployeeMap = CollUtil.isEmpty(userIds) ? Collections.emptyMap()
                : employeeMapper.selectList(new LambdaQueryWrapperX<HrmEmployeeDO>()
                        .in(HrmEmployeeDO::getUserId, userIds)).stream()
                .filter(employee -> employee.getUserId() != null)
                .collect(Collectors.toMap(HrmEmployeeDO::getUserId, Function.identity(), (a, b) -> a));
        return leaves.stream().map(leave -> {
            HrmEmployeeDO employee = userEmployeeMap.get(leave.getUserId());
            if (employee == null) {
                return null;
            }
            HrmEmployeeScheduleLeaveRespVO respVO = new HrmEmployeeScheduleLeaveRespVO();
            respVO.setId(leave.getId());
            respVO.setEmployeeId(employee.getId());
            respVO.setEmployeeName(employee.getName());
            respVO.setLeaveType(leave.getLeaveType());
            respVO.setLeaveTypeName(HrmLeaveTypeEnum.getNameByStatus(leave.getLeaveType()));
            respVO.setStartTime(leave.getStartTime());
            respVO.setEndTime(leave.getEndTime());
            respVO.setDuration(leave.getDuration());
            return respVO;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }
 
    /**
     * 构建「员工ID -> 请假日集合」映射(只取审批通过的请假,且裁剪到排班日期范围内)
     */
    private Map<Long, Set<LocalDate>> buildLeaveDateMap(List<HrmEmployeeDO> employees,
                                                        LocalDate startDate, LocalDate endDate) {
        Map<Long, HrmEmployeeDO> userEmployeeMap = employees.stream()
                .filter(employee -> employee.getUserId() != null)
                .collect(Collectors.toMap(HrmEmployeeDO::getUserId, Function.identity(), (a, b) -> a));
        if (userEmployeeMap.isEmpty()) {
            return Collections.emptyMap();
        }
        List<HrmLeaveApplicationDO> leaves = leaveApplicationMapper.selectApprovedListByTimeRange(
                startDate.atStartOfDay(), endDate.atTime(LocalTime.MAX), userEmployeeMap.keySet());
        if (CollUtil.isEmpty(leaves)) {
            return Collections.emptyMap();
        }
        Map<Long, Set<LocalDate>> result = new HashMap<>();
        for (HrmLeaveApplicationDO leave : leaves) {
            HrmEmployeeDO employee = userEmployeeMap.get(leave.getUserId());
            if (employee == null || leave.getStartTime() == null || leave.getEndTime() == null) {
                continue;
            }
            LocalDate from = leave.getStartTime().toLocalDate().isBefore(startDate)
                    ? startDate : leave.getStartTime().toLocalDate();
            LocalDate to = leave.getEndTime().toLocalDate().isAfter(endDate)
                    ? endDate : leave.getEndTime().toLocalDate();
            Set<LocalDate> dates = result.computeIfAbsent(employee.getId(), key -> new HashSet<>());
            for (LocalDate date = from; !date.isAfter(to); date = date.plusDays(1)) {
                dates.add(date);
            }
        }
        return result;
    }
 
    private Set<Long> getSelfAndChildDeptIds(Long deptId) {
        Set<Long> deptIds = new LinkedHashSet<>();
        deptIds.add(deptId);
        List<DeptRespDTO> childDepts = deptApi.getChildDeptList(deptId);
        if (CollUtil.isNotEmpty(childDepts)) {
            childDepts.forEach(dept -> deptIds.add(dept.getId()));
        }
        return deptIds;
    }
 
    /** employeeStatus 为空时包含在职和试用,排除离职 */
    private boolean matchEmployeeStatus(HrmEmployeeDO employee, Integer employeeStatus) {
        if (employeeStatus != null) {
            return Objects.equals(employee.getEmployeeStatus(), employeeStatus);
        }
        return !Objects.equals(employee.getEmployeeStatus(), 3);
    }
 
    private boolean matchPost(HrmEmployeeDO employee, Long postId) {
        if (postId == null) {
            return true;
        }
        if (StrUtil.isBlank(employee.getPostIds())) {
            return false;
        }
        return JSONUtil.toList(employee.getPostIds(), Long.class).contains(postId);
    }
 
    private boolean isRestDay(LocalDate date, String restMode, Integer holidayType) {
        // 节假日配置优先:休息日强制休息,工作日强制上班(含调休上班的周末)
        if (holidayType != null) {
            return HrmAttendanceHolidayTypeEnum.REST_DAY.getType().equals(holidayType);
        }
        if ("double".equals(restMode)) {
            return date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY;
        }
        if ("single".equals(restMode)) {
            return date.getDayOfWeek() == DayOfWeek.SUNDAY;
        }
        return false;
    }
 
    private String scheduleKey(Long employeeId, LocalDate date) {
        return employeeId + "_" + date;
    }
 
}