智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

285 lines
9.8 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\course_schedule;
use app\model\course_schedule\CourseSchedule;
use app\model\person_course_schedule\PersonCourseSchedule;
use app\service\admin\venue\VenueService;
use core\base\BaseAdminService;
/**
* 课程安排服务层
* Class CourseScheduleService
* @package app\service\admin\course_schedule
*/
class CourseScheduleService extends BaseAdminService
{
public function __construct()
{
parent::__construct();
$this->model = new CourseSchedule();
}
/**
* 获取课程安排列表
* @param array $where
* @return array
*/
public function getPage(array $where = [])
{
$field = 'id,campus_id,venue_id,course_date,time_slot,course_id,coach_id,participants,student_ids,available_capacity,status,created_by,created_at,updated_at,deleted_at,is_system_add';
$order = 'id desc';
$search_model = $this->model->withSearch(["campus_id", "venue_id", "course_date"], $where)
->with(['venue', 'coach', 'course'])
->field($field)
->order($order);
$list = $this->pageQuery($search_model);
return $list;
}
/**
* 获取课程安排信息
* @param int $id
* @return array
*/
public function getInfo(int $id)
{
$field = 'id,campus_id,venue_id,course_date,time_slot,course_id,coach_id,participants,student_ids,available_capacity,status,created_by,created_at,updated_at,deleted_at,is_system_add';
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
return $info;
}
/**
* 添加课程安排
* @param array $data
* @return mixed
*/
public function add(array $data)
{
$create = [
'campus_id' => $data['campus_id'],
'venue_id' => $data['venue_id'],
'course_date' => $data['course_date'],
'time_slot' => $data['time_slot'],
'course_id' => $data['course_id'],
'coach_id' => $data['coach_id'],
'participants' => $data['participants'] ? $data['participants'] : [],
'student_ids' => $data['student_ids'] ? $data['student_ids'] : [],
'is_system_add' => $data['is_system_add']
];
$status = $this->model->where([
['course_date', '=', $data['course_date']],
['time_slot', '=', $data['time_slot']],
['campus_id', '=', $data['campus_id']],
['venue_id', '=', $data['venue_id']]
])->find();
if ($status) {
throw new \Exception('该时间段已有课程安排');
}
$res = $this->model->create($create);
return $res->id;
}
/**
* 课程安排编辑
* @param int $id
* @param array $data
* @return bool
*/
public function edit(int $id, array $data)
{
$create = [
'campus_id' => $data['campus_id'],
'venue_id' => $data['venue_id'],
'course_date' => $data['course_date'],
'time_slot' => $data['time_slot'],
'course_id' => $data['course_id'],
'coach_id' => $data['coach_id'],
'participants' => $data['participants'] ? $data['participants'] : [],
'student_ids' => $data['student_ids'] ? $data['student_ids'] : [],
'is_system_add' => $data['is_system_add']
];
$status = $this->model->where([
['course_date', '=', $data['course_date']],
['time_slot', '=', $data['time_slot']],
['campus_id', '=', $data['campus_id']],
['venue_id', '=', $data['venue_id']],
['id', '<>', $id]
])->find();
if ($status) {
throw new \Exception('该时间段已有课程安排');
}
$this->model->where([['id', '=', $id]])->update($create);
return true;
}
/**
* 删除课程安排
* @param int $id
* @return bool
*/
public function del(int $id)
{
$model = $this->model->where([['id', '=', $id]])->find();
$res = $model->delete();
return $res;
}
/**
* 获取课程表数据
* @param array $where
* @return array
*/
public function getTimetables(array $where = [])
{
// 获取日期范围,默认为本周
$start_date = $where['start_date'] ?? date('Y-m-d', strtotime('monday this week'));
$end_date = $where['end_date'] ?? date('Y-m-d', strtotime('sunday this week'));
// 校区ID
$campus_id = isset($where['campus_id']) && !empty($where['campus_id']) ? intval($where['campus_id']) : 0;
// 查询条件
$query_condition = [
['course_date', '>=', $start_date],
['course_date', '<=', $end_date]
];
// 如果指定了校区,添加校区筛选条件
if ($campus_id > 0) {
$query_condition[] = ['campus_id', '=', $campus_id];
}
// 查询指定日期范围内的课程安排
$schedules = $this->model->where($query_condition)->select()->toArray();
// 获取所有相关的人员课程安排关系
$schedule_ids = array_column($schedules, 'id');
$person_schedules = [];
if (!empty($schedule_ids)) {
$person_schedules = (new PersonCourseSchedule())->where([
['schedule_id', 'in', $schedule_ids]
])->select()->toArray();
}
// 组织数据结构
$days = [];
$weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
// 获取所有时间段和教室
$time_slots = [];
$classrooms = [];
// 如果没有数据,设置默认的时间段和教室
if (empty($schedules)) {
//获取校区下的所有教室
$venues = (new VenueService())->getVenueAll($campus_id);
$time_slots = (new VenueService())->getVenueTime($venues);
$classrooms = $venues;
} else {
foreach ($schedules as $schedule) {
if (!in_array($schedule['time_slot'], $time_slots)) {
$time_slots[] = $schedule['time_slot'];
}
// 假设venue_id代表教室
if (!in_array($schedule['venue_id'], $classrooms)) {
$classrooms[] = $schedule['venue_id'];
}
}
}
// 按日期组织数据
$current_date = $start_date;
$day_index = (int)date('N', strtotime($current_date)) - 1; // 获取周几(1-7),转为索引(0-6)
while (strtotime($current_date) <= strtotime($end_date)) {
$day_schedules = array_filter($schedules, function ($schedule) use ($current_date) {
return $schedule['course_date'] == $current_date;
});
$day_classrooms = empty($day_schedules) ? $classrooms : [];
if (!empty($day_schedules)) {
foreach ($day_schedules as $schedule) {
if (!in_array($schedule['venue_id'], $day_classrooms)) {
$day_classrooms[] = $schedule['venue_id'];
}
}
}
// 构建每个时间段的数据
$day_time_slots = [];
foreach ($time_slots as $time_slot) {
$slot_data = [
'timeRange' => $time_slot,
'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), // 随机颜色
];
// 查找该时间段的课程
$slot_schedule = array_filter($day_schedules, function ($schedule) use ($time_slot) {
return $schedule['time_slot'] == $time_slot;
});
if (!empty($slot_schedule)) {
$schedule = reset($slot_schedule);
// 查找该课程的学员
$students = array_filter($person_schedules, function ($person) use ($schedule) {
return $person['schedule_id'] == $schedule['id'];
});
$student_names = array_column($students, 'person_id');
$slot_data['course'] = [
'teacher' => $schedule['coach_id'] ?? '',
'students' => $student_names,
'classroom' => $schedule['venue_id'],
'hasnumber' => $schedule['available_capacity'] ?? 0,
];
}
$day_time_slots[] = $slot_data;
}
$days[] = [
'date' => $weekdays[$day_index % 7] . ' (' . $current_date . ')',
'timeSlots' => $day_time_slots,
'classrooms' => $day_classrooms,
];
$current_date = date('Y-m-d', strtotime($current_date . ' +1 day'));
$day_index = ($day_index + 1) % 7;
}
return $days;
}
/**
* @param $id
* @return CourseSchedule[]|array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCampusVenue($id)
{
return $this->model->where('availability_status', 1)->where('campus_id', $id)->select();
}
}