Ejemplo n.º 1
0
        public ActionResult Search([FromBody] SearchPartnerDTO searchPartnerDTO)
        {
            if (searchPartnerDTO == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            List <string> regions = new List <string>();
            List <PartnerCoursePackage>   partnerCoursePackages   = new List <PartnerCoursePackage>();
            List <PartnerCenterInfo>      partnerCenterInfos      = new List <PartnerCenterInfo>();
            List <CRMPartnerDTO>          crmPartnerDTOs          = new List <CRMPartnerDTO>();
            List <SearchPartnerResultDTO> searchPartnerResultDTOs = new List <SearchPartnerResultDTO>();
            List <int> partnerIdList = new List <int>();

            try
            {
                // Note : here performance hit is there as we are loading first data based on partnertype
                // then in memory we are doing filtring based on regions
                // as regions are stored as ; seperated value
                // TODO: In future need to come up with better appraoch to avoid split and triming
                searchPartnerDTO.PartnerType = searchPartnerDTO.PartnerType.Select(x => x.Trim().ToLower()).ToList();

                foreach (var regionId in searchPartnerDTO.RegionIds)
                {
                    var regionInDb = _choiceRepository.GetById <Region>(regionId);
                    if (regionInDb != null && !string.IsNullOrWhiteSpace(regionInDb.Name))
                    {
                        regions.Add(regionInDb.Name.Trim().ToLower());
                    }
                }

                if (searchPartnerDTO.CoursePackageIds.Any())
                {
                    //Step 1: If coursepackageid is provided then we have to first retreive all the associated partnercourspackage
                    // for that course package id which are offered.

                    IList <int> removeCrmPartnerIds = new List <int>();

                    // First retrieve all crm partner from db which offers the corrosponding course package
                    var tempPartnerCoursePackageList = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                       .Where(x => searchPartnerDTO.CoursePackageIds.Contains(x.CoursePackageId) &&
                                                              x.Offered != null && x.Offered == true);

                    // Now grioup by CrmPartnerId and find any partner who doesn't support all the coursepackageids
                    // which are provided as input
                    foreach (var tempPartnerCoursePackage in tempPartnerCoursePackageList.GroupBy(x => x.CRMPartnerId))
                    {
                        var coursePackageIdsSupportedByPartner = tempPartnerCoursePackage.Select(x => x.CoursePackageId);

                        //var r = searchPartnerDTO.CoursePackageIds.Except(coursePackageIdsSupportedByPartner);

                        if (searchPartnerDTO.CoursePackageIds.Except(coursePackageIdsSupportedByPartner).Count() != 0)
                        {
                            removeCrmPartnerIds.Add(tempPartnerCoursePackage.Key);
                        }
                    }

                    // Now remove the crm partner from the tempPartnerCoursePackageList based on above selection
                    var partnerCoursePackageList = tempPartnerCoursePackageList
                                                   .Where(x => !removeCrmPartnerIds.Contains(x.CRMPartnerId))
                                                   .DistinctBy(a => a.CRMPartnerId);


                    //var partnerCoursePackageList = _choiceRepository.GetAll<PartnerCoursePackage>()
                    //           .Where(x => searchPartnerDTO.CoursePackageIds.Any(a=> a == x.CoursePackageId)
                    //            && x.Offered != null && x.Offered == true) // Offered is always true
                    //           .DistinctBy(p => p.CRMPartnerId)
                    //           .ToList();

                    //Step 2: Now for each and every partnercoursepackage selected above
                    //check their partnertype and regions are they matching then store it in a partnerCoursePackages list.
                    foreach (var partnerCoursePackage in partnerCoursePackageList)
                    {
                        if (searchPartnerDTO.PartnerType.Any() && searchPartnerDTO.RegionIds.Any())
                        {
                            var crmPartner = _choiceRepository
                                             .GetById <CRMPartner>
                                                 (x => x.CRMPartnerId == partnerCoursePackage.CRMPartnerId &&
                                                 searchPartnerDTO.PartnerType.Any(a => a == x.Partnertype.Trim().ToLower()));

                            if (crmPartner != null && !string.IsNullOrWhiteSpace(crmPartner.Regions))
                            {
                                List <string> regionsArrayInDb = crmPartner.Regions.Split(';').Select(x => x.Trim().ToLower()).ToList();
                                if (regionsArrayInDb != null && regions.Intersect(regionsArrayInDb).Count() > 0)
                                {
                                    partnerCoursePackages.Add(partnerCoursePackage);
                                }
                            }
                        }
                        else if (searchPartnerDTO.PartnerType.Any())
                        {
                            var crmPartner = _choiceRepository
                                             .GetById <CRMPartner>
                                                 (x => x.CRMPartnerId == partnerCoursePackage.CRMPartnerId &&
                                                 searchPartnerDTO.PartnerType.Any(a => a == x.Partnertype.Trim().ToLower()));

                            if (crmPartner != null)
                            {
                                partnerCoursePackages.Add(partnerCoursePackage);
                            }
                        }
                        else if (searchPartnerDTO.RegionIds.Any())
                        {
                            var crmPartner = _choiceRepository
                                             .GetById <CRMPartner>
                                                 (x => x.CRMPartnerId == partnerCoursePackage.CRMPartnerId);

                            if (crmPartner != null)
                            {
                                List <string> regionsArrayInDb = crmPartner.Regions.Split(';').Select(x => x.Trim().ToLower()).ToList();
                                if (regionsArrayInDb != null && regions.Intersect(regionsArrayInDb).Count() > 0)
                                {
                                    partnerCoursePackages.Add(partnerCoursePackage);
                                }
                            }
                        }
                        else
                        {
                            partnerCoursePackages.Add(partnerCoursePackage);
                        }
                    }
                }

                //Else Step 1: If coursepackageid is not provided. Get CRMPartner first based on partner type and regions.
                else
                {
                    if (searchPartnerDTO.PartnerType.Any() && searchPartnerDTO.RegionIds.Any())
                    {
                        var crmPartnersInDb = _choiceRepository
                                              .GetAll <CRMPartner>(x => searchPartnerDTO.PartnerType.Contains(x.Partnertype.ToLower()))
                                              .ToList();

                        //Step2 : Gets the crm partner course package for the crmpartners which are filtered above in step 1
                        // Note: we are querying the data for all available partnercoursepackages
                        foreach (var crmPartner in crmPartnersInDb)
                        {
                            if (!string.IsNullOrWhiteSpace(crmPartner.Regions))
                            {
                                List <string> regionsArrayInDb = crmPartner.Regions.Split(';').Select(x => x.Trim().ToLower()).ToList();
                                if (regionsArrayInDb != null && regions.Intersect(regionsArrayInDb).Count() > 0)
                                {
                                    // NOTE: We retrieve the partner if any one of the course package is offered by him
                                    var partnerCoursePackage = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                               .Where(x => x.CRMPartnerId == crmPartner.CRMPartnerId &&
                                                                      x.Offered != null && x.Offered == true)
                                                               .FirstOrDefault();

                                    if (partnerCoursePackage != null)
                                    {
                                        partnerCoursePackages.Add(partnerCoursePackage);
                                    }
                                }
                            }
                        }
                    }
                    else if (searchPartnerDTO.PartnerType.Any())
                    {
                        var crmPartnersInDb = _choiceRepository
                                              .GetAll <CRMPartner>(x => searchPartnerDTO.PartnerType.Contains(x.Partnertype.ToLower()))
                                              .ToList();

                        foreach (var crmPartner in crmPartnersInDb)
                        {
                            var partnerCoursePackage = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                       .Where(x => x.CRMPartnerId == crmPartner.CRMPartnerId &&
                                                              x.Offered != null && x.Offered == true)
                                                       .FirstOrDefault();

                            if (partnerCoursePackage != null)
                            {
                                partnerCoursePackages.Add(partnerCoursePackage);
                            }
                        }
                    }
                    else if (searchPartnerDTO.RegionIds.Any())
                    {
                        var crmPartnersInDb = _choiceRepository
                                              .GetAll <CRMPartner>(x => !string.IsNullOrWhiteSpace(x.Regions));


                        foreach (var crmPartner in crmPartnersInDb)
                        {
                            if (!string.IsNullOrWhiteSpace(crmPartner.Regions))
                            {
                                List <string> regionsArrayInDb = crmPartner.Regions.Split(';').Select(x => x.Trim().ToLower()).ToList();
                                if (regionsArrayInDb != null && regions.Intersect(regionsArrayInDb).Count() > 0)
                                {
                                    // NOTE: We retrieve the partner if any one of the course package is offered by him
                                    var partnerCoursePackage = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                               .Where(x => x.CRMPartnerId == crmPartner.CRMPartnerId &&
                                                                      x.Offered != null && x.Offered == true)
                                                               .FirstOrDefault();

                                    if (partnerCoursePackage != null)
                                    {
                                        partnerCoursePackages.Add(partnerCoursePackage);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var crmPartnersInDb = _choiceRepository.DbContext.CRMPartner;

                        foreach (var crmPartner in crmPartnersInDb)
                        {
                            var partnerCoursePackage = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                       .Where(x => x.CRMPartnerId == crmPartner.CRMPartnerId &&
                                                              x.Offered != null && x.Offered == true)
                                                       .FirstOrDefault();

                            if (partnerCoursePackage != null)
                            {
                                partnerCoursePackages.Add(partnerCoursePackage);
                            }
                        }
                    }
                }


                //Step3 : check wether the partnerCenterInfo provides these ameneties
                foreach (var partnerCoursePackage in partnerCoursePackages)
                {
                    var query = _choiceRepository.GetAll <PartnerCenterInfo>()
                                .Where(x => x.CRMPartnerId == partnerCoursePackage.CRMPartnerId).AsQueryable();


                    // Avoid all this if we make a chnage in db value to not nullable and put -1 instead as default value
                    if (searchPartnerDTO.StateAgreement != null)
                    {
                        query = query.Where(GetDynamicLinqExpressionQueryForBoolean("StateAgreement", searchPartnerDTO.StateAgreement.Value));
                    }
                    if (searchPartnerDTO.TotalRooms != null)
                    {
                        query = query.Where(GetDynamicLinqExpressionQueryForInt("TotalRooms", searchPartnerDTO.TotalRooms.Value));
                    }
                    if (searchPartnerDTO.GroupRooms != null)
                    {
                        query = query.Where(GetDynamicLinqExpressionQueryForInt("GroupRooms", searchPartnerDTO.GroupRooms.Value));
                    }
                    if (searchPartnerDTO.MaxSpaceAtRowOfChairs != null)
                    {
                        query = query.Where(GetDynamicLinqExpressionQueryForInt("MaxSpaceAtRowOfChairs", searchPartnerDTO.GroupRooms.Value));
                    }
                    if (searchPartnerDTO.MaxSpaceAtRowOfChairs != null)
                    {
                        query = query.Where(GetDynamicLinqExpressionQueryForInt("MaxSpaceAtRowOfChairs", searchPartnerDTO.GroupRooms.Value));
                    }

                    foreach (var refereshmentItemId in searchPartnerDTO.RefreshmentList)
                    {
                        var d = _choiceRepository.GetRefreshments();
                        var refereshmentEntryInDb = _choiceRepository.GetById <Refreshment>(refereshmentItemId);
                        if (refereshmentEntryInDb != null)
                        {
                            var refereshmentName = refereshmentEntryInDb.Name;
                            query = query.Where(GetDynamicLinqExpressionQueryForBoolean(refereshmentName));
                        }
                        else
                        {
                            //No entry found for the corrosponding refreshment id
                            query = null;
                        }
                    }

                    var res = query?.ToList();

                    if (res != null && res.Any())
                    {
                        partnerCenterInfos.AddRange(res);
                    }
                }

                //Step4 : Prepare the final list of crm partner dto
                foreach (var partnerCenterInfo in partnerCenterInfos)
                {
                    SearchPartnerResultDTO searchPartnerResultDTO =
                        new SearchPartnerResultDTO();

                    // Get partner Name
                    var crmPartner = _choiceRepository.GetById <CRMPartner>(x => x.CRMPartnerId == partnerCenterInfo.CRMPartnerId);
                    //CRMPartnerDTO cRMPartnerDTO = _mapper.Map<CRMPartner, CRMPartnerDTO>(crmPartner);
                    //crmPartnerDTOs.Add(cRMPartnerDTO);
                    searchPartnerResultDTO.PartnerName = string.Format("{0} ({1})", crmPartner.PartnerName, crmPartner.Partnertype);

                    // Get PartnerCoursePackageInfo

                    if (searchPartnerDTO.CoursePackageIds.Any())
                    {
                        foreach (var item in partnerCoursePackages)
                        {
                            if (item.CRMPartnerId == partnerCenterInfo.CRMPartnerId)
                            {
                                var partnerCoursePackageList =
                                    _choiceRepository.GetAll <PartnerCoursePackage>
                                        (x => x.CRMPartnerId == item.CRMPartnerId &&
                                        searchPartnerDTO.CoursePackageIds.Contains(x.CoursePackageId))
                                    .ToList();

                                var partnerCoursePackage =
                                    _mapper.Map <List <PartnerCoursePackage>, List <PartnerCoursePackageDTO> >(partnerCoursePackageList);
                                foreach (var PartnerCourse in partnerCoursePackage)
                                {
                                    var course = _choiceRepository.GetCoursePackage().Where(x => x.CoursePackageId == PartnerCourse.CoursePackageId).FirstOrDefault();
                                    if (course != null)
                                    {
                                        PartnerCourse.CoursePackageName   = course.CoursePackageName;
                                        PartnerCourse.CoursePackageNameEN = course.CoursePackageNameEN;
                                    }
                                }

                                searchPartnerResultDTO.PartnerCoursePackageDTOs.AddRange(partnerCoursePackage);
                            }
                        }
                    }
                    else
                    {
                        var partnerCoursePackagesInDb = _choiceRepository.GetAll <PartnerCoursePackage>()
                                                        .Where(x => x.CRMPartnerId == crmPartner.CRMPartnerId &&
                                                               x.Offered != null && x.Offered == true);

                        foreach (var item in partnerCoursePackagesInDb)
                        {
                            var partnerCoursePackage =
                                _mapper.Map <PartnerCoursePackage, PartnerCoursePackageDTO>(item);

                            searchPartnerResultDTO.PartnerCoursePackageDTOs.Add(partnerCoursePackage);
                        }
                    }

                    PartnerCenterInfoDTO partnerCenterInfoDTO =
                        _mapper.Map <PartnerCenterInfo, PartnerCenterInfoDTO>(partnerCenterInfo);

                    searchPartnerResultDTO.PartnerCenterInfoDTO = partnerCenterInfoDTO;

                    searchPartnerResultDTOs.Add(searchPartnerResultDTO);
                }

                //// now based on final list of crm partner dto prepare PartnerCoursePackage and PartnerEmployee
                //foreach (var crmPartner in crmPartnerDTOs)
                //{
                //    SearchPartnerResultDTO searchPartnerResultDTO = new SearchPartnerResultDTO();
                //    searchPartnerResultDTO.CRMPartnerDTO = crmPartner;

                //    var partnerCoursePackageInDb = _choiceRepository.GetAllPartnerCoursePackagesByCrmPartnerId(crmPartner.CRMPartnerId);
                //    var partnerCoursePackageList = _mapper.Map<List<PartnerCoursePackage>, List<PartnerCoursePackageDTO>>(partnerCoursePackageInDb);
                //    var partnerEmployeeInDb = _choiceRepository.GetAll<PartnerEmployee>(x => x.CRMPartnerId == crmPartner.CRMPartnerId).ToList();
                //    var partnerEmployeeList = _mapper.Map<List<PartnerEmployee>, List<PartnerEmployeeDTO>>(partnerEmployeeInDb);
                //    //partnerEmployeeDTOs.AddRange(partnerEmployeeList);

                //    searchPartnerResultDTO.PartnerCoursePackageDtos.AddRange(partnerCoursePackageList);
                //    searchPartnerResultDTO.PartnerEmployeeDTOs.AddRange(partnerEmployeeList);
                //    searchPartnerResultDTOs.Add(searchPartnerResultDTO);
                //}
            }
            catch (Exception ex)
            {
                // log it here in future add appropriate message
            }

            return(Ok(searchPartnerResultDTOs));
        }
Ejemplo n.º 2
0
        // [Route("UpdatePartnerCenterInfo/{PartnerCenterInfoId:int}")]
        public IActionResult UpdatePartnerCenterInfo(int PartnerCenterInfoId, [FromBody] PartnerCenterInfoDTO partnerCenterInfoDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (partnerCenterInfoDTO == null)
            {
                return(BadRequest());
            }

            var objPartnerCenterInfo = _choiceRepoistory.GetById <PartnerCenterInfo>(c => c.PartnerCenterInfoId == PartnerCenterInfoId);

            if (objPartnerCenterInfo == null)
            {
                return(BadRequest());
            }

            //objPartnerCenterInfo.PartnerCenterInfoId = partnerCenterInfoDTO.PartnerCenterInfoId;
            objPartnerCenterInfo.CRMPartnerId    = partnerCenterInfoDTO.CRMPartnerId;
            objPartnerCenterInfo.ContentStatusId = partnerCenterInfoDTO.ContentStatusId;
            objPartnerCenterInfo.TotalRooms      = partnerCenterInfoDTO.TotalRooms;
            objPartnerCenterInfo.GroupRooms      = partnerCenterInfoDTO.GroupRooms;
            // objPartnerCenterInfo.MaxSpaceAtRowOfChairs = partnerCenterInfoDTO.MaxSpaceAtRowOfChairs;
            //objPartnerCenterInfo.MaxspaceAtTables = partnerCenterInfoDTO.MaxspaceAtTables;
            objPartnerCenterInfo.StateAgreement       = partnerCenterInfoDTO.StateAgreement;
            objPartnerCenterInfo.MaxAccommodation     = partnerCenterInfoDTO.MaxAccommodation;
            objPartnerCenterInfo.PartnerCenfoInfoSPId = partnerCenterInfoDTO.PartnerCenfoInfoSPId;
            objPartnerCenterInfo.NumberOfSingleRooms  = partnerCenterInfoDTO.NumberOfSingleRooms;
            objPartnerCenterInfo.NumberOfDoubleRooms  = partnerCenterInfoDTO.NumberOfDoubleRooms;
            objPartnerCenterInfo.Suite = partnerCenterInfoDTO.Suite;
            objPartnerCenterInfo.DistanceToAddtiionalAccommodation = partnerCenterInfoDTO.DistanceToAddtiionalAccommodation;
            objPartnerCenterInfo.Chamber                     = partnerCenterInfoDTO.Chamber;
            objPartnerCenterInfo.HandicapRooms               = partnerCenterInfoDTO.HandicapRooms;
            objPartnerCenterInfo.MaximumNumberOfVisitors     = partnerCenterInfoDTO.MaximumNumberOfVisitors;
            objPartnerCenterInfo.MaxDiningPlacesInRestaurant = partnerCenterInfoDTO.MaxDiningPlacesInRestaurant;
            objPartnerCenterInfo.MaxDiningPlacesInRoom       = partnerCenterInfoDTO.MaxDiningPlacesInRoom;
            objPartnerCenterInfo.MaxSpaceInAuditorium        = partnerCenterInfoDTO.MaxSpaceInAuditorium;
            objPartnerCenterInfo.MinParticipants             = partnerCenterInfoDTO.MinParticipants;
            objPartnerCenterInfo.AirportDistance             = partnerCenterInfoDTO.AirportDistance;
            objPartnerCenterInfo.StationDdistance            = partnerCenterInfoDTO.StationDdistance;
            objPartnerCenterInfo.DistanceToBus               = partnerCenterInfoDTO.DistanceToBus;
            objPartnerCenterInfo.DistanceToMotorway          = partnerCenterInfoDTO.DistanceToMotorway;
            objPartnerCenterInfo.NumberOfFreeParkingSpaces   = partnerCenterInfoDTO.NumberOfFreeParkingSpaces;
            objPartnerCenterInfo.DistanceToFreeParking       = partnerCenterInfoDTO.DistanceToFreeParking;
            objPartnerCenterInfo.NumberOfParkingSpaces       = partnerCenterInfoDTO.NumberOfParkingSpaces;
            objPartnerCenterInfo.DistanceToPayParking        = partnerCenterInfoDTO.DistanceToPayParking;
            objPartnerCenterInfo.EnvironmentalCertificate    = partnerCenterInfoDTO.EnvironmentalCertificate;
            objPartnerCenterInfo.AgreementForEmployees       = partnerCenterInfoDTO.AgreementForEmployees;
            objPartnerCenterInfo.Handicapfriendly            = partnerCenterInfoDTO.Handicapfriendly;
            objPartnerCenterInfo.RegionsAgreement            = partnerCenterInfoDTO.RegionsAgreement;
            objPartnerCenterInfo.Bar    = partnerCenterInfoDTO.Bar;
            objPartnerCenterInfo.Lounge = partnerCenterInfoDTO.Lounge;
            //objPartnerCenterInfo.BilliardsDartTableTennis = partnerCenterInfoDTO.BilliardsDartTableTennis;
            objPartnerCenterInfo.Spa                 = partnerCenterInfoDTO.Spa;
            objPartnerCenterInfo.Pool                = partnerCenterInfoDTO.Pool;
            objPartnerCenterInfo.FitnessRoom         = partnerCenterInfoDTO.FitnessRoom;
            objPartnerCenterInfo.Casino              = partnerCenterInfoDTO.Casino;
            objPartnerCenterInfo.DiningArea          = partnerCenterInfoDTO.DiningArea;
            objPartnerCenterInfo.GreenArea           = partnerCenterInfoDTO.GreenArea;
            objPartnerCenterInfo.Golf                = partnerCenterInfoDTO.Golf;
            objPartnerCenterInfo.AirCondition        = partnerCenterInfoDTO.AirCondition;
            objPartnerCenterInfo.CookingSchool       = partnerCenterInfoDTO.CookingSchool;
            objPartnerCenterInfo.NoOfRooms           = partnerCenterInfoDTO.PartnerCenterInfoId;
            objPartnerCenterInfo.Auditoriums         = partnerCenterInfoDTO.Auditoriums;
            objPartnerCenterInfo.CreatedDate         = partnerCenterInfoDTO.CreatedDate;
            objPartnerCenterInfo.CreatedBy           = partnerCenterInfoDTO.CreatedBy;
            objPartnerCenterInfo.LastModified        = partnerCenterInfoDTO.LastModified;
            objPartnerCenterInfo.LastModifiedBY      = partnerCenterInfoDTO.LastModifiedBY;
            objPartnerCenterInfo.PartnerCentInfoSpId = partnerCenterInfoDTO.PartnerCentInfoSpId;
            _choiceRepoistory.Attach(objPartnerCenterInfo);
            _choiceRepoistory.Complete();

            var crmPartner = _choiceRepoistory.GetById <CRMPartner>(x => x.CRMPartnerId == objPartnerCenterInfo.CRMPartnerId);

            crmPartner.LastModified   = DateTime.UtcNow;
            crmPartner.LastModifiedBy = "CloudMission";

            _choiceRepoistory.Attach(crmPartner);
            _choiceRepoistory.Complete();

            return(NoContent());
        }