Exemplo n.º 1
0
        public PagedData <Models.Cinema> GetByCityId(SearchCinemaCondition condition)
        {
            if (condition.CityId <= 0)
            {
                return(new PagedData <Models.Cinema>());
            }

            if (condition.PageNow == 1)
            {
                var cinemas = Enumerable.Empty <Models.Cinema>();
                var count   = _cinemaRepository.QueryCount(condition.CityId);

                if (count > 0)
                {
                    cinemas = _cinemaRepository.Query(condition);
                }
                else
                {
                    lock (_locker)
                    {
                        count = _cinemaRepository.QueryCount(condition.CityId);
                        if (count > 0)
                        {
                            cinemas = _cinemaRepository.Query(condition);
                        }
                        else
                        {
                            var data = _remoteCall.SendAsync(new CinemaRequest {
                                CityId = condition.CityId
                            }).Result;
                            cinemas = data.cinemas.Select(x => new Models.Cinema()
                            {
                                CityId         = condition.CityId,
                                CinemaId       = x.id,
                                Name           = x.nm,
                                Address        = x.addr,
                                LastUpdateTime = DateTime.Now
                            });

                            var splitArr = cinemas.Split(20);
                            foreach (var arr in splitArr)
                            {
                                _cinemaRepository.InsertBatch(arr);
                            }
                            count = cinemas.Count();
                        }
                    }
                }
                return(new PagedData <Models.Cinema>()
                {
                    PageNow = condition.PageNow, PageSize = condition.PageSize, TotalCount = count, Records = cinemas
                });
            }
            return(_cinemaRepository.QueryPaged(condition));
        }
Exemplo n.º 2
0
        public IEnumerable <Hall> GetByCinemaId(int cinemaId)
        {
            if (cinemaId <= 0)
            {
                return(Enumerable.Empty <Hall>());
            }

            IEnumerable <Hall> halls = _repository.QueryByCinemaId(cinemaId);

            if (halls.IsNullOrEmpty())
            {
                lock (_locker)
                {
                    halls = _repository.QueryByCinemaId(cinemaId);
                    if (halls.IsNullOrEmpty())
                    {
                        CinemaMoviesResponse movies = _remoteCall.SendAsync(new CinemaMoviesRequest()
                        {
                            CinemaId = cinemaId
                        }).Result;

                        var shows = movies.showData.movies.SelectMany(x => x.shows.SelectMany(o => o.plist))
                                    .AsParallel().Select(x =>
                        {
                            try
                            {
                                var result = _remoteCall.SendAsync(new SeatInfoRequest()
                                {
                                    SeqNo = x.seqNo
                                }).Result;
                                if (result?.seatData?.hall == null)
                                {
                                    _logger.LogError("结果为null, seqNo:" + x.seqNo);
                                    return(null);
                                }

                                return(result);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("获取场次异常, seqNo:" + x.seqNo, ex);
                                return(null);
                            }
                        }).Where(x => x != null).Distinct(new SeatListResponseEqualityComparer()).ToList();

                        halls = shows.Select(x =>
                        {
                            string html = String.Empty;
                            try
                            {
                                html = _remoteCall.FeatchHtmlAsync(new FetchSeatHtmlRequest()
                                {
                                    SeqNo = x.seatData.show.seqNo
                                }).Result;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("获取此次html异常, seqNo:" + x.seatData.show.seqNo, ex);
                            }

                            return(new Hall()
                            {
                                HallId = x.seatData.hall.hallId,
                                Name = x.seatData.hall.hallName,
                                CinemaId = x.seatData.cinema.cinemaId,
                                SeatJson = JsonConvert.SerializeObject(x.seatData.seat),
                                SeatHtml = html.IsNullOrEmpty()
                                        ? null
                                        : Regex.Replace(html, @"\s*(<[^>]+>)\s*", "$1", RegexOptions.Singleline)
                                           .Replace("seat sold", "seat selectable"),
                                LastUpdateTime = DateTime.Now
                            });
                        }).Where(x => x != null)
                                .ToList();

                        if (halls.Any())
                        {
                            _repository.InsertBatch(halls);
                        }
                    }
                }
            }

            return(halls);
        }