true, 'generated_count' => 0, 'message' => '服务记录生成成功' ]; // 获取所有启用的服务 $services = Db::table('school_service') ->where('status', 1) ->where('deleted_at', 0) ->select() ->toArray(); if (empty($services)) { $result['message'] = '没有找到启用的服务'; return $result; } foreach ($services as $service) { $generatedCount = $this->generateServiceLogsForService($studentCourse, $service); $result['generated_count'] += $generatedCount; } Log::write("为课程ID:{$studentCourse['id']} 学员ID:{$studentCourse['student_id']} 生成了 {$result['generated_count']} 条服务记录"); return $result; } catch (\Exception $e) { Log::write('生成服务记录异常: ' . $e->getMessage()); return [ 'success' => false, 'generated_count' => 0, 'message' => '生成服务记录失败: ' . $e->getMessage() ]; } } /** * 为特定服务生成服务记录 * @param object $studentCourse 学员课程对象 * @param array $service 服务信息 * @return int 生成的记录数量 */ private function generateServiceLogsForService($studentCourse, $service) { $generatedCount = 0; // 根据服务类型确定负责人员 $staffId = null; if ($service['service_type'] == 1) { // 教务服务 $staffId = $studentCourse['education_id']; } elseif ($service['service_type'] == 2) { // 教练服务 $staffId = $studentCourse['main_coach_id']; } // 如果没有对应的人员ID,跳过处理 if (empty($staffId)) { return 0; } // 计算需要生成的服务记录时间点 $serviceDates = $this->calculateServiceDates($studentCourse['start_date'], $studentCourse['end_date'], $service['execution_rules']); foreach ($serviceDates as $serviceDate) { // 检查是否已经存在相同的服务记录 $existingLog = Db::table('school_service_logs') ->where('course_id', $studentCourse['id']) ->where('service_id', $service['id']) ->where('staff_id', $staffId) ->where('student_id', $studentCourse['student_id']) ->where('created_at', $serviceDate) ->find(); if (!$existingLog) { // 插入新的服务记录 Db::table('school_service_logs')->insert([ 'resource_id' => $studentCourse['resource_id'], 'course_id' => $studentCourse['id'], 'service_id' => $service['id'], 'student_id' => $studentCourse['student_id'], 'staff_id' => $staffId, 'status' => 0, // 未服务状态 'created_at' => $serviceDate, 'updated_at' => date('Y-m-d H:i:s') ]); $generatedCount++; } } return $generatedCount; } /** * 根据执行规则计算服务时间点 * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @param string $executionRule 执行规则 * @return array 时间点数组 */ private function calculateServiceDates($startDate, $endDate, $executionRule) { $dates = []; $start = strtotime($startDate); $end = strtotime($endDate); // 根据执行规则确定间隔天数 $intervalDays = $this->getIntervalDaysByRule($executionRule); if ($intervalDays === 0) { return $dates; } $currentTime = $start; while ($currentTime <= $end) { $dates[] = date('Y-m-d H:i:s', $currentTime); $currentTime += ($intervalDays * 24 * 60 * 60); // 增加间隔天数 } return $dates; } /** * 根据执行规则获取间隔天数 * @param string $executionRule 执行规则 * @return int 间隔天数 */ private function getIntervalDaysByRule($executionRule) { switch ($executionRule) { case 'daily_once': return 1; // 每天一次 case 'weekly_once': return 7; // 每周一次 case 'twice_per_week': return 3; // 一周两次,约每3-4天一次,这里简化为3天 case 'biweekly_once': return 14; // 两周一次 case 'monthly_once': return 30; // 每月一次,约30天 default: return 0; // 未知规则 } } /** * 检查并更新人员变更 * @return array */ public function checkAndUpdatePersonnelChanges() { try { $updatedCount = 0; // 获取所有未服务的服务记录 $pendingLogs = Db::table('school_service_logs') ->alias('sl') ->leftJoin('school_student_courses sc', 'sl.course_id = sc.id') ->leftJoin('school_service s', 'sl.service_id = s.id') ->where('sl.status', 0) // 未服务状态 ->where('sc.status', 1) // 课程有效 ->where('s.status', 1) // 服务有效 ->where('s.deleted_at', 0) ->field('sl.*, sc.main_coach_id, sc.education_id, s.service_type') ->select() ->toArray(); foreach ($pendingLogs as $log) { $newStaffId = null; // 根据服务类型确定新的负责人员 if ($log['service_type'] == 1) { // 教务服务 $newStaffId = $log['education_id']; } elseif ($log['service_type'] == 2) { // 教练服务 $newStaffId = $log['main_coach_id']; } // 如果人员发生变更且新人员不为空 if ($newStaffId && $log['staff_id'] != $newStaffId) { Db::table('school_service_logs') ->where('id', $log['id']) ->update([ 'staff_id' => $newStaffId, 'updated_at' => date('Y-m-d H:i:s') ]); $updatedCount++; } } Log::write("检查人员变更完成,更新了 {$updatedCount} 条服务记录"); return [ 'success' => true, 'updated_count' => $updatedCount, 'message' => "人员变更检查完成,更新了 {$updatedCount} 条记录" ]; } catch (\Exception $e) { Log::write('检查人员变更异常: ' . $e->getMessage()); return [ 'success' => false, 'updated_count' => 0, 'message' => '检查人员变更失败: ' . $e->getMessage() ]; } } /** * 根据员工ID获取服务记录 * @param int $staffId 员工ID * @param int $page 页码 * @param int $limit 每页数量 * @param int $status 状态筛选 (null:全部, 0:未服务, 1:已服务) * @return array */ public function getServiceLogsByStaffId($staffId, $page = 1, $limit = 20, $status = null) { try { $query = Db::table('school_service_logs') ->alias('sl') ->leftJoin('school_service s', 'sl.service_id = s.id') ->leftJoin('school_student_courses sc', 'sl.course_id = sc.id') ->leftJoin('school_student st', 'sl.student_id = st.id') ->where('sl.staff_id', $staffId) ->where('s.deleted_at', 0); if ($status !== null) { $query->where('sl.status', $status); } $total = $query->count(); $list = $query->field('sl.*, s.service_name, s.description as service_desc, st.name as student_name, sc.start_date, sc.end_date') ->order('sl.created_at DESC') ->page($page, $limit) ->select() ->toArray(); return [ 'success' => true, 'data' => [ 'list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit ], 'message' => '查询成功' ]; } catch (\Exception $e) { Log::write('查询员工服务记录异常: ' . $e->getMessage()); return [ 'success' => false, 'data' => [], 'message' => '查询失败: ' . $e->getMessage() ]; } } /** * 根据学员ID获取服务记录 * @param int $studentId 学员ID * @param int $page 页码 * @param int $limit 每页数量 * @param int $status 状态筛选 (null:全部, 0:未服务, 1:已服务) * @return array */ public function getServiceLogsByStudentId($studentId, $page = 1, $limit = 20, $status = null) { try { $query = Db::table('school_service_logs') ->alias('sl') ->leftJoin('school_service s', 'sl.service_id = s.id') ->leftJoin('school_student_courses sc', 'sl.course_id = sc.id') ->leftJoin('school_personnel p', 'sl.staff_id = p.id') ->where('sl.student_id', $studentId) ->where('s.deleted_at', 0); if ($status !== null) { $query->where('sl.status', $status); } $total = $query->count(); $list = $query->field('sl.*, s.service_name, s.description as service_desc, p.name as staff_name, sc.start_date, sc.end_date') ->order('sl.created_at DESC') ->page($page, $limit) ->select() ->toArray(); return [ 'success' => true, 'data' => [ 'list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit ], 'message' => '查询成功' ]; } catch (\Exception $e) { Log::write('查询学员服务记录异常: ' . $e->getMessage()); return [ 'success' => false, 'data' => [], 'message' => '查询失败: ' . $e->getMessage() ]; } } }