fix(wms/attendance): 修正倒班排班生成逻辑,优化交接班日判断

- 将排班生成逻辑从基于累计天数切换为基于日期判断,每月1日、11日、21日(非31日)作为交接班日
- 调整交接班处理逻辑,统一班次标识切换方式,避免重复切换错误
- 优化代码注释和变量命名,提升可读性
- 修复批量插入结果判断变量名错误
This commit is contained in:
2026-05-25 11:14:19 +08:00
parent cd099f2e6b
commit 3d92528179

View File

@@ -186,20 +186,28 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
/**
* 生成倒班排班
* 根据用户选择的倒班规则和起始日期,生成指定日期范围内的倒班排班表
*
* @param bo 生成排班业务对象包含用户ID、规则ID、起始班次等信息
* @param startDate 排班开始日期
* @param endDate 排班结束日期
*/
private void generateShiftSchedule(GenerateScheduleBo bo, LocalDate startDate, LocalDate endDate) {
// 获取倒班规则
// 通过规则ID查询倒班规则信息
WmsAttendanceShiftRuleVo rule = shiftRuleService.queryById(bo.getRuleId());
if (rule == null) {
throw new RuntimeException("倒班规则不存在");
}
// 验证班次匹配
// 检查用户选择的班次是否与规则中的班次匹配
if (!bo.getShiftId().equals(rule.getShiftA()) && !bo.getShiftId().equals(rule.getShiftB())) {
throw new RuntimeException("班次ID与倒班规则不匹配");
}
// 获取班次信息
// 查询规则中定义的所有班次信息,包括常规班次和特殊交接班次
WmsAttendanceShiftVo shiftA = shiftService.queryById(rule.getShiftA());
WmsAttendanceShiftVo shiftB = shiftService.queryById(rule.getShiftB());
WmsAttendanceShiftVo changeShiftB = rule.getChangeShiftBId() != null ?
@@ -212,26 +220,31 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
}
// 一次查询整个日期范围内已存在的排班
// 查询用户在指定日期范围内已有的排班日期,避免重复生成
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;
boolean firstDay = true;
while (!currentDate.isAfter(endDate)) {
int dayOfMonth = currentDate.getDayOfMonth();
// 判断是否为交接班日每月1日、11日、21日
boolean isSwapDay = !firstDay
&& (dayOfMonth == 1 || dayOfMonth == 11 || dayOfMonth == 21)
&& dayOfMonth != 31;
if (!existingDates.contains(currentDate)) {
WmsAttendanceSchedule schedule = new WmsAttendanceSchedule();
schedule.setUserId(bo.getUserId());
schedule.setWorkDate(java.sql.Date.valueOf(currentDate));
boolean isChangeDay = daysFromStart > 0 && daysFromStart % cycleDays == 0;
if (isChangeDay) {
// 处理交接班日
if (isSwapDay) {
if (isCurrentShiftA) {
// 白班转夜班
if (changeShiftB != null) {
schedule.setShiftId(rule.getChangeShiftBId());
schedule.setShiftName(changeShiftB.getShiftName());
@@ -239,7 +252,6 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
schedule.setShiftId(rule.getShiftB());
schedule.setShiftName(shiftB.getShiftName());
}
isCurrentShiftA = false;
} else {
// 夜班转白班
if (changeShiftA != null) {
@@ -249,8 +261,9 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
schedule.setShiftId(rule.getShiftA());
schedule.setShiftName(shiftA.getShiftName());
}
isCurrentShiftA = true;
}
// 切换班次标识
isCurrentShiftA = !isCurrentShiftA;
} else {
// 普通日
if (isCurrentShiftA) {
@@ -265,13 +278,12 @@ public class WmsAttendanceScheduleServiceImpl implements IWmsAttendanceScheduleS
schedules.add(schedule);
}
currentDate = currentDate.plusDays(1);
daysFromStart++;
firstDay = false;
}
// 批量插入
if (!schedules.isEmpty()) {
boolean i = baseMapper.insertBatch(schedules);
if (!i) {
boolean ok = baseMapper.insertBatch(schedules);
if (!ok) {
throw new RuntimeException("批量插入倒班排班失败");
}
}