distributeToAcademicAffairs(); $this->distributeToCoaches(); Log::write('服务记录分发任务完成 ' . date('Y-m-d H:i:s')); } catch (\Exception $e) { Log::write('服务记录分发任务异常: ' . $e->getMessage()); return false; } return true; } /** * 给教务分发服务记录 */ private function distributeToAcademicAffairs() { // 获取需要分发的服务记录(最近24小时内完成的记录) $yesterday = date('Y-m-d H:i:s', strtotime('-1 day')); $serviceLogs = ServiceLogs::where('status', 1) // 已完成状态 ->where('created_at', '>=', $yesterday) ->where('is_distributed_to_academic', 0) // 未分发给教务 ->with(['service', 'staff']) ->select(); if ($serviceLogs->isEmpty()) { Log::write('没有需要分发给教务的服务记录'); return; } // 获取教务人员列表 $academicStaff = Personnel::where('status', Personnel::STATUS_NORMAL) ->where('position', 'like', '%教务%') ->select(); if ($academicStaff->isEmpty()) { Log::write('未找到教务人员'); return; } $distributionCount = 0; foreach ($serviceLogs as $serviceLog) { foreach ($academicStaff as $staff) { // 发送通知给教务 $this->sendServiceLogNotice($staff, $serviceLog, 'academic'); $distributionCount++; } // 标记为已分发给教务 $serviceLog->is_distributed_to_academic = 1; $serviceLog->distribution_time = time(); $serviceLog->save(); Log::write("已分发给教务,服务记录ID: {$serviceLog->id}"); } Log::write("成功分发给教务 {$distributionCount} 条服务记录通知"); } /** * 给教练分发服务记录 */ private function distributeToCoaches() { // 获取需要分发的服务记录(最近24小时内完成的记录) $yesterday = date('Y-m-d H:i:s', strtotime('-1 day')); $serviceLogs = ServiceLogs::where('status', 1) // 已完成状态 ->where('created_at', '>=', $yesterday) ->where('is_distributed_to_coach', 0) // 未分发给教练 ->with(['service', 'staff']) ->select(); if ($serviceLogs->isEmpty()) { Log::write('没有需要分发给教练的服务记录'); return; } // 获取教练人员列表 $coaches = Personnel::where('status', Personnel::STATUS_NORMAL) ->where('position', 'like', '%教练%') ->select(); if ($coaches->isEmpty()) { Log::write('未找到教练人员'); return; } $distributionCount = 0; foreach ($serviceLogs as $serviceLog) { // 找到对应的教练(根据服务记录中的staff_id) $coach = $coaches->where('id', $serviceLog->staff_id)->first(); if ($coach) { // 发送通知给教练 $this->sendServiceLogNotice($coach, $serviceLog, 'coach'); $distributionCount++; } // 标记为已分发给教练 $serviceLog->is_distributed_to_coach = 1; $serviceLog->distribution_time = time(); $serviceLog->save(); Log::write("已分发给教练,服务记录ID: {$serviceLog->id}"); } Log::write("成功分发给教练 {$distributionCount} 条服务记录通知"); } /** * 发送服务记录通知 * @param Personnel $staff 接收人员 * @param ServiceLogs $serviceLog 服务记录 * @param string $type 通知类型 (academic|coach) */ private function sendServiceLogNotice($staff, $serviceLog, $type) { try { // 准备通知数据 $noticeData = [ 'staff_name' => $staff->name, 'service_name' => $serviceLog->service->service_name ?? '未知服务', 'service_remark' => $serviceLog->service_remark ?? '', 'score' => $serviceLog->score ?? 0, 'feedback' => $serviceLog->feedback ?? '', 'created_at' => date('Y-m-d H:i:s', $serviceLog->created_at), 'type' => $type ]; // 根据类型选择不同的通知模板 $noticeKey = $type === 'academic' ? 'service_log_academic_notice' : 'service_log_coach_notice'; // 发送通知 NoticeService::send($noticeKey, $noticeData); Log::write("成功发送服务记录通知给 {$staff->name},服务记录ID: {$serviceLog->id}"); } catch (\Exception $e) { Log::write("发送服务记录通知失败: {$e->getMessage()}"); } } /** * 获取分发统计信息 * @return array */ public function getDistributionStats() { $yesterday = date('Y-m-d H:i:s', strtotime('-1 day')); $stats = [ 'total_completed' => ServiceLogs::where('status', 1) ->where('created_at', '>=', $yesterday) ->count(), 'distributed_to_academic' => ServiceLogs::where('status', 1) ->where('created_at', '>=', $yesterday) ->where('is_distributed_to_academic', 1) ->count(), 'distributed_to_coach' => ServiceLogs::where('status', 1) ->where('created_at', '>=', $yesterday) ->where('is_distributed_to_coach', 1) ->count(), 'academic_staff_count' => Personnel::where('status', Personnel::STATUS_NORMAL) ->where('position', 'like', '%教务%') ->count(), 'coach_count' => Personnel::where('status', Personnel::STATUS_NORMAL) ->where('position', 'like', '%教练%') ->count(), ]; return $stats; } }