| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class RouteMailConfig extends Model
- {
- const TABLE = 'QPAccountsDB.dbo.RouteMailConfig';
- protected $table = self::TABLE;
- public $timestamps = false;
- protected $fillable = [
- 'MailMark',
- 'TitleString',
- 'TextString',
- 'Status',
- 'CreatedAt',
- 'UpdatedAt',
- ];
- public static function sendPendingForUser($userId)
- {
- $userId = (int) $userId;
- if ($userId <= 0) {
- return;
- }
- try {
- $configs = DB::connection('write')->table(self::TABLE)
- ->where('Status', 1)
- ->orderBy('ID')
- ->get();
- } catch (\Throwable $exception) {
- Log::warning('route_mail_config_load_failed', [
- 'UserID' => $userId,
- 'message' => $exception->getMessage(),
- ]);
- return;
- }
- foreach ($configs as $config) {
- self::sendOnce($userId, $config);
- }
- }
- private static function sendOnce($userId, $config)
- {
- $mark = trim((string) ($config->MailMark ?? ''));
- if ($mark === '') {
- return;
- }
- if (!RouteMailSendLog::markSent($userId, $mark)) {
- return;
- }
- try {
- PrivateMail::sendMail(
- 2,
- $userId,
- (string) $config->TitleString,
- (string) $config->TextString,
- '',
- 'route_mail_' . $mark . '_' . $userId,
- 0,
- 3
- );
- } catch (\Throwable $exception) {
- DB::connection('write')->table(RouteMailSendLog::TABLE)
- ->where('UserID', $userId)
- ->where('MailMark', $mark)
- ->delete();
- Log::warning('route_mail_send_failed', [
- 'UserID' => $userId,
- 'MailMark' => $mark,
- 'message' => $exception->getMessage(),
- ]);
- }
- }
- }
|