From e084576f1e0a9ae12b4334053a5c1fde084da15d Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Fri, 22 May 2026 16:03:24 +0800 Subject: [PATCH] =?UTF-8?q?perf(wms/attendance):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=8E=92=E7=8F=AD=E7=94=9F=E6=88=90=E6=80=A7=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9F=A5=E8=AF=A2=E5=B7=B2=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E5=B9=B6=E9=87=8D=E6=9E=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将单日排班存在性检查改为批量查询整个日期范围内的已存在排班日期集合 - 移除冗余的日期检查逻辑,统一使用批量查询结果进行过滤 - 简化倒班日判断和班次切换逻辑,移除注释掉的复杂处理代码 - 优化导入语句,使用通配符和流式处理提高代码简洁性 --- .../WmsAttendanceScheduleServiceImpl.java | 58 +++++++------------ 1 file changed, 20 insertions(+), 38 deletions(-) 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()); } }