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

97 lines
3.2 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
2023-10-09 12:20:00 +05:00
using System;
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;
}
2023-10-09 12:20:00 +05:00
public async Task<IEnumerable<CompanyWithContactsDto>> 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))
2023-10-09 12:20:00 +05:00
.Select(c => new CompanyWithContactsDto()
{
Caption = c.Caption,
Id = c.Id,
2023-10-09 12:20:00 +05:00
Contacts = c.Contacts
.OrderBy(u => u.FIO)
.Select(u => u.Adapt<ContactDto>())
});
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;
}
2023-10-09 12:20:00 +05:00
public async Task<int> InsertAsync(ContactDto contactDto, CancellationToken token)
{
var entity = contactDto.Adapt<Contact>();
//var entity = new Contact()
//{
// Email = contactDto.Email,
// FIO = contactDto.FIO,
// IdCompany = contactDto.IdCompany,
// Phone = contactDto.Phone,
// Position = contactDto.Position
//};
db.Contacts.Add(entity);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return entity.Id;
}
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);
}
}
}