Example #1
0
        public ActionResult GetNextBatchTags(string tag)
        {
            //TODO делаешь unban - виснет может из-за Session["NEXT_MEDIA_ID"]
            if (!UserManager.CanUserTranslateShow(_account.USER_ID))
            {
                List <MediaTag> res = new List <MediaTag>()
                {
                    new MediaTag {
                        INSTAGRAM_MEDIA_ID  = "0",
                        INSTAGRAM_USER_ID   = "0",
                        INSTAGRAM_USER_NAME = UtilManager.GetVarValue("app.caption"),
                        INSTAGRAM_MEDIA_STANDARD_RES_URL = Url.Content("~/Content/img/trans_show_end.png"),
                        INSTAGRAM_USER_PROFILEPICTURE    = Url.Content("~/Content/img/logo.png"),
                        INSTAGRAM_CAPTION = "Трансляция шоу завершена!!!"
                    }
                };
                return(Json(res, JsonRequestBehavior.AllowGet));
            }

            UserOptions opts;

            if (Session["OPTIONS"] == null)
            {
                opts = UserManager.GetUserOptions(_account.USER_ID);
                Session["OPTIONS"] = opts;
            }
            else
            {
                opts = (UserOptions)Session["OPTIONS"];
            }
            //auction
            int next_media_id = (Session["NEXT_MEDIA_ID"] == null ? 0 : Convert.ToInt32(Session["NEXT_MEDIA_ID"]));

            var batch = HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, tag, next_media_id, opts.USER_SLIDE_BATCH_SIZE);

            if (batch.Count == 0)
            {
                if (next_media_id == 0) //значит в базе нет нужных тегов
                {
                    return(new EmptyResult());
                }
                else
                {
                    //запрос с начала базы
                    next_media_id = 0;
                    batch         = HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, tag, next_media_id, opts.USER_SLIDE_BATCH_SIZE);
                }
            }

            if (batch.Count > 0)
            {
                if (opts.USER_SLIDE_BATCH_SIZE > batch.Count)
                {
                    next_media_id = 0;
                    batch.AddRange(HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, tag, next_media_id, opts.USER_SLIDE_BATCH_SIZE - batch.Count));
                }

                batch = batch.GroupBy(x => x.MEDIA_ID, (key, group) => group.FirstOrDefault()).ToList();
                Session["NEXT_MEDIA_ID"] = batch.Last().MEDIA_ID;

                //для того, чтобы работала BS Carousel нужно мин. 5 элементов в писке
                int batch_size = batch.Count;
                var rnd        = new Random((int)DateTime.Now.Ticks);
                while (batch.Count < 5)
                {
                    batch.Add(new MediaTag()
                    {
                        MEDIA_ID            = rnd.Next(1, 10000),
                        INSTAGRAM_MEDIA_ID  = batch[batch.Count - batch_size].INSTAGRAM_MEDIA_ID,
                        INSTAGRAM_USER_ID   = batch[batch.Count - batch_size].INSTAGRAM_USER_ID,
                        INSTAGRAM_USER_NAME = batch[batch.Count - batch_size].INSTAGRAM_USER_NAME,
                        INSTAGRAM_MEDIA_STANDARD_RES_URL = batch[batch.Count - batch_size].INSTAGRAM_MEDIA_STANDARD_RES_URL,
                        INSTAGRAM_USER_PROFILEPICTURE    = batch[batch.Count - batch_size].INSTAGRAM_USER_PROFILEPICTURE,
                        INSTAGRAM_CAPTION = batch[batch.Count - batch_size].INSTAGRAM_CAPTION
                    });
                    batch_size--;
                    if (batch_size == 0)
                    {
                        batch_size = batch.Count;
                    }
                }
                //@Url.Action("Translate", "Show")

                if (Session["ACTIONTAG"] != null)
                {
                    string          actionTag            = (string)Session["ACTIONTAG"];
                    int             action_next_media_id = Session["ACTION_NEXT_MEDIA_ID"] == null ? 0 : (int)Session["ACTION_NEXT_MEDIA_ID"];
                    List <MediaTag> actionMediaTag       = HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, actionTag, action_next_media_id, 1);
                    if (actionMediaTag == null || actionMediaTag.Count == 0)
                    {
                        if (action_next_media_id > 0)
                        {
                            //значит достигнут конец списка аукционного тега
                            action_next_media_id = 0;
                            actionMediaTag       = HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, actionTag, action_next_media_id, 1);
                        }
                        else
                        {
                            //значит список пуст
                        }
                    }

                    if (actionMediaTag.Count > 0)
                    {
                        action_next_media_id            = actionMediaTag[0].MEDIA_ID;
                        Session["ACTION_NEXT_MEDIA_ID"] = action_next_media_id;
                        batch.Add(actionMediaTag[0]);
                    }
                }

                return(Json(batch, JsonRequestBehavior.AllowGet));//.GroupBy(x => x.MEDIA_ID, (key, group) => group.FirstOrDefault()).ToList()
            }

            //var total = HashTagManager.GetNextBatchDataBaseMediaTags(_account.USER_ID, tag, next_media_id, opts.USER_SLIDE_BATCH_SIZE);
            //if (total.Count > 0)
            //{
            //    List<MediaTag> res = total.Where(x => x.MEDIA_ID > next_media_id).Take(opts.USER_SLIDE_BATCH_SIZE).ToList<MediaTag>();
            //    if (res.Count() < opts.USER_SLIDE_BATCH_SIZE)
            //    {
            //        //дошли до конца
            //        //res = total.Skip(Math.Max(0, total.Count - opts.USER_SLIDE_BATCH_SIZE)).ToList<MediaTag>();
            //        res.AddRange(total.Take(opts.USER_SLIDE_BATCH_SIZE - res.Count()));
            //    }

            //    Session["NEXT_MEDIA_ID"] = res.Last().MEDIA_ID;

            //    return Json(res, JsonRequestBehavior.AllowGet);
            //}

            return(new EmptyResult());
        }