85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.FineAgg;
|
|
using CompanyManagment.App.Contracts.Fine;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class FineRepository : RepositoryBase<long, Fine>, IFineRepository
|
|
{
|
|
private readonly CompanyContext _companyContext;
|
|
|
|
public FineRepository(CompanyContext companyContext) : base(companyContext)
|
|
{
|
|
_companyContext = companyContext;
|
|
}
|
|
|
|
public List<FineViewModel> GetSearchList(FineSearchViewModel searchModel)
|
|
{
|
|
var query = _companyContext.Fines.Where(x => x.WorkshopId == searchModel.WorkshopId);
|
|
|
|
if (searchModel.EmployeeId != 0)
|
|
query = query.Where(x => x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate))
|
|
{
|
|
var startDate = searchModel.StartDate.ToGeorgianDateTime();
|
|
var endDate = searchModel.EndDate.ToGeorgianDateTime();
|
|
query = query.Where(x => x.FineDate >= startDate && x.FineDate <= endDate);
|
|
}
|
|
var result = query.OrderByDescending(x => x.CreationDate).Skip(searchModel.PageIndex).Take(30).Select(x => new FineViewModel()
|
|
{
|
|
Id = x.id,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
Title = x.Title,
|
|
Amount = x.Amount.ToMoney(),
|
|
FineDate = x.FineDate.ToFarsi(),
|
|
IsActive = x.IsActive,
|
|
CreationDate = x.CreationDate.ToFarsi()
|
|
}).AsEnumerable();
|
|
var employees = _companyContext.Employees.Where(x => result.Any(a => a.EmployeeId == x.id)).ToList();
|
|
var personnelCodes = _companyContext.PersonnelCodeSet
|
|
.Where(x => result.Any(a => a.EmployeeId == x.EmployeeId && x.WorkshopId == a.WorkshopId)).ToList();
|
|
return result.Select(x => new FineViewModel()
|
|
{
|
|
Id = x.Id,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
EmployeeFullName = employees.FirstOrDefault(a => a.id == x.EmployeeId)?.FullName,
|
|
PersonnelCode = personnelCodes.FirstOrDefault(a => x.WorkshopId == a.WorkshopId && a.EmployeeId == x.EmployeeId)?.PersonnelCode.ToString(),
|
|
Title = x.Title,
|
|
Amount = x.Amount,
|
|
FineDate = x.FineDate,
|
|
IsActive = x.IsActive,
|
|
CreationDate = x.CreationDate
|
|
}).ToList();
|
|
}
|
|
|
|
public List<Fine> GetBy(IEnumerable<long> ids)
|
|
{
|
|
return _companyContext.Fines.Where(x => ids.Contains(x.id)).ToList();
|
|
|
|
}
|
|
|
|
public EditFineViewModel GetDetails(long id)
|
|
{
|
|
var entity = _companyContext.Fines.FirstOrDefault(x => x.id == id);
|
|
if (entity == null)
|
|
return new();
|
|
|
|
return new EditFineViewModel()
|
|
{
|
|
Id = entity.id,
|
|
WorkshopId = entity.WorkshopId,
|
|
EmployeeId = entity.EmployeeId,
|
|
EmployeeFullname = _companyContext.Employees.FirstOrDefault(x => x.id == entity.EmployeeId)?.FullName ?? "",
|
|
Title = entity.Title,
|
|
Amount = entity.Amount.ToMoney(),
|
|
IsActive = entity.IsActive,
|
|
FineDate = entity.FineDate.ToFarsi(),
|
|
};
|
|
}
|
|
} |