智慧教务系统
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.
 
 
 
 
 
 

279 lines
10 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 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';
$order = 'id desc';
$search_model = $this->model->withSearch(["id","campus_id","venue_id","course_date","time_slot","course_id","coach_id","participants","student_ids","available_capacity","status"], $where)->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';
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
return $info;
}
/**
* 添加课程安排
* @param array $data
* @return mixed
*/
public function add(array $data)
{
$res = $this->model->create($data);
return $res->id;
}
/**
* 课程安排编辑
* @param int $id
* @param array $data
* @return bool
*/
public function edit(int $id, array $data)
{
$this->model->where([['id', '=', $id]])->update($data);
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)) {
$time_slots = ['9:00-10:00', '10:00-11:00', '11:00-12:00', '14:00-15:00', '15:00-16:00', '16:00-17:00'];
$classrooms = [1, 2]; // 默认教室ID
} 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 string $start_date 开始日期
* @param string $end_date 结束日期
* @param int $campus_id 校区ID
* @return array 创建的课程安排记录
*/
private function autoCreateSchedules(string $start_date, string $end_date, int $campus_id): array
{
$schedules = [];
$default_time_slots = ['9:00-10:00', '10:00-11:00', '11:00-12:00', '14:00-15:00', '15:00-16:00', '16:00-17:00'];
$default_venues = [1, 2]; // 默认教室ID
$current_date = $start_date;
// 遍历日期范围
while (strtotime($current_date) <= strtotime($end_date)) {
foreach ($default_venues as $venue_id) {
foreach ($default_time_slots as $time_slot) {
// 创建课程安排记录
$schedule_data = [
'campus_id' => $campus_id,
'venue_id' => $venue_id,
'course_date' => $current_date,
'time_slot' => $time_slot,
'course_id' => 0, // 默认课程ID
'coach_id' => 0, // 默认教练ID
'participants' => json_encode([]), // 空参与者列表
'student_ids' => json_encode([]), // 空学生列表
'available_capacity' => 10, // 默认容量
'status' => 'pending', // 默认状态
'created_by' => 'system', // 系统创建
];
// 插入数据库
$res = $this->model->create($schedule_data);
// 添加到返回结果
$schedule_data['id'] = $res->id;
$schedules[] = $schedule_data;
}
}
$current_date = date('Y-m-d', strtotime($current_date . ' +1 day'));
}
return $schedules;
}
public function getCampusVenue($id)
{
return $this->model->where('availability_status', 1)->where('campus_id',$id)->select();
}
}