217 lines
7.7 KiB
C#
217 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.RepresentativeAgg;
|
|
using CompanyManagment.App.Contracts.PersonalContractingParty;
|
|
using CompanyManagment.App.Contracts.Representative;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class RepresentativeRepository : RepositoryBase<long, Representative>, IRepresentativeRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
public RepresentativeRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public List<RepresentativeViewModel> GetRepresentatives()
|
|
{
|
|
return _context.RepresentativeSet.Select(x => new RepresentativeViewModel()
|
|
{
|
|
Id = x.id,
|
|
|
|
//FName = x.FName,
|
|
FullName = x.FullName,
|
|
//Nationalcode = x.Nationalcode,
|
|
//IdNumber = x.IdNumber,
|
|
//RegisterId = x.RegisterId,
|
|
//NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
//Phone = x.Phone,
|
|
//AgentPhone = x.AgentPhone,
|
|
//Address = x.Address
|
|
|
|
}).ToList();
|
|
}
|
|
|
|
public List<PersonalContractingPartyViewModel> GetContractingParties(long id)
|
|
{
|
|
var contractingParty = _context.PersonalContractingParties.Select(x=> new PersonalContractingPartyViewModel()
|
|
{
|
|
|
|
id = x.id,
|
|
IsLegal = x.IsLegal,
|
|
FName = x.FName,
|
|
LName = x.LName,
|
|
FullName = x.FName + " " + x.LName,
|
|
RepresentativeId = x.RepresentativeId,
|
|
SureName = x.SureName
|
|
}).Where(n=>n.RepresentativeId == id).ToList();
|
|
|
|
var result = new List<PersonalContractingPartyViewModel>();
|
|
foreach (var item in contractingParty)
|
|
{
|
|
if (item.IsLegal == "حقوقی")
|
|
{
|
|
item.FullName = item.LName;
|
|
|
|
}
|
|
|
|
result.Add(item);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public EditRepresentative GetDetails(long id)
|
|
{
|
|
return _context.RepresentativeSet.Select(x => new EditRepresentative()
|
|
{
|
|
Id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = x.LName,
|
|
LegalName = x.LegalName,
|
|
FullName = x.FullName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
Phone = x.Phone,
|
|
AgentPhone = x.AgentPhone,
|
|
Address = x.Address
|
|
|
|
})
|
|
.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public List<RepresentativeViewModel> Search(RepresentativeSearchModel searchModel)
|
|
{
|
|
var query = _context.RepresentativeSet.Where(x=>x.FullName != "-").Select(x => new RepresentativeViewModel()
|
|
{
|
|
Id = x.id,
|
|
FName = x.FName,
|
|
LName = x.LName,
|
|
LegalName = x.LegalName,
|
|
FullName = x.FullName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
Phone = x.Phone,
|
|
AgentPhone = x.AgentPhone,
|
|
Address = x.Address,
|
|
IsActive = x.IsActive
|
|
});
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.FullName))
|
|
query = query.Where(x => x.FName.Contains(searchModel.FullName) || x.LName.Contains(searchModel.FullName) || x.LegalName.Contains(searchModel.FullName));
|
|
|
|
//if (!string.IsNullOrWhiteSpace(searchModel.LName))
|
|
// query = query.Where(x => x.LName.Contains(searchModel.LName));
|
|
if (!string.IsNullOrWhiteSpace(searchModel.Nationalcode))
|
|
query = query.Where(x => x.Nationalcode.Contains(searchModel.Nationalcode)||x.NationalId.Contains(searchModel.Nationalcode));
|
|
if (!string.IsNullOrWhiteSpace(searchModel.IdNumber))
|
|
query = query.Where(x => x.IdNumber.Contains(searchModel.IdNumber));
|
|
//if (!string.IsNullOrWhiteSpace(searchModel.RegisterId))
|
|
// query = query.Where(x => x.RegisterId.Contains(searchModel.RegisterId));
|
|
//if (!string.IsNullOrWhiteSpace(searchModel.NationalId))
|
|
// query = query.Where(x => x.NationalId.Contains(searchModel.NationalId));
|
|
|
|
|
|
if (!string.IsNullOrEmpty(searchModel.ContractingPartyName))
|
|
{
|
|
List<long> representativeIds = _context.PersonalContractingParties.Where(x => (x.IsLegal == "حقیقی" &&
|
|
((!string.IsNullOrWhiteSpace(x.FName) && x.FName.StartsWith(searchModel.ContractingPartyName)) ||
|
|
(!string.IsNullOrWhiteSpace(x.LName) &&x.LName.StartsWith(searchModel.ContractingPartyName))))
|
|
|| (x.IsLegal == "حقوقی" && (!string.IsNullOrWhiteSpace(x.LName) && x.LName.Contains(searchModel.ContractingPartyName)))).Select(x=>x.RepresentativeId).ToList();
|
|
|
|
query = query.Where(x => representativeIds.Contains(x.Id));
|
|
}
|
|
|
|
if (searchModel.ContractingPartyId>0)
|
|
{
|
|
List<long> representativeIds = _context.PersonalContractingParties.Where(x => x.id== searchModel.ContractingPartyId).Select(x => x.RepresentativeId).ToList();
|
|
|
|
query = query.Where(x => representativeIds.Contains(x.Id));
|
|
}
|
|
|
|
if (searchModel.IsActive == "false")
|
|
{
|
|
query = query.Where(x => x.IsActive == "false");
|
|
}
|
|
else if (searchModel.IsActive == "both")
|
|
{
|
|
query = query.Where(x => x.IsActive == "false" || x.IsActive == "true");
|
|
}
|
|
else if (searchModel.IsActive == "true")
|
|
{
|
|
query = query.Where(x => x.IsActive == "true");
|
|
}
|
|
|
|
|
|
|
|
if (searchModel.IsLegal == "false")
|
|
{
|
|
query = query.Where(x => x.IsLegal == "false");
|
|
}
|
|
else if (searchModel.IsLegal == "both")
|
|
{
|
|
query = query.Where(x => x.IsLegal == "false" || x.IsLegal == "true");
|
|
}
|
|
else if (searchModel.IsLegal == "true")
|
|
{
|
|
query = query.Where(x => x.IsLegal == "true");
|
|
}
|
|
|
|
|
|
|
|
return query.OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
|
|
|
|
#region NewByHeydari
|
|
public List<RepresentativeViewModel> GetRepresentativeListForSearchText(string searchText)
|
|
{
|
|
var result = _context.RepresentativeSet.Where(x => ( x.IsLegal == "false" && ((!string.IsNullOrEmpty(x.FName) && x.FName.StartsWith(searchText) )
|
|
|| (!string.IsNullOrEmpty(x.LName) && x.LName.StartsWith(searchText)))
|
|
|| (!string.IsNullOrEmpty(x.FullName) && x.FullName.Contains(searchText)))
|
|
|| (x.IsLegal == "true" && (!string.IsNullOrEmpty(x.FullName) && x.FullName.Contains(searchText)))
|
|
)
|
|
.Select(x => new RepresentativeViewModel
|
|
{
|
|
Id = x.id,
|
|
FName = x.FName,
|
|
LName = x.LName,
|
|
IsLegal = x.IsLegal,
|
|
FullName =(x.IsLegal== "true"? x.FullName:(x.FName + " " + x.LName))
|
|
}).Take(100).ToList();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public bool DeleteRepresentative(long id)
|
|
{
|
|
|
|
try
|
|
{
|
|
var representativeList = _context.RepresentativeSet.Where(x => x.id == id).ToList();
|
|
_context.RepresentativeSet.RemoveRange(representativeList);
|
|
|
|
_context.SaveChanges();
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
} |