支持跨天预约并新增员工全天排班SQL

This commit is contained in:
2026-07-22 12:21:26 +08:00
parent 0b33d72a1f
commit 73319a1613
17 changed files with 800 additions and 130 deletions
@@ -24,6 +24,8 @@ public final class ArtistScheduleRuleSupport {
public static final String EXCEPTION_REST = "REST";
public static final String EXCEPTION_EXTRA_WORK = "EXTRA_WORK";
public static final String EXCEPTION_CHANGE_SHIFT = "CHANGE_SHIFT";
public static final LocalTime FULL_DAY_START_TIME = LocalTime.MIN;
public static final LocalTime FULL_DAY_END_TIME = LocalTime.of(23, 59, 59);
private static final List<Integer> EVERYDAY_WORK_DAYS = List.of(1, 2, 3, 4, 5, 6, 7);
@@ -109,10 +111,10 @@ public final class ArtistScheduleRuleSupport {
ArtistScheduleRuleDO rule = new ArtistScheduleRuleDO();
rule.setShopId(shopId);
rule.setArtistId(artistId);
rule.setPresetType(PRESET_DOUBLE_REST);
rule.setWorkDays(encodeWorkDays(List.of(1, 2, 3, 4, 5)));
rule.setStartTime(LocalTime.of(9, 0));
rule.setEndTime(LocalTime.of(17, 0));
rule.setPresetType(PRESET_EVERYDAY);
rule.setWorkDays(encodeWorkDays(EVERYDAY_WORK_DAYS));
rule.setStartTime(FULL_DAY_START_TIME);
rule.setEndTime(FULL_DAY_END_TIME);
rule.setSlotMinutes(DEFAULT_SLOT_MINUTES);
rule.setCapacity(DEFAULT_CAPACITY);
rule.setStatus(1);
@@ -15,8 +15,11 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
@@ -28,6 +31,7 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
private static final String STATUS_RELEASED = "RELEASED";
private static final String UNAVAILABLE_MESSAGE = "涓嶅彲棰勭害";
private static final String FULL_MESSAGE = "褰撳墠缇庣敳甯堣鏃堕棿娈靛凡绾︽弧锛岃閲嶆柊閫夋嫨棰勭害鏃堕棿";
private static final LocalTime FULL_DAY_QUERY_END_TIME = LocalTime.of(23, 59);
private final AppointmentReservationMapper reservationMapper;
private final ArtistScheduleMapper scheduleMapper;
@@ -145,21 +149,33 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
}
private int resolveCapacity(Long shopId, Long artistId, LocalDateTime startTime, LocalDateTime endTime) {
List<AppointmentTimeSegment> segments = splitAppointmentSegments(startTime, endTime);
int capacity = Integer.MAX_VALUE;
for (AppointmentTimeSegment segment : segments) {
capacity = Math.min(capacity, resolveSegmentCapacity(shopId, artistId, segment));
}
if (capacity == Integer.MAX_VALUE) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return capacity;
}
private int resolveSegmentCapacity(Long shopId, Long artistId, AppointmentTimeSegment segment) {
ArtistScheduleDO schedule = scheduleMapper.selectCoveringSchedule(
shopId,
artistId,
startTime.toLocalDate(),
startTime.toLocalTime(),
endTime.toLocalTime()
segment.workDate(),
segment.startTime(),
toCoverageQueryEndTime(segment.endTime())
);
if (schedule != null && isManualScheduleOverride(schedule)) {
return resolveExplicitScheduleCapacity(schedule);
}
ArtistScheduleDO daySchedule = scheduleMapper.selectDayScheduleForUpdate(shopId, artistId, startTime.toLocalDate());
ArtistScheduleDO daySchedule = scheduleMapper.selectDayScheduleForUpdate(shopId, artistId, segment.workDate());
if (daySchedule != null && isManualScheduleOverride(daySchedule)) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return resolveRuleCapacity(shopId, artistId, startTime, endTime);
return resolveRuleCapacity(shopId, artistId, segment);
}
private int resolveExplicitScheduleCapacity(ArtistScheduleDO schedule) {
@@ -171,7 +187,7 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
return schedule.getCapacity();
}
private int resolveRuleCapacity(Long shopId, Long artistId, LocalDateTime startTime, LocalDateTime endTime) {
private int resolveRuleCapacity(Long shopId, Long artistId, AppointmentTimeSegment segment) {
ArtistScheduleRuleDO rule = ruleMapper.selectOne(new QueryWrapper<ArtistScheduleRuleDO>()
.eq("shop_id", shopId)
.eq("artist_id", artistId)
@@ -183,17 +199,17 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
ArtistScheduleExceptionDO exception = exceptionMapper.selectOne(new QueryWrapper<ArtistScheduleExceptionDO>()
.eq("shop_id", shopId)
.eq("artist_id", artistId)
.eq("work_date", startTime.toLocalDate())
.eq("work_date", segment.workDate())
.eq("is_delete", 0)
.last("LIMIT 1"));
ArtistScheduleRuleSupport.ResolvedSchedule resolved = ArtistScheduleRuleSupport.resolveGeneratedSchedule(
rule,
exception,
startTime.toLocalDate()
segment.workDate()
);
if (!resolved.working()
|| resolved.capacity() == null || resolved.capacity() < MIN_CAPACITY
|| !isTimeCovered(resolved.startTime(), resolved.endTime(), startTime.toLocalTime(), endTime.toLocalTime())) {
|| !isTimeCovered(resolved.startTime(), resolved.endTime(), segment.startTime(), segment.endTime())) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return resolved.capacity();
@@ -204,7 +220,42 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
if (scheduleStartTime == null || scheduleEndTime == null || appointmentStartTime == null || appointmentEndTime == null) {
return false;
}
return !appointmentStartTime.isBefore(scheduleStartTime) && !appointmentEndTime.isAfter(scheduleEndTime);
return !appointmentStartTime.isBefore(scheduleStartTime) && isEndTimeCovered(appointmentEndTime, scheduleEndTime);
}
private boolean isEndTimeCovered(LocalTime appointmentEndTime, LocalTime scheduleEndTime) {
if (!appointmentEndTime.isAfter(scheduleEndTime)) {
return true;
}
return LocalTime.MAX.equals(appointmentEndTime) && isFullDayEndTime(scheduleEndTime);
}
private boolean isFullDayEndTime(LocalTime endTime) {
return endTime != null && !endTime.isBefore(FULL_DAY_QUERY_END_TIME);
}
private LocalTime toCoverageQueryEndTime(LocalTime endTime) {
if (LocalTime.MAX.equals(endTime)) {
return FULL_DAY_QUERY_END_TIME;
}
return endTime;
}
private List<AppointmentTimeSegment> splitAppointmentSegments(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime == null || endTime == null || !startTime.isBefore(endTime)) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
List<AppointmentTimeSegment> segments = new ArrayList<>();
LocalDateTime cursor = startTime;
while (cursor.isBefore(endTime)) {
LocalDate workDate = cursor.toLocalDate();
LocalDateTime nextDayStart = workDate.plusDays(1L).atStartOfDay();
LocalDateTime segmentEndDateTime = endTime.isBefore(nextDayStart) ? endTime : nextDayStart;
LocalTime segmentEndTime = segmentEndDateTime.equals(nextDayStart) ? LocalTime.MAX : segmentEndDateTime.toLocalTime();
segments.add(new AppointmentTimeSegment(workDate, cursor.toLocalTime(), segmentEndTime));
cursor = segmentEndDateTime;
}
return segments;
}
private boolean isManualScheduleOverride(ArtistScheduleDO schedule) {
@@ -214,4 +265,7 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
String remark = schedule.getRemark().trim();
return !"规则生成排班".equals(remark) && !remark.startsWith("例外排班:");
}
private record AppointmentTimeSegment(LocalDate workDate, LocalTime startTime, LocalTime endTime) {
}
}
@@ -1138,16 +1138,55 @@ public class PlanningServiceImpl implements PlanningService {
private void ensureWithinBusinessHours(Long shopId, LocalDateTime startTime, LocalDateTime endTime) {
ShopDO shop = shopMapper.selectById(shopId);
LocalDate workDate = startTime.toLocalDate();
ShopScheduleSupport.LocalTimeRange hours = ShopScheduleSupport.resolveOpenRange(shop, selectShopScheduleException(shopId, workDate), workDate);
if (!hours.open()) {
throw new RuntimeException("当前日期为店铺休息日,不能预约:" + workDate);
List<AppointmentTimeSegment> segments = splitAppointmentSegments(startTime, endTime);
for (AppointmentTimeSegment segment : segments) {
ShopScheduleSupport.LocalTimeRange hours = ShopScheduleSupport.resolveOpenRange(
shop,
selectShopScheduleException(shopId, segment.workDate()),
segment.workDate()
);
if (!hours.open()) {
throw new RuntimeException("当前日期为店铺休息日,不能预约:" + segment.workDate());
}
if (!isSegmentWithinBusinessHours(segment, hours)) {
throw new RuntimeException("预约时间超出店铺营业时间:" + segment.workDate());
}
}
LocalTime start = startTime.toLocalTime();
LocalTime end = endTime.toLocalTime();
if (start.isBefore(hours.startTime()) || end.isAfter(hours.endTime()) || !startTime.toLocalDate().equals(endTime.toLocalDate())) {
throw new RuntimeException("预约时间超出店铺营业时间");
}
private boolean isSegmentWithinBusinessHours(AppointmentTimeSegment segment, ShopScheduleSupport.LocalTimeRange hours) {
if (segment == null || hours == null || hours.startTime() == null || hours.endTime() == null) {
return false;
}
return !segment.startTime().isBefore(hours.startTime()) && isSegmentEndCovered(segment.endTime(), hours.endTime());
}
private boolean isSegmentEndCovered(LocalTime segmentEndTime, LocalTime businessEndTime) {
if (!segmentEndTime.isAfter(businessEndTime)) {
return true;
}
return LocalTime.MAX.equals(segmentEndTime) && isFullDayBusinessEndTime(businessEndTime);
}
private boolean isFullDayBusinessEndTime(LocalTime businessEndTime) {
return businessEndTime != null && !businessEndTime.isBefore(LocalTime.of(23, 59));
}
private List<AppointmentTimeSegment> splitAppointmentSegments(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime == null || endTime == null || !startTime.isBefore(endTime)) {
throw new RuntimeException("预约时间范围不正确");
}
List<AppointmentTimeSegment> segments = new ArrayList<>();
LocalDateTime cursor = startTime;
while (cursor.isBefore(endTime)) {
LocalDate workDate = cursor.toLocalDate();
LocalDateTime nextDayStart = workDate.plusDays(1L).atStartOfDay();
LocalDateTime segmentEndDateTime = endTime.isBefore(nextDayStart) ? endTime : nextDayStart;
LocalTime segmentEndTime = segmentEndDateTime.equals(nextDayStart) ? LocalTime.MAX : segmentEndDateTime.toLocalTime();
segments.add(new AppointmentTimeSegment(workDate, cursor.toLocalTime(), segmentEndTime));
cursor = segmentEndDateTime;
}
return segments;
}
private ShopScheduleExceptionDO selectShopScheduleException(Long shopId, LocalDate workDate) {
@@ -1224,6 +1263,9 @@ public class PlanningServiceImpl implements PlanningService {
.collect(Collectors.toMap(UserDO::getId, Function.identity(), (a, b) -> a));
}
private record AppointmentTimeSegment(LocalDate workDate, LocalTime startTime, LocalTime endTime) {
}
private String resolveArtistColor(NailArtistInfoDO info, Long artistId, int index) {
if (info != null && hasText(info.getColor()) && info.getColor().trim().matches("^#[0-9a-fA-F]{6}$")) {
return info.getColor().trim().toUpperCase();
@@ -8,6 +8,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.jayucloud.core.config.BrandProperties;
import com.xiaowang.jayucloud.core.common.dto.*;
import com.xiaowang.jayucloud.core.modules.artist.dto.*;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleRuleDO;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleRuleMapper;
import com.xiaowang.jayucloud.core.modules.artist.support.ArtistScheduleRuleSupport;
import com.xiaowang.jayucloud.core.modules.auth.dto.*;
import com.xiaowang.jayucloud.core.modules.content.dto.*;
import com.xiaowang.jayucloud.core.modules.file.dto.*;
@@ -180,6 +183,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
* Variable `nailArtistInfoService`: Spring-injected collaborator used by this class to finish database access, validation, or downstream calls.
*/
private final NailArtistInfoService nailArtistInfoService;
private final ArtistScheduleRuleMapper artistScheduleRuleMapper;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
/**
* Variable `verificationCodeService`: Spring-injected collaborator used by this class to finish database access, validation, or downstream calls.
@@ -471,10 +475,26 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
nailArtistInfoDTO.setStatus(1); // 默认状态为营业中
nailArtistInfoDTO.setPortfolio(""); // 默认作品集为空
nailArtistInfoService.saveOrUpdateNailArtistInfo(nailArtistInfoDTO);
ensureDefaultArtistScheduleRule(validatedShopId, userDO.getId());
}
return userDO.getId();
}
private void ensureDefaultArtistScheduleRule(Long shopId, Long artistId) {
if (shopId == null || artistId == null) {
throw new RuntimeException("创建员工默认排班缺少店铺或员工ID");
}
ArtistScheduleRuleDO existingRule = artistScheduleRuleMapper.selectOne(new LambdaQueryWrapper<ArtistScheduleRuleDO>()
.eq(ArtistScheduleRuleDO::getShopId, shopId)
.eq(ArtistScheduleRuleDO::getArtistId, artistId)
.eq(ArtistScheduleRuleDO::getIsDelete, 0)
.last("LIMIT 1"));
if (existingRule != null) {
return;
}
artistScheduleRuleMapper.insert(ArtistScheduleRuleSupport.defaultRule(shopId, artistId));
}
/**
* 分配默认权限