using AsbCloudApp.Data; using AsbCloudApp.Data.User; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { public class WellContactService : IWellContactService { private readonly IAsbCloudDbContext db; public WellContactService(IAsbCloudDbContext db) { this.db = db; } public async Task> GetAsync(int wellId, int contactTypeId, CancellationToken token) { var query = db.Companies .Where(c => c.IdCompanyType == contactTypeId) .Where(c => c.RelationCompaniesWells.Any(rc => rc.IdWell == wellId)) .Select(c => new CompanyWithUsersDto() { Caption = c.Caption, Id = c.Id, Users = c.Users .Where(u => u.IdState == 1) .OrderBy(u => u.Surname) .Select(u => new UserContactDto() { Id = u.Id, Name = u.Name, Patronymic = u.Patronymic, Surname = u.Surname, Company = u.Company.Adapt(), Email = u.Email, Phone = u.Phone, Position = u.Position, IsContact = u.RelationContactsWells.Any(rel => rel.IdWell == wellId) }) }); var entities = await query.AsNoTracking() .ToArrayAsync(token) .ConfigureAwait(false); return entities; } public async Task> GetTypesAsync(int idWell, CancellationToken token) { var query = db.CompaniesTypes .Where(t => t.IsContact) .Where(t => t.Companies.Any(c => c.Users.Any() && c.RelationCompaniesWells.Any(w => w.IdWell == idWell))) .OrderBy(t => t.Order); var entities = await query.AsNoTracking() .ToArrayAsync(token) .ConfigureAwait(false); var dtos = entities.Adapt>(); return dtos; } public async Task UpdateRangeAsync(int idWell, int contactTypeId, IEnumerable userIds, CancellationToken token) { var items = db.RelationContactsWells .Where(x => x.IdWell == idWell && x.User.Company.IdCompanyType == contactTypeId); var wellContacts = userIds.Select(userId => new RelationContactWell() { IdWell = idWell, IdUser = userId, }); db.RelationContactsWells.RemoveRange(items); db.RelationContactsWells.AddRange(wellContacts); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } } }