Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 kB
4
Indexable
Never
        /// <summary>
        /// Metodo que devuelve las relaciones con personas de contacto en un negocio
        /// </summary>
        /// <param name="companyId">Listado de compañías</param>
        /// <param name="brandId">Negocio de la oficina</param>
        /// <param name="authorization">JWT de la peticion</param>
        /// <returns>Lista de relaciones con personas de contacto</returns>
        public async Task<List<ContactPersonRelModel>> GetContactPersonRels(int contactPersonId, List<string> companyId, string brandId, string authorization)
        {
            // Variables LOG
            object[] parameters = { contactPersonId, companyId != null ? string.Join("-", companyId) : null, brandId, authorization };
            string messageLog = "obtiene las relaciones con personas de contacto en un negocio";

            logger.Debug("Inicio - " + messageLog, parameters);

            var modelList = new List<ContactPersonRelModel>();
            var profCompany = companyId.ToList();
            Dictionary<string, RolModel> gestorRol = new Dictionary<string, RolModel>();
            List<Domain.Entities.ClientsModel.PositionInterlocFBI> positionFBIList = new List<Domain.Entities.ClientsModel.PositionInterlocFBI>();


            var queryRelsGroups = unitOfWork.ContactPersonRelsRepository.Fetch()
                .Where(x => x.ActiveChk == GlobalId.Active && x.TypeId == ContactPersonRelType.Group && x.ContactPersonId == contactPersonId
                    && x.ClientGroup.Client.Any(y => y.ClientsCenters.Any(z => profCompany.Contains(z.Branch.CompanyId))));

            var queryRelsClients = unitOfWork.ContactPersonRelsRepository.Fetch()
                .Where(x => x.ActiveChk == GlobalId.Active && x.TypeId == ContactPersonRelType.Client && x.ContactPersonId == contactPersonId
                    && x.Client.ClientsCenters.Any(y => profCompany.Contains(y.Branch.CompanyId)));

            var queryRelsClientCenters = unitOfWork.ContactPersonRelsRepository.Fetch()
                .Where(x => profCompany.Contains(x.ClientCenter.Branch.CompanyId) && x.ActiveChk == GlobalId.Active
                    && x.ContactPersonId == contactPersonId && x.TypeId == ContactPersonRelType.Center);

            var rels = await queryRelsGroups.Union(queryRelsClients).Union(queryRelsClientCenters)
                .Include(x => x.Client).ThenInclude(y => y.ClientsCenters)
                .Include(x => x.User).ThenInclude(q => q.Employee)
                .Include(x => x.User).ThenInclude(q => q.Branch)
                .Include(x => x.MailingTypes)
                .Include(x => x.ContactTypes)
                .Include(x => x.ContactPersonRol)
                .Include(x => x.ClientCentersCenterCost)
                .Include(x => x.ClientGroup)
                .Include(x => x.Client)
                .Include(x => x.ClientCenter).ToListAsync();

            foreach (var rel in rels)
            {
                var positionIdList = new List<string>();

                if (rel.User.CompanyId == CompanyId.RWS)
                {
                    foreach (var contact in rel.ContactPersonRol.Where(x => x.DeletedId == GlobalId.NotDeletedId))
                    {
                        var currentPositionFBI = positionFBIList.Where(x => x.PositionId == contact.PositionId
                            && x.PCompanyId == contact.PCompanyId && x.ProductId == contact.ProductId).Any();

                        if (!positionIdList.Contains(contact.PositionId) && !currentPositionFBI)
                        {
                            positionIdList.Add(contact.PositionId);
                        }
                    }
                }
                else
                {
                    var currentPositionFBI = positionFBIList.Where(x => x.PositionId == rel.PositionInterlocFBIId).Any();

                    if (!currentPositionFBI)
                    {
                        positionIdList.Add(rel.PositionInterlocFBIId);
                    }
                }

                if (positionIdList.Any())
                {
                    var positionFBI = await unitOfWork.PositionInterlocFBIRepository.Fetch().Where(q => positionIdList.Contains(q.PositionId) && q.DeletedId == GlobalId.NotDeletedId).ToListAsync();
                    positionFBIList.AddRange(positionFBI);
                }

                //Nombre Compañia
                string companyName = string.Empty;

                switch (rel.User.CompanyId)
                {
                    case "RWS":
                        {
                            companyName = "Staffing";
                            break;
                        }
                    case "RPS":
                    case "VEX":
                        {
                            companyName = "Outsourcing";
                            break;
                        }
                    case "RCO":
                        {
                            companyName = brandId == rel.User.Branch.BrandId ? "Proffesionals" : "";
                            break;
                        }
                    case "EXP":
                        {
                            companyName = brandId == rel.User.Branch.BrandId ? "Proffesionals" : "Technologies";
                            break;
                        }
                    case "FUN":
                        {
                            companyName = "Fundación Randstad";
                            break;
                        }
                }

                //Última visita
                Visit lastVisit = null;

                if (rel.User.CompanyId == CompanyId.RWS || rel.User.CompanyId == CompanyId.FUN)
                {
                    lastVisit = await unitOfWork.VisitRepository.Fetch()
                        .Where(x => x.ContactPersonId == contactPersonId &&
                            x.ClientCenter.ClientId == rel.ClientId && x.ClientCenter.Branch.BrandId == rel.User.Branch.BrandId &&
                            (string.IsNullOrEmpty(x.ActivityId) ||
                            x.ActivityId == ClientCenterActivity.PhysicalVisit ||
                            x.ActivityId == ClientCenterActivity.DigitalVisit ||
                            x.ActivityId == ClientCenterActivity.PhoneVisit) &&
                            x.StatusId == ClientCenterActivityStatus.RealizedId)
                        .OrderByDescending(x => x.VisitDate)
                        .FirstOrDefaultAsync();
                }
                else
                {
                    lastVisit = await unitOfWork.VisitRepository.Fetch()
                        .Where(x => x.ContactPersonId == contactPersonId &&
                            x.ClientCenter.ClientId == rel.ClientId && x.ClientCenter.Branch.BrandId == rel.User.Branch.BrandId &&
                            (string.IsNullOrEmpty(x.ActivityId) ||
                            x.ActivityId == ClientCenterActivity.PhysicalVisit ||
                            x.ActivityId == ClientCenterActivity.DigitalVisit)
                            && x.StatusId == ClientCenterActivityStatus.RealizedId)
                        .OrderByDescending(x => x.VisitDate)
                        .FirstOrDefaultAsync();
                }

                var model = new ContactPersonRelModel()
                {
                    ContactPersonId = rel.ContactPersonId,
                    ContactPersonRelId = rel.ContactPersonRelId,
                    UserId = rel.UserId,
                    EmployeeId = rel.User.EmployeeId,
                    AllName = $"{rel.User.Employee.FirstName} {rel.User.Employee.Surname1} {rel.User.Employee.Surname2}",
                    FiscalDocNumber = rel.User.Employee.FiscalDocNumber,
                    ContactTypeId = rel.ContactTypeId,
                    ContactTypeDescription = rel.ContactTypes?.Description,
                    MailingTypeId = rel.MailingTypeId,
                    MailingTypeDescription = string.Empty,
                    ShouldReceiveSurveyChk = rel.ShouldReceiveSurveyChk.HasValue ? rel.ShouldReceiveSurveyChk.Value : GlobalId.Inactive,
                    WantToReceiveASurveyChk = rel.WantToReceiveASurveyChk.HasValue ? rel.WantToReceiveASurveyChk.Value : GlobalId.Inactive,
                    CenterCostSeq = rel.CenterCostSeq,
                    CenterCostSeqDescription = rel.ClientCentersCenterCost?.Description,
                    PositionInterlocFBIId = rel.PositionInterlocFBIId,
                    Branch = rel.User.Branch.Name,
                    Company = companyName.ToUpper(),
                    LastVisitDate = lastVisit != null ? (lastVisit.VisitDate.HasValue ? lastVisit.VisitDate.Value.ToString("dd/MM/yyyy") : string.Empty) : string.Empty,
                    OrderVisitDate = lastVisit != null ? (lastVisit.VisitDate.HasValue ? lastVisit.VisitDate : DateTime.Today.AddYears(-10)) : DateTime.Today,
                    CompanyId = rel.User.CompanyId,
                    RelationTypeDesc = (rel.TypeId == ContactPersonRelType.Group ? "Grupo" : (rel.TypeId == ContactPersonRelType.Client ? "Cliente" : (rel.TypeId == ContactPersonRelType.Center ? "Centro" : ""))),
                    RelationValueId = $"{(rel.TypeId == ContactPersonRelType.Group ? rel.GroupId : (rel.TypeId == ContactPersonRelType.Client ? rel.ClientId : rel.ClientId + "." + rel.ClientCenterId))}",
                    RelationValueDesc = (rel.TypeId == ContactPersonRelType.Group ? rel.ClientGroup?.GroupName : (rel.TypeId == ContactPersonRelType.Client ? rel.Client?.ShortName : rel.ClientCenter?.CenterName)),
                };

                if (rel.User.CompanyId == CompanyId.RCO || rel.User.CompanyId == CompanyId.EXP)
                {
                    model.MailingTypeDescription = rel.MailingTypes?.Description;
                }

                if (rel.User.CompanyId == CompanyId.RWS)
                {
                    var posIdList = rel.ContactPersonRol.Where(x => x.DeletedId == GlobalId.NotDeletedId).Select(x => x.PositionId).ToList();

                    var currentPositionFBI = positionFBIList.Where(x => posIdList.Contains(x.PositionId)
                        && rel.ContactPersonRol.Any(c => c.PCompanyId == x.PCompanyId && c.ProductId == x.ProductId)).ToList();

                    model.PositionInterlocFBIIdList = new List<Tuple<string, string>>();
                    model.PositionDescription = string.Join(", ", currentPositionFBI.Select(x => x.Description));

                    foreach (var rol in rel.ContactPersonRol.Where(x => x.DeletedId == GlobalId.NotDeletedId))
                    {
                        var current = currentPositionFBI.Where(x => x.PositionId == rol.PositionId).FirstOrDefault();
                        model.PositionInterlocFBIIdList.Add(Tuple.Create(rol.PositionId, current?.Description));
                    }
                }
                else
                {
                    var currentPositionFBI = positionFBIList.Where(x => x.PositionId == rel.PositionInterlocFBIId).FirstOrDefault();

                    model.PositionDescription = currentPositionFBI?.Description;
                }

                modelList = modelList.OrderByDescending(x => x.OrderVisitDate).ThenBy(x => x.Company).ToList();

                modelList.Add(model);
            }

            logger.Debug("Fin - " + messageLog, parameters);

            return modelList;
        }