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

197 lines
6.6 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\person_course_schedule;
use app\model\course_schedule\CourseSchedule;
use app\model\customer_resources\CustomerResources;
use app\model\person_course_schedule\PersonCourseSchedule;
use app\model\student\Student;
use app\model\venue\Venue;
use core\base\BaseAdminService;
/**
* 人员与课程安排关系服务层
* Class PersonCourseScheduleService
* @package app\service\admin\person_course_schedule
*/
class PersonCourseScheduleService extends BaseAdminService
{
public function __construct()
{
parent::__construct();
$this->model = new PersonCourseSchedule();
}
/**
* 获取人员与课程安排关系列表
* @param array $where
* @return array
*/
public function getPage(array $where = [])
{
$field = 'id,person_id,person_type,schedule_id,course_date,time_slot,created_at,updated_at';
$order = 'id desc';
$search_model = $this->model->withSearch(["id", "person_id", "person_type", "schedule_id", "course_date", "time_slot"], $where)->field($field)->order($order);
$list = $this->pageQuery($search_model);
return $list;
}
/**
* 获取人员与课程安排关系信息
* @param int $id
* @return array
*/
public function getInfo(int $id)
{
$field = 'id,person_id,person_type,schedule_id,course_date,time_slot,created_at,updated_at';
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
return $info;
}
/**
* 添加人员与课程安排关系
* @param array $data
* @return mixed
*/
public function add(array $data)
{
// 获取所有要新增的学生
$students = (new Student())->whereIn('user_id', $data['resources_id'])->column('user_id', 'id');
// 新增的 participant 列表
$newParticipants = $data['resources_id'];
// 获取当前课程安排
$schedule = (new CourseSchedule())->find($data['schedule_id']);
if (!$schedule) {
return $this->error('课程安排不存在');
}
$capacity = (new Venue())->where('id', $schedule->venue_id)->value('capacity');
// 原来的 participant 列表
$oldParticipants = $schedule->participants ?? [];
// 计算差集:哪些是被删除的(需要从预约记录中删除)
$deletedParticipants = array_diff($oldParticipants, $newParticipants);
// 计算可用容量
$addedCount = count($newParticipants) - count($oldParticipants);
$deletedCount = count($deletedParticipants);
$available_capacity = $capacity + $deletedCount - $addedCount;
if ($available_capacity < 0) {
return $this->error('当前课程安排已满');
}
try {
$this->model->startTrans();
// 1. 更新课程安排信息
(new CourseSchedule())->where('id', $data['schedule_id'])->update([
'participants' => $newParticipants,
'student_ids' => array_keys($students),
'available_capacity' => $available_capacity
]);
// 2. 删除旧的预约记录(针对被移除的人)
if (!empty($deletedParticipants)) {
$this->model->whereIn('resources_id', $deletedParticipants)
->where('course_date', $schedule->course_date)
->where('time_slot', $schedule->time_slot)
->delete();
}
// 3. 删除原有的,再批量插入新的预约记录(避免重复)
foreach ($newParticipants as $participant) {
$student_id = array_flip($students)[$participant] ?? 0;
// 先删除已存在的记录(防止重复)
$this->model->where([
['resources_id', '=', $participant],
['course_date', '=', $schedule->course_date],
['time_slot', '=', $schedule->time_slot]
])->delete();
// 插入新的记录
$this->model->create([
'resources_id' => $participant,
'person_id' => $this->uid,
'schedule_id' => $data['schedule_id'],
'student_id' => $student_id,
'person_type' => $student_id ? 'student' : 'customer_resource',
'course_date' => $schedule->course_date,
'time_slot' => $schedule->time_slot
]);
}
$this->model->commit();
} catch (\Exception $e) {
$this->model->rollback();
return $this->error('操作失败: ' . $e->getMessage());
}
return true;
}
/**
* 人员与课程安排关系编辑
* @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;
}
public function getTryCoursePerson($schedule_id)
{
$list = $this->model->where('person_type', 'customer_resource')
->where('schedule_id', $schedule_id)
->select();
$resources = (new CustomerResources())->whereIn('id', $list->column('resources_id'))->select()->toArray();
$data = [];
foreach ($resources as $key => $value) {
// 构建符合需求的对象
$data[] = [
"name" => $value['name'],
"phone_number" => $value['phone_number'],
"id" => $value['id'],
"student_name" => null,
"age" => $value['age'] ?? null,
"student_id" => null,
"order_id" => null,
"checked" => true
];
}
return $data;
}
}