Files
Backend-Api/CompanyManagment.EFCore/Repository/RepresentativeRepository.cs
2025-08-06 09:55:33 +03:30

297 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using _0_Framework.Application.Enums;
using System.Threading.Tasks;
using _0_Framework.InfraStructure;
using Company.Domain.RepresentativeAgg;
using CompanyManagment.App.Contracts.PersonalContractingParty;
using CompanyManagment.App.Contracts.Representative;
using Microsoft.EntityFrameworkCore;
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
#region Api
public async Task<ICollection<RepresentativeGetListViewModel>> GetList(RepresentativeGetListSearchModel searchModel)
{
var query = _context.RepresentativeSet.Include(x => x.ContractingParties)
.Where(x => x.FullName != "-");
if (!string.IsNullOrWhiteSpace(searchModel.CompanyNameOrFullName))
query = query.Where(x => x.FullName.Contains(searchModel.CompanyNameOrFullName));
if (!string.IsNullOrWhiteSpace(searchModel.NationalCodeOrNationalId))
query = query.Where(x => x.Nationalcode.Contains(searchModel.NationalCodeOrNationalId)
|| x.NationalId.Contains(searchModel.NationalCodeOrNationalId));
if (!string.IsNullOrWhiteSpace(searchModel.IdNumber))
query = query.Where(x => x.IdNumber.Contains(searchModel.IdNumber));
if (searchModel.ContractingPartyId > 0)
{
query = query.Where(x => x.ContractingParties.Any(c => c.id == searchModel.ContractingPartyId));
}
if (searchModel.RepresentativeType != LegalType.None)
{
string isLegal = searchModel.RepresentativeType switch
{
LegalType.Real => "false",
LegalType.Legal => "true",
_ => ""
};
query = query.Where(x => x.IsLegal == isLegal);
}
if (searchModel.RepresentativeStatus != ActivationStatus.None)
{
string status = searchModel.RepresentativeStatus switch
{
ActivationStatus.Active => "true",
ActivationStatus.DeActive => "false",
_ => ""
};
query = query.Where(x => x.IsActive == status);
}
var result = await query.Skip(searchModel.PageIndex).Take(30)
.Select(x => new RepresentativeGetListViewModel()
{
Id = x.id,
NationalIdOrNationalCode = x.IsLegal == "true" ? x.NationalId : x.Nationalcode,
RealNameOrLegalName = x.FullName,
HasAnyContractingParty = x.ContractingParties.Any(),
RepresentativeStatus = x.IsActive == "false" ? ActivationStatus.DeActive : ActivationStatus.Active,
RepresentativeType = x.IsLegal == "true" ? LegalType.Legal : LegalType.Real,
}).ToListAsync();
return result;
}
public bool HasAnyContractingParty(long id)
{
return _context.RepresentativeSet.Where(x => x.id == id).Include(x => x.ContractingParties)
.Any(x => x.ContractingParties.Any());
}
public async Task<List<GetSelectListRepresentativeViewModel>> GetSelectList()
{
return await _context.RepresentativeSet.Select(x => new GetSelectListRepresentativeViewModel()
{
Id = x.id,
Text = x.FullName
}).ToListAsync();
}
#endregion
}