using Back.Common; using Back.Data.Contracts; using Back.Data.Models; using Microsoft.EntityFrameworkCore; using Shared.DTOs; namespace Back.Services { public class servCompany { private readonly IAsyncRepository _repoRolUser; private readonly IAsyncRepository _repoCompany; //private readonly servSendMsg _servSendMsg; public servCompany(IAsyncRepository repoRolUser, IAsyncRepository repoCompany) { _repoCompany = repoCompany; _repoRolUser = repoRolUser; } public async Task ExsistCompanyByComoanyIDandUserID(int ComoanyID, int UserID, bool InAdmin = false) { var res = _repoRolUser.Get(w => w.CompanyID == ComoanyID && w.UserID == UserID && w.Company.IsActive); if (InAdmin) res = res.Where(w => w.IsAdmin); return await res.AnyAsync(); } public async Task GetCompany(int ComoanyID) { return await _repoCompany.Get(w => w.ID == ComoanyID && w.IsActive) .Select(s => new CompanyDTO() { BranchID = s.BranchID, EconomicCode = s.EconomicCode, ID = s.ID, Email = s.Email, Logo = s.Logo == null ? null : System.Text.Encoding.UTF8.GetString(s.Logo), Mobile = s.Mobile, Name = s.Name, Phone = s.Phone, PrivateKey = s.PrivateKey, UniqeMemory = s.UniqeMemory }).FirstOrDefaultAsync(); } public async Task GetCompanyOrg(int ComoanyID, bool IsActive = true) { var inv = _repoCompany.Get(w => w.ID == ComoanyID); if (IsActive) inv = inv.Where(w => w.IsActive); return await inv.FirstOrDefaultAsync(); } public async Task GetCompanyOrgByMobileAndCompanynotActive(string Mobile) { var inv = _repoCompany.Get(w => w.Mobile == Mobile && !w.IsActive); return await inv.FirstOrDefaultAsync(); } public async Task AddORUpdateCompany(Company item) { try { //SysLog log = new SysLog() //{ // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, // Datetime = DateTime.Now.ConvertMiladiToShamsi(), // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany", // Value = "*" + JsonConvert.SerializeObject(item), // Route = _httpContextAccessor.HttpContext.Request.Path, // Type = "User" //}; //_contextMongodb.InsertItem(log); if (item.ID == null || item.ID == 0) { return await _repoCompany.AddAsync(item); } else { return await _repoCompany.UpdateByObjAsync(item); } } catch (Exception ex) { //SysLog log = new SysLog() //{ // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, // Datetime = DateTime.Now.ConvertMiladiToShamsi(), // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany", // Value = ex.Message, // Route = _httpContextAccessor.HttpContext.Request.Path, // Type = "catch" //}; //_contextMongodb.InsertItem(log); return null; } } public async Task AddORUpdateCompanyBoolResult(Company item) { try { //SysLog log = new SysLog() //{ // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, // Datetime = DateTime.Now.ConvertMiladiToShamsi(), // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany", // Value = "*" + JsonConvert.SerializeObject(item), // Route = _httpContextAccessor.HttpContext.Request.Path, // Type = "User" //}; //_contextMongodb.InsertItem(log); if (item.ID == null || item.ID == 0) { return await _repoCompany.AddBoolResultAsync(item); } else { return await _repoCompany.UpdateAsync(item); } } catch (Exception ex) { //SysLog log = new SysLog() //{ // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, // Datetime = DateTime.Now.ConvertMiladiToShamsi(), // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany", // Value = ex.Message, // Route = _httpContextAccessor.HttpContext.Request.Path, // Type = "catch" //}; //_contextMongodb.InsertItem(log); return false; } } public async Task ExistMobileInCompany(string mobile, bool IsActive = true) { var resquest = _repoCompany.GetAll().Where(w => w.Mobile == mobile); if (IsActive) resquest = resquest.Where(w => w.IsActive); return await resquest.AnyAsync(); } public async Task GetTaxAuth(int ComoanyID) { return await _repoCompany.Get(w => w.ID == ComoanyID && w.IsActive) .Select(s => new CheckAuthDTO() { PrivateKey = s.PrivateKey, UniqueMemory = s.UniqeMemory }).FirstOrDefaultAsync(); } } }