course/app/common/service/TeacherScheduleTimeService.php

152 lines
5.6 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\Teacher;
use app\common\model\TeacherScheduleTime;
use app\constant\ResponseCode;
use DateTime;
use think\Exception;
class TeacherScheduleTimeService
{
/**
* @desc 添加空闲时间
* @param $request
* @return array|void
*/
public function addScheduleTime($request)
{
try {
if (empty($request->teacher)) {
throw new Exception('请教师登陆后再设置');
}
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
if ($teacher->isEmpty()) {
throw new Exception('未找到教师信息,设置失败');
}
$data = $request->post();
$free_time = json_decode($data['schedule_time'], true);
if (empty($free_time)) {
throw new Exception('请选择时间段之后再提交');
}
foreach ($free_time as $free_date => $times) {
if ($times) {
foreach ($times as $time) {
$time_period = explode(' - ', $time);
$firstDate = new DateTime($free_date . ' ' . $time_period[0]);
$secondDate = new DateTime($free_date . ' ' . $time_period[1]);
$diff = $secondDate->diff($firstDate);
$h = $diff->h;
$m = round($diff->i / 60, 2);
$hour = round($h + $m, 2);
$free_data = [
'teacher_id' => $request->teacher->id,
'date' => $free_date,
'time' => $time,
'hour' => $hour,
'start_time' => date('Y-m-d H:i:s', $firstDate->getTimestamp()),
'end_time' => date('Y-m-d H:i:s', $secondDate->getTimestamp()),
'month' => date('Y-m', strtotime($free_date)),
];
//@todo:判断是否已经存在
$res = TeacherScheduleTime::create($free_data);
if (!$res) {
throw new Exception('保存失败');
}
}
}
}
return [
'code' => ResponseCode::SUCCESS,
'msg' => '保存成功'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 获取排课列表
* @param $request
* @return array
*/
public function getScheduleTime($request)
{
try {
if (empty($request->teacher)) {
throw new Exception('请教师登陆后再设置');
}
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
if ($teacher->isEmpty()) {
throw new Exception('未找到教师信息,设置失败');
}
$data = $request->get();
$schedule = TeacherScheduleTime::order('id desc')->where(['teacher_id' => $teacher->id, 'is_publish' => 1]);
if (isset($data['month']) && !empty($data['month'])) {
$schedule->where('month', $data['month']);
}
if (isset($data['date']) && !empty($data['date'])) {
$schedule->where('date', $data['date']);
}
$list = $schedule->field('id,teacher_id,date,en_date,time,en_time,hour,month,en_month,time_zone_id,time_zone_name,time_zone_abbr,time_zone_offset,subject_id,is_publish')
->with(['subject', 'studentSchedule', 'teacher', 'studentHomework'])
->select()->toArray();
foreach ($list as $key => &$item) {
if ($item['studentSchedule']) {
foreach ($item['studentSchedule'] as $index => $value) {
if (!$value['is_publish']) {
unset($item['studentSchedule'][$index]);
} else {
unset($item['studentSchedule'][$index]['deleted_at']);
unset($item['studentSchedule'][$index]['updated_at']);
unset($item['studentSchedule'][$index]['created_at']);
unset($item['studentSchedule'][$index]['month']);
unset($item['studentSchedule'][$index]['teacher_schedule_time_detail']);
unset($item['studentSchedule'][$index]['end_time']);
unset($item['studentSchedule'][$index]['start_time']);
}
}
}
if ($item['studentHomework']) {
$item['has_homework'] = 0;
foreach ($item['studentHomework'] as $index => $value) {
if ($value['is_publish']) {
$item['has_homework'] = 1;
} else {
unset($item['studentHomework'][$index]);
}
}
} else {
$item['has_homework'] = 0;
}
}
return [
'code' => ResponseCode::SUCCESS,
'data' => $list,
'msg' => 'success',
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
}