RouteMailConfig.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class RouteMailConfig extends Model
  7. {
  8. const TABLE = 'QPAccountsDB.dbo.RouteMailConfig';
  9. protected $table = self::TABLE;
  10. public $timestamps = false;
  11. protected $fillable = [
  12. 'MailMark',
  13. 'TitleString',
  14. 'TextString',
  15. 'Status',
  16. 'CreatedAt',
  17. 'UpdatedAt',
  18. ];
  19. public static function sendPendingForUser($userId)
  20. {
  21. $userId = (int) $userId;
  22. if ($userId <= 0) {
  23. return;
  24. }
  25. try {
  26. $configs = DB::connection('write')->table(self::TABLE)
  27. ->where('Status', 1)
  28. ->orderBy('ID')
  29. ->get();
  30. } catch (\Throwable $exception) {
  31. Log::warning('route_mail_config_load_failed', [
  32. 'UserID' => $userId,
  33. 'message' => $exception->getMessage(),
  34. ]);
  35. return;
  36. }
  37. foreach ($configs as $config) {
  38. self::sendOnce($userId, $config);
  39. }
  40. }
  41. private static function sendOnce($userId, $config)
  42. {
  43. $mark = trim((string) ($config->MailMark ?? ''));
  44. if ($mark === '') {
  45. return;
  46. }
  47. if (!RouteMailSendLog::markSent($userId, $mark)) {
  48. return;
  49. }
  50. try {
  51. PrivateMail::sendMail(
  52. 2,
  53. $userId,
  54. (string) $config->TitleString,
  55. (string) $config->TextString,
  56. '',
  57. 'route_mail_' . $mark . '_' . $userId,
  58. 0,
  59. 3
  60. );
  61. } catch (\Throwable $exception) {
  62. DB::connection('write')->table(RouteMailSendLog::TABLE)
  63. ->where('UserID', $userId)
  64. ->where('MailMark', $mark)
  65. ->delete();
  66. Log::warning('route_mail_send_failed', [
  67. 'UserID' => $userId,
  68. 'MailMark' => $mark,
  69. 'message' => $exception->getMessage(),
  70. ]);
  71. }
  72. }
  73. }