public GetEntityResult<ExternalActivityViewModel> DownLoadExternalOrders(OrderSearchOptions orderSearchOptions)
        {
            GetEntityResult<ExternalActivityViewModel> externalActivityResult = new GetEntityResult<ExternalActivityViewModel>() { Success = false };

            ExternalOrderResult orderResult = null;

            orderSearchOptions.ToDate = DateTime.Now;

            orderResult = new ExternalOrderClient().GetUnshippedOrders(orderSearchOptions);

            if (orderResult.Success)
            {
                EntityOperationResultBase saveResult = SaveExternalOrder(orderResult.Orders);

            }
            else
            {
                externalActivityResult.ErrorMessage = "Error occurred while retrieving orders for " + orderSearchOptions.StoreProvider.ToString();

                return externalActivityResult;
            }

            ExternalActivityViewModel activity = new ExternalActivityViewModel()
            {
                ActivityStatus = ExternalActivityStatus.Completed,
                ExternalActivityType = ExternalActivityTypeEnum.GetUnshippedOrders,
                StoreProviderType = orderSearchOptions.StoreProvider,
                LastPerformedOn = orderSearchOptions.ToDate,
            };

            try
            {
                UpdateResult<ExternalActivity> externalActivityUpdateResult = UpdateExternalActivityDetails(activity);

                GetEntityResult<ExternalActivityViewModel> result = GetLastExternalActivityDetails(ExternalActivityTypeEnum.GetUnshippedOrders, orderSearchOptions.StoreProvider);

                externalActivityResult.Success = result.Success;
                externalActivityResult.Entity = result.Entity;

            }
            catch (Exception ex)
            {
                externalActivityResult.Success = false;
                externalActivityResult.ErrorMessage = "Error occurred while update activity details. Please refresh the page";
            }

            return externalActivityResult;
        }
        public UpdateResult<ExternalActivity> UpdateExternalActivityDetails(ExternalActivityViewModel externalActivity)
        {
            UpdateResult<ExternalActivity> updateResult = new UpdateResult<ExternalActivity>() { Success = false };

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                DbSet<ExternalActivity> externalActivitySet = dbContext.ExternalActivitySet;

                ExternalActivity existingActivity = externalActivitySet.Where(x => x.StoreProviderType == (int)externalActivity.StoreProviderType
                                                                && x.ExternalActivityType == (int)externalActivity.ExternalActivityType).FirstOrDefault();

                if (existingActivity != null)
                {
                    Guid existingId = existingActivity.ActivityId;

                    existingActivity.ActivityStatus = (int)ExternalActivityStatus.Completed;
                    existingActivity.ExternalActivityType = (int)externalActivity.ExternalActivityType;
                    existingActivity.LastPerformedOn = externalActivity.LastPerformedOn;
                    existingActivity.StoreProviderType = (int)externalActivity.StoreProviderType;
                    externalActivity.UnshippedOrdersCount = externalActivity.UnshippedOrdersCount;

                    externalActivitySet.Attach(existingActivity);

                    dbContext.Entry(existingActivity).State = EntityState.Modified;

                }
                else
                {

                    existingActivity = existingActivity = new ExternalActivity()
                    {
                        ActivityId = Guid.NewGuid(),
                        ActivityStatus = (int)externalActivity.ActivityStatus,
                        ExternalActivityType = (int)externalActivity.ExternalActivityType,
                        LastPerformedOn = externalActivity.LastPerformedOn,
                        StoreProviderType = (int)externalActivity.StoreProviderType,
                    };

                    externalActivitySet.Add(existingActivity);
                }

                dbContext.SaveChanges();
                updateResult.Entity = existingActivity;

            }

            return updateResult;
        }
        public GetEntityResult<ExternalActivityViewModel> GetLastExternalActivityDetails(ExternalActivityTypeEnum activityType, StoreProviderTypeEnum storeProvider)
        {
            GetEntityResult<ExternalActivityViewModel> externalActivityResult = new GetEntityResult<ExternalActivityViewModel>() { Success = false };

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                DbSet<ExternalActivity> externalActivitySet = dbContext.ExternalActivitySet;

                List<ExternalActivity> externalActivities = externalActivitySet.Where(x => x.ExternalActivityType == (int)activityType
                                                    && x.StoreProviderType == (int)storeProvider).ToList();

                if (externalActivities.Count == 0)
                {
                    externalActivityResult.Entity = null;

                    externalActivityResult.Success = true;

                    externalActivityResult.SuccessMessage = "No activity found...";
                }

                else
                {
                    externalActivities = externalActivities.OrderByDescending(x => x.LastPerformedOn).ToList();

                    ExternalActivity activity = externalActivities.FirstOrDefault();

                    ExternalActivityViewModel activityViewModel = new ExternalActivityViewModel()
                    {
                        ActivityStatus = (ExternalActivityStatus)activity.ActivityStatus,
                        ExternalActivityType = (ExternalActivityTypeEnum)activity.ExternalActivityType,
                        LastPerformedOn = activity.LastPerformedOn,
                        StoreProviderType = (StoreProviderTypeEnum)activity.StoreProviderType,

                    };

                    int unshippedCount = dbContext.ExternalOrderSet.Where(x => x.StoreOrderStatus == StoreOrderStatusEnum.Unshipped.ToString()
                                                                        && x.StoreProviderId == (int)storeProvider).Count();

                    activityViewModel.UnshippedOrdersCount = unshippedCount;

                    externalActivityResult.Entity = activityViewModel;

                    externalActivityResult.Success = true;

                }
            }

            return externalActivityResult;
        }