From c493718452873f1a29ba450a0d6b75c157234ae1 Mon Sep 17 00:00:00 2001 From: gozareshgir Date: Tue, 3 Feb 2026 14:08:16 +0330 Subject: [PATCH] InstantSms change signalR --- .../Hubs/SendSmsHub.cs | 15 +++++ .../SmsSettingApplication.cs | 65 ++++++++++++++----- ...InstitutionContractSmsServiceRepository.cs | 62 ++++++++++++++---- .../Services/SmsService.cs | 17 ++--- .../Admin/Controllers/SmsReportController.cs | 12 ++-- 5 files changed, 127 insertions(+), 44 deletions(-) diff --git a/CompanyManagment.App.Contracts/Hubs/SendSmsHub.cs b/CompanyManagment.App.Contracts/Hubs/SendSmsHub.cs index 0327bf2d..d2a49ccd 100644 --- a/CompanyManagment.App.Contracts/Hubs/SendSmsHub.cs +++ b/CompanyManagment.App.Contracts/Hubs/SendSmsHub.cs @@ -1,5 +1,6 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; +using _0_Framework.Application; using Microsoft.AspNetCore.SignalR; namespace CompanyManagment.App.Contracts.Hubs; @@ -7,6 +8,20 @@ namespace CompanyManagment.App.Contracts.Hubs; public class SendSmsHub : Hub { + private readonly IAuthHelper _authHelper; + + public SendSmsHub(IAuthHelper authHelper) + { + _authHelper = authHelper; + } + + public override async Task OnConnectedAsync() + { + var accountId = _authHelper.CurrentAccountId(); + var connectionId = Context.ConnectionId; + await send(accountId); + await base.OnConnectedAsync(); + } public async Task send(long id) { diff --git a/CompanyManagment.Application/SmsSettingApplication.cs b/CompanyManagment.Application/SmsSettingApplication.cs index 6b1d5651..8ffb12dc 100644 --- a/CompanyManagment.Application/SmsSettingApplication.cs +++ b/CompanyManagment.Application/SmsSettingApplication.cs @@ -136,11 +136,33 @@ public class SmsSettingApplication : ISmsSettingApplication public async Task InstantSendReminderSms(List command) { var op = new OperationResult(); + List devModeNumberList = []; if (_hostEnvironment.IsDevelopment()) { - - return op.Failed(" در محیط توسعه امکان ارسال وجود ندارد "); - + int taker = 0; + switch (command.Count) + { + case >= 3: + taker = 3; + break; + case > 0: + case < 3: + taker = command.Count; + break; + default: + taker = 0; + } + + if (taker > 0) + { + devModeNumberList = ["09114221321", "09116967898", "09116067106"]; + command = command.Take(taker).ToList(); + for (int i = 0; i < 3; i++) + { + command[i].PhoneNumber = devModeNumberList[i]; + } + } + } string typeOfSms = "یادآور بدهی ماهانه"; string sendMessStart = "شروع یادآور آنی"; @@ -163,10 +185,32 @@ public class SmsSettingApplication : ISmsSettingApplication { var op = new OperationResult(); + List devModeNumberList = []; if (_hostEnvironment.IsDevelopment()) { - - return op.Failed(" در محیط توسعه امکان ارسال وجود ندارد "); + int taker = 0; + switch (command.Count) + { + case >= 3: + taker = 3; + break; + case > 0: + case < 3: + taker = command.Count; + break; + default: + taker = 0; + } + + if (taker > 0) + { + devModeNumberList = ["09114221321", "09116967898", "09116067106"]; + command = command.Take(taker).ToList(); + for (int i = 0; i < 3; i++) + { + command[i].PhoneNumber = devModeNumberList[i]; + } + } } string typeOfSms = "اعلام مسدودی طرف حساب"; @@ -289,16 +333,7 @@ public class SmsSettingApplication : ISmsSettingApplication { var op = new OperationResult(); - if (_hostEnvironment.IsDevelopment()) - { - var str = ""; - foreach (var item in phoneNumbers) - { - str += $" {item}, "; - } - return op.Failed(" در محیط توسعه امکان ارسال وجود ندارد " + " لیست ارسال شما " + str); - - } + if (typeOfSmsSetting == TypeOfSmsSetting.InstitutionContractDebtReminder) { if (phoneNumbers.Any()) diff --git a/CompanyManagment.EFCore/Repository/InstitutionContractSmsServiceRepository.cs b/CompanyManagment.EFCore/Repository/InstitutionContractSmsServiceRepository.cs index fa3e9d70..ddb5bf0e 100644 --- a/CompanyManagment.EFCore/Repository/InstitutionContractSmsServiceRepository.cs +++ b/CompanyManagment.EFCore/Repository/InstitutionContractSmsServiceRepository.cs @@ -31,8 +31,9 @@ public class InstitutionContractSmsServiceRepository : RepositoryBase _hubContext; private readonly IPersonalContractingPartyRepository _personalContractingPartyRepository; + private readonly IAuthHelper _authHelper; - public InstitutionContractSmsServiceRepository(CompanyContext context, ISmsService smsService, ILogger logger, ISmsResultRepository smsResultRepository, IHubContext hubContext, IPersonalContractingPartyRepository personalContractingPartyRepository) : base(context) + public InstitutionContractSmsServiceRepository(CompanyContext context, ISmsService smsService, ILogger logger, ISmsResultRepository smsResultRepository, IHubContext hubContext, IPersonalContractingPartyRepository personalContractingPartyRepository, IAuthHelper authHelper) : base(context) { _context = context; _smsService = smsService; @@ -40,6 +41,7 @@ public class InstitutionContractSmsServiceRepository : RepositoryBase smsListData, string typeOfSms, string sendMessStart, string sendMessEnd) { + var currentAccountId = _authHelper.CurrentAccountId(); + var signalR = _hubContext.Clients.Group(SendSmsHub.GetGroupName(currentAccountId)); if (smsListData.Any()) { - await _smsService.Alarm("09114221321", sendMessStart); - Thread.Sleep(1000); - await _smsService.Alarm("09111485044", sendMessStart); - Thread.Sleep(1000); + try + { + var sendingStart = await _smsService.Alarm("09114221321", sendMessStart); + Thread.Sleep(1000); + + if (!sendingStart) + { + await signalR.SendAsync("sendToApi", "failed"); + return; + } + } + catch (Exception e) + { + await signalR.SendAsync("sendToApi", "failed"); + return; + } int successProcess = 1; int countList = smsListData.Count; @@ -960,6 +976,7 @@ public class InstitutionContractSmsServiceRepository : RepositoryBase 18 ? item.PartyName.Substring(0, 18) : item.PartyName; string errMess = $"{name}-خطا"; + await signalR.SendAsync("sendStatus", false, item.PhoneNumber); await _smsService.Alarm("09114221321", errMess); } Thread.Sleep(600); var percent = (successProcess / (double)countList) * 100; - await _hubContext.Clients.Group(SendSmsHub.GetGroupName(10)) - .SendAsync("showStatus", (int)percent); + await signalR.SendAsync("showStatus", (int)percent); successProcess += 1; } @@ -2099,13 +2117,27 @@ public class InstitutionContractSmsServiceRepository : RepositoryBase 18 ? item.PartyName.Substring(0, 18) : item.PartyName; string errMess = $"{name}-خطا"; + await signalR.SendAsync("sendStatus", false, item.PhoneNumber); _logger.LogError(errMess); await _smsService.Alarm("09114221321", errMess); } var percent = (successProcess / (double)countList) * 100; - await _hubContext.Clients.Group(SendSmsHub.GetGroupName(7)) - .SendAsync("showStatus", (int)percent); + await signalR.SendAsync("showStatus", (int)percent); successProcess += 1; } diff --git a/CompanyManagment.EFCore/Services/SmsService.cs b/CompanyManagment.EFCore/Services/SmsService.cs index d73ac87f..2703e8e7 100644 --- a/CompanyManagment.EFCore/Services/SmsService.cs +++ b/CompanyManagment.EFCore/Services/SmsService.cs @@ -607,17 +607,14 @@ public class SmsService : ISmsService //var bulkSendResult = smsIr.BulkSendAsync(95007079000006, "your text message", new string[] { "9120000000" }); - var verificationSendResult = + var verificationSendResult = await smsIr.VerifySendAsync(number, 662874, new VerifySendParameter[] { new("ALARM", message) }); - Thread.Sleep(1000); - var status = verificationSendResult.Result.Status; - var mess = verificationSendResult.Result.Message; - var messaeId = verificationSendResult.Result.Data.MessageId; - if (verificationSendResult.IsCompletedSuccessfully) return true; - - var resStartStatus = verificationSendResult.Result; - var resResult = verificationSendResult.Status; - var reseExceptiont = verificationSendResult.Exception; + Thread.Sleep(800); + if (verificationSendResult.Message == "موفق") + { + return true; + } + return false; } diff --git a/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs b/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs index fe24a1b9..327e4960 100644 --- a/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs +++ b/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs @@ -260,10 +260,10 @@ public class SmsReportController : AdminBaseController /// /// [HttpPost("InstantReminderSmsSend")] - public async Task> InstantReminderSmsSend([FromBody] List phoneNumbers) + public async Task InstantReminderSmsSend([FromBody] List phoneNumbers) { - var result = await _smsSettingApplication.InstantSmsSendApi(TypeOfSmsSetting.InstitutionContractDebtReminder, phoneNumbers); - return result; + var result = _smsSettingApplication.InstantSmsSendApi(TypeOfSmsSetting.InstitutionContractDebtReminder, phoneNumbers); + return Ok(); } /// @@ -272,10 +272,10 @@ public class SmsReportController : AdminBaseController /// /// [HttpPost("InstantBlockSmsSend")] - public async Task> InstantBlockSmsSend([FromBody] List phoneNumbers) + public async Task InstantBlockSmsSend([FromBody] List phoneNumbers) { - var result = await _smsSettingApplication.InstantSmsSendApi(TypeOfSmsSetting.BlockContractingParty, phoneNumbers); - return result; + var result = _smsSettingApplication.InstantSmsSendApi(TypeOfSmsSetting.BlockContractingParty, phoneNumbers); + return Ok(); } #endregion } \ No newline at end of file