using _0_Framework.Application; using _0_Framework.InfraStructure; using AccountManagement.Application.Contracts.Account; using AccountManagement.Domain.AccountAgg; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; using AccountManagement.Domain.PositionAgg; using AccountManagement.Domain.RoleAgg; using Microsoft.AspNetCore.Http; namespace AccountMangement.Infrastructure.EFCore.Repository; public class AccountRepository : RepositoryBase, IAccountRepository { private readonly AccountContext _context; private readonly IHttpContextAccessor _contextAccessor; private readonly IPositionRepository _positionRepository; public AccountRepository(AccountContext context, IHttpContextAccessor contextAccessor, IPositionRepository positionRepository) : base(context) { _context = context; _contextAccessor = contextAccessor; _positionRepository = positionRepository; } public Account GetBy(string username) { return _context.Accounts.FirstOrDefault(x => x.Username == username); } public Account GetById(long id) { return _context.Accounts.FirstOrDefault(x => x.id == id); } public EditAccount GetDetails(long id) { return _context.Accounts.Select(x => new EditAccount { Id = x.id, Fullname = x.Fullname, Mobile = x.Mobile, RoleId = x.RoleId, Username = x.Username, IsActiveString = x.IsActiveString }).FirstOrDefault(x => x.Id == id); } public EditAccount GetByVerifyCode(string code, string phone) { return _context.Accounts.Select(x => new EditAccount { Id = x.id, Fullname = x.Fullname, Mobile = x.Mobile, RoleId = x.RoleId, Username = x.Username, VerifyCode = x.VerifyCode, ProfilePhotoStr = x.ProfilePhoto, }).FirstOrDefault(x => x.VerifyCode == code && x.Mobile == phone); } public EditAccount GetByUserNameAndId(long id, string username) { return _context.Accounts.Select(x => new EditAccount { Id = x.id, Fullname = x.Fullname, Mobile = x.Mobile, RoleId = x.RoleId, Username = x.Username, VerifyCode = x.VerifyCode, IsActiveString = x.IsActiveString }).FirstOrDefault(x => x.Id == id && x.Username == username ); } public List GetAccounts() { return _context.Accounts.Select(x => new AccountViewModel { Id = x.id, Fullname = x.Fullname, RoleName = x.RoleName, Username = x.Username, RoleId = x.RoleId, Mobile = x.Mobile, CreationDateGr = x.CreationDate, IsActiveString = x.IsActiveString, AdminAreaPermission = x.AdminAreaPermission }).ToList(); } public List GetAdminAccountSelectList() { return _context.Accounts.Where(x => x.AdminAreaPermission == "true").Select(x => new AccountViewModel() { Id = x.id, Fullname = x.Fullname, }).ToList(); } public List GetClientsAccount() { return _context.Accounts.Select(x => new AccountViewModel { Id = x.id, Fullname = x.Fullname, RoleName = x.RoleName, Username = x.Username, RoleId = x.RoleId, Mobile = x.Mobile, CreationDateGr = x.CreationDate, IsActiveString = x.IsActiveString }).Where(x=>x.RoleId == 15 && x.IsActiveString == "true").ToList(); } [SuppressMessage("ReSharper.DPA", "DPA0007: Large number of DB records", MessageId = "count: 282")] public List Search(AccountSearchModel searchModel) { var query = _context.Accounts.Include(x => x.Role).Select(x => new AccountViewModel { Id = x.id, Fullname = x.Fullname, Mobile = x.Mobile, ProfilePhoto = x.ProfilePhoto, Role = x.Role.Name, RoleId = x.RoleId, Username = x.Username, RoleName = x.RoleName, CreationDate = x.CreationDate.ToFarsi(), IsActiveString = x.IsActiveString, }); if (searchModel.Id > 0) query = query.Where(x => x.Id == searchModel.Id); if (!string.IsNullOrWhiteSpace(searchModel.VerifyCode)) query = query.Where(x => x.VerifyCode == searchModel.VerifyCode); if (!string.IsNullOrWhiteSpace(searchModel.Fullname)) query = query.Where(x => x.Fullname.Contains(searchModel.Fullname)); if (!string.IsNullOrWhiteSpace(searchModel.Username)) query = query.Where(x => x.Username.Contains(searchModel.Username)); if (!string.IsNullOrWhiteSpace(searchModel.Mobile)) query = query.Where(x => x.Mobile==searchModel.Mobile); if (searchModel.RoleId > 0) query = query.Where(x => x.RoleId == searchModel.RoleId); if (string.IsNullOrWhiteSpace(searchModel.IsActiveString)) { query = query.Where(x => x.IsActiveString == "true"); } if (searchModel.IsActiveString == "false") { //hasSearch = true; query = query.Where(x => x.IsActiveString == "false"); } else if (searchModel.IsActiveString == "both") { //hasSearch = true; query = query.Where(x => x.IsActiveString == "false" || x.IsActiveString == "true"); } return query.OrderByDescending(x => x.Id).ToList(); } public async Task RemoveCode(long id) { TimeSpan delay = TimeSpan.FromSeconds(30); await Task.Delay(delay); var acc = Get(id); acc.VerifyCode = ""; _context.SaveChanges(); } #region Mahan public Account GetIncludePositions(long id) { return _context.Accounts.Include(x => x.Position).FirstOrDefault(x => x.id == id); } public List GetAccountsByPositionId(long positionId) { return _context.Accounts.Include(x => x.Position).Where(x => x.PositionId == positionId).Select(x => new AccountViewModel() { CreationDateGr = x.CreationDate, Fullname = x.Fullname, Id = x.id, PositionValue = x.Position.PositionValue, PositionId = x.Position.id, IsActiveString = x.IsActiveString, Username = x.Username, PositionIsActive = x.PositionIsActive }).ToList(); } public List AccountsForAssign(long taskId) { var posValue = int.Parse(_contextAccessor.HttpContext.User.FindFirst("PositionValue").Value); var assignedIds = _context.Assigns.Where(x => x.TaskId == taskId).Select(x => x.AssignedId).ToList(); var assignerIds = _context.Assigns.Where(x => x.TaskId == taskId).Select(x => x.AssignerId).ToList(); return _context.Accounts.Include(x => x.Position).Where(x => x.Position.PositionValue >= posValue && !assignerIds.Contains(x.id) && !assignedIds.Contains(x.id)).Select( x => new AccountViewModel() { PositionValue = x.Position.PositionValue, CreationDateGr = x.CreationDate, Fullname = x.Fullname, Id = x.id, IsActiveString = x.IsActiveString, Username = x.Username }).ToList(); } public List GetAccountEqualToLowerPositionValue() { var account = GetIncludePositions(int.Parse(_contextAccessor.HttpContext.User.FindFirst("AccountId").Value)); return _context.Accounts.Include(x => x.Position) .Where(x => x.Position.PositionValue >= account.Position.PositionValue && x.IsActiveString == "true" && x.id != account.id).Select(x => new AccountViewModel() { PositionValue = x.Position.PositionValue, Id = x.id, Fullname = x.Fullname, Mobile = x.Mobile, ProfilePhoto = x.ProfilePhoto, Role = x.Role.Name, RoleId = x.RoleId, Username = x.Username, RoleName = x.RoleName, CreationDate = x.CreationDate.ToFarsi(), IsActiveString = x.IsActiveString, CreationDateGr = x.CreationDate, }).ToList(); } public List GetAccountsByIds(List ids) { var res = _context.Accounts.Include(x => x.Position).Where(x => x.PositionId != null); return res.Where(x => ids.Contains(x.id)).ToList(); } public AccountViewModel GetAccountViewModel(long id) { return _context.Accounts.Include(x => x.Position).Select(x => new AccountViewModel() { Id = x.id, Fullname = x.Fullname, RoleName = x.RoleName, Username = x.Username, RoleId = x.RoleId, Mobile = x.Mobile, CreationDateGr = x.CreationDate, IsActiveString = x.IsActiveString, PositionValue = x.Position.PositionValue }).FirstOrDefault(x => x.Id == id); } public List GetAccountsDeactivePositionValue(long Positionid) { return _context.Accounts.Where(x => x.PositionId == Positionid && x.PositionIsActive == "false").Select(x => new AccountViewModel() { Id = x.id, Fullname = x.Fullname }).ToList(); } public List GetAdminAccountsNew() { List roleIds = _0_Framework.Application.StaticWorkshopAccounts.SelectedAccountsRoleIds; return _context.Accounts .Where(x => x.IsActiveString == "true" && roleIds.Contains(x.RoleId)) .Select(x => new AccountViewModel { Id = x.id, Fullname = x.Fullname, RoleName = x.RoleName, Username = x.Username, RoleId = x.RoleId, }).ToList(); } public List GetAccountsToEditWorkshop(long workshopId) { List roleIds = _0_Framework.Application.StaticWorkshopAccounts.SelectedAccountsRoleIds; return _context.AccountLeftWorks .Where(x => x.WorkshopId == workshopId && roleIds.Contains(x.RoleId)) .Include(x => x.Account) .Select(x => new AccountViewModel { Id = x.AccountId, Fullname = x.Account.Fullname, RoleName = x.Account.RoleName, RoleId = x.RoleId, IsActiveString = x.IsActive ? "true" : "false", }).ToList(); } public async Task> GetAdminSelectList() { return await _context.Accounts.Where(x => x.AdminAreaPermission == "true").Select(x => new AccountSelectListViewModel() { Id = x.id, Name = x.Fullname }).ToListAsync(); } //public List GetAdminAccounts() //{ // return _context.Accounts.Where(x=>x.AdminAreaPermission == "true" && x.IsActiveString == "true").Select(x => new AccountViewModel() // { // Id = x.id, // Fullname = x.Fullname, // RoleName = x.RoleName, // Username = x.Username, // RoleId = x.RoleId, // Mobile = x.Mobile, // CreationDateGr = x.CreationDate, // IsActiveString = x.IsActiveString, // }).ToList(); //} #endregion public bool CheckExistClientAccount(string userName) { return _context.Accounts.Any(x => x.Username == userName); } }