validator; } /** * 创建一条消息 */ public function createMessage(array $data): int { $msgId = Db::table('message_notifications')->insertGetId([ 'msg_no' => generateMsgId(), 'sender_id' => $data['sender_id'], 'sender_type' => $data['sender_type'], 'title' => $data['title'], 'content' => $data['content'], 'scene' => $data['scene'], 'recipient' => $data['recipient'], 'created_at' => time(), ]); return $msgId; } /** * 批量插入用户消息状态 */ public function addUserStatus(int $msgId, array $uids) { $rows = []; foreach ($uids as $uid) { $rows[] = [ 'msg_id' => $msgId, 'uid' => $uid, 'status' => 0, 'read_at' => null ]; } if ($rows) { Db::table('message_user_status')->insert($rows); } } /** * 标记消息已读 */ public function markAsRead(array|string $msgIds, int $uid) { $query = Db::table('message_user_status')->where('uid', $uid); if (is_array($msgIds)) { $query->whereIn('msg_id', $msgIds); } return $query->update([ 'status' => 1, 'read_at' => time() ]); } /** * 获取所有用户ID */ public function getAllUserIds(): array { return User::where('status', 1)->pluck('id')->toArray(); } }