修复营业时间处理
This commit is contained in:
1 parent
dd0da90af3
commit
434cb1b258
1 file changed
+36
-34
+36
-34
@@ -25,7 +25,10 @@ public final class MchJobTimeMealSegmentUtils {
|
||||
.optionalEnd()
|
||||
.toFormatter();
|
||||
|
||||
private static final String[] DEFAULT_SEGMENT_NAMES = {"早餐", "午餐", "晚餐"};
|
||||
/** 按时段开始时刻划分:<10 早餐,<15 午餐,<21 晚餐,否则宵夜 */
|
||||
private static final LocalTime BREAKFAST_END = LocalTime.of(10, 0);
|
||||
private static final LocalTime LUNCH_END = LocalTime.of(15, 0);
|
||||
private static final LocalTime DINNER_END = LocalTime.of(21, 0);
|
||||
|
||||
private MchJobTimeMealSegmentUtils() {
|
||||
}
|
||||
@@ -36,13 +39,14 @@ public final class MchJobTimeMealSegmentUtils {
|
||||
|
||||
public static List<OrderingFoodMealSegmentVO> parseMealSegments(String jobTime) {
|
||||
List<OrderingFoodMealSegmentVO> out = new ArrayList<>();
|
||||
List<TimeRange> merged = mergeJobTimeIntervals(jobTime);
|
||||
for (int i = 0; i < merged.size(); i++) {
|
||||
TimeRange r = merged.get(i);
|
||||
List<TimeRange> intervals = parseRawIntervals(jobTime);
|
||||
for (int i = 0; i < intervals.size(); i++) {
|
||||
TimeRange r = intervals.get(i);
|
||||
int mealType = resolveMealTypeByStart(r.start);
|
||||
OrderingFoodMealSegmentVO vo = new OrderingFoodMealSegmentVO();
|
||||
vo.setSegmentIndex(i);
|
||||
vo.setMealSegmentType(resolveMealType(i));
|
||||
vo.setMealSegmentName(resolveMealName(i));
|
||||
vo.setMealSegmentType(mealType);
|
||||
vo.setMealSegmentName(resolveMealNameByType(mealType));
|
||||
vo.setStartTime(r.start.format(HM));
|
||||
vo.setEndTime(r.end.format(HM));
|
||||
out.add(vo);
|
||||
@@ -65,21 +69,36 @@ public final class MchJobTimeMealSegmentUtils {
|
||||
return findSegment(jobTime, segmentIndex) != null;
|
||||
}
|
||||
|
||||
private static int resolveMealType(int index) {
|
||||
if (index >= 0 && index < DEFAULT_SEGMENT_NAMES.length) {
|
||||
return index + 1;
|
||||
/**
|
||||
* 根据时段开始时间推断餐段(支持早餐留空如 {@code ,11:00-12:00,17:00-18:00})。
|
||||
*/
|
||||
private static int resolveMealTypeByStart(LocalTime start) {
|
||||
if (start == null) {
|
||||
return 99;
|
||||
}
|
||||
return 99;
|
||||
if (start.isBefore(BREAKFAST_END)) {
|
||||
return 1;
|
||||
}
|
||||
if (start.isBefore(LUNCH_END)) {
|
||||
return 2;
|
||||
}
|
||||
if (start.isBefore(DINNER_END)) {
|
||||
return 3;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
|
||||
private static String resolveMealName(int index) {
|
||||
if (index >= 0 && index < DEFAULT_SEGMENT_NAMES.length) {
|
||||
return DEFAULT_SEGMENT_NAMES[index];
|
||||
}
|
||||
return "时段" + (index + 1);
|
||||
private static String resolveMealNameByType(int mealType) {
|
||||
return switch (mealType) {
|
||||
case 1 -> "早餐";
|
||||
case 2 -> "午餐";
|
||||
case 3 -> "晚餐";
|
||||
case 4 -> "宵夜";
|
||||
default -> "时段";
|
||||
};
|
||||
}
|
||||
|
||||
private static List<TimeRange> mergeJobTimeIntervals(String jobTime) {
|
||||
private static List<TimeRange> parseRawIntervals(String jobTime) {
|
||||
List<TimeRange> raw = new ArrayList<>();
|
||||
if (isBlankJobTime(jobTime)) {
|
||||
return raw;
|
||||
@@ -100,25 +119,8 @@ public final class MchJobTimeMealSegmentUtils {
|
||||
}
|
||||
raw.add(new TimeRange(a, b));
|
||||
}
|
||||
if (raw.isEmpty()) {
|
||||
return raw;
|
||||
}
|
||||
raw.sort(Comparator.comparing(r -> r.start));
|
||||
List<TimeRange> merged = new ArrayList<>();
|
||||
TimeRange cur = raw.get(0);
|
||||
for (int i = 1; i < raw.size(); i++) {
|
||||
TimeRange next = raw.get(i);
|
||||
if (!next.start.isAfter(cur.end)) {
|
||||
if (next.end.isAfter(cur.end)) {
|
||||
cur = new TimeRange(cur.start, next.end);
|
||||
}
|
||||
} else {
|
||||
merged.add(cur);
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
merged.add(cur);
|
||||
return merged;
|
||||
return raw;
|
||||
}
|
||||
|
||||
private static LocalTime parseHm(String s) {
|
||||
|
||||
Reference in new issue
Block a user