diff --git a/0_Framework/Application/Enums/TypeOfSmsSetting.cs b/0_Framework/Application/Enums/TypeOfSmsSetting.cs index 6d66fc47..28089ced 100644 --- a/0_Framework/Application/Enums/TypeOfSmsSetting.cs +++ b/0_Framework/Application/Enums/TypeOfSmsSetting.cs @@ -2,6 +2,8 @@ public enum TypeOfSmsSetting { + //همه انواع پیامک + All = 0, /// /// پیامک @@ -23,7 +25,7 @@ public enum TypeOfSmsSetting /// /// پیامک - /// هشدار اول + /// هشدار بدهی /// Warning, @@ -38,4 +40,14 @@ public enum TypeOfSmsSetting /// InstitutionContractConfirm, + /// + /// ارسال کد تاییدیه قرارداد مالی + /// + SendInstitutionContractConfirmationCode, + + /// + /// یادآور وظایف + /// + TaskReminder, + } \ No newline at end of file diff --git a/Company.Domain/SmsResultAgg/ISmsResultRepository.cs b/Company.Domain/SmsResultAgg/ISmsResultRepository.cs index 1dac4376..6a4ab25c 100644 --- a/Company.Domain/SmsResultAgg/ISmsResultRepository.cs +++ b/Company.Domain/SmsResultAgg/ISmsResultRepository.cs @@ -15,7 +15,7 @@ public interface ISmsResultRepository : IRepository /// /// /// - Task> GetSmsReportList(SmsReportSearchModel command); + Task> GetSmsReportList(SmsReportSearchModel searchModel); #endregion List Search(SmsResultSearchModel searchModel); diff --git a/CompanyManagment.App.Contracts/SmsResult/Dto/SendStatus.cs b/CompanyManagment.App.Contracts/SmsResult/Dto/SendStatus.cs new file mode 100644 index 00000000..a2bee8d0 --- /dev/null +++ b/CompanyManagment.App.Contracts/SmsResult/Dto/SendStatus.cs @@ -0,0 +1,15 @@ +namespace CompanyManagment.App.Contracts.SmsResult.Dto; + +/// +/// وضعیت ارسال پیامک +/// +public enum SendStatus +{ + All=0, + /// + /// موفق + /// + Success, + //ناموفق + Failed, +} \ No newline at end of file diff --git a/CompanyManagment.App.Contracts/SmsResult/Dto/SmsReportSearchModel.cs b/CompanyManagment.App.Contracts/SmsResult/Dto/SmsReportSearchModel.cs index a386d47e..cec3162a 100644 --- a/CompanyManagment.App.Contracts/SmsResult/Dto/SmsReportSearchModel.cs +++ b/CompanyManagment.App.Contracts/SmsResult/Dto/SmsReportSearchModel.cs @@ -7,6 +7,11 @@ public class SmsReportSearchModel //نوع پیامک public TypeOfSmsSetting TypeOfSms { get; set; } + /// + /// وضعیت ارسال پیامک + /// + public SendStatus SendStatus { get; set; } + /// /// شماره موبایل /// diff --git a/CompanyManagment.App.Contracts/SmsResult/ISmsResultApplication.cs b/CompanyManagment.App.Contracts/SmsResult/ISmsResultApplication.cs index 786716c3..b3aee079 100644 --- a/CompanyManagment.App.Contracts/SmsResult/ISmsResultApplication.cs +++ b/CompanyManagment.App.Contracts/SmsResult/ISmsResultApplication.cs @@ -15,9 +15,9 @@ public interface ISmsResultApplication /// /// دریافت لیست پیامکها /// - /// + /// /// - Task> GetSmsReportList(SmsReportSearchModel command); + Task> GetSmsReportList(SmsReportSearchModel searchModel); #endregion diff --git a/CompanyManagment.Application/SmsResultApplication.cs b/CompanyManagment.Application/SmsResultApplication.cs index abf9b4f4..85ce62a6 100644 --- a/CompanyManagment.Application/SmsResultApplication.cs +++ b/CompanyManagment.Application/SmsResultApplication.cs @@ -20,9 +20,9 @@ public class SmsResultApplication : ISmsResultApplication #region ForApi - public async Task> GetSmsReportList(SmsReportSearchModel command) + public async Task> GetSmsReportList(SmsReportSearchModel searchModel) { - return await _smsResultRepository.GetSmsReportList(command); + return await _smsResultRepository.GetSmsReportList(searchModel); } #endregion diff --git a/CompanyManagment.EFCore/Repository/SmsResultRepository.cs b/CompanyManagment.EFCore/Repository/SmsResultRepository.cs index 496d6085..943b32d1 100644 --- a/CompanyManagment.EFCore/Repository/SmsResultRepository.cs +++ b/CompanyManagment.EFCore/Repository/SmsResultRepository.cs @@ -1,18 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using _0_Framework.Application; +using _0_Framework.Application; using _0_Framework.InfraStructure; using Company.Domain.SmsResultAgg; using CompanyManagment.App.Contracts.SmsResult; using CompanyManagment.App.Contracts.SmsResult.Dto; using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using _0_Framework.Application.Enums; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace CompanyManagment.EFCore.Repository; -public class SmsResultRepository : RepositoryBase , ISmsResultRepository +public class SmsResultRepository : RepositoryBase, ISmsResultRepository { private readonly CompanyContext _context; public SmsResultRepository(CompanyContext context) : base(context) @@ -22,28 +24,148 @@ public class SmsResultRepository : RepositoryBase , ISmsResultR #region ForApi - public async Task> GetSmsReportList(SmsReportSearchModel command) + + + public async Task> GetSmsReportList(SmsReportSearchModel searchModel) { - var watch = new Stopwatch(); - watch.Start(); - var res = _context.SmsResults.GroupBy(x => x.CreationDate); - Console.WriteLine("query : " + watch.Elapsed); - watch.Stop(); - watch.Reset(); - watch.Start(); - var b = await res.Take(9000).Select(x => new SmsReportDto() + + // مرحله 1: همه رکوردها را با projection ساده بگیرید + var rawQuery = await _context.SmsResults + .Select(x => new + { + x.id, + x.ContractingPatyId, + x.Mobile, + x.Status, + x.TypeOfSms, + x.CreationDate, + DateOnly = x.CreationDate.Date // فقط تاریخ بدون ساعت + }) + .AsNoTracking() + .ToListAsync(); // اینجا SQL اجرا می‌شود و همه داده‌ها به client می‌آیند + + if (searchModel.ContractingPatyId > 0) { - SentDate = x.Key.ToFarsi() - }).ToListAsync(); - Console.WriteLine("ToList : " + watch.Elapsed); - return b; + rawQuery = rawQuery.Where(x => x.ContractingPatyId == searchModel.ContractingPatyId).ToList(); + } + + if (!string.IsNullOrWhiteSpace(searchModel.Mobile)) + { + rawQuery = rawQuery.Where(x => x.Mobile.Contains(searchModel.Mobile)).ToList(); + } + + if (searchModel.TypeOfSms != TypeOfSmsSetting.All && searchModel.TypeOfSms != TypeOfSmsSetting.Warning) + { + var typeOfSms = "All"; + switch (searchModel.TypeOfSms) + { + case TypeOfSmsSetting.InstitutionContractDebtReminder: + typeOfSms = "یادآور بدهی ماهانه"; + break; + case TypeOfSmsSetting.MonthlyInstitutionContract: + typeOfSms = "صورت حساب ماهانه"; + break; + case TypeOfSmsSetting.BlockContractingParty: + typeOfSms = "اعلام مسدودی طرف حساب"; + break; + case TypeOfSmsSetting.LegalAction: + typeOfSms = "اقدام قضایی"; + break; + case TypeOfSmsSetting.InstitutionContractConfirm: + typeOfSms = "یادآور تایید قرارداد مالی"; + break; + case TypeOfSmsSetting.SendInstitutionContractConfirmationCode: + typeOfSms = "کد تاییدیه قرارداد مالی"; + break; + case TypeOfSmsSetting.TaskReminder: + typeOfSms = "یادآور وظایف"; + break; + } + + rawQuery = rawQuery.Where(x => x.TypeOfSms == typeOfSms).ToList(); + } + + if (searchModel.TypeOfSms == TypeOfSmsSetting.Warning) + { + rawQuery = rawQuery.Where(x => x.TypeOfSms.Contains("هشدار")).ToList(); + } + + if (searchModel.SendStatus != SendStatus.All) + { + var status = "All"; + + switch (searchModel.SendStatus) + { + case SendStatus.Success: status = "موفق"; + break; + case SendStatus.Failed: status = "ناموفق"; + break; + + + } + + rawQuery = rawQuery.Where(x => x.Status == status).ToList(); + + } + #region searchByDate + + if (!string.IsNullOrWhiteSpace(searchModel.StartDateFa) && + !string.IsNullOrWhiteSpace(searchModel.EndDateFa)) + { + if (searchModel.StartDateFa.TryToGeorgianDateTime(out var startGr) == false || + searchModel.EndDateFa.TryToGeorgianDateTime(out var endGr) == false) + return new List(); + + rawQuery = rawQuery.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date).ToList(); + + } + else + { + if (!string.IsNullOrWhiteSpace(searchModel.Year) && !string.IsNullOrWhiteSpace(searchModel.Month)) + { + var start = searchModel.Year + "/" + searchModel.Month + "/01"; + var end = start.FindeEndOfMonth(); + var startGr = start.ToGeorgianDateTime(); + var endGr = end.ToGeorgianDateTime(); + rawQuery = rawQuery.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date).ToList(); + + + } + else if (!string.IsNullOrWhiteSpace(searchModel.Year) && string.IsNullOrWhiteSpace(searchModel.Month)) + { + var start = searchModel.Year + "/01/01"; + var findEndOfYear = searchModel.Year + "/12/01"; + var end = findEndOfYear.FindeEndOfMonth(); + var startGr = start.ToGeorgianDateTime(); + var endGr = end.ToGeorgianDateTime(); + rawQuery = rawQuery.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date).ToList(); + + } + } + + #endregion + + // مرحله 2: گروه‌بندی و انتخاب آخرین رکورد هر روز روی Client + var grouped = rawQuery + .GroupBy(x => x.DateOnly) + .Select(g => g.OrderByDescending(x => x.CreationDate).First()) + .OrderByDescending(x => x.CreationDate) + .ToList(); + + // مرحله 3: تبدیل به DTO و ToFarsi + var result = grouped.Select(x => new SmsReportDto + { + SentDate = x.CreationDate.ToFarsi() + }).ToList(); + return result; } + #endregion public List Search(SmsResultSearchModel searchModel) { - + var query = _context.SmsResults.Select(x => new App.Contracts.SmsResult.SmsResultViewModel() { Id = x.id, @@ -90,7 +212,7 @@ public class SmsResultRepository : RepositoryBase , ISmsResultR var endGr = end.ToGeorgianDateTime(); query = query.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date); - + } else if (!string.IsNullOrWhiteSpace(searchModel.Year) && string.IsNullOrWhiteSpace(searchModel.Month)) { @@ -100,7 +222,7 @@ public class SmsResultRepository : RepositoryBase , ISmsResultR var startGr = start.ToGeorgianDateTime(); var endGr = end.ToGeorgianDateTime(); query = query.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date); - + } } @@ -108,12 +230,12 @@ public class SmsResultRepository : RepositoryBase , ISmsResultR query = query.OrderByDescending(x => x.CreationDate) - .ThenByDescending(x=>x.CreationDate.Hour).ThenByDescending(x=>x.CreationDate.Minute); - + .ThenByDescending(x => x.CreationDate.Hour).ThenByDescending(x => x.CreationDate.Minute); + return query.Skip(searchModel.PageIndex).Take(30).ToList(); - - + + } - + } \ No newline at end of file diff --git a/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs b/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs index da294057..c3d42a97 100644 --- a/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs +++ b/ServiceHost/Areas/Admin/Controllers/SmsReportController.cs @@ -15,10 +15,10 @@ public class SmsReportController : AdminBaseController } [HttpGet] - public async Task> GetSmsReportList() + public async Task> GetSmsReportList(SmsReportSearchModel searchModel) { - var search = new SmsReportSearchModel(); - var result =await _smsResultApplication.GetSmsReportList(search); + + var result =await _smsResultApplication.GetSmsReportList(searchModel); return result; } } \ No newline at end of file