using AutoMapper; using Complex.Application.Utility; using Complex.Common.Dto; using Complex.Domain.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Complex.Application.PersonService { public interface IPersonService { public ResultDto InsertPerson(PersonDto person); public ResultDto EditPerson(PersonDto person); public ResultDto> GetAllPersons(PersonDto personSearchObj); } public class PersonService : IPersonService { private readonly IComplexDBContext _complexDBContext; private IMapper _mapper; public PersonService(IComplexDBContext complexDBContext, IMapper mapper) { _complexDBContext = complexDBContext; _mapper = mapper; } public ResultDto> GetAllPersons(PersonDto personSearchObj) { var persons = _complexDBContext.ComplexPersons.Where(u => u.ComplexId == personSearchObj.ComplexId).Select(cp => cp.PersonMobileNumber); //در تمام فیلدها با شماره تلفن میتوان جستجو کرد if (personSearchObj.MobileNumber.IsNotNullOrEmpty()) { persons = persons.Where(p => p.Id.Contains(personSearchObj.MobileNumber)); persons = persons.Where(p => p.FirstName.Contains(personSearchObj.MobileNumber)); persons = persons.Where(p => p.FirstName.Contains(personSearchObj.MobileNumber)); } //if (personSearchObj.FirstName.IsNotNullOrEmpty()) // persons = persons.Where(p => p.FirstName.Contains(personSearchObj.FirstName)); //if (personSearchObj.LastName.IsNotNullOrEmpty()) // persons = persons.Where(p => p.LastName.Contains(personSearchObj.LastName)); //var persons = _complexDBContext.Units.Where(u => u.ComplexId == personSearchObj.ComplexId).Select(p => p.Tenant); //persons = persons.Union(_complexDBContext.Units.Where(u => u.ComplexId == personSearchObj.ComplexId).Select(p => p.Owner)); //if (personSearchObj.MobileNumber.IsNotNullOrEmpty()) // persons = persons.Where(p => p.Id.Contains(personSearchObj.MobileNumber)); //var personResult = persons.Distinct().ToList(); return new ResultDto> { Data = persons.Select(p => _mapper.Map(p)).ToList(), IsSuccess = true }; } public ResultDto EditPerson(PersonDto person) { throw new NotImplementedException(); } public ResultDto InsertPerson(PersonDto personDto) { var newPerson = _mapper.Map(personDto); var isExist = _complexDBContext.Persons.Any(p => p.Id == personDto.MobileNumber); if (!isExist) { newPerson.Id = personDto.MobileNumber; _complexDBContext.Persons.Add(newPerson); _complexDBContext.SaveChanges(); } _complexDBContext.ComplexPersons.Add(new ComplexPerson { ComplexId = personDto.ComplexId.Value, PersonMobileNumberId = personDto.MobileNumber, }); return new ResultDto { Data = newPerson.Id, IsSuccess = true }; } } public class PersonDto { public Guid? ComplexId { set; get; } public string? MobileNumber { set; get; } public string? FirstName { get; set; } public string? LastName { get; set; } public byte? Gender { set; get; } //public int? MobileCode { set; get; } public byte[]? Avatar { set; get; } } }