171 lines
4.4 KiB
C#
171 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using EnVisage.Models;
|
|
|
|
namespace EnVisage.Code.BLL
|
|
{
|
|
public class ClientManager : ManagerBase<Client, ClientModel>
|
|
{
|
|
public ClientManager(EnVisageEntities dbContext)
|
|
: base(dbContext)
|
|
{
|
|
}
|
|
|
|
protected override Client InitInstance()
|
|
{
|
|
return new Client { Id = Guid.NewGuid() };
|
|
}
|
|
|
|
protected override Client RetrieveReadOnlyById(Guid key)
|
|
{
|
|
return DataTable.AsNoTracking().FirstOrDefault(t => t.Id == key);
|
|
}
|
|
|
|
public override DbSet<Client> DataTable
|
|
{
|
|
get
|
|
{
|
|
return DbContext.Clients;
|
|
}
|
|
}
|
|
|
|
public ClientModel LoadWithChildCollections(Guid clientId)
|
|
{
|
|
var model = LoadClientModel(clientId);
|
|
if (model != null)
|
|
{
|
|
var companies = DbContext.Company2Client
|
|
.AsNoTracking()
|
|
.Where(x => x.ClientId == clientId)
|
|
.Select(x => x.CompanyId)
|
|
.ToList();
|
|
|
|
model.CompanyId = companies;
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
public ClientModel LoadClientModel(Guid clientId)
|
|
{
|
|
return ClientModelBasicQuery.FirstOrDefault(x => x.Id == clientId);
|
|
}
|
|
|
|
public ClientModel LoadClientModelByName(string name)
|
|
{
|
|
return ClientModelBasicQuery.FirstOrDefault(x => x.Name == name);
|
|
}
|
|
|
|
public override Client Save(ClientModel model)
|
|
{
|
|
var client = base.Save(model);
|
|
|
|
if (Guid.Empty.Equals(model.Id))
|
|
{
|
|
if (model.CompanyId != null)
|
|
{
|
|
foreach (var companyId in model.CompanyId)
|
|
{
|
|
var company2Client = DbContext.Company2Client.Create();
|
|
company2Client.Id = Guid.NewGuid();
|
|
company2Client.ClientId = client.Id;
|
|
company2Client.CompanyId = companyId;
|
|
DbContext.Company2Client.Add(company2Client);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var currentCompany2Clients = DbContext.Company2Client.Where(x => x.ClientId == model.Id).ToList();
|
|
currentCompany2Clients.Where(x => model.CompanyId == null || !model.CompanyId.Contains(x.CompanyId)).ToList().
|
|
ForEach(x => DbContext.Entry(x).State = EntityState.Deleted);
|
|
|
|
if (model.CompanyId != null)
|
|
{
|
|
foreach (var companyId in model.CompanyId.Where(c => currentCompany2Clients.All(x => x.CompanyId != c)))
|
|
{
|
|
var company2Client = DbContext.Company2Client.Create();
|
|
company2Client.Id = Guid.NewGuid();
|
|
company2Client.ClientId = client.Id;
|
|
company2Client.CompanyId = companyId;
|
|
DbContext.Company2Client.Add(company2Client);
|
|
}
|
|
}
|
|
}
|
|
return client;
|
|
}
|
|
|
|
public IEnumerable<ClientListModel> GetListReadOnly()
|
|
{
|
|
IEnumerable<ClientListModel> query = (
|
|
from c in DbContext.Clients
|
|
join ac in DbContext.GLAccounts on c.GLAccountId equals ac.Id into lnks
|
|
from lnk in lnks.DefaultIfEmpty()
|
|
select new ClientListModel()
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
ClientNumber = c.ClientNumber ?? string.Empty,
|
|
ProjectsCount = c.Projects.Count(),
|
|
GLAccountId = c.GLAccountId,
|
|
GlAccount = (lnk == null) ? string.Empty : lnk.Name,
|
|
Companies = c.Company2Client.Select(x => x.Company.Name).ToList()
|
|
}).AsNoTracking();
|
|
|
|
return query;
|
|
}
|
|
|
|
public Dictionary<Guid, ClientApiModel> GetByProjectStatues(List<Guid> projectStatuses, Guid? userId)
|
|
{
|
|
if (projectStatuses == null)
|
|
throw new ArgumentNullException(nameof(projectStatuses));
|
|
|
|
if (!projectStatuses.Any())
|
|
return new Dictionary<Guid, ClientApiModel>();
|
|
|
|
var result =
|
|
(from status in DbContext.Status
|
|
join prj in DbContext.VW_ProjectAccessByUserExtended on status.Id equals prj.StatusId
|
|
join client in DbContext.Clients on prj.ClientId equals client.Id
|
|
where projectStatuses.Contains(status.Id) && prj.ClientId.HasValue &&
|
|
(!userId.HasValue || (prj.UserId.Equals(userId.Value) &&
|
|
prj.Read.HasValue && (prj.Read.Value == 1) || prj.Write.HasValue && (prj.Write.Value == 1)))
|
|
select client)
|
|
.Distinct().ToDictionary(k => k.Id, v => new ClientApiModel()
|
|
{
|
|
Id = v.Id,
|
|
Name = v.Name,
|
|
ClientNumber = v.ClientNumber,
|
|
GLAccountId = v.GLAccountId
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
#region Private Members
|
|
|
|
private IQueryable<ClientModel> ClientModelBasicQuery
|
|
{
|
|
get
|
|
{
|
|
var query = DbContext.Clients.AsNoTracking()
|
|
.Select(x =>
|
|
new ClientModel()
|
|
{
|
|
Id = x.Id,
|
|
Name = x.Name,
|
|
GLAccountId = x.GLAccountId,
|
|
ClientNumber = x.ClientNumber,
|
|
ProjectsCount = x.Projects.Count
|
|
});
|
|
|
|
return query;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |