2023-06-21 11:44:04 +05:00
|
|
|
|
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)
|
|
|
|
|
})
|
2023-06-21 11:44:04 +05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var entities = await query.AsNoTracking()
|
|
|
|
|
.ToArrayAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<CompanyTypeDto>> GetTypesAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = db.CompaniesTypes.Where(t => t.IsContact);
|
|
|
|
|
|
|
|
|
|
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-21 11:44:04 +05:00
|
|
|
|
{
|
2023-06-23 12:13:17 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdUser = userId,
|
|
|
|
|
});
|
2023-06-21 11:44:04 +05:00
|
|
|
|
|
|
|
|
|
db.RelationContactsWells.RemoveRange(items);
|
|
|
|
|
db.RelationContactsWells.AddRange(wellContacts);
|
|
|
|
|
|
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|