perf(wms/attendance): 优化排班生成性能,批量查询已存在日期并重构逻辑

- 将单日排班存在性检查改为批量查询整个日期范围内的已存在排班日期集合
- 移除冗余的日期检查逻辑,统一使用批量查询结果进行过滤
- 简化倒班日判断和班次切换逻辑,移除注释掉的复杂处理代码
- 优化导入语句,使用通配符和流式处理提高代码简洁性
This commit is contained in:
2026-05-22 16:03:24 +08:00
parent 30f9b533b2
commit e084576f1e

View File

@@ -25,12 +25,8 @@ import com.klp.service.IWmsAttendanceShiftService;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
* 排班(谁在哪天上班)Service业务层处理
@@ -157,18 +153,18 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
* 生成正常排班
*/
private void generateNormalSchedule(GenerateScheduleBo bo, LocalDate startDate, LocalDate endDate) {
List<WmsAttendanceSchedule> schedules = new ArrayList<>();
// 获取班次信息
WmsAttendanceShiftVo shift = shiftService.queryById(bo.getShiftId());
if (shift == null) {
throw new RuntimeException("班次不存在");
}
// 一次查询整个日期范围内已存在的排班
Set<LocalDate> existingDates = getExistingScheduleDates(bo.getUserId(), startDate, endDate);
List<WmsAttendanceSchedule> schedules = new ArrayList<>();
LocalDate currentDate = startDate;
while (!currentDate.isAfter(endDate)) {
// 检查是否已存在排班
if (!isScheduleExists(bo.getUserId(), currentDate)) {
if (!existingDates.contains(currentDate)) {
WmsAttendanceSchedule schedule = new WmsAttendanceSchedule();
schedule.setUserId(bo.getUserId());
schedule.setWorkDate(java.sql.Date.valueOf(currentDate));
@@ -215,28 +211,27 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
throw new RuntimeException("倒班规则中的班次不存在");
}
// 一次查询整个日期范围内已存在的排班
Set<LocalDate> existingDates = getExistingScheduleDates(bo.getUserId(), startDate, endDate);
List<WmsAttendanceSchedule> schedules = new ArrayList<>();
LocalDate currentDate = startDate;
long daysFromStart = 0;
// 确定初始班次
boolean isCurrentShiftA = bo.getShiftId().equals(rule.getShiftA());
long cycleDays = rule.getCycleDays() != null ? rule.getCycleDays() : 10;
while (!currentDate.isAfter(endDate)) {
if (!isScheduleExists(bo.getUserId(), currentDate)) {
if (!existingDates.contains(currentDate)) {
WmsAttendanceSchedule schedule = new WmsAttendanceSchedule();
schedule.setUserId(bo.getUserId());
schedule.setWorkDate(java.sql.Date.valueOf(currentDate));
// 判断是否为倒班日
long cycleDays = rule.getCycleDays() != null ? rule.getCycleDays() : 10;
// boolean isChangeDay = (daysFromStart + 1) % cycleDays == 0;
boolean isChangeDay = daysFromStart > 0 && daysFromStart % cycleDays == 0;
if (isChangeDay) {
// 倒班日
if (isCurrentShiftA) {
// 白班转夜班
if (changeShiftB != null) {
schedule.setShiftId(rule.getChangeShiftBId());
schedule.setShiftName(changeShiftB.getShiftName());
@@ -244,24 +239,7 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
schedule.setShiftId(rule.getShiftB());
schedule.setShiftName(shiftB.getShiftName());
}
// // 下一天也使用倒班班次工作18小时
// LocalDate nextDay = currentDate.plusDays(1);
// if (!nextDay.isAfter(endDate) && !isScheduleExists(bo.getUserId(), nextDay)) {
// WmsAttendanceSchedule nextSchedule = new WmsAttendanceSchedule();
// nextSchedule.setUserId(bo.getUserId());
// nextSchedule.setWorkDate(java.sql.Date.valueOf(nextDay));
// if (changeShiftB != null) {
// nextSchedule.setShiftId(rule.getChangeShiftBId());
// nextSchedule.setShiftName(changeShiftB.getShiftName());
// } else {
// nextSchedule.setShiftId(rule.getShiftB());
// nextSchedule.setShiftName(shiftB.getShiftName());
// }
// schedules.add(nextSchedule);
// }
isCurrentShiftA = false;
// currentDate = currentDate.plusDays(1); // 跳过下一天,因为已经处理了
// daysFromStart++;
} else {
// 夜班转白班
if (changeShiftA != null) {
@@ -300,12 +278,16 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
}
/**
* 检查排班是否已存在
* 批量查询指定用户日期范围内已存在的排班日期
*/
private boolean isScheduleExists(Long userId, LocalDate workDate) {
private Set<LocalDate> getExistingScheduleDates(Long userId, LocalDate startDate, LocalDate endDate) {
LambdaQueryWrapper<WmsAttendanceSchedule> wrapper = Wrappers.lambdaQuery();
wrapper.eq(WmsAttendanceSchedule::getUserId, userId)
.eq(WmsAttendanceSchedule::getWorkDate, java.sql.Date.valueOf(workDate));
return baseMapper.selectCount(wrapper) > 0;
.between(WmsAttendanceSchedule::getWorkDate, java.sql.Date.valueOf(startDate), java.sql.Date.valueOf(endDate))
.select(WmsAttendanceSchedule::getWorkDate);
List<WmsAttendanceSchedule> list = baseMapper.selectList(wrapper);
return list.stream()
.map(s -> new java.sql.Date(s.getWorkDate().getTime()).toLocalDate())
.collect(Collectors.toSet());
}
}