修复新建预约客户选择和排班可约时间

This commit is contained in:
2026-07-01 15:48:39 +08:00
parent a4d040d019
commit c647172c80
8 changed files with 820 additions and 171 deletions
@@ -11,6 +11,7 @@ import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleException
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleRuleMapper;
import com.xiaowang.jayucloud.core.modules.artist.service.ArtistScheduleService;
import com.xiaowang.jayucloud.core.modules.artist.support.ArtistScheduleRuleSupport;
import com.xiaowang.jayucloud.core.modules.artist.vo.ArtistScheduleExceptionVO;
import com.xiaowang.jayucloud.core.modules.artist.vo.ArtistScheduleRuleSettingVO;
import com.xiaowang.jayucloud.core.modules.artist.vo.ArtistScheduleRuleVO;
@@ -21,14 +22,10 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -37,17 +34,6 @@ import java.util.stream.Collectors;
public class ArtistScheduleServiceImpl implements ArtistScheduleService {
private static final int GENERATED_DAYS = 60;
private static final int DEFAULT_SLOT_MINUTES = 60;
private static final int DEFAULT_CAPACITY = 1;
private static final String PRESET_SINGLE_REST = "SINGLE_REST";
private static final String PRESET_DOUBLE_REST = "DOUBLE_REST";
private static final String PRESET_NINE_TO_FIVE = "NINE_TO_FIVE";
private static final String PRESET_LATE_SHIFT = "LATE_SHIFT";
private static final String PRESET_CUSTOM = "CUSTOM";
private static final String EXCEPTION_REST = "REST";
private static final String EXCEPTION_EXTRA_WORK = "EXTRA_WORK";
private static final String EXCEPTION_CHANGE_SHIFT = "CHANGE_SHIFT";
private final ArtistScheduleMapper artistScheduleMapper;
private final ArtistScheduleRuleMapper ruleMapper;
private final ArtistScheduleExceptionMapper exceptionMapper;
@@ -157,21 +143,21 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
if (dto == null || dto.getShopId() == null || dto.getArtistId() == null) {
throw new RuntimeException("排班规则信息不完整");
}
String presetType = normalizePresetType(dto.getPresetType());
LocalTime startTime = dto.getStartTime() == null ? presetStartTime(presetType) : dto.getStartTime();
LocalTime endTime = dto.getEndTime() == null ? presetEndTime(presetType) : dto.getEndTime();
validateTimeRange(startTime, endTime);
String presetType = ArtistScheduleRuleSupport.normalizePresetType(dto.getPresetType());
LocalTime startTime = dto.getStartTime() == null ? ArtistScheduleRuleSupport.presetStartTime(presetType) : dto.getStartTime();
LocalTime endTime = dto.getEndTime() == null ? ArtistScheduleRuleSupport.presetEndTime(presetType) : dto.getEndTime();
ArtistScheduleRuleSupport.validateTimeRange(startTime, endTime);
ArtistScheduleRuleDO rule = new ArtistScheduleRuleDO();
rule.setShopId(dto.getShopId());
rule.setArtistId(dto.getArtistId());
rule.setPresetType(presetType);
rule.setWorkDays(encodeWorkDays(resolveWorkDays(dto.getWorkDays(), presetType)));
rule.setWorkDays(ArtistScheduleRuleSupport.encodeWorkDays(ArtistScheduleRuleSupport.resolveWorkDays(dto.getWorkDays(), presetType)));
rule.setStartTime(startTime);
rule.setEndTime(endTime);
rule.setSlotMinutes(DEFAULT_SLOT_MINUTES);
rule.setCapacity(DEFAULT_CAPACITY);
rule.setSlotMinutes(ArtistScheduleRuleSupport.DEFAULT_SLOT_MINUTES);
rule.setCapacity(ArtistScheduleRuleSupport.DEFAULT_CAPACITY);
rule.setStatus(dto.getStatus() == null ? 1 : dto.getStatus());
rule.setRemark(trimText(dto.getRemark()));
rule.setRemark(ArtistScheduleRuleSupport.trimText(dto.getRemark()));
rule.setIsDelete(0);
return rule;
}
@@ -180,14 +166,14 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
if (dto == null || dto.getShopId() == null || dto.getArtistId() == null || dto.getWorkDate() == null) {
throw new RuntimeException("排班例外信息不完整");
}
String exceptionType = normalizeExceptionType(dto.getExceptionType());
String exceptionType = ArtistScheduleRuleSupport.normalizeExceptionType(dto.getExceptionType());
LocalTime startTime = dto.getStartTime();
LocalTime endTime = dto.getEndTime();
if (!EXCEPTION_REST.equals(exceptionType)) {
if (!ArtistScheduleRuleSupport.EXCEPTION_REST.equals(exceptionType)) {
if (startTime == null || endTime == null) {
throw new RuntimeException("临时上班或改班必须填写开始和结束时间");
}
validateTimeRange(startTime, endTime);
ArtistScheduleRuleSupport.validateTimeRange(startTime, endTime);
}
ArtistScheduleExceptionDO exception = new ArtistScheduleExceptionDO();
exception.setShopId(dto.getShopId());
@@ -196,9 +182,9 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
exception.setExceptionType(exceptionType);
exception.setStartTime(startTime);
exception.setEndTime(endTime);
exception.setSlotMinutes(DEFAULT_SLOT_MINUTES);
exception.setCapacity(DEFAULT_CAPACITY);
exception.setRemark(trimText(dto.getRemark()));
exception.setSlotMinutes(ArtistScheduleRuleSupport.DEFAULT_SLOT_MINUTES);
exception.setCapacity(ArtistScheduleRuleSupport.DEFAULT_CAPACITY);
exception.setRemark(ArtistScheduleRuleSupport.trimText(dto.getRemark()));
exception.setIsDelete(0);
return exception;
}
@@ -243,29 +229,16 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
}
private void refreshScheduleForDate(ArtistScheduleRuleDO rule, ArtistScheduleExceptionDO exception, LocalDate workDate) {
GeneratedSchedule generated = resolveGeneratedSchedule(rule, exception, workDate);
ArtistScheduleRuleSupport.ResolvedSchedule generated = ArtistScheduleRuleSupport.resolveGeneratedSchedule(rule, exception, workDate);
if (!generated.working()) {
assertNoActiveReservations(rule.getShopId(), rule.getArtistId(), workDate);
upsertGeneratedSchedule(rule, workDate, rule.getStartTime(), rule.getEndTime(), rule.getSlotMinutes(), rule.getCapacity(), 0, generated.remark());
upsertGeneratedSchedule(rule, workDate, generated.startTime(), generated.endTime(), generated.slotMinutes(), generated.capacity(), 0, generated.remark());
return;
}
assertNoReservationsOutsideSchedule(rule.getShopId(), rule.getArtistId(), workDate, generated.startTime(), generated.endTime());
upsertGeneratedSchedule(rule, workDate, generated.startTime(), generated.endTime(), generated.slotMinutes(), generated.capacity(), 1, generated.remark());
}
private GeneratedSchedule resolveGeneratedSchedule(ArtistScheduleRuleDO rule, ArtistScheduleExceptionDO exception, LocalDate workDate) {
if (exception != null) {
if (EXCEPTION_REST.equals(exception.getExceptionType())) {
return new GeneratedSchedule(false, rule.getStartTime(), rule.getEndTime(), rule.getSlotMinutes(), rule.getCapacity(), exceptionRemark(exception));
}
if (EXCEPTION_EXTRA_WORK.equals(exception.getExceptionType()) || EXCEPTION_CHANGE_SHIFT.equals(exception.getExceptionType())) {
return new GeneratedSchedule(true, exception.getStartTime(), exception.getEndTime(), exception.getSlotMinutes(), exception.getCapacity(), exceptionRemark(exception));
}
}
boolean workingDay = Integer.valueOf(1).equals(rule.getStatus()) && decodeWorkDays(rule.getWorkDays()).contains(workDate.getDayOfWeek().getValue());
return new GeneratedSchedule(workingDay, rule.getStartTime(), rule.getEndTime(), rule.getSlotMinutes(), rule.getCapacity(), "规则生成排班");
}
private void upsertGeneratedSchedule(ArtistScheduleRuleDO rule, LocalDate workDate, LocalTime startTime, LocalTime endTime,
Integer slotMinutes, Integer capacity, Integer status, String remark) {
ArtistScheduleDO schedule = artistScheduleMapper.selectDayScheduleForUpdate(rule.getShopId(), rule.getArtistId(), workDate);
@@ -306,24 +279,13 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
}
private ArtistScheduleRuleDO defaultRule(Long shopId, Long artistId) {
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.setSlotMinutes(DEFAULT_SLOT_MINUTES);
rule.setCapacity(DEFAULT_CAPACITY);
rule.setStatus(1);
rule.setIsDelete(0);
return rule;
return ArtistScheduleRuleSupport.defaultRule(shopId, artistId);
}
private ArtistScheduleRuleVO toRuleVO(ArtistScheduleRuleDO rule) {
ArtistScheduleRuleVO vo = new ArtistScheduleRuleVO();
BeanUtils.copyProperties(rule, vo);
vo.setWorkDays(decodeWorkDays(rule.getWorkDays()));
vo.setWorkDays(ArtistScheduleRuleSupport.decodeWorkDays(rule.getWorkDays()));
return vo;
}
@@ -333,91 +295,4 @@ public class ArtistScheduleServiceImpl implements ArtistScheduleService {
return vo;
}
private String normalizePresetType(String presetType) {
String value = trimText(presetType);
if (value == null || value.isBlank()) {
return PRESET_CUSTOM;
}
return value;
}
private String normalizeExceptionType(String exceptionType) {
String value = trimText(exceptionType);
if (EXCEPTION_REST.equals(value) || EXCEPTION_EXTRA_WORK.equals(value) || EXCEPTION_CHANGE_SHIFT.equals(value)) {
return value;
}
throw new RuntimeException("不支持的排班例外类型:" + value);
}
private List<Integer> resolveWorkDays(List<Integer> inputWorkDays, String presetType) {
List<Integer> presetDays = switch (presetType) {
case PRESET_DOUBLE_REST, PRESET_NINE_TO_FIVE -> List.of(1, 2, 3, 4, 5);
case PRESET_SINGLE_REST, PRESET_LATE_SHIFT -> List.of(1, 2, 3, 4, 5, 6);
default -> inputWorkDays;
};
Set<Integer> normalized = new LinkedHashSet<>();
for (Integer day : presetDays == null ? List.<Integer>of() : presetDays) {
if (day != null && day >= DayOfWeek.MONDAY.getValue() && day <= DayOfWeek.SUNDAY.getValue()) {
normalized.add(day);
}
}
if (normalized.isEmpty()) {
throw new RuntimeException("请至少选择一个工作日");
}
return normalized.stream().sorted().toList();
}
private String encodeWorkDays(List<Integer> workDays) {
return workDays.stream()
.sorted()
.map(String::valueOf)
.collect(Collectors.joining(","));
}
private List<Integer> decodeWorkDays(String workDays) {
if (workDays == null || workDays.isBlank()) {
return new ArrayList<>();
}
return List.of(workDays.split(",")).stream()
.map(String::trim)
.filter(value -> !value.isBlank())
.map(Integer::valueOf)
.sorted()
.toList();
}
private LocalTime presetStartTime(String presetType) {
if (PRESET_NINE_TO_FIVE.equals(presetType)) {
return LocalTime.of(9, 0);
}
return LocalTime.of(10, 0);
}
private LocalTime presetEndTime(String presetType) {
if (PRESET_NINE_TO_FIVE.equals(presetType)) {
return LocalTime.of(17, 0);
}
return LocalTime.of(22, 0);
}
private void validateTimeRange(LocalTime startTime, LocalTime endTime) {
if (startTime == null || endTime == null || !startTime.isBefore(endTime)) {
throw new RuntimeException("排班开始时间必须早于结束时间");
}
}
private String trimText(String value) {
return value == null ? null : value.trim();
}
private String exceptionRemark(ArtistScheduleExceptionDO exception) {
if (exception.getRemark() != null && !exception.getRemark().isBlank()) {
return exception.getRemark();
}
return "例外排班:" + exception.getExceptionType();
}
private record GeneratedSchedule(boolean working, LocalTime startTime, LocalTime endTime, Integer slotMinutes,
Integer capacity, String remark) {
}
}
@@ -0,0 +1,178 @@
package com.xiaowang.jayucloud.core.modules.artist.support;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleExceptionDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleRuleDO;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public final class ArtistScheduleRuleSupport {
public static final int DEFAULT_SLOT_MINUTES = 60;
public static final int DEFAULT_CAPACITY = 1;
public static final String PRESET_EVERYDAY = "EVERYDAY";
public static final String PRESET_SINGLE_REST = "SINGLE_REST";
public static final String PRESET_DOUBLE_REST = "DOUBLE_REST";
public static final String PRESET_NINE_TO_FIVE = "NINE_TO_FIVE";
public static final String PRESET_LATE_SHIFT = "LATE_SHIFT";
public static final String PRESET_CUSTOM = "CUSTOM";
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";
private static final List<Integer> EVERYDAY_WORK_DAYS = List.of(1, 2, 3, 4, 5, 6, 7);
private ArtistScheduleRuleSupport() {
}
public static String normalizePresetType(String presetType) {
String value = trimText(presetType);
if (value == null || value.isBlank()) {
return PRESET_CUSTOM;
}
if (PRESET_EVERYDAY.equals(value) || PRESET_SINGLE_REST.equals(value) || PRESET_DOUBLE_REST.equals(value)
|| PRESET_NINE_TO_FIVE.equals(value) || PRESET_LATE_SHIFT.equals(value) || PRESET_CUSTOM.equals(value)) {
return value;
}
throw new RuntimeException("不支持的员工排班预设:" + value);
}
public static String normalizeExceptionType(String exceptionType) {
String value = trimText(exceptionType);
if (EXCEPTION_REST.equals(value) || EXCEPTION_EXTRA_WORK.equals(value) || EXCEPTION_CHANGE_SHIFT.equals(value)) {
return value;
}
throw new RuntimeException("不支持的员工排班例外类型:" + value);
}
public static List<Integer> resolveWorkDays(List<Integer> inputWorkDays, String presetType) {
String normalizedPreset = normalizePresetType(presetType);
List<Integer> presetDays = switch (normalizedPreset) {
case PRESET_EVERYDAY -> EVERYDAY_WORK_DAYS;
case PRESET_DOUBLE_REST, PRESET_NINE_TO_FIVE -> List.of(1, 2, 3, 4, 5);
case PRESET_SINGLE_REST, PRESET_LATE_SHIFT -> List.of(1, 2, 3, 4, 5, 6);
default -> inputWorkDays;
};
List<Integer> normalized = normalizeWorkDays(presetDays);
if (normalized.isEmpty()) {
throw new RuntimeException("请至少选择一个员工工作日");
}
return normalized;
}
public static String encodeWorkDays(List<Integer> workDays) {
return normalizeWorkDays(workDays).stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
}
public static List<Integer> decodeWorkDays(String workDays) {
if (workDays == null || workDays.isBlank()) {
return new ArrayList<>();
}
return List.of(workDays.split(",")).stream()
.map(String::trim)
.filter(value -> !value.isBlank())
.map(Integer::valueOf)
.sorted()
.toList();
}
public static LocalTime presetStartTime(String presetType) {
String normalizedPreset = normalizePresetType(presetType);
if (PRESET_NINE_TO_FIVE.equals(normalizedPreset)) {
return LocalTime.of(9, 0);
}
return LocalTime.of(10, 0);
}
public static LocalTime presetEndTime(String presetType) {
String normalizedPreset = normalizePresetType(presetType);
if (PRESET_NINE_TO_FIVE.equals(normalizedPreset)) {
return LocalTime.of(17, 0);
}
return LocalTime.of(22, 0);
}
public static void validateTimeRange(LocalTime startTime, LocalTime endTime) {
if (startTime == null || endTime == null || !startTime.isBefore(endTime)) {
throw new RuntimeException("员工排班开始时间必须早于结束时间");
}
}
public static ArtistScheduleRuleDO defaultRule(Long shopId, Long artistId) {
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.setSlotMinutes(DEFAULT_SLOT_MINUTES);
rule.setCapacity(DEFAULT_CAPACITY);
rule.setStatus(1);
rule.setIsDelete(0);
return rule;
}
public static ResolvedSchedule resolveGeneratedSchedule(ArtistScheduleRuleDO rule, ArtistScheduleExceptionDO exception,
LocalDate workDate) {
if (rule == null || workDate == null) {
throw new RuntimeException("员工排班规则和日期不能为空");
}
validateTimeRange(rule.getStartTime(), rule.getEndTime());
if (exception != null) {
if (EXCEPTION_REST.equals(exception.getExceptionType())) {
return new ResolvedSchedule(false, rule.getStartTime(), rule.getEndTime(),
normalizeSlotMinutes(rule.getSlotMinutes()), normalizeCapacity(rule.getCapacity()), exceptionRemark(exception));
}
if (EXCEPTION_EXTRA_WORK.equals(exception.getExceptionType()) || EXCEPTION_CHANGE_SHIFT.equals(exception.getExceptionType())) {
validateTimeRange(exception.getStartTime(), exception.getEndTime());
return new ResolvedSchedule(true, exception.getStartTime(), exception.getEndTime(),
normalizeSlotMinutes(exception.getSlotMinutes()), normalizeCapacity(exception.getCapacity()), exceptionRemark(exception));
}
}
boolean workingDay = Integer.valueOf(1).equals(rule.getStatus())
&& decodeWorkDays(rule.getWorkDays()).contains(workDate.getDayOfWeek().getValue());
return new ResolvedSchedule(workingDay, rule.getStartTime(), rule.getEndTime(),
normalizeSlotMinutes(rule.getSlotMinutes()), normalizeCapacity(rule.getCapacity()), "规则生成排班");
}
public static int normalizeSlotMinutes(Integer slotMinutes) {
return slotMinutes == null || slotMinutes <= 0 ? DEFAULT_SLOT_MINUTES : slotMinutes;
}
public static int normalizeCapacity(Integer capacity) {
return capacity == null || capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
}
public static String trimText(String value) {
return value == null ? null : value.trim();
}
private static List<Integer> normalizeWorkDays(List<Integer> workDays) {
Set<Integer> normalized = new LinkedHashSet<>();
for (Integer day : workDays == null ? List.<Integer>of() : workDays) {
if (day != null && day >= DayOfWeek.MONDAY.getValue() && day <= DayOfWeek.SUNDAY.getValue()) {
normalized.add(day);
}
}
return normalized.stream().sorted().toList();
}
private static String exceptionRemark(ArtistScheduleExceptionDO exception) {
if (exception.getRemark() != null && !exception.getRemark().isBlank()) {
return exception.getRemark();
}
return "例外排班:" + exception.getExceptionType();
}
public record ResolvedSchedule(boolean working, LocalTime startTime, LocalTime endTime, Integer slotMinutes,
Integer capacity, String remark) {
}
}
@@ -1,15 +1,22 @@
package com.xiaowang.jayucloud.core.modules.planning.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleExceptionDO;
import com.xiaowang.jayucloud.core.modules.planning.entity.AppointmentReservationDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleRuleDO;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleExceptionMapper;
import com.xiaowang.jayucloud.core.modules.planning.mapper.AppointmentReservationMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleRuleMapper;
import com.xiaowang.jayucloud.core.modules.planning.service.AppointmentCapacityService;
import com.xiaowang.jayucloud.core.modules.artist.support.ArtistScheduleRuleSupport;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.LocalTime;
@Service
@RequiredArgsConstructor
@@ -24,6 +31,8 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
private final AppointmentReservationMapper reservationMapper;
private final ArtistScheduleMapper scheduleMapper;
private final ArtistScheduleRuleMapper ruleMapper;
private final ArtistScheduleExceptionMapper exceptionMapper;
@Override
@Transactional
@@ -143,11 +152,58 @@ public class AppointmentCapacityServiceImpl implements AppointmentCapacityServic
startTime.toLocalTime(),
endTime.toLocalTime()
);
if (schedule == null || Integer.valueOf(1).equals(schedule.getIsDelete())
if (schedule != null) {
return resolveExplicitScheduleCapacity(schedule);
}
ArtistScheduleDO daySchedule = scheduleMapper.selectDayScheduleForUpdate(shopId, artistId, startTime.toLocalDate());
if (daySchedule != null) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return resolveRuleCapacity(shopId, artistId, startTime, endTime);
}
private int resolveExplicitScheduleCapacity(ArtistScheduleDO schedule) {
if (Integer.valueOf(1).equals(schedule.getIsDelete())
|| !Integer.valueOf(1).equals(schedule.getStatus())
|| schedule.getCapacity() == null || schedule.getCapacity() < MIN_CAPACITY) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return schedule.getCapacity();
}
private int resolveRuleCapacity(Long shopId, Long artistId, LocalDateTime startTime, LocalDateTime endTime) {
ArtistScheduleRuleDO rule = ruleMapper.selectOne(new QueryWrapper<ArtistScheduleRuleDO>()
.eq("shop_id", shopId)
.eq("artist_id", artistId)
.eq("is_delete", 0)
.last("LIMIT 1"));
if (rule == null) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
ArtistScheduleExceptionDO exception = exceptionMapper.selectOne(new QueryWrapper<ArtistScheduleExceptionDO>()
.eq("shop_id", shopId)
.eq("artist_id", artistId)
.eq("work_date", startTime.toLocalDate())
.eq("is_delete", 0)
.last("LIMIT 1"));
ArtistScheduleRuleSupport.ResolvedSchedule resolved = ArtistScheduleRuleSupport.resolveGeneratedSchedule(
rule,
exception,
startTime.toLocalDate()
);
if (!resolved.working()
|| resolved.capacity() == null || resolved.capacity() < MIN_CAPACITY
|| !isTimeCovered(resolved.startTime(), resolved.endTime(), startTime.toLocalTime(), endTime.toLocalTime())) {
throw new RuntimeException(UNAVAILABLE_MESSAGE);
}
return resolved.capacity();
}
private boolean isTimeCovered(LocalTime scheduleStartTime, LocalTime scheduleEndTime,
LocalTime appointmentStartTime, LocalTime appointmentEndTime) {
if (scheduleStartTime == null || scheduleEndTime == null || appointmentStartTime == null || appointmentEndTime == null) {
return false;
}
return !appointmentStartTime.isBefore(scheduleStartTime) && !appointmentEndTime.isAfter(scheduleEndTime);
}
}
@@ -5,7 +5,9 @@ import com.xiaowang.jayucloud.core.modules.planning.dto.PlanningAppointmentCreat
import com.xiaowang.jayucloud.core.modules.planning.dto.PlanningAppointmentMoveDTO;
import com.xiaowang.jayucloud.core.modules.planning.dto.PlanningAppointmentStatusDTO;
import com.xiaowang.jayucloud.core.modules.planning.entity.AppointmentReservationDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleExceptionDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleRuleDO;
import com.xiaowang.jayucloud.core.modules.artist.entity.NailArtistInfoDO;
import com.xiaowang.jayucloud.core.modules.order.entity.OrderDO;
import com.xiaowang.jayucloud.core.modules.order.entity.OrderItemDO;
@@ -14,7 +16,9 @@ import com.xiaowang.jayucloud.core.modules.shop.entity.ShopDO;
import com.xiaowang.jayucloud.core.modules.shop.entity.ShopScheduleExceptionDO;
import com.xiaowang.jayucloud.core.modules.user.entity.UserDO;
import com.xiaowang.jayucloud.core.modules.planning.mapper.AppointmentReservationMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleExceptionMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.ArtistScheduleRuleMapper;
import com.xiaowang.jayucloud.core.modules.artist.mapper.NailArtistInfoMapper;
import com.xiaowang.jayucloud.core.modules.order.mapper.OrderItemMapper;
import com.xiaowang.jayucloud.core.modules.order.mapper.OrderMapper;
@@ -26,6 +30,7 @@ import com.xiaowang.jayucloud.core.modules.planning.service.AppointmentCapacityS
import com.xiaowang.jayucloud.core.modules.order.service.OrderService;
import com.xiaowang.jayucloud.core.modules.planning.service.PlanningService;
import com.xiaowang.jayucloud.core.modules.security.support.AdminScopeHelper;
import com.xiaowang.jayucloud.core.modules.artist.support.ArtistScheduleRuleSupport;
import com.xiaowang.jayucloud.core.modules.order.support.OrderStatus;
import com.xiaowang.jayucloud.core.modules.planning.support.PlanningCalendarSupport;
import com.xiaowang.jayucloud.core.modules.shop.support.ShopScheduleSupport;
@@ -81,6 +86,8 @@ public class PlanningServiceImpl implements PlanningService {
private final UserMapper userMapper;
private final NailArtistInfoMapper nailArtistInfoMapper;
private final ArtistScheduleMapper artistScheduleMapper;
private final ArtistScheduleRuleMapper artistScheduleRuleMapper;
private final ArtistScheduleExceptionMapper artistScheduleExceptionMapper;
private final AppointmentReservationMapper appointmentReservationMapper;
private final OrderMapper orderMapper;
private final OrderItemMapper orderItemMapper;
@@ -110,7 +117,7 @@ public class PlanningServiceImpl implements PlanningService {
Map<LocalDate, ShopScheduleExceptionDO> shopExceptionByDate = listShopScheduleExceptions(shopId, start, end);
List<PlanningArtistVO> artists = listArtists(shopId, artistId);
List<ArtistScheduleDO> schedules = listSchedules(shopId, start, end, artistId);
List<ArtistScheduleDO> schedules = listSchedules(shopId, start, end, artists, artistId);
List<PlanningScheduleVO> scheduleVos = schedules.stream()
.map(schedule -> toScheduleVO(schedule, artists))
.sorted(Comparator.comparing(PlanningScheduleVO::getDate)
@@ -335,7 +342,16 @@ public class PlanningServiceImpl implements PlanningService {
.collect(Collectors.toMap(NailArtistInfoDO::getUserId, Function.identity(), (a, b) -> a));
}
private List<ArtistScheduleDO> listSchedules(Long shopId, LocalDate start, LocalDate end, Long artistId) {
private List<ArtistScheduleDO> listSchedules(Long shopId, LocalDate start, LocalDate end,
List<PlanningArtistVO> artists, Long artistId) {
List<ArtistScheduleDO> explicitSchedules = listExplicitSchedules(shopId, start, end, artistId);
if (artists == null || artists.isEmpty()) {
return explicitSchedules;
}
return mergeRuleSchedules(shopId, start, end, artists, explicitSchedules);
}
private List<ArtistScheduleDO> listExplicitSchedules(Long shopId, LocalDate start, LocalDate end, Long artistId) {
QueryWrapper<ArtistScheduleDO> wrapper = new QueryWrapper<>();
wrapper.eq("shop_id", shopId)
.between("work_date", start, end)
@@ -348,6 +364,90 @@ public class PlanningServiceImpl implements PlanningService {
return artistScheduleMapper.selectList(wrapper);
}
private List<ArtistScheduleDO> mergeRuleSchedules(Long shopId, LocalDate start, LocalDate end,
List<PlanningArtistVO> artists,
List<ArtistScheduleDO> explicitSchedules) {
Set<Long> artistIds = artists.stream()
.map(PlanningArtistVO::getId)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
if (artistIds.isEmpty()) {
return explicitSchedules;
}
Map<Long, ArtistScheduleRuleDO> ruleByArtist = loadArtistScheduleRules(shopId, artistIds);
if (ruleByArtist.isEmpty()) {
return explicitSchedules;
}
Map<String, ArtistScheduleExceptionDO> exceptionByArtistDate = loadArtistScheduleExceptions(shopId, artistIds, start, end);
Set<String> explicitDayKeys = explicitSchedules.stream()
.filter(schedule -> schedule.getArtistId() != null && schedule.getWorkDate() != null)
.map(this::scheduleKey)
.collect(Collectors.toCollection(HashSet::new));
List<ArtistScheduleDO> schedules = new ArrayList<>(explicitSchedules);
LocalDate cursor = start;
while (!cursor.isAfter(end)) {
for (PlanningArtistVO artist : artists) {
ArtistScheduleRuleDO rule = ruleByArtist.get(artist.getId());
if (rule == null || explicitDayKeys.contains(scheduleKey(artist.getId(), cursor))) {
continue;
}
ArtistScheduleRuleSupport.ResolvedSchedule resolved = ArtistScheduleRuleSupport.resolveGeneratedSchedule(
rule,
exceptionByArtistDate.get(scheduleKey(artist.getId(), cursor)),
cursor
);
schedules.add(toRuleSchedule(rule, cursor, resolved));
}
cursor = cursor.plusDays(1L);
}
return schedules;
}
private Map<Long, ArtistScheduleRuleDO> loadArtistScheduleRules(Long shopId, Set<Long> artistIds) {
return artistScheduleRuleMapper.selectList(new QueryWrapper<ArtistScheduleRuleDO>()
.eq("shop_id", shopId)
.eq("is_delete", 0)
.in("artist_id", artistIds))
.stream()
.collect(Collectors.toMap(ArtistScheduleRuleDO::getArtistId, Function.identity(), (a, b) -> a));
}
private Map<String, ArtistScheduleExceptionDO> loadArtistScheduleExceptions(Long shopId, Set<Long> artistIds,
LocalDate start, LocalDate end) {
return artistScheduleExceptionMapper.selectList(new QueryWrapper<ArtistScheduleExceptionDO>()
.eq("shop_id", shopId)
.eq("is_delete", 0)
.in("artist_id", artistIds)
.between("work_date", start, end))
.stream()
.collect(Collectors.toMap(exception -> scheduleKey(exception.getArtistId(), exception.getWorkDate()),
Function.identity(), (a, b) -> a));
}
private ArtistScheduleDO toRuleSchedule(ArtistScheduleRuleDO rule, LocalDate workDate,
ArtistScheduleRuleSupport.ResolvedSchedule resolved) {
ArtistScheduleDO schedule = new ArtistScheduleDO();
schedule.setShopId(rule.getShopId());
schedule.setArtistId(rule.getArtistId());
schedule.setWorkDate(workDate);
schedule.setStartTime(resolved.startTime());
schedule.setEndTime(resolved.endTime());
schedule.setSlotMinutes(resolved.slotMinutes());
schedule.setCapacity(resolved.capacity());
schedule.setStatus(resolved.working() ? 1 : 0);
schedule.setRemark(resolved.remark());
schedule.setIsDelete(0);
return schedule;
}
private String scheduleKey(ArtistScheduleDO schedule) {
return scheduleKey(schedule.getArtistId(), schedule.getWorkDate());
}
private String scheduleKey(Long artistId, LocalDate workDate) {
return artistId + ":" + workDate;
}
private PlanningScheduleVO toScheduleVO(ArtistScheduleDO schedule, List<PlanningArtistVO> artists) {
Map<Long, String> artistNames = artists.stream()
.collect(Collectors.toMap(PlanningArtistVO::getId, PlanningArtistVO::getName, (a, b) -> a));