Files
moadiran/Back/Services/servCompany.cs

169 lines
6.2 KiB
C#
Raw Permalink Normal View History

2024-04-17 15:49:34 +03:30
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<RolUser> _repoRolUser;
private readonly IAsyncRepository<Company> _repoCompany;
//private readonly servSendMsg _servSendMsg;
public servCompany(IAsyncRepository<RolUser> repoRolUser, IAsyncRepository<Company> repoCompany)
{
_repoCompany = repoCompany;
_repoRolUser = repoRolUser;
}
2024-06-09 17:23:57 +03:30
public async Task<bool> ExsistCompanyByComoanyIDandUserID(int ComoanyID, int UserID, bool InAdmin = false)
2024-04-17 15:49:34 +03:30
{
2024-06-09 17:23:57 +03:30
var res = _repoRolUser.Get(w => w.CompanyID == ComoanyID && w.UserID == UserID && w.Company.IsActive);
2024-04-17 15:49:34 +03:30
if (InAdmin)
2024-06-09 17:23:57 +03:30
res = res.Where(w => w.IsAdmin);
2024-04-17 15:49:34 +03:30
2024-06-09 17:23:57 +03:30
return await res.AnyAsync();
2024-04-17 15:49:34 +03:30
}
public async Task<CompanyDTO?> GetCompany(int ComoanyID)
{
2024-06-09 17:23:57 +03:30
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();
2024-04-17 15:49:34 +03:30
}
2024-06-09 17:23:57 +03:30
public async Task<Company?> GetCompanyOrg(int ComoanyID, bool IsActive = true)
2024-04-17 15:49:34 +03:30
{
var inv = _repoCompany.Get(w => w.ID == ComoanyID);
if (IsActive)
2024-06-09 17:23:57 +03:30
inv = inv.Where(w => w.IsActive);
return await inv.FirstOrDefaultAsync();
2024-04-17 15:49:34 +03:30
}
public async Task<Company?> GetCompanyOrgByMobileAndCompanynotActive(string Mobile)
{
var inv = _repoCompany.Get(w => w.Mobile == Mobile && !w.IsActive);
return await inv.FirstOrDefaultAsync();
}
public async Task<Company> 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);
2024-06-09 17:23:57 +03:30
if (item.ID == null || item.ID == 0)
2024-04-17 15:49:34 +03:30
{
2024-06-09 17:23:57 +03:30
return await _repoCompany.AddAsync(item);
2024-04-17 15:49:34 +03:30
}
else
{
2024-06-09 17:23:57 +03:30
return await _repoCompany.UpdateByObjAsync(item);
2024-04-17 15:49:34 +03:30
}
2024-06-09 17:23:57 +03:30
2024-04-17 15:49:34 +03:30
}
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;
}
}
2024-04-29 18:15:46 +03:30
public async Task<bool> AddORUpdateCompanyBoolResult(Company item)
2024-04-17 15:49:34 +03:30
{
2024-04-29 18:15:46 +03:30
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;
}
2024-04-29 07:58:41 +03:30
}
2024-06-09 17:23:57 +03:30
public async Task<bool> ExistMobileInCompany(string mobile, bool IsActive = true)
2024-04-29 18:15:46 +03:30
{
2024-06-09 17:23:57 +03:30
var resquest = _repoCompany.GetAll().Where(w => w.Mobile == mobile);
2024-04-29 18:15:46 +03:30
if (IsActive)
resquest = resquest.Where(w => w.IsActive);
2024-06-09 17:23:57 +03:30
2024-04-29 18:15:46 +03:30
return await resquest.AnyAsync();
}
2024-06-09 17:23:57 +03:30
public async Task<CheckAuthDTO> GetTaxAuth(int ComoanyID)
{
return await _repoCompany.Get(w => w.ID == ComoanyID && w.IsActive)
.Select(s => new CheckAuthDTO()
{
PrivateKey = s.PrivateKey,
UniqueMemory = s.UniqeMemory
}).FirstOrDefaultAsync();
}
2024-04-17 15:49:34 +03:30
}
}