Files
Backend-Api/CompanyManagment.EFCore/Repository/SmsResultRepository.cs
2026-01-07 14:49:44 +03:30

119 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
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;
namespace CompanyManagment.EFCore.Repository;
public class SmsResultRepository : RepositoryBase<long, SmsResult> , ISmsResultRepository
{
private readonly CompanyContext _context;
public SmsResultRepository(CompanyContext context) : base(context)
{
_context = context;
}
#region ForApi
public async Task<List<SmsReportDto>> GetSmsReportList(SmsReportSearchModel command)
{
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()
{
SentDate = x.Key.ToFarsi()
}).ToListAsync();
Console.WriteLine("ToList : " + watch.Elapsed);
return b;
}
#endregion
public List<App.Contracts.SmsResult.SmsResultViewModel> Search(SmsResultSearchModel searchModel)
{
var query = _context.SmsResults.Select(x => new App.Contracts.SmsResult.SmsResultViewModel()
{
Id = x.id,
MessageId = x.MessageId,
Status = x.Status,
TypeOfSms = x.TypeOfSms,
ContractingPartyName = x.ContractingPartyName,
Mobile = x.Mobile,
ContractingPartyId = x.ContractingPatyId,
InstitutionContractId = x.InstitutionContractId,
CreationDate = x.CreationDate,
Hour = x.CreationDate.Hour > 9 ? $"{x.CreationDate.Hour}" : $"0{x.CreationDate.Hour}",
Minute = x.CreationDate.Minute > 9 ? $"{x.CreationDate.Minute}" : $"0{x.CreationDate.Minute}",
});
if (searchModel.ContractingPatyId > 0)
query = query.Where(x => x.ContractingPartyId == searchModel.ContractingPatyId);
if (!string.IsNullOrWhiteSpace(searchModel.Status))
query = query.Where(x => x.Status == searchModel.Status);
if (!string.IsNullOrWhiteSpace(searchModel.TypeOfSms))
query = query.Where(x => x.TypeOfSms == searchModel.TypeOfSms);
if (!string.IsNullOrWhiteSpace(searchModel.Mobile))
query = query.Where(x => x.Mobile.Contains(searchModel.Mobile));
#region searchByDate
if (!string.IsNullOrWhiteSpace(searchModel.StartDateFa) &&
!string.IsNullOrWhiteSpace(searchModel.EndDateFa))
{
var startGr = searchModel.StartDateFa.ToGeorgianDateTime();
var endGr = searchModel.EndDateFa.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))
{
var start = searchModel.Year + "/" + searchModel.Month + "/01";
var end = start.FindeEndOfMonth();
var startGr = start.ToGeorgianDateTime();
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))
{
var start = searchModel.Year + "/01/01";
var findEndOfYear = searchModel.Year + "/12/01";
var end = findEndOfYear.FindeEndOfMonth();
var startGr = start.ToGeorgianDateTime();
var endGr = end.ToGeorgianDateTime();
query = query.Where(x => x.CreationDate.Date >= startGr.Date && x.CreationDate.Date <= endGr.Date);
}
}
#endregion
query = query.OrderByDescending(x => x.CreationDate)
.ThenByDescending(x=>x.CreationDate.Hour).ThenByDescending(x=>x.CreationDate.Minute);
return query.Skip(searchModel.PageIndex).Take(30).ToList();
}
}