Skip to content

Commit 50884ba

Browse files
committed
fix calendar config
1 parent f1fe7b4 commit 50884ba

File tree

2 files changed

+113
-50
lines changed

2 files changed

+113
-50
lines changed

src/ProdScheduler/SchedulerComputeTrait.php

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,34 @@ public function compute()
103103
private function initialStartTimeCompute(): int
104104
{
105105
$dayCalendar = $this->getDayCalendar($this->ISTS);
106-
if (empty($dayCalendar)) {
107-
$dayCalendar = $this->getDefaultDayCalendar();
108-
}
109106

110107
$initialScheduleDate = date(self::SCHEDULER_DATE_FORMAT, $this->ISTS);
111108
if ($this->computeDirection === ComputeDirection::Forward) {
112-
foreach ($dayCalendar as $k => $day) {
113-
if ($initialScheduleDate === $day['date']) {
114-
if ($day['is_rest'] === 1) {
115-
if ($k !== count($dayCalendar)) {
116-
$nextDayCalendar = $dayCalendar[$k + 1];
117-
if ($nextDayCalendar['is_rest'] === 1) {
118-
continue;
119-
} else {
120-
$nextDayCalendarDate = $nextDayCalendar['date'];
121-
$nextDayCalendarStartTime = $this->getDayCalendarStartTime($nextDayCalendarDate);
109+
if (empty($dayCalendar)) {
110+
$dayCalendarStartTime = $this->getDayCalendarStartTime($this->defaultDayCalendar);
122111

123-
$this->ISDT = $nextDayCalendarDate . " " . $nextDayCalendarStartTime;
112+
$this->ISDT = $initialScheduleDate . " " . $dayCalendarStartTime;
113+
} else {
114+
foreach ($dayCalendar as $k => $day) {
115+
if ($initialScheduleDate === $day['date']) {
116+
if ($day['is_rest'] === 1) {
117+
if ($k !== count($dayCalendar)) {
118+
$nextDayCalendar = $dayCalendar[$k + 1];
119+
if ($nextDayCalendar['is_rest'] === 1) {
120+
continue;
121+
} else {
122+
$nextDayCalendarDate = $nextDayCalendar['date'];
123+
$nextDayCalendarStartTime = $this->getDayCalendarStartTime($nextDayCalendar);
124+
125+
$this->ISDT = $nextDayCalendarDate . " " . $nextDayCalendarStartTime;
126+
}
124127
}
125-
}
126-
} else {
127-
$dayCalendarDate = $day['date'];
128-
$dayCalendarStartTime = $this->getDayCalendarStartTime($day);
128+
} else {
129+
$dayCalendarDate = $day['date'];
130+
$dayCalendarStartTime = $this->getDayCalendarStartTime($day);
129131

130-
$this->ISDT = $dayCalendarDate . " " . $dayCalendarStartTime;
132+
$this->ISDT = $dayCalendarDate . " " . $dayCalendarStartTime;
133+
}
131134
}
132135
}
133136
}
@@ -167,8 +170,8 @@ private function phaseTimeWithCalendarCompute(int $originStart, int &$start, boo
167170
if ($diff >= $dayDuration) {
168171
while ($diff > 0) {
169172
$dayCalendar = $this->getDayCalendar($originStart);
170-
if ($dayCalendar['duration'] < $diff) {
171-
$diff -= $dayCalendar['duration'];
173+
if ($dayCalendar['dayDuration'] < $diff) {
174+
$diff -= $dayCalendar['dayDuration'];
172175
if ($isReverse) {
173176
$originStart -= self::SCHEDULER_DAY_SECONDS;
174177
} else {
@@ -290,24 +293,4 @@ private function phaseTimeWithRestDayCompute(int &$time, bool $isReverse = false
290293

291294
return $time;
292295
}
293-
294-
private function phaseCompleteTimeCompute(int $start, int $cost): int
295-
{
296-
while ($cost > 0) {
297-
$dayCalendar = $this->getDayCalendar();
298-
if ($dayCalendar['duration'] < $cost) {
299-
$cost -= $dayCalendar['duration'];
300-
$start += self::SCHEDULER_DAY_SECONDS;
301-
} else {
302-
break;
303-
}
304-
}
305-
306-
$originStart = $start;
307-
$end = $start + $cost;
308-
$dayCalendar = $this->getDayCalendar();
309-
310-
311-
return 0;
312-
}
313296
}

src/ProdScheduler/SchedulerConfigTrait.php

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,97 @@ private function parseConfig(array $config)
5757
$this->ISTS = strtotime($config['start']);
5858
$this->list = $this->parseList($config['list']);
5959
$this->listPhase = $config['listPhase'];
60-
$this->defaultDayCalendar = $config['calendar'];
60+
$this->defaultDayCalendar = $this->parseCalendar($config['calendar']);
6161
$this->EDQ = $config['edq'];
62-
$this->monthCalendar = $config['monthCalendar'];
63-
$this->nextMonthCalendar = $config['nextMonthCalendar'];
64-
$this->prevMonthCalendar = $config['prevMonthCalendar'];
62+
$this->monthCalendar = $this->parseCalendars($config['monthCalendar']);
63+
$this->nextMonthCalendar = $this->parseCalendars($config['nextMonthCalendar']);
64+
$this->prevMonthCalendar = $this->parseCalendars($config['prevMonthCalendar']);
6565
$this->initialPhase = $config['initialPhase'];
6666

6767
$this->initialStartTimeCompute();
6868
}
6969

70-
private function parseCalendar(array $calendar): array
70+
private function parseCalendars(array $calendar): array
7171
{
72+
foreach ($calendar as $k => $c) {
73+
$calendar[$k]['profile'] = $this->parseCalendar($c['profile'])['profile'];
74+
}
75+
7276
return $calendar;
7377
}
7478

79+
private function parseCalendar(string $calendarStr): array
80+
{
81+
$calendar = [];
82+
try {
83+
$calendar = json_decode($calendarStr, true);
84+
$c = $calendar['profile'];
85+
$c['profile']['date'] = $calendar['date'] ?? '';
86+
87+
$c['profile']['rest'] = [
88+
[
89+
"end" => $c['profile']['times'][1]['start'],
90+
"name" => "上午",
91+
"start" => $c['profile']['times'][0]['end'],
92+
"duration" => 5400
93+
],
94+
[
95+
"end" => $c['profile']['times'][2]['start'],
96+
"name" => "下午",
97+
"start" => $c['profile']['times'][1]['end'],
98+
"duration" => 1800
99+
],
100+
];
101+
102+
return $c;
103+
} catch (\Throwable $th) {
104+
return [
105+
'profile' => [
106+
"name" => "白班",
107+
"dayStart" => "07:30",
108+
"dayEnd" => "20:30",
109+
"dayDuration" => 14400 + 14400 + 10800,
110+
"times" => [
111+
[
112+
"end" => "11:30",
113+
"name" => "上午",
114+
"start" => "07:30",
115+
"duration" => 14400
116+
],
117+
[
118+
"end" => "17:00",
119+
"name" => "下午",
120+
"start" => "13:00",
121+
"duration" => 14400,
122+
"interval" => 5400
123+
],
124+
[
125+
"end" => "20:30",
126+
"name" => "晚上",
127+
"start" => "17:30",
128+
"duration" => 10800,
129+
"interval" => 1800
130+
]
131+
],
132+
"rest" => [
133+
[
134+
"end" => "13:00",
135+
"name" => "上午",
136+
"start" => "11:30",
137+
"duration" => 5400
138+
],
139+
[
140+
"end" => "17:30",
141+
"name" => "下午",
142+
"start" => "17:00",
143+
"duration" => 1800
144+
],
145+
]
146+
]
147+
];
148+
}
149+
}
150+
75151
private function parseList(array $list): array
76152
{
77153
foreach ($list as $k => $item) {
@@ -133,7 +209,9 @@ private function getDayCalendarStartTime(array $calendar): string
133209
$dayCalendarStartTime = '';
134210
if (isset($calendar['profile']) && count($calendar['profile']) > 0) {
135211
$profile = $calendar['profile'][0];
136-
if (isset($profile['times']) && count($profile['times']) > 0) {
212+
if (isset($profile['dayStart'])) {
213+
$dayCalendarStartTime = $profile['dayStart'];
214+
} elseif (isset($profile['times']) && count($profile['times']) > 0) {
137215
if ($profile['times'][0]['start']) {
138216
$dayCalendarStartTime = $profile['times'][0]['start'];
139217
}
@@ -144,7 +222,9 @@ private function getDayCalendarStartTime(array $calendar): string
144222
$defaultDayCalendar = $this->getDefaultDayCalendar();
145223
if (isset($defaultDayCalendar['profile']) && count($defaultDayCalendar['profile']) > 0) {
146224
$profile = $defaultDayCalendar['profile'][0];
147-
if (isset($profile['times']) && count($profile['times']) > 0) {
225+
if (isset($profile['dayStart'])) {
226+
$dayCalendarStartTime = $profile['dayStart'];
227+
} elseif (isset($profile['times']) && count($profile['times']) > 0) {
148228
if ($profile['times'][0]['start']) {
149229
$dayCalendarStartTime = $profile['times'][0]['start'];
150230
}
@@ -265,10 +345,10 @@ public function getDayCalendar(int $ts = 0): array
265345
}
266346
}
267347

268-
return $c;
348+
return $c['profile'];
269349
}
270350

271-
return $this->dayCalendar;
351+
return $this->defaultDayCalendar['profile'];
272352
}
273353

274354
public function getNextDayCalendar(): array

0 commit comments

Comments
 (0)