using System.Collections.Generic; using System.Globalization; using System.Linq; using _0_Framework.Application; using _0_Framework.InfraStructure; using Company.Domain.RewardAgg; using CompanyManagment.App.Contracts.Reward; using Microsoft.EntityFrameworkCore; using SharpCompress.Compressors.Xz; namespace CompanyManagment.EFCore.Repository; public class RewardRepository : RepositoryBase, IRewardRepository { private readonly CompanyContext _companyContext; public RewardRepository(CompanyContext companyContext) : base(companyContext) { _companyContext = companyContext; } public List GetSearchList(RewardSearchModel searchViewModel) { var query = _companyContext.Rewards.Where(x => x.WorkshopId == searchViewModel.WorkshopId && x.IsActive == IsActive.True); if (searchViewModel.EmployeeId != 0) query = query.Where(x => x.EmployeeId == searchViewModel.EmployeeId); if (!string.IsNullOrWhiteSpace(searchViewModel.StartDate) && !string.IsNullOrWhiteSpace(searchViewModel.EndDate)) { var startDate = searchViewModel.StartDate.ToGeorgianDateTime(); var endDate = searchViewModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.GrantDate >= startDate && x.GrantDate <= endDate); } var result = query.OrderByDescending(x => x.CreationDate).Select(x => new RewardViewModel() { Id = x.id, Title = x.Title, EmployeeId = x.EmployeeId, Description = x.Description, Amount = x.Amount.ToMoney(), WorkshopId = x.WorkshopId, CreationDate = x.CreationDate.ToFarsi(), GrantDateFa = x.GrantDate.ToFarsi() }).Skip(searchViewModel.PageIndex).Take(30); var employees = _companyContext.Employees.Where(x => result.Any(a => a.EmployeeId == x.id)); var personnelCodes = _companyContext.PersonnelCodeSet .Where(x => result.Any(a => a.EmployeeId == x.EmployeeId && x.WorkshopId == a.WorkshopId)); return result.Select(x => new RewardViewModel() { Title = x.Title, Amount = x.Amount, CreationDate = x.CreationDate, Description = x.Description??"", GrantDateFa = x.GrantDateFa, EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId).FullName, Id = x.Id, PersonnelCode = personnelCodes .FirstOrDefault(a => a.WorkshopId == x.WorkshopId && a.EmployeeId == x.EmployeeId).PersonnelCode .ToString(), WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId }).ToList(); } public EditRewardViewModel GetDetails(long id) { var entity = _companyContext.Rewards.FirstOrDefault(x => x.id == id); if (entity == null) return new(); var res = new EditRewardViewModel() { Id = entity.id, Title = entity.Title, WorkshopId = entity.WorkshopId, EmployeeId = entity.EmployeeId, Amount = entity.Amount.ToMoney(), Description = entity.Description, GrantDate = entity.GrantDate.ToFarsi() }; res.EmployeeFullName = _companyContext.Employees.Find(entity.EmployeeId)?.FullName; return res; } public List GetBy(IEnumerable ids) { return _companyContext.Rewards.Where(x => ids.Contains(x.id)).ToList(); } #region Pooya /// /// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند /// public RewardsGroupedViewModel GetSearchListAsGrouped(RewardSearchModel searchModel) { var result = new RewardsGroupedViewModel(); var query = _companyContext.Rewards.Where(x => x.WorkshopId == searchModel.WorkshopId && x.IsActive == IsActive.True); var personnelCodes = _companyContext.PersonnelCodeSet .Where(x => x.WorkshopId == searchModel.WorkshopId).ToList(); var pc = new PersianCalendar(); var employees = _companyContext.LeftWorkList.Where(x => x.WorkshopId == searchModel.WorkshopId).Include(x => x.Employee).Select(x => x.Employee).ToList(); if (searchModel.ShowAsGrouped) { if (searchModel.EmployeeId > 0) { if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var startDate = searchModel.StartDate.ToGeorgianDateTime(); var endDate = searchModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.GrantDate >= startDate && x.GrantDate <= endDate); } query = query.Where(x => x.EmployeeId == searchModel.EmployeeId); var list = query.ToList().OrderByDescending(x=>x.GrantDate).Select(x => new RewardViewModel { WorkshopId = x.WorkshopId, Amount = x.Amount.ToMoney(), AmountDouble = x.Amount, Description = x.Description ?? "", EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId).FullName, EmployeeId = x.EmployeeId, GrantDateFa = x.GrantDate.ToFarsi(), GrantDateGr = x.GrantDate, Id = x.id, Title = x.Title, IsActive = x.IsActive, MonthFa = pc.GetMonth(x.GrantDate).ToFarsiMonthByIntNumber(), YearFa = pc.GetYear(x.GrantDate).ToString(), PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == x.EmployeeId).PersonnelCode.ToString(), }).ToList(); result.RewardsGroupedByDate = list.GroupBy(x => new { x.YearFa, x.MonthFa }).Select(x => new RewardsGroupedByDate() { Month = x.Key.MonthFa, Year = x.Key.YearFa, Rewards = x.Select(r => new RewardsGroupedByDateItems() { PersonnelCode = r.PersonnelCode, Amount = r.Amount, Description = r.Description, GrantDateFa = r.GrantDateFa, Title = r.Title, EmployeeName = r.EmployeeFullName, Id = r.Id }).ToList(), TotalAmount = x.Sum(r => r.AmountDouble).ToMoney() }).ToList(); return result; } else if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var startDate = searchModel.StartDate.ToGeorgianDateTime(); var endDate = searchModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.GrantDate >= startDate && x.GrantDate <= endDate); var resList = query.GroupBy(x => x.EmployeeId).ToList(); result.RewardsGroupedByEmployee = resList.Select(x => new RewardsGroupedByEmployeeViewModel() { EmployeeId = x.Key, EmployeeName = employees.FirstOrDefault(e => e.id == x.Key).FullName, Rewards = x.Select(r => new RewardsGroupedByEmployeeViewModelItems { PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == r.EmployeeId).PersonnelCode.ToString(), Amount = r.Amount.ToMoney(), Description = r.Description, GrantDate = r.GrantDate.ToFarsi(), Title = r.Title, Id = r.id }).ToList(), TotalAmount = x.Sum(r => r.Amount).ToMoney() }).ToList(); return result; } } 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.GrantDate >= startDate && x.GrantDate <= endDate); } result.RewardListViewModels = query.Skip(searchModel.PageIndex).Take(30).ToList().Select(x => new RewardViewModel() { WorkshopId = x.WorkshopId, Amount = x.Amount.ToMoney(), AmountDouble = x.Amount, Description = x.Description??"", EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId).FullName, EmployeeId = x.EmployeeId, GrantDateFa = x.GrantDate.ToFarsi(), GrantDateGr = x.GrantDate, Id = x.id, Title = x.Title, IsActive = x.IsActive, MonthFa = pc.GetMonth(x.GrantDate).ToFarsiMonthByIntNumber(), YearFa = pc.GetYear(x.GrantDate).ToString(), PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == x.EmployeeId).PersonnelCode.ToString(), }).ToList(); return result; //var result = query.Skip(searchModel.PageIndex).Select(x => new RewardViewModel() //{ // Id = x.id, // EmployeeId = x.EmployeeId, // Description = x.Description, // Amount = x.Amount.ToMoney(), // WorkshopId = x.WorkshopId, // CreationDate = x.CreationDate.ToFarsi(), // GrantDateFa = x.GrantDate.ToFarsi(), //}).ToList(); } #endregion }