diff --git a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImpl.java b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImpl.java index 9115af3a..6e14c1b5 100644 --- a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImpl.java +++ b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImpl.java @@ -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 resolveWorkDays(List inputWorkDays, String presetType) { - List 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 normalized = new LinkedHashSet<>(); - for (Integer day : presetDays == null ? List.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 workDays) { - return workDays.stream() - .sorted() - .map(String::valueOf) - .collect(Collectors.joining(",")); - } - - private List 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) { - } } diff --git a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/support/ArtistScheduleRuleSupport.java b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/support/ArtistScheduleRuleSupport.java new file mode 100644 index 00000000..2258381d --- /dev/null +++ b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/artist/support/ArtistScheduleRuleSupport.java @@ -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 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 resolveWorkDays(List inputWorkDays, String presetType) { + String normalizedPreset = normalizePresetType(presetType); + List 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 normalized = normalizeWorkDays(presetDays); + if (normalized.isEmpty()) { + throw new RuntimeException("请至少选择一个员工工作日"); + } + return normalized; + } + + public static String encodeWorkDays(List workDays) { + return normalizeWorkDays(workDays).stream() + .map(String::valueOf) + .collect(Collectors.joining(",")); + } + + public static List 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 normalizeWorkDays(List workDays) { + Set normalized = new LinkedHashSet<>(); + for (Integer day : workDays == null ? List.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) { + } +} diff --git a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImpl.java b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImpl.java index dd67225e..374487ad 100644 --- a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImpl.java +++ b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImpl.java @@ -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() + .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() + .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); + } } diff --git a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImpl.java b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImpl.java index e98ade79..378f39e8 100644 --- a/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImpl.java +++ b/JY-Core/src/main/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImpl.java @@ -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 shopExceptionByDate = listShopScheduleExceptions(shopId, start, end); List artists = listArtists(shopId, artistId); - List schedules = listSchedules(shopId, start, end, artistId); + List schedules = listSchedules(shopId, start, end, artists, artistId); List 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 listSchedules(Long shopId, LocalDate start, LocalDate end, Long artistId) { + private List listSchedules(Long shopId, LocalDate start, LocalDate end, + List artists, Long artistId) { + List explicitSchedules = listExplicitSchedules(shopId, start, end, artistId); + if (artists == null || artists.isEmpty()) { + return explicitSchedules; + } + return mergeRuleSchedules(shopId, start, end, artists, explicitSchedules); + } + + private List listExplicitSchedules(Long shopId, LocalDate start, LocalDate end, Long artistId) { QueryWrapper 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 mergeRuleSchedules(Long shopId, LocalDate start, LocalDate end, + List artists, + List explicitSchedules) { + Set artistIds = artists.stream() + .map(PlanningArtistVO::getId) + .filter(Objects::nonNull) + .collect(Collectors.toCollection(HashSet::new)); + if (artistIds.isEmpty()) { + return explicitSchedules; + } + Map ruleByArtist = loadArtistScheduleRules(shopId, artistIds); + if (ruleByArtist.isEmpty()) { + return explicitSchedules; + } + Map exceptionByArtistDate = loadArtistScheduleExceptions(shopId, artistIds, start, end); + Set explicitDayKeys = explicitSchedules.stream() + .filter(schedule -> schedule.getArtistId() != null && schedule.getWorkDate() != null) + .map(this::scheduleKey) + .collect(Collectors.toCollection(HashSet::new)); + List 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 loadArtistScheduleRules(Long shopId, Set artistIds) { + return artistScheduleRuleMapper.selectList(new QueryWrapper() + .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 loadArtistScheduleExceptions(Long shopId, Set artistIds, + LocalDate start, LocalDate end) { + return artistScheduleExceptionMapper.selectList(new QueryWrapper() + .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 artists) { Map artistNames = artists.stream() .collect(Collectors.toMap(PlanningArtistVO::getId, PlanningArtistVO::getName, (a, b) -> a)); diff --git a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImplTest.java b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImplTest.java index f86ac1e8..b7db8d05 100644 --- a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImplTest.java +++ b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/artist/service/impl/ArtistScheduleServiceImplTest.java @@ -62,6 +62,18 @@ class ArtistScheduleServiceImplTest { assertThat(statusCaptor.getAllValues()).contains(1, 0); } + @Test + void saveEverydayRuleGeneratesAllDays() { + service.saveRule(rule("EVERYDAY", null, LocalTime.of(10, 0), LocalTime.of(22, 0))); + + ArgumentCaptor ruleCaptor = ArgumentCaptor.forClass(ArtistScheduleRuleDO.class); + ArgumentCaptor statusCaptor = ArgumentCaptor.forClass(Integer.class); + verify(ruleMapper).insert(ruleCaptor.capture()); + verify(scheduleMapper, org.mockito.Mockito.atLeastOnce()).updateGeneratedSchedule(any(), any(), any(), any(), any(), statusCaptor.capture(), any()); + assertThat(ruleCaptor.getValue().getWorkDays()).isEqualTo("1,2,3,4,5,6,7"); + assertThat(statusCaptor.getAllValues()).allMatch(value -> value == 1); + } + @Test void saveCustomRuleUsesSelectedWeekdaysOnly() { service.saveRule(rule("CUSTOM", List.of(2, 4), LocalTime.of(10, 0), LocalTime.of(20, 0))); diff --git a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImplTest.java b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImplTest.java index 1506ba91..4e13cb85 100644 --- a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImplTest.java +++ b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/AppointmentCapacityServiceImplTest.java @@ -1,9 +1,13 @@ package com.xiaowang.jayucloud.core.modules.planning.service.impl; 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.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.artist.mapper.ArtistScheduleExceptionMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -16,6 +20,7 @@ import java.time.LocalTime; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.never; import static org.mockito.Mockito.when; @@ -26,13 +31,17 @@ class AppointmentCapacityServiceImplTest { private AppointmentReservationMapper reservationMapper; @Mock private ArtistScheduleMapper scheduleMapper; + @Mock + private ArtistScheduleRuleMapper ruleMapper; + @Mock + private ArtistScheduleExceptionMapper exceptionMapper; private AppointmentCapacityServiceImpl service; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); - service = new AppointmentCapacityServiceImpl(reservationMapper, scheduleMapper); + service = new AppointmentCapacityServiceImpl(reservationMapper, scheduleMapper, ruleMapper, exceptionMapper); } @Test @@ -113,6 +122,56 @@ class AppointmentCapacityServiceImplTest { verify(reservationMapper, never()).insert(org.mockito.ArgumentMatchers.any(AppointmentReservationDO.class)); } + @Test + void reserveUsesRuleWhenDailyScheduleMissing() { + LocalDateTime start = LocalDateTime.of(2026, 7, 2, 9, 0); + LocalDateTime end = start.plusHours(1); + when(scheduleMapper.selectCoveringSchedule(9L, 30L, start.toLocalDate(), start.toLocalTime(), end.toLocalTime())) + .thenReturn(null); + when(scheduleMapper.selectDayScheduleForUpdate(9L, 30L, start.toLocalDate())).thenReturn(null); + when(ruleMapper.selectOne(any())).thenReturn(rule("DOUBLE_REST", "1,2,3,4,5", LocalTime.of(9, 0), LocalTime.of(17, 0))); + when(exceptionMapper.selectOne(any())).thenReturn(null); + when(reservationMapper.countOverlappingActiveForUpdate(30L, start, end)).thenReturn(0); + + service.reserveOrThrow(1L, 10L, 9L, 30L, "French", start, end); + + verify(reservationMapper).insert(any(AppointmentReservationDO.class)); + } + + @Test + void reserveRejectsWeekendByRuleWhenDailyScheduleMissing() { + LocalDateTime start = LocalDateTime.of(2026, 7, 4, 9, 0); + LocalDateTime end = start.plusHours(1); + when(scheduleMapper.selectCoveringSchedule(9L, 30L, start.toLocalDate(), start.toLocalTime(), end.toLocalTime())) + .thenReturn(null); + when(scheduleMapper.selectDayScheduleForUpdate(9L, 30L, start.toLocalDate())).thenReturn(null); + when(ruleMapper.selectOne(any())).thenReturn(rule("DOUBLE_REST", "1,2,3,4,5", LocalTime.of(9, 0), LocalTime.of(17, 0))); + when(exceptionMapper.selectOne(any())).thenReturn(null); + + assertThatThrownBy(() -> service.reserveOrThrow(1L, 10L, 9L, 30L, "French", start, end)) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("涓嶅彲棰勭害"); + + verify(reservationMapper, never()).insert(any(AppointmentReservationDO.class)); + } + + @Test + void reserveRejectsRestExceptionByRuleWhenDailyScheduleMissing() { + LocalDateTime start = LocalDateTime.of(2026, 7, 2, 9, 0); + LocalDateTime end = start.plusHours(1); + when(scheduleMapper.selectCoveringSchedule(9L, 30L, start.toLocalDate(), start.toLocalTime(), end.toLocalTime())) + .thenReturn(null); + when(scheduleMapper.selectDayScheduleForUpdate(9L, 30L, start.toLocalDate())).thenReturn(null); + when(ruleMapper.selectOne(any())).thenReturn(rule("DOUBLE_REST", "1,2,3,4,5", LocalTime.of(9, 0), LocalTime.of(17, 0))); + when(exceptionMapper.selectOne(any())).thenReturn(restException(start.toLocalDate())); + + assertThatThrownBy(() -> service.reserveOrThrow(1L, 10L, 9L, 30L, "French", start, end)) + .isInstanceOf(RuntimeException.class) + .hasMessageContaining("涓嶅彲棰勭害"); + + verify(reservationMapper, never()).insert(any(AppointmentReservationDO.class)); + } + @Test void reserveRejectsWhenCoveringScheduleIsDisabled() { LocalDateTime start = LocalDateTime.of(2026, 6, 1, 10, 0); @@ -172,4 +231,29 @@ class AppointmentCapacityServiceImplTest { schedule.setIsDelete(0); return schedule; } + + private ArtistScheduleRuleDO rule(String presetType, String workDays, LocalTime startTime, LocalTime endTime) { + ArtistScheduleRuleDO rule = new ArtistScheduleRuleDO(); + rule.setShopId(9L); + rule.setArtistId(30L); + rule.setPresetType(presetType); + rule.setWorkDays(workDays); + rule.setStartTime(startTime); + rule.setEndTime(endTime); + rule.setSlotMinutes(60); + rule.setCapacity(1); + rule.setStatus(1); + rule.setIsDelete(0); + return rule; + } + + private ArtistScheduleExceptionDO restException(LocalDate workDate) { + ArtistScheduleExceptionDO exception = new ArtistScheduleExceptionDO(); + exception.setShopId(9L); + exception.setArtistId(30L); + exception.setWorkDate(workDate); + exception.setExceptionType("REST"); + exception.setIsDelete(0); + return exception; + } } diff --git a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImplTest.java b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImplTest.java index 2ce09c72..f47aef4a 100644 --- a/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImplTest.java +++ b/JY-Core/src/test/java/com/xiaowang/jayucloud/core/modules/planning/service/impl/PlanningServiceImplTest.java @@ -1,8 +1,11 @@ package com.xiaowang.jayucloud.core.modules.planning.service.impl; 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.ArtistScheduleExceptionMapper; import com.xiaowang.jayucloud.core.modules.artist.mapper.NailArtistInfoMapper; import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleDO; +import com.xiaowang.jayucloud.core.modules.artist.entity.ArtistScheduleRuleDO; import com.xiaowang.jayucloud.core.modules.order.entity.OrderDO; import com.xiaowang.jayucloud.core.modules.order.entity.OrderItemDO; import com.xiaowang.jayucloud.core.modules.order.mapper.OrderItemMapper; @@ -54,6 +57,12 @@ class PlanningServiceImplTest { @Mock private ArtistScheduleMapper artistScheduleMapper; + @Mock + private ArtistScheduleRuleMapper artistScheduleRuleMapper; + + @Mock + private ArtistScheduleExceptionMapper artistScheduleExceptionMapper; + @Mock private AppointmentReservationMapper appointmentReservationMapper; @@ -90,6 +99,8 @@ class PlanningServiceImplTest { userMapper, nailArtistInfoMapper, artistScheduleMapper, + artistScheduleRuleMapper, + artistScheduleExceptionMapper, appointmentReservationMapper, orderMapper, orderItemMapper, @@ -256,6 +267,7 @@ class PlanningServiceImplTest { when(userMapper.selectList(any())).thenReturn(List.of(freeArtist, shortShiftArtist)); when(nailArtistInfoMapper.selectList(any())).thenReturn(List.of()); when(artistScheduleMapper.selectList(any())).thenReturn(List.of(freeSchedule, shortSchedule)); + when(artistScheduleRuleMapper.selectList(any())).thenReturn(List.of()); when(appointmentReservationMapper.selectList(any())).thenReturn(List.of(reservation)); when(orderItemMapper.selectList(any())).thenReturn(List.of()); when(orderItemMapper.selectBatchIds(any())).thenReturn(List.of(item)); @@ -272,6 +284,42 @@ class PlanningServiceImplTest { .contains("20:working", "21:full"); } + @Test + void calendarUsesRuleWhenDailyScheduleMissing() { + UserDO manager = user(2, 8L); + UserDO artist = user(3, 8L); + artist.setId(20L); + artist.setNickname("美甲师1"); + LocalDate date = LocalDate.of(2026, 7, 2); + ShopDO shop = new ShopDO(); + shop.setBusinessHours("09:00-18:00"); + + when(adminScopeHelper.resolveShopScope(manager, null)).thenReturn(8L); + when(shopMapper.selectById(8L)).thenReturn(shop); + when(shopScheduleExceptionMapper.selectList(any())).thenReturn(List.of()); + when(userMapper.selectList(any())).thenReturn(List.of(artist)); + when(nailArtistInfoMapper.selectList(any())).thenReturn(List.of()); + when(artistScheduleMapper.selectList(any())).thenReturn(List.of()); + when(artistScheduleRuleMapper.selectList(any())) + .thenReturn(List.of(scheduleRule(20L, "DOUBLE_REST", "1,2,3,4,5", LocalTime.of(9, 0), LocalTime.of(17, 0)))); + when(artistScheduleExceptionMapper.selectList(any())).thenReturn(List.of()); + when(appointmentReservationMapper.selectList(any())).thenReturn(List.of()); + + var calendar = planningService.getCalendar(manager, date, date, 20L); + + assertThat(calendar.getSchedules()).hasSize(1); + var schedule = calendar.getSchedules().get(0); + assertThat(schedule.getArtistId()).isEqualTo(20L); + assertThat(schedule.getDate()).isEqualTo("2026-07-02"); + assertThat(schedule.getStartTime()).isEqualTo("09:00"); + assertThat(schedule.getEndTime()).isEqualTo("17:00"); + assertThat(schedule.getStatus()).isEqualTo(1); + assertThat(calendar.getDaySummaries().get(0).getDayStatus()).isEqualTo("free"); + assertThat(calendar.getDaySummaries().get(0).getArtistCounts()) + .extracting(count -> count.getArtistId() + ":" + count.getStatus()) + .containsExactly("20:free"); + } + private UserDO user(Integer type, Long shopId) { UserDO user = new UserDO(); user.setId(10L + type); @@ -344,4 +392,19 @@ class PlanningServiceImplTest { product.setIsDelete(0); return product; } + + private ArtistScheduleRuleDO scheduleRule(Long artistId, String presetType, String workDays, LocalTime startTime, LocalTime endTime) { + ArtistScheduleRuleDO rule = new ArtistScheduleRuleDO(); + rule.setShopId(8L); + rule.setArtistId(artistId); + rule.setPresetType(presetType); + rule.setWorkDays(workDays); + rule.setStartTime(startTime); + rule.setEndTime(endTime); + rule.setSlotMinutes(60); + rule.setCapacity(1); + rule.setStatus(1); + rule.setIsDelete(0); + return rule; + } } diff --git a/JY-Salon-Front/src/views/AppointmentFormView.vue b/JY-Salon-Front/src/views/AppointmentFormView.vue index 61114470..21027b9f 100644 --- a/JY-Salon-Front/src/views/AppointmentFormView.vue +++ b/JY-Salon-Front/src/views/AppointmentFormView.vue @@ -6,13 +6,37 @@
-
-
-
- +
+ +
+ + +
+
+
正在搜索客户...
+ +
+ 新客户 + + + +
+
@@ -38,7 +62,7 @@ @update:model-value="updateProductModalVisible" >
- +
+
@@ -55,6 +83,17 @@ + + +
+ +
图
+ + +
@@ -95,6 +134,7 @@ import { managerCustomersApi } from '@/api/managerCustomers' import { managerEmployeesApi } from '@/api/managerEmployees' import { managerPlanningApi } from '@/api/managerPlanning' import { managerProductsApi } from '@/api/managerProducts' +import { fileApi } from '@/api/file' import type { AppointmentForm, Artist, Product } from '@/types/manager' import type { UserInfo } from '@/types/common' import { useSessionStore } from '@/stores/session' @@ -111,6 +151,7 @@ interface ProductCard extends Product { priceText: string durationText: string artistText: string + tagList: string[] } interface AppointmentModalState { @@ -148,6 +189,11 @@ const form = reactive({ const draftSavedAtText = ref(baseForm ? '已恢复本机草稿' : '') const customerOptions = ref([]) const customerTimer = ref(null) +const customerSearchText = ref([form.customerName, form.phone].filter(Boolean).join(' ')) +const customerDropdownOpen = ref(false) +const customerSearching = ref(false) +const newCustomerName = ref(form.customerName) +const newCustomerPhone = ref(form.phone) const products = ref([]) const filteredProducts = ref([]) const productKeyword = ref('') @@ -161,12 +207,17 @@ const timeSelectTip = ref('请先选择美甲师并填写时长') const conflictTip = ref('') const conflictLevel = ref<'free' | 'danger'>('free') const loading = ref(false) +const manualImageUploading = ref(false) const manualProductChanged = computed(() => { return Boolean( form.manualProductTitle || form.manualProductPrice || form.manualProductTags || form.manualProductDescription + || form.manualProductTargetAudience + || form.manualProductPrecautions + || form.manualProductImageFileIds + || form.manualProductImageUrl ) }) const appointmentFormChanged = computed(() => { @@ -231,15 +282,45 @@ const selectedProductTitle = computed(() => { return '请选择作品' }) -const normalizeProduct = (product: Product): ProductCard => { +const parseProductTags = (product: Product): string[] => { + if (Array.isArray(product.tagList) && product.tagList.length > 0) { + return product.tagList.map(tag => String(tag || '').trim()).filter(Boolean) + } + if (Array.isArray(product.tagInfos) && product.tagInfos.length > 0) { + return product.tagInfos.map(tag => String(tag.tagName || '').trim()).filter(Boolean) + } + return String(product.tags || '') + .split(/[;,,、]/) + .map(tag => tag.trim()) + .filter(Boolean) +} + +const resolveProductArtistText = (product: Product, artistOptions: Artist[]): string => { + const artistIds = String(product.bookableArtists || '') + .split(',') + .map(id => id.trim()) + .filter(Boolean) + if (artistIds.length === 0) { + return '全部美甲师' + } + const artistNames = artistIds + .map(id => artistOptions.find(artist => String(artist.id) === id)) + .filter((artist): artist is Artist => Boolean(artist)) + .map(artist => artist.name || artist.nickname || '') + .filter(Boolean) + return artistNames.length > 0 ? artistNames.join('、') : '指定美甲师' +} + +const normalizeProduct = (product: Product, artistOptions: Artist[]): ProductCard => { const imageUrls = Array.isArray(product.imageUrls) ? product.imageUrls : [] const durationText = product.approximateDuration || (product.serviceDurationMinutes ? `${product.serviceDurationMinutes}分钟` : '') return { ...product, coverImage: imageUrls[0] || '', + tagList: parseProductTags(product).slice(0, 4), priceText: product.price || product.price === 0 ? `¥${product.price}` : '价格待定', durationText, - artistText: '全部美甲师' + artistText: resolveProductArtistText(product, artistOptions) } } @@ -249,7 +330,8 @@ const filterProducts = (): void => { if (!keyword) { return true } - return [product.title, product.priceText, product.durationText, product.artistText].some(value => String(value || '').toLowerCase().includes(keyword)) + return [product.title, product.priceText, product.durationText, product.artistText, ...product.tagList] + .some(value => String(value || '').toLowerCase().includes(keyword)) }) } @@ -258,10 +340,10 @@ const loadOptions = async (): Promise => { managerProductsApi.list({ page: 1, size: 100 }), managerEmployeesApi.list() ]) - const productList = Array.isArray(productData) ? productData : productData.list - products.value = productList.map(normalizeProduct) - filteredProducts.value = products.value artists.value = employeeData + const productList = Array.isArray(productData) ? productData : productData.list + products.value = productList.map(product => normalizeProduct(product, artists.value)) + filteredProducts.value = products.value if (form.productId) { selectedProduct.value = products.value.find(item => String(item.id) === String(form.productId)) || null } @@ -277,16 +359,67 @@ const scheduleCustomerSearch = (): void => { } const searchCustomers = async (): Promise => { - const keyword = form.customerName || form.phone - const data = await managerCustomersApi.list({ page: 1, size: 20, keyword }) - customerOptions.value = data.list || [] + const keyword = customerSearchText.value.trim() + customerSearching.value = true + try { + const data = await managerCustomersApi.list({ page: 1, size: 20, keyword }) + customerOptions.value = data.list || [] + } finally { + customerSearching.value = false + } +} + +const openCustomerDropdown = (): void => { + customerDropdownOpen.value = true + if (customerOptions.value.length === 0) { + scheduleCustomerSearch() + } +} + +const onCustomerSearchInput = (): void => { + form.customerUserId = '' + form.customerName = '' + form.phone = '' + newCustomerName.value = customerSearchText.value + customerDropdownOpen.value = true + scheduleCustomerSearch() +} + +const clearCustomerSelection = (): void => { + form.customerUserId = '' + form.customerName = '' + form.phone = '' + customerSearchText.value = '' + newCustomerName.value = '' + newCustomerPhone.value = '' + customerOptions.value = [] + customerDropdownOpen.value = true } const selectCustomer = (customer: UserInfo): void => { form.customerUserId = String(customer.id) form.customerName = customer.nickname || customer.account || '' form.phone = customer.phone || customer.contactPhone || '' + customerSearchText.value = [form.customerName, form.phone].filter(Boolean).join(' ') + newCustomerName.value = form.customerName + newCustomerPhone.value = form.phone customerOptions.value = [] + customerDropdownOpen.value = false +} + +const useNewCustomer = (): void => { + const name = newCustomerName.value.trim() + const phone = newCustomerPhone.value.trim() + if (!name || !phone) { + toast.error('请填写新客户姓名和手机号') + return + } + form.customerUserId = '' + form.customerName = name + form.phone = phone + customerSearchText.value = [name, phone].filter(Boolean).join(' ') + customerOptions.value = [] + customerDropdownOpen.value = false } const onDateChange = async (): Promise => { @@ -299,6 +432,12 @@ const selectProduct = (product: ProductCard): void => { form.productId = String(product.id) form.manualProductTitle = '' form.manualProductPrice = '' + form.manualProductDescription = '' + form.manualProductTags = '' + form.manualProductTargetAudience = '' + form.manualProductPrecautions = '' + form.manualProductImageFileIds = '' + form.manualProductImageUrl = '' form.durationMinutes = String(Number.parseInt(product.approximateDuration || '', 10) || product.serviceDurationMinutes || form.durationMinutes) updateProductModalVisible(false) void refreshAvailableTimes() @@ -316,6 +455,28 @@ const useManualProduct = (): void => { updateProductModalVisible(false) } +const uploadManualProductImage = async (event: Event): Promise => { + const input = event.target as HTMLInputElement + const file = input.files?.[0] + if (!file) { + return + } + manualImageUploading.value = true + try { + const uploaded = await fileApi.uploadObjectFile(file) + form.manualProductImageFileIds = String(uploaded.id) + form.manualProductImageUrl = uploaded.primaryUrl || uploaded.fileUrl || '' + } finally { + input.value = '' + manualImageUploading.value = false + } +} + +const removeManualProductImage = (): void => { + form.manualProductImageFileIds = '' + form.manualProductImageUrl = '' +} + const refreshAvailableTimes = async (): Promise => { const durationMinutes = normalizeDuration(form.durationMinutes) if (!form.appointmentDate || !form.artistId || !durationMinutes) { @@ -447,18 +608,83 @@ watch(timeSheetVisible, syncAppointmentModalState) font-weight: 800; } -.customer-options { - display: grid; - gap: 6px; - padding: 8px 0; +.customer-combobox { + position: relative; + overflow: visible; } -.customer-options button { +.customer-control { + position: relative; + width: 100%; +} + +.customer-control input { + padding-right: 42px; +} + +.customer-clear { + position: absolute; + top: 50%; + right: 8px; + width: 28px; + height: 28px; + transform: translateY(-50%); + border-radius: 50%; + background: var(--subtle); + color: var(--muted); + font-size: 18px; + line-height: 1; +} + +.customer-dropdown { + position: absolute; + z-index: 20; + top: calc(100% + 8px); + right: 0; + left: 0; + display: grid; + max-height: min(420px, 70vh); + gap: 8px; + overflow-y: auto; + padding: 10px; + border: 1px solid var(--border); + border-radius: 12px; + background: var(--surface); + box-shadow: 0 16px 36px rgba(19, 32, 24, 0.16); + -webkit-overflow-scrolling: touch; +} + +.customer-option { display: flex; + align-items: center; justify-content: space-between; - padding: 8px 10px; + gap: 10px; + width: 100%; + padding: 9px 10px; border-radius: 10px; background: var(--subtle); + text-align: left; +} + +.customer-option span, +.customer-option strong, +.customer-option small { + display: block; +} + +.customer-option small, +.customer-option em, +.customer-muted { + color: var(--muted); + font-size: 12px; + font-style: normal; +} + +.customer-new { + display: grid; + gap: 8px; + padding-top: 8px; + border-top: 1px solid var(--border); } .picker-value { @@ -519,11 +745,66 @@ watch(timeSheetVisible, syncAppointmentModalState) font-size: 12px; } +.tag-row { + display: flex; + flex-wrap: wrap; + gap: 5px; + margin-top: 6px; +} + +.tag-row i { + padding: 2px 6px; + border-radius: 999px; + background: var(--accent-soft); + color: var(--accent); + font-size: 11px; + font-style: normal; + font-weight: 700; +} + .manual-form { display: grid; gap: 8px; } +.manual-image-row { + display: grid; + grid-template-columns: 72px 1fr auto; + align-items: center; + gap: 10px; +} + +.manual-image-row img, +.manual-image-placeholder { + width: 72px; + height: 72px; + border-radius: 12px; + object-fit: cover; +} + +.manual-image-placeholder { + display: grid; + place-items: center; + background: var(--subtle); + color: var(--muted); + font-weight: 800; +} + +.image-upload { + position: relative; + display: inline-flex; + align-items: center; + justify-content: center; + min-height: 40px; + overflow: hidden; +} + +.image-upload input { + position: absolute; + inset: 0; + opacity: 0; +} + .textarea { min-height: 88px; padding: 12px;