header('token'); if (!$token) { throw new CommonException('未登录'); } try { $tokenData = TokenAuth::parseToken($token, 'api'); if (empty($tokenData)) { throw new CommonException('Token无效'); } } catch (\Exception $e) { throw new CommonException('Token解析失败: ' . $e->getMessage()); } // 获取客户资源信息 $customerResources = new CustomerResources(); $userInfo = $customerResources->where('id', $tokenData['user_id'])->find(); if (!$userInfo) { throw new CommonException('用户信息不存在'); } return $userInfo; } /** * 获取家长下的孩子列表 * @return array * @throws \Exception */ public function getChildrenList() { $currentUser = $this->getCurrentUser(); // 查询该家长下的所有孩子 $children = Db::table('school_student') ->alias('s') ->leftJoin('school_campus ca', 's.campus_id = ca.id') ->leftJoin('school_class cl', 's.class_id = cl.id') ->where('s.user_id', $currentUser['id']) ->where('s.deleted_at', 0) ->field('s.*, ca.campus_name, cl.class_name') ->select() ->toArray(); // 计算每个孩子的课程统计信息 foreach ($children as &$child) { // 计算年龄 if ($child['birthday']) { $birthDate = new \DateTime($child['birthday']); $today = new \DateTime(); $age = $today->diff($birthDate)->y; $child['age'] = $age; } // 获取课程统计信息 $courseStats = $this->getChildCourseStats($child['id']); $child = array_merge($child, $courseStats); } return [ 'data' => $children, 'parent_info' => [ 'id' => $currentUser['id'], 'name' => $currentUser['name'], 'phone_number' => $currentUser['phone_number'] ] ]; } /** * 获取孩子的课程统计信息 * @param int $childId * @return array */ private function getChildCourseStats($childId) { // 获取该学员的所有课程 $courses = Db::table('school_student_courses') ->where('student_id', $childId) ->select() ->toArray(); $totalCourses = 0; $remainingCourses = 0; $completedCourses = 0; foreach ($courses as $course) { $totalCourses += $course['course_hours'] ?? 0; $remainingCourses += $course['remaining_hours'] ?? 0; } $completedCourses = $totalCourses - $remainingCourses; // 计算出勤率 $attendanceRate = 0; if ($completedCourses > 0) { $presentCount = Db::table('school_student_course_usage') ->where('student_id', $childId) ->where('status', 'present') ->count(); $attendanceRate = round(($presentCount / $completedCourses) * 100, 1); } return [ 'total_courses' => $totalCourses, 'remaining_courses' => $remainingCourses, 'completed_courses' => $completedCourses, 'attendance_rate' => $attendanceRate ]; } /** * 获取指定孩子的详细信息 * @param int $childId * @return array * @throws \Exception */ public function getChildInfo($childId) { $currentUser = $this->getCurrentUser(); $student = new Student(); $childInfo = $student->alias('s') ->leftJoin('school_campus ca', 's.campus_id = ca.id') ->leftJoin('school_classes cl', 's.class_id = cl.id') ->where('s.id', $childId) ->where('s.user_id', $currentUser['id']) ->where('s.deleted_at', 0) ->field('s.*, ca.name as campus_name, cl.name as class_name') ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // 计算年龄 if ($childInfo['birthday']) { $birthDate = new \DateTime($childInfo['birthday']); $today = new \DateTime(); $age = $today->diff($birthDate)->y; $childInfo['age'] = $age; } // 获取课程统计信息 $courseStats = $this->getChildCourseStats($childId); $childInfo = array_merge($childInfo->toArray(), $courseStats); return $childInfo; } /** * 新增孩子信息 * @param array $data * @return array * @throws \Exception */ public function addChild($data) { $currentUser = $this->getCurrentUser(); // 计算年龄 if ($data['birthday']) { $birthDate = new \DateTime($data['birthday']); $today = new \DateTime(); $age = $today->diff($birthDate)->y; $data['age'] = $age; } $childData = [ 'name' => $data['name'], 'gender' => $data['gender'], 'age' => $data['age'], 'birthday' => $data['birthday'], 'user_id' => $currentUser['id'], 'campus_id' => 0, // 默认未分配校区 'class_id' => 0, // 默认未分配班级 'note' => $data['remark'] ?? '', 'status' => 1, // 默认正常状态 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; $result = Db::table('school_student')->insertGetId($childData); if (!$result) { throw new CommonException('新增孩子信息失败'); } return [ 'id' => $result, 'name' => $data['name'], 'message' => '新增孩子信息成功' ]; } /** * 更新孩子信息 * @param array $data * @return array * @throws \Exception */ public function updateChildInfo($data) { $currentUser = $this->getCurrentUser(); $student = new Student(); $childInfo = $student->where('id', $data['child_id']) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // 计算年龄 if ($data['birthday']) { $birthDate = new \DateTime($data['birthday']); $today = new \DateTime(); $age = $today->diff($birthDate)->y; $data['age'] = $age; } $updateData = [ 'name' => $data['name'], 'gender' => $data['gender'], 'age' => $data['age'], 'birthday' => $data['birthday'], 'note' => $data['remark'] ?? '', 'updated_at' => date('Y-m-d H:i:s') ]; $result = $childInfo->save($updateData); if (!$result) { throw new CommonException('更新孩子信息失败'); } return [ 'message' => '更新孩子信息成功' ]; } /** * 获取指定孩子的课程信息 * @param int $childId * @return array * @throws \Exception */ public function getChildCourses($childId) { $currentUser = $this->getCurrentUser(); // 验证孩子是否属于当前用户 $student = new Student(); $childInfo = $student->where('id', $childId) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // 获取课程信息 $studentCourses = new StudentCourses(); $courses = $studentCourses->alias('sc') ->leftJoin('school_course c', 'sc.course_id = c.id') ->where('sc.student_id', $childId) ->field('sc.*, c.name as course_name, c.description') ->select() ->toArray(); return [ 'data' => $courses, 'child_info' => [ 'id' => $childInfo['id'], 'name' => $childInfo['name'] ] ]; } /** * 获取指定孩子的订单信息 * @param int $childId * @return array * @throws \Exception */ public function getChildOrders($childId) { $currentUser = $this->getCurrentUser(); // 验证孩子是否属于当前用户 $student = new Student(); $childInfo = $student->where('id', $childId) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // 获取订单信息 $orderTable = new OrderTable(); $orders = $orderTable->where('customer_resources_id', $currentUser['id']) ->where('student_id', $childId) ->order('created_at DESC') ->select() ->toArray(); return [ 'data' => $orders, 'child_info' => [ 'id' => $childInfo['id'], 'name' => $childInfo['name'] ] ]; } /** * 获取指定孩子的服务记录 * @param int $childId * @return array * @throws \Exception */ public function getChildServices($childId) { $currentUser = $this->getCurrentUser(); // 验证孩子是否属于当前用户 $student = new Student(); $childInfo = $student->where('id', $childId) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // TODO: 这里需要根据实际的服务记录表结构来实现 // 暂时返回空数据 return [ 'data' => [], 'child_info' => [ 'id' => $childInfo['id'], 'name' => $childInfo['name'] ] ]; } /** * 获取指定孩子的消息记录 * @param int $childId * @return array * @throws \Exception */ public function getChildMessages($childId) { $currentUser = $this->getCurrentUser(); // 验证孩子是否属于当前用户 $student = new Student(); $childInfo = $student->where('id', $childId) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // 获取消息记录 $chatMessages = new ChatMessages(); $messages = $chatMessages->where('to_customer_id', $currentUser['id']) ->order('created_at DESC') ->limit(50) ->select() ->toArray(); return [ 'data' => $messages, 'child_info' => [ 'id' => $childInfo['id'], 'name' => $childInfo['name'] ] ]; } /** * 获取指定孩子的合同信息 * @param int $childId * @return array * @throws \Exception */ public function getChildContracts($childId) { $currentUser = $this->getCurrentUser(); // 验证孩子是否属于当前用户 $student = new Student(); $childInfo = $student->where('id', $childId) ->where('user_id', $currentUser['id']) ->where('deleted_at', 0) ->find(); if (!$childInfo) { throw new CommonException('孩子信息不存在'); } // TODO: 这里需要根据实际的合同表结构来实现 // 暂时返回空数据 return [ 'data' => [], 'child_info' => [ 'id' => $childInfo['id'], 'name' => $childInfo['name'] ] ]; } }