Beispiel #1
0
        public ActionResult DownloadBusiness(DownloadViewModel item)
        {
            var result = new List<DownloadViewModel>();
            var userId = User.Identity.GetUserId();
            if (item != null)
            {
                foreach (var zipcode in item.Zipcodes)
                {
                    var query = new Query();

                    try
                    {

                        Category_ZipCode cate = category_ZipCodeService.Get().FirstOrDefault(t => t.ZipcodeName.Equals(zipcode) && item.CategoryInclude.Contains(t.CategoryId.ToString()));

                        if (cate != null)
                        {
                            query.And(query.Field("locality").Equal(item.City),
                                    query.Field("region").Equal(item.State),
                                    query.Field("postcode").Equal(zipcode),
                                    query.Field("category_ids").Includes(item.CategoryInclude),
                                    query.Offset(cate.Downloaded),
                                    query.Limit(50),
                                    query.IncludeRowCount(true));

                            string re = Factual.Fetch("restaurants-us", query);
                            if (!String.IsNullOrEmpty(re))
                            {
                                dynamic jsonResponse = JsonConvert.DeserializeObject(re);
                                //import to business and update Downloaded

                                if (jsonResponse.response != null && jsonResponse.status == "ok")
                                {
                                    dynamic data = jsonResponse.response.data;
                                    dynamic total_row_count = jsonResponse.response.total_row_count;
                                    dynamic included_rows = jsonResponse.response.included_rows;

                                    if (data != null)
                                    {
                                        JArray listData = data as JArray;
                                        if (listData != null)
                                        {
                                            for (int i = 0; i < listData.Count; i++)
                                            {
                                                dynamic biz = listData[i];
                                                //insert to business
                                                Business bus = new Business()
                                                {
                                                    Id = Guid.NewGuid(),
                                                    Address = biz.address,
                                                    Phone = biz.tel,
                                                    Latitude = biz.latitude ?? 0,
                                                    Longtitude = biz.longitude ?? 0,
                                                    Locality = biz.locality,
                                                    Active = true,
                                                    BusinessCategoryId = cate.BusinessCategoryId,
                                                    Country = biz.country,
                                                    CreateBy = HttpContext.User.Identity.Name,
                                                    CreateDate = DateTime.Now,
                                                    Name = biz.name,
                                                    Region = biz.region,
                                                    Status = Infrastructure.Domain.Status.Approved,
                                                    //reference
                                                    UserId = userId,
                                                    Zipcode = zipcode
                                                };

                                                if (businessService.NotExist(bus.Name, bus.Zipcode, cate.CategoryName))
                                                {
                                                    businessService.Create(bus);
                                                    cate.TotalRecord = total_row_count == null ? 0 : total_row_count;
                                                    cate.Downloaded += 1;
                                                    category_ZipCodeService.Update(cate);
                                                }
                                            }
                                        }
                                    }

                                    //item.TotalDownloaded = cate.Downloaded;
                                    //item.Availble = cate.TotalRecord;
                                    result.Add(new DownloadViewModel() { TotalDownloaded = cate.Downloaded, Availble = cate.TotalRecord });

                                }
                            }
                        }
                    }
                    catch (FactualApiException ce)
                    {
                        Console.WriteLine(ce.StackTrace);
                    }
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }