DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellContactService.cs

88 lines
3.0 KiB
C#
Raw Normal View History

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<IEnumerable<CompanyWithUsersDto>> 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,
2023-06-23 12:13:17 +05:00
Users = c.Users.Select(u => new UserContactDto()
{
Id = u.Id,
Name = u.Name,
Patronymic = u.Patronymic,
Surname = u.Surname,
Company = u.Company.Adapt<CompanyDto>(),
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<IEnumerable<CompanyTypeDto>> 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<IEnumerable<CompanyTypeDto>>();
return dtos;
}
public async Task<int> UpdateRangeAsync(int idWell, int contactTypeId, IEnumerable<int> userIds, CancellationToken token)
{
var items = db.RelationContactsWells
.Where(x => x.IdWell == idWell && x.User.Company.IdCompanyType == contactTypeId);
2023-06-23 12:13:17 +05:00
var wellContacts = userIds.Select(userId => new RelationContactWell()
{
2023-06-23 12:13:17 +05:00
IdWell = idWell,
IdUser = userId,
});
db.RelationContactsWells.RemoveRange(items);
db.RelationContactsWells.AddRange(wellContacts);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
}
}