121 lines
4.7 KiB
C#
121 lines
4.7 KiB
C#
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<string> InsertPerson(PersonDto person);
|
|
public ResultDto<bool> EditPerson(PersonDto person);
|
|
public ResultDto<List<PersonDto>> GetAllPersons(PersonDto personSearchObj);
|
|
public ResultDto<List<PersonDto>> SearchPersons(string? searchTerm);
|
|
}
|
|
public class PersonService : IPersonService
|
|
{
|
|
private readonly IComplexDBContext _complexDBContext;
|
|
private IMapper _mapper;
|
|
public PersonService(IComplexDBContext complexDBContext, IMapper mapper)
|
|
{
|
|
_complexDBContext = complexDBContext;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public ResultDto<List<PersonDto>> SearchPersons(string? searchTerm)
|
|
{
|
|
var query = _complexDBContext.Persons.AsQueryable();
|
|
if (!string.IsNullOrEmpty(searchTerm))
|
|
{
|
|
query = query.Where(p =>
|
|
p.FirstName.Contains(searchTerm) ||
|
|
p.LastName.Contains(searchTerm) ||
|
|
p.Id.Contains(searchTerm));
|
|
}
|
|
var result = query.Select(p => new PersonDto
|
|
{
|
|
MobileNumber = p.Id,
|
|
FirstName = p.FirstName,
|
|
LastName = p.LastName,
|
|
Gender = p.Gender,
|
|
Avatar = p.Avatar
|
|
}).ToList();
|
|
|
|
return new ResultDto<List<PersonDto>>
|
|
{
|
|
Data = result,
|
|
IsSuccess = true,
|
|
RowCount = result.Count
|
|
};
|
|
}
|
|
|
|
public ResultDto<List<PersonDto>> 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<List<PersonDto>>
|
|
{
|
|
Data = persons.Select(p => _mapper.Map<PersonDto>(p)).ToList(),
|
|
IsSuccess = true
|
|
};
|
|
}
|
|
|
|
public ResultDto<bool> EditPerson(PersonDto person)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public ResultDto<string> InsertPerson(PersonDto personDto)
|
|
{
|
|
var newPerson = _mapper.Map<Person>(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<string> { 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; }
|
|
}
|
|
}
|