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;
|
2023-10-09 12:20:00 +05:00
|
|
|
|
using System;
|
2023-06-21 11:44:04 +05:00
|
|
|
|
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)
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
|
|
|
|
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()
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
|
|
|
|
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>())
|
2023-06-21 11:44:04 +05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var entities = await query.AsNoTracking()
|
|
|
|
|
.ToArrayAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 10:51:38 +05:00
|
|
|
|
public async Task<IEnumerable<CompanyTypeDto>> GetTypesAsync(int idWell, CancellationToken token)
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
2023-07-13 10:51:38 +05:00
|
|
|
|
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);
|
2023-06-21 11:44:04 +05:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 11:44:04 +05:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|