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.
243 lines
8.2 KiB
243 lines
8.2 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\admin\person_course_schedule;
|
|
|
|
use app\model\course\Course;
|
|
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\student_courses\StudentCourses;
|
|
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 $data = [])
|
|
{
|
|
$order = 'a.id desc';
|
|
$where = [];
|
|
if($data['name']){
|
|
$where[] = ['b.name','=',$data['name']];
|
|
}
|
|
|
|
if($data['phone_number']){
|
|
$where[] = ['b.phone_number','=',$data['phone_number']];
|
|
}
|
|
|
|
if($data['campus_name']){
|
|
$where[] = ['c.campus_name','=',$data['campus_name']];
|
|
}
|
|
$search_model = $this->model
|
|
->alias("a")
|
|
->join(['school_customer_resources' => 'b'],'a.person_id = b.id','left')
|
|
->join(['school_campus'=>'c'],'b.campus = c.id','left')
|
|
->where($where)
|
|
->field('a.*,b.name')
|
|
->order($order);
|
|
return $this->pageQuery($search_model, function ($item, $key) {
|
|
// $course = new Course();
|
|
$studentCourses = new StudentCourses();
|
|
$course_schedule = new CourseSchedule();
|
|
$course_type = $course_schedule
|
|
->alias("a")
|
|
->join(['school_course' => 'b'],'a.course_id = b.id','left')
|
|
->value("b.course_type");
|
|
$item['course_type'] = $course_type;
|
|
|
|
|
|
$schedule_info = $course_schedule->where(['id' => $item['schedule_id']])->find();
|
|
$studentCourses_info = $studentCourses->where(['student_id' => $item['student_id'],'course_id' => $schedule_info['course_id']])->find();
|
|
|
|
if($studentCourses_info){
|
|
$item['is_xk'] = true;
|
|
}else{
|
|
$item['is_xk'] = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取人员与课程安排关系信息
|
|
* @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,deleted_at';
|
|
|
|
$info = $this->model->field($field)->where([['id', "=", $id]])->with(['customerResources'])->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;
|
|
}
|
|
public function xk(int $id){
|
|
$data = $this->model->where([['id', '=', $id]])->find();
|
|
event('Student', ['event_type' => 'xiaoke','data' => $data]);
|
|
return true;
|
|
}
|
|
|
|
public function getCustomerResourcesAll(){
|
|
$customerResourcesModel = new CustomerResources();
|
|
return $customerResourcesModel->select()->toArray();
|
|
}
|
|
}
|
|
|