diff --git a/klp-wms/src/main/java/com/klp/service/impl/WmsAttendanceScheduleServiceImpl.java b/klp-wms/src/main/java/com/klp/service/impl/WmsAttendanceScheduleServiceImpl.java index af1e48bf..9d2a2b7e 100644 --- a/klp-wms/src/main/java/com/klp/service/impl/WmsAttendanceScheduleServiceImpl.java +++ b/klp-wms/src/main/java/com/klp/service/impl/WmsAttendanceScheduleServiceImpl.java @@ -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 schedules = new ArrayList<>(); - - // 获取班次信息 WmsAttendanceShiftVo shift = shiftService.queryById(bo.getShiftId()); if (shift == null) { throw new RuntimeException("班次不存在"); } + // 一次查询整个日期范围内已存在的排班 + Set existingDates = getExistingScheduleDates(bo.getUserId(), startDate, endDate); + + List 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 existingDates = getExistingScheduleDates(bo.getUserId(), startDate, endDate); + List 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 getExistingScheduleDates(Long userId, LocalDate startDate, LocalDate endDate) { LambdaQueryWrapper 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 list = baseMapper.selectList(wrapper); + return list.stream() + .map(s -> new java.sql.Date(s.getWorkDate().getTime()).toLocalDate()) + .collect(Collectors.toSet()); } }