private async Task<ImportValidationResultInfo> MapAndValidate(DistributorDTO dto, int index)
        {
            return await Task.Run(() =>
            {
                if (dto == null) return null;
                var entity = _mappingService.Map(dto);
                var exist = _ctx.tblCostCentre.FirstOrDefault(p => p.Cost_Centre_Code == dto.CostCentreCode.ToLower()||
                    p.Name.ToLower()==dto.Name.ToLower() && p.CostCentreType==(int)CostCentreType.Distributor);
                var producer = ObjectFactory.GetInstance<IProducerRepository>().GetProducer();
                if (exist == null)
                {
                    var groupId = AddUserGroup("admin");
                   
                    entity.Id = Guid.NewGuid();
                    entity.ASM = AddUser("ASM", producer.Id, UserType.ASM, groupId);
                    entity.SalesRep = AddUser("SalesRep", producer.Id, UserType.SalesRep, groupId);
                    entity.Surveyor = AddUser("Surveyor", producer.Id, UserType.Surveyor, groupId);
                }
                else
                {
                    var groupId = AddUserGroup("admin");
                    var  test= ObjectFactory.GetInstance<ICostCentreRepository>().GetById(exist.Id) as Distributor;
                    entity.ASM = AddUser("ASM", producer.Id, UserType.ASM, groupId);
                    entity.SalesRep = AddUser("SalesRep", producer.Id, UserType.SalesRep, groupId);
                    entity.Surveyor = AddUser("Surveyor", producer.Id, UserType.Surveyor, groupId);
                    entity.Id = test.Id;

                }
                entity.ParentCostCentre = new CostCentreRef() {Id = producer.Id};
                var res = _repository.Validate(entity);
                var vResult = new ImportValidationResultInfo()
                {
                    Results = res.Results,
                    Description =string.Format("Row-{0} name or code=>{1}", index,
                                      entity.Name ?? entity.CostCentreCode),
                    Entity = entity
                };
                return vResult;

            });


        }
 public Distributor Map(DistributorDTO dto)
 {
     if (dto == null) return null;
     var distributor = Mapper.Map<DistributorDTO, Distributor>(dto);
     distributor.Region = _regionRepository.GetById(dto.RegionMasterId);
     distributor.ASM = _userRepository.GetById(dto.ASMUserMasterId.Value);
     distributor.SalesRep = _userRepository.GetById(dto.SalesRepUserMasterId.Value);
     distributor.Surveyor = _userRepository.GetById(dto.SurveyorUserMasterId.Value);
     distributor.ProductPricingTier = _pricingTierRepository.GetById(dto.ProductPricingTierMasterId);
     return distributor;
 }
 private DistributorDTO Map(tblCostCentre tbl)
 {
     var dto = new DistributorDTO
                   {
                       MasterId = tbl.Id,
                       DateCreated = tbl.IM_DateCreated,
                       DateLastUpdated = tbl.IM_DateLastUpdated,
                       StatusId = tbl.IM_Status,
                       Name = tbl.Name,
                       CostCentreCode = tbl.Cost_Centre_Code,
                       ParentCostCentreId = tbl.ParentCostCentreId ?? Guid.Empty,
                       CostCentreTypeId = tbl.CostCentreType ?? 0,
                       ProducerId = tbl.ParentCostCentreId ?? Guid.Empty,
                       Owner = tbl.Distributor_Owner,
                       PIN = tbl.Distributor_PIN,
                       AccountNo = tbl.Cost_Centre_Code,
                       RegionMasterId = tbl.Distributor_RegionId ?? Guid.Empty,
                       ASMUserMasterId = tbl.Distributor_ASM_Id,
                       SalesRepUserMasterId = tbl.SalesRep_Id,
                       SurveyorUserMasterId = tbl.Surveyor_Id,
                       ProductPricingTierMasterId = tbl.Tier_Id ?? Guid.Empty,
                       PaybillNumber = tbl.PaybillNumber,
                       MerchantNumber = tbl.MerchantNumber
                   };
     var asm = _context.tblUsers.FirstOrDefault(n => n.Id == dto.ASMUserMasterId);
     var salesRep = _context.tblUsers.FirstOrDefault(n => n.Id == dto.SalesRepUserMasterId);
     var surveyor = _context.tblUsers.FirstOrDefault(n => n.Id == dto.SurveyorUserMasterId);
     if (salesRep != null)
     {
         dto.SalesRepDTO = new UserDTO
                               {
                                   MasterId = salesRep.Id,
                                   DateCreated = salesRep.IM_DateCreated,
                                   DateLastUpdated = salesRep.IM_DateLastUpdated ?? DateTime.Now,
                                   StatusId = salesRep.IM_Status,
                                   Username = salesRep.UserName,
                                   CostCentre = salesRep.CostCenterId,
                                   Password = salesRep.Password,
                                   PIN = salesRep.PIN,
                                   UserTypeId = salesRep.UserType,
                                   Mobile = salesRep.Mobile,
                                   GroupMasterId = salesRep.GroupId ?? Guid.Empty,
                                   TillNumber = salesRep.TillNumber
                               };
     }
     if (asm != null)
     {
         dto.ASMDTO = new UserDTO
                               {
                                   MasterId = asm.Id,
                                   DateCreated = asm.IM_DateCreated,
                                   DateLastUpdated = asm.IM_DateLastUpdated ?? DateTime.Now,
                                   StatusId = asm.IM_Status,
                                   Username = asm.UserName,
                                   CostCentre = asm.CostCenterId,
                                   Password = asm.Password,
                                   PIN = asm.PIN,
                                   UserTypeId = asm.UserType,
                                   Mobile = asm.Mobile,
                                   GroupMasterId = asm.GroupId ?? Guid.Empty,
                                   TillNumber = asm.TillNumber
                               };
     }
     if (surveyor != null)
     {
         dto.SurveyorDTO = new UserDTO
         {
             MasterId = surveyor.Id,
             DateCreated = surveyor.IM_DateCreated,
             DateLastUpdated = surveyor.IM_DateLastUpdated ?? DateTime.Now,
             StatusId = surveyor.IM_Status,
             Username = surveyor.UserName,
             CostCentre = surveyor.CostCenterId,
             Password = surveyor.Password,
             PIN = surveyor.PIN,
             UserTypeId = surveyor.UserType,
             Mobile = surveyor.Mobile,
             GroupMasterId = surveyor.GroupId ?? Guid.Empty,
             TillNumber = surveyor.TillNumber
         };
     }
     return dto;
 }