芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.sksb.smartcon-survey.com/app/Models/Invitation.php
value('value'); } self::create([ 'slug' => $slug, 'from' => Carbon::parse($from)->format('Y-m-d'), 'to' => Carbon::parse($to)->format('Y-m-d'), 'wave' => $wave, 'contact_id' => $contact->id, 'topic_id' => $topicId, 'topic_id_2' => $topicId2, 'mailing_id' => $mailingId, ]); } public function Contact(): BelongsTo { return $this->belongsTo(Contact::class); } public function Topic(): BelongsTo { return $this->belongsTo(Topic::class); } public function mailing(): BelongsTo { return $this->belongsTo(Mailing::class); } public static function link(string $slug): string { $baseUrl = config('app.url'); return "$baseUrl/ask?inv=$slug"; } public static function generateInvitations(?int $topicId = null): array { if ($topicId) { return self::generateInvitationsForTopic($topicId); } return self::generateRandomInvitations(); } public static function generateIndividualInvitations(?int $topicId, array $contactIds): array { if ($topicId) { return self::generateInvitationsForTopic($topicId, $contactIds); } return self::generateRandomInvitations($contactIds); } private static function generateInvitationsForTopic(int $topicId, array $contactIds = []): array { if (!empty($contactIds)) { $contacts = Contact::whereIn('id', $contactIds)->get(); } else { $contacts = Contact::get(); } $topic = Topic::select('id')->findOrFail($topicId); $unsavedInvitations = []; foreach ($contacts as $contact) { $unsavedInvitations[] = self::createInvitationEntry($contact, $topic->id); } return $unsavedInvitations; } private static function generateRandomInvitations(array $contactIds = []): array { $topics = Topic::select('id')->where(['is_active' => 1, 'is_extra' => 0])->get(); $currentWave = Setting::where('key', 'wave')->value('value'); $unsavedInvitations = []; if (!empty($contactIds)) { $contacts = Contact::select('id', 'email') ->whereIn('id', $contactIds) ->with(['invitations' => fn($query) => $query->where('wave', $currentWave) ->select('topic_id', 'topic_id_2', 'contact_id')]) ->get(); } else { $contacts = Contact::select('id', 'email') ->with(['invitations' => fn($query) => $query->where('wave', $currentWave) ->select('topic_id', 'topic_id_2', 'contact_id')]) ->get(); } foreach ($contacts as $contact) { $pickedTopicIds = self::pick2TopicsForContact($contact->invitations, $topics); unset($contact->invitations); if (!empty($pickedTopicIds) && count($pickedTopicIds) === 2) { $unsavedInvitations[] = self::createInvitationEntry($contact, $pickedTopicIds[0], $pickedTopicIds[1]); } } return $unsavedInvitations; } private static function pick2TopicsForContact(Collection|EloquentCollection $invitations, Collection|EloquentCollection $topics): array { $usedTopicIds = $invitations->select('topic_id', 'topic_id_2') ->flatten()->unique()->values()->toArray(); return $topics ->shuffle()->filter(fn($topic) => (!in_array($topic->id, $usedTopicIds, true)))->take(2) ->pluck('id') // Get the IDs of the selected topics ->toArray() ?? []; // Return the array of IDs } private static function createInvitationEntry(Contact $contact, int $topicId, int $topicId2 = null): array { return [ 'contact' => $contact, 'topic_id' => $topicId, 'topic_id_2' => $topicId2, 'slug' => Str::uuid()->toString(), ]; } }