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

428 lines
15 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\api\apiService;
use app\model\customer_resources\CustomerResources;
use app\model\student_courses\StudentCourses;
use app\model\person_course_schedule\PersonCourseSchedule;
use app\model\student_course_usage\StudentCourseUsage;
use app\model\course\Course;
use app\model\campus\Campus;
use app\model\venue\Venue;
use app\model\personnel\Personnel;
use core\base\BaseApiService;
use think\facade\Log;
/**
* 学员课程服务类
* Class StudentCourseService
* @package app\service\api\apiService
*/
class StudentCourseService extends BaseApiService
{
/**
* 获取课程详情
* @param array $where
* @return array
*/
public function getCourseDetail(array $where)
{
try {
Log::debug('StudentCourseService::getCourseDetail - 查询条件: ' . json_encode($where));
$course_id = $where['course_id'] ?? '';
$resource_id = $where['resource_id'] ?? '';
if (empty($course_id) || empty($resource_id)) {
return ['code' => 0, 'msg' => '参数不完整'];
}
// 1. 获取学员课程基本信息
$courseInfo = $this->getCourseInfo($course_id, $resource_id);
if (!$courseInfo) {
return ['code' => 0, 'msg' => '课程信息不存在'];
}
// 2. 获取课程安排列表
$scheduleList = $this->getScheduleList($course_id, $resource_id);
// 3. 获取课程使用记录
$usageList = $this->getUsageList($course_id, $resource_id);
$result = [
'course_info' => $courseInfo,
'schedule_list' => $scheduleList,
'usage_list' => $usageList
];
Log::debug('StudentCourseService::getCourseDetail - 返回数据: ' . json_encode($result));
return ['code' => 1, 'data' => $result];
} catch (\Exception $e) {
Log::error('StudentCourseService::getCourseDetail - 异常: ' . $e->getMessage());
return ['code' => 0, 'msg' => '获取课程详情失败: ' . $e->getMessage()];
}
}
/**
* 获取课程基本信息
* @param int $course_id
* @param int $resource_id
* @return array|null
*/
private function getCourseInfo($course_id, $resource_id)
{
$studentCourse = new StudentCourses();
$info = $studentCourse
->alias('sc')
->join(['school_course' => 'c'], 'sc.course_id = c.id', 'left')
->join(['school_campus' => 'campus'], 'sc.campus_id = campus.id', 'left')
->join(['school_personnel' => 'coach'], 'sc.main_coach_id = coach.id', 'left')
->join(['school_personnel' => 'education'], 'sc.education_id = education.id', 'left')
->where([
'sc.course_id' => $course_id,
'sc.resource_id' => $resource_id
])
->field([
'sc.id',
'sc.course_id',
'sc.resource_id',
'sc.total_hours',
'sc.gift_hours',
'sc.start_date',
'sc.end_date',
'sc.use_total_hours',
'sc.use_gift_hours',
'sc.status',
'sc.single_session_count',
'c.course_name',
'campus.campus_name',
'coach.name as main_coach_name',
'education.name as education_name'
])
->find();
return $info ? $info->toArray() : null;
}
/**
* 获取课程安排列表
* @param int $course_id
* @param int $resource_id
* @return array
*/
private function getScheduleList($course_id, $resource_id)
{
// 首先获取学员课程ID
$studentCourseId = $this->getStudentCourseId($course_id, $resource_id);
if (!$studentCourseId) {
return [];
}
$personCourseSchedule = new PersonCourseSchedule();
$list = $personCourseSchedule
->alias('pcs')
->join(['school_course_schedule' => 'cs'], 'pcs.schedule_id = cs.id', 'left')
->join(['school_venue' => 'v'], 'cs.venue_id = v.id', 'left')
->join(['school_personnel' => 'coach'], 'cs.coach_id = coach.id', 'left')
->where([
'pcs.student_course_id' => $studentCourseId,
'pcs.resources_id' => $resource_id
])
->field([
'pcs.id',
'pcs.schedule_id',
'pcs.course_date',
'pcs.time_slot',
'pcs.schedule_type',
'pcs.course_type',
'pcs.status',
'pcs.remark',
'v.venue_name',
'coach.name as coach_name'
])
->order('pcs.course_date desc, pcs.time_slot desc')
->select();
return $list ? $list->toArray() : [];
}
/**
* 获取课程使用记录
* @param int $course_id
* @param int $resource_id
* @return array
*/
private function getUsageList($course_id, $resource_id)
{
// 首先获取学员课程ID
$studentCourseId = $this->getStudentCourseId($course_id, $resource_id);
if (!$studentCourseId) {
return [];
}
$studentCourseUsage = new StudentCourseUsage();
$list = $studentCourseUsage
->alias('scu')
->join(['school_course_schedule' => 'cs'], 'scu.schedule_id = cs.id', 'left')
->join(['school_venue' => 'v'], 'cs.venue_id = v.id', 'left')
->where([
'scu.student_course_id' => $studentCourseId,
'scu.resource_id' => $resource_id
])
->field([
'scu.id',
'scu.usage_date',
'scu.hours_used',
'scu.usage_type',
'scu.remark',
'cs.time_slot',
'v.venue_name'
])
->order('scu.usage_date desc')
->select();
return $list ? $list->toArray() : [];
}
/**
* 获取学员课程ID
* @param int $course_id
* @param int $resource_id
* @return int|null
*/
private function getStudentCourseId($course_id, $resource_id)
{
$studentCourse = new StudentCourses();
$info = $studentCourse
->where([
'course_id' => $course_id,
'resource_id' => $resource_id
])
->find();
return $info ? $info->id : null;
}
/**
* 获取教练列表
* @param int $campus_id
* @return array
*/
public function getCoachList($campus_id = 0)
{
try {
// 查询dept_id=24的教练角色
$roleIds = \app\model\sys_role\SysRole::where('dept_id', 24)
->where('status', 1)
->column('role_id');
if (empty($roleIds)) {
return ['code' => 0, 'msg' => '没有找到教练角色'];
}
// 查询校区人员角色关系表
$query = \app\model\campus_person_role\CampusPersonRole::alias('cpr')
->join(['school_personnel' => 'p'], 'cpr.person_id = p.id', 'inner')
->whereIn('cpr.role_id', $roleIds)
->where('cpr.deleted_at', 0)
->where('p.status', 1); // 只查询状态正常的人员
// 如果指定了校区,添加校区筛选
if ($campus_id > 0) {
$query->where('cpr.campus_id', $campus_id);
}
$list = $query->field([
'p.id',
'p.name',
'p.phone',
'p.status',
'cpr.campus_id',
'cpr.role_id'
])->select();
return ['code' => 1, 'data' => $list ? $list->toArray() : []];
} catch (\Exception $e) {
Log::error('StudentCourseService::getCoachList - 异常: ' . $e->getMessage());
return ['code' => 0, 'msg' => '获取教练列表失败: ' . $e->getMessage()];
}
}
/**
* 获取教务人员列表
* @param int $campus_id
* @return array
*/
public function getEducationList($campus_id = 0)
{
try {
// 查询dept_id=2的教务角色
$roleIds = \app\model\sys_role\SysRole::where('dept_id', 2)
->where('status', 1)
->column('role_id');
if (empty($roleIds)) {
return ['code' => 0, 'msg' => '没有找到教务角色'];
}
// 查询校区人员角色关系表
$query = \app\model\campus_person_role\CampusPersonRole::alias('cpr')
->join(['school_personnel' => 'p'], 'cpr.person_id = p.id', 'inner')
->whereIn('cpr.role_id', $roleIds)
->where('cpr.deleted_at', 0)
->where('p.status', 1); // 只查询状态正常的人员
// 如果指定了校区,添加校区筛选
if ($campus_id > 0) {
$query->where('cpr.campus_id', $campus_id);
}
$list = $query->field([
'p.id',
'p.name',
'p.phone',
'p.status',
'cpr.campus_id',
'cpr.role_id'
])->select();
return ['code' => 1, 'data' => $list ? $list->toArray() : []];
} catch (\Exception $e) {
Log::error('StudentCourseService::getEducationList - 异常: ' . $e->getMessage());
return ['code' => 0, 'msg' => '获取教务人员列表失败: ' . $e->getMessage()];
}
}
/**
* 更新学员课程信息
* @param array $data
* @return array
*/
public function updateCourseInfo($data)
{
try {
$studentCourseId = $data['student_course_id'];
$mainCoachId = $data['main_coach_id'] ?? 0;
$assistantIds = $data['assistant_ids'] ?? '';
$educationId = $data['education_id'] ?? 0;
$classId = $data['class_id'] ?? 0;
// 1. 更新学员课程表
$updateData = [];
if ($mainCoachId > 0) {
$updateData['main_coach_id'] = $mainCoachId;
}
if (!empty($assistantIds)) {
$updateData['assistant_ids'] = $assistantIds;
}
if ($educationId > 0) {
$updateData['education_id'] = $educationId;
}
if (!empty($updateData)) {
$updateData['updated_at'] = date('Y-m-d H:i:s');
$result = StudentCourses::where('id', $studentCourseId)->update($updateData);
if (!$result) {
return ['code' => 0, 'msg' => '更新学员课程信息失败'];
}
}
// 2. 如果需要更新班级关联
if ($classId > 0) {
// 先获取学员的resource_id
$studentCourse = StudentCourses::where('id', $studentCourseId)->find();
if (!$studentCourse) {
return ['code' => 0, 'msg' => '学员课程不存在'];
}
$resourceId = $studentCourse->resource_id;
// 检查是否已存在班级关联
$existingRel = \app\model\class_resources_rel\ClassResourcesRel::where([
'resource_id' => $resourceId,
'status' => 1
])->find();
if ($existingRel) {
// 更新现有关联
$existingRel->class_id = $classId;
$existingRel->update_time = date('Y-m-d H:i:s');
$existingRel->save();
} else {
// 创建新的班级关联
$classRel = new \app\model\class_resources_rel\ClassResourcesRel();
$classRel->class_id = $classId;
$classRel->resource_id = $resourceId;
$classRel->campus_id = $studentCourse->campus_id ?? 1; // 默认校区ID
$classRel->source_type = 'student';
$classRel->join_time = time();
$classRel->status = 1;
$classRel->save();
}
}
return ['code' => 1, 'data' => ['id' => $studentCourseId], 'msg' => '更新成功'];
} catch (\Exception $e) {
Log::error('StudentCourseService::updateCourseInfo - 异常: ' . $e->getMessage());
return ['code' => 0, 'msg' => '更新失败: ' . $e->getMessage()];
}
}
/**
* 检查学员班级关联情况
* @param int $resource_id
* @return array
*/
public function checkClassRelation($resource_id)
{
try {
$classRel = \app\model\class_resources_rel\ClassResourcesRel::alias('crr')
->join(['school_class' => 'c'], 'crr.class_id = c.id', 'left')
->where([
'crr.resource_id' => $resource_id,
'crr.status' => 1
])
->field([
'crr.id',
'crr.class_id',
'c.class_name',
'c.head_coach',
'c.educational_id'
])
->find();
$hasClass = !empty($classRel);
$classInfo = $hasClass ? $classRel->toArray() : null;
return [
'code' => 1,
'data' => [
'has_class' => $hasClass,
'class_info' => $classInfo
]
];
} catch (\Exception $e) {
Log::error('StudentCourseService::checkClassRelation - 异常: ' . $e->getMessage());
return ['code' => 0, 'msg' => '检查班级关联失败: ' . $e->getMessage()];
}
}
}