using _0_Framework.Application; using _0_Framework.Application.Enums; using _0_Framework.Application.Sms; using Company.Domain.ContarctingPartyAgg; using Company.Domain.InstitutionContractAgg; using Hangfire; namespace BackgroundInstitutionContract.Task.Jobs; public class JobSchedulerRegistrator { private readonly IBackgroundJobClient _backgroundJobClient; private readonly SmsReminder _smsReminder; private readonly IInstitutionContractRepository _institutionContractRepository; private readonly IInstitutionContractSmsServiceRepository _institutionContractSmsServiceRepository; private static DateTime? _lastRunCreateTransaction; private static DateTime? _lastRunSendMonthlySms; private readonly ISmsService _smsService; private readonly ILogger _logger; public JobSchedulerRegistrator(SmsReminder smsReminder, IBackgroundJobClient backgroundJobClient, IInstitutionContractRepository institutionContractRepository, ISmsService smsService, ILogger logger, IInstitutionContractSmsServiceRepository institutionContractSmsServiceRepository) { _smsReminder = smsReminder; _backgroundJobClient = backgroundJobClient; _institutionContractRepository = institutionContractRepository; _smsService = smsService; _logger = logger; _institutionContractSmsServiceRepository = institutionContractSmsServiceRepository; } public void Register() { _logger.LogInformation("hangfire Started"); RecurringJob.AddOrUpdate( "InstitutionContract.CreateFinancialTransaction", () => CreateFinancialTransaction(), "*/30 * * * *" // هر 30 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendMonthlySms", () => SendFirstDayOfMonthSms(), "*/20 * * * *" // هر 30 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendReminderSms", () => SendReminderSms(), "*/1 * * * *" // هر 1 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendBlockSms", () => SendBlockSms(), "*/1 * * * *" // هر 1 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendInstitutionContractConfirmSms", () => SendInstitutionContractConfirmSms(), "*/1 * * * *" // هر 1 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendWarningSms", () => SendWarningSms(), "*/1 * * * *" // هر 1 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.SendLegalActionSms", () => SendLegalActionSms(), "*/1 * * * *" // هر 1 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.Block", () => Block(), "*/30 * * * *" // هر 30 دقیقه یکبار چک کن ); RecurringJob.AddOrUpdate( "InstitutionContract.UnBlock", () => UnBlock(), "*/10 * * * *" ); RecurringJob.AddOrUpdate( "InstitutionContract.DeActiveInstitutionEndOfContract", () => DeActiveInstitutionEndOfContract(), "*/30 * * * *" ); RecurringJob.AddOrUpdate( "InstitutionContract.BlueDeActiveAfterZeroDebt", () => BlueDeActiveAfterZeroDebt(), "*/10 * * * *" ); } /// /// ایجاد سند بدهی ماهیانه برای قراداد مالی /// /// [DisableConcurrentExecution(timeoutInSeconds: 1200)] public async System.Threading.Tasks.Task CreateFinancialTransaction() { var now = DateTime.Now; var endOfMonth = now.ToFarsi().FindeEndOfMonth(); var endOfMonthGr = endOfMonth.ToGeorgianDateTime(); _logger.LogInformation("CreateFinancialTransaction job run"); if (now.Date == endOfMonthGr.Date && now.Hour >= 2 && now.Hour < 4 && now.Date != _lastRunCreateTransaction?.Date) { var month = endOfMonth.Substring(5, 2); var year = endOfMonth.Substring(0, 4); var monthName = month.ToFarsiMonthByNumber(); var description = $"{monthName} {year}"; var endnew = ($"{endOfMonth.Substring(0, 8)}01").FindeEndOfMonth(); var endNewGr = endnew.ToGeorgianDateTime(); var endNewFa = endNewGr.ToFarsi(); try { await _institutionContractRepository.CreateTransactionForInstitutionContracts(endNewGr, endNewFa, description); _lastRunCreateTransaction = now; Console.WriteLine("CreateTransAction executed"); } catch (Exception e) { await _smsService.Alarm("09114221321", "خطا-ایجاد سند مالی"); } } } /// /// ارسال پیامک صورت حساب ماهانه /// /// [DisableConcurrentExecution(timeoutInSeconds: 600)] public async System.Threading.Tasks.Task SendFirstDayOfMonthSms() { //var now = new DateTime(2025,11,21, 10,30,0); var now = DateTime.Now; var endOfMonth = now.ToFarsi().FindeEndOfMonth(); var endOfMonthGr = endOfMonth.ToGeorgianDateTime(); _logger.LogInformation("SendFirstDayOfMonthSms job run"); if (now.Date == endOfMonthGr.Date && now.Hour >= 10 && now.Hour < 11 && now.Date != _lastRunSendMonthlySms?.Date) { try { await _institutionContractSmsServiceRepository.SendMonthlySms(now); _lastRunSendMonthlySms = now; Console.WriteLine("Send Monthly sms executed"); } catch (Exception e) { //_smsService.Alarm("09114221321", "خطا-ایجاد سند مالی"); } } } /// /// ارسال پیامک یاد آور بدهی /// /// [DisableConcurrentExecution(timeoutInSeconds: 1200)] public async System.Threading.Tasks.Task SendReminderSms() { _logger.LogInformation("SendReminderSms job run"); await _institutionContractSmsServiceRepository.SendReminderSmsForBackgroundTask(); } /// /// ارسال پیامک مسدودی /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task SendBlockSms() { _logger.LogInformation("SendBlockSms job run"); await _institutionContractSmsServiceRepository.SendBlockSmsForBackgroundTask(); } /// /// ارسال پیامک یادآور تایید قراداد مالی /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task SendInstitutionContractConfirmSms() { _logger.LogInformation("SendInstitutionContractConfirmSms job run"); await _institutionContractSmsServiceRepository.SendInstitutionContractConfirmSmsTask(); } /// /// ارسال پیامک هشدار /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task SendWarningSms() { _logger.LogInformation("SendWarningSms job run"); await _institutionContractSmsServiceRepository.SendWarningOrLegalActionSmsTask(TypeOfSmsSetting.Warning); } /// /// پیامک اقدام قضایی /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task SendLegalActionSms() { _logger.LogInformation("SendWarningSms job run"); await _institutionContractSmsServiceRepository.SendWarningOrLegalActionSmsTask(TypeOfSmsSetting.LegalAction); } /// /// بلاگ سازی /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task Block() { _logger.LogInformation("block job run"); var now = DateTime.Now; var executeDate = now.ToFarsi().Substring(8, 2); if (executeDate == "20") { if (now.Hour >= 9 && now.Hour < 10) { await _institutionContractSmsServiceRepository.Block(now); } } } /// /// آنبلاک /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task UnBlock() { _logger.LogInformation("UnBlock job run"); await _institutionContractSmsServiceRepository.UnBlock(); } /// /// غیر فعال سازی قراداد های پایان یافته /// /// [DisableConcurrentExecution(timeoutInSeconds: 100)] public async System.Threading.Tasks.Task DeActiveInstitutionEndOfContract() { _logger.LogInformation("DeActiveInstitutionEndOfContract job run"); var now = DateTime.Now; var executeDate = now.ToFarsi().Substring(8, 2); if (executeDate == "01") { if (now.Hour >= 9 && now.Hour < 10) { await _institutionContractSmsServiceRepository.DeActiveInstitutionEndOfContract(now); } } } /// /// غیرفعال سازس قرارداد های آبی که بدهی ندارند /// /// [DisableConcurrentExecution(timeoutInSeconds: 800)] public async System.Threading.Tasks.Task BlueDeActiveAfterZeroDebt() { _logger.LogInformation("BlueDeActiveAfterZeroDebt job run"); await _institutionContractSmsServiceRepository.BlueDeActiveAfterZeroDebt(); } }