Files
zyt/server/app/common/service/doctor/RosterSegmentService.php
2026-05-11 17:49:38 +08:00

88 lines
2.3 KiB
PHP
Executable File

<?php
namespace app\common\service\doctor;
/**
* 排班时段:起止时间 + 号源间隔
*/
class RosterSegmentService
{
/**
* 从排班记录解析 [开始 HH:mm, 结束 HH:mm],无效返回 null
*/
public static function resolveWindow(array $roster): ?array
{
$start = isset($roster['start_time']) ? trim((string) $roster['start_time']) : '';
$end = isset($roster['end_time']) ? trim((string) $roster['end_time']) : '';
if ($start !== '' && $end !== ''
&& preg_match('/^\d{2}:\d{2}$/', $start)
&& preg_match('/^\d{2}:\d{2}$/', $end)
&& $start < $end) {
return [$start, $end];
}
$p = $roster['period'] ?? '';
if ($p == 1 || $p === 'morning' || $p === '上午') {
return ['09:00', '12:00'];
}
if ($p == 2 || $p === 'afternoon' || $p === '下午') {
return ['14:00', '18:00'];
}
if ($p === 'night' || $p === '夜班' || $p === 'evening') {
return ['18:00', '22:00'];
}
return null;
}
public static function normalizeSlotMinutes($raw): int
{
$n = (int) $raw;
if ($n < 5) {
return 15;
}
if ($n > 120) {
return 120;
}
return $n;
}
/**
* 左闭右开 [start, end) 按 slotMinutes 切分,得到每个号的开始时刻 HH:mm
*/
public static function generateSlotTimes(string $start, string $end, int $slotMinutes): array
{
$slotMinutes = self::normalizeSlotMinutes($slotMinutes);
$base = '1970-01-01 ';
$cur = strtotime($base . $start . ':00');
$endTs = strtotime($base . $end . ':00');
if ($cur === false || $endTs === false || $endTs <= $cur) {
return [];
}
$times = [];
$step = $slotMinutes * 60;
while ($cur < $endTs) {
$times[] = date('H:i', $cur);
$cur += $step;
}
return $times;
}
/**
* quota>0 时限制最大可约槽位数;quota=0 表示不限制(由时段长度决定)
*/
public static function applyQuotaCap(array $times, int $quota): array
{
if ($quota > 0 && count($times) > $quota) {
return array_slice($times, 0, $quota);
}
return $times;
}
}