public ActionResult GetAvailableLocations(DateTime date, TimeSpan start_time, TimeSpan end_time)
        {
            LocationHelpers helper = new LocationHelpers(db);
            IEnumerable<Location> locations = helper.GetAvailableLocations(date, start_time, end_time).ToList();
            AvailableLocation[] locations_for_view = new AvailableLocation[locations.Count()];

            for (int i = 0; i < locations_for_view.Length; ++i)
            {
                locations_for_view[i] = new AvailableLocation
                {
                    Id = locations.ElementAt(i).Id,
                    Name = locations.ElementAt(i).Name
                };
            }

            return Json(locations_for_view, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Verify(int id, bool is_passed, string reject_reason, int page_number = 1, string search = "", string search_option = "Title", string order = "Title",
            string pass_filter = "")
        {
            Event e = db.Events.Include(t => t.FundApplication).Include(t => t.SubEvents.Select(s => s.LocationApplications)).
                Include(t => t.SubEvents.Select(s => s.AssetApplications)).ToList().SingleOrDefault(t => t.Id == id && t.Status == Application.NOT_VERIFIED);

            if (e != null)
            {
                LocationHelpers loc_hlp = new LocationHelpers(db);
                AssetHelpers ass_hlp = new AssetHelpers(db);

                foreach (SubEvent sub_event in e.SubEvents)
                {
                    foreach (LocationApplication loc_app in sub_event.LocationApplications)
                    {
                        loc_hlp.VerifyLocationApplication(loc_app, is_passed, false);
                    }

                    foreach (AssetApplication ass_app in sub_event.AssetApplications)
                    {
                        ass_hlp.VerifyAssetApplication(ass_app, is_passed, false);
                    }

                    e.Status = is_passed ? Application.PASSED : Application.FAILED;

                    if (e.FundApplication != null)
                    {
                        e.FundApplication.Status = is_passed ? Application.PASSED : Application.FAILED;
                    }

                    if (!is_passed)
                    {
                        e.RejectReason = new EventRejectReason
                        {
                            Reason = reject_reason
                        };
                    }
                }

                db.SaveChanges();

                return RedirectToAction("Applications", new
                {
                    page_number = page_number,
                    order = order,
                    pass_filter = pass_filter,
                    search = search,
                    search_option = search_option
                });
            }

            return View("InvalidOperationError");
        }
        public ActionResult Edit(Event e, HttpPostedFileBase PosterUrl, HttpPostedFileBase PlanUrl, string[] event_organizers, bool has_modified_sub_events = false, bool has_modified_organizers = false)
        {
            if (ModelState.IsValid)
            {
                EventHelpers event_helper = new EventHelpers(db);
                AssetHelpers asset_helper = new AssetHelpers(db);
                LocationHelpers location_helper = new LocationHelpers(db);

                Event modifying_event = db.Events
                    .Include(t => t.Description)
                    .Include(t => t.FundApplication)
                    .Include(t => t.Organizers)
                    .Include(t => t.SubEvents.Select(s => s.AssetApplications.Select(f => f.ApplicatedAssets)))
                    .Include(t => t.SubEvents.Select(s => s.Description.Description))
                    .Include(t => t.SubEvents.Select(s => s.LocationApplications.Select(f => f.Locations)))
                    .Include(t => t.SubEvents.Select(s => s.Times))
                    .Find(e.Id);

                if (ScmMembershipProvider.IsMe(modifying_event.ChiefEventOrganizerId))
                {
                    if (modifying_event != null)
                    {
                        if (e.Description != null)
                        {
                            if (modifying_event.Description == null)
                                modifying_event.Description = new EventDescription();

                            modifying_event.Description.Description = e.Description.Description;
                        }

                        modifying_event.EndDate = e.EndDate;
                        modifying_event.StartDate = e.StartDate;

                        if (e.FundApplication != null)
                        {
                            if (e.FundApplication.Quantity > 0)
                            {
                                if (modifying_event.FundApplication == null)
                                    modifying_event.FundApplication = new FundApplication
                                    {
                                        Id = db.GenerateIdFor(IdentityForTPC.APPLICATION),
                                        ApplicantUserName = e.ChiefEventOrganizerId,
                                        ClubId = modifying_event.ClubId,
                                        Date = DateTime.Now,
                                        Status = Application.NOT_SUBMITTED
                                    };

                                modifying_event.FundApplication.Quantity = e.FundApplication.Quantity;
                            }
                            else
                            {
                                if (modifying_event.FundApplication != null)
                                    db.FundApplications.Delete(modifying_event.FundApplication);
                            }
                        }

                        if (has_modified_organizers)
                        {
                            e.Organizers = new List<Student>();

                            if (event_organizers != null)
                            {
                                foreach (string organizer in event_organizers)
                                {
                                    Student student = db.Students.Find(organizer);

                                    if (student != null)
                                    {
                                        e.Organizers.Add(student);
                                    }
                                }
                            }

                            if (e.Organizers != null)
                            {
                                // 检察是否有不存在的组织者
                                if (e.Organizers.Any(t => db.Students.ToList().All(s => s.UserName != t.UserName)))
                                {
                                    ModelState.AddModelError("Organizers", "非法用户");
                                    return Json(new { success = false, msg = "存在非法用户,保存失败" });
                                }

                                // 检察是否存在重复用户
                                IDictionary<string, int> organizer_occurance_counter = new Dictionary<string, int>();
                                foreach (var organizer in e.Organizers)
                                {
                                    if (!organizer_occurance_counter.ContainsKey(organizer.UserName))
                                        organizer_occurance_counter.Add(new KeyValuePair<string, int>(organizer.UserName, 0));

                                    organizer_occurance_counter[organizer.UserName]++;
                                }
                                foreach (var organizer_occurance in organizer_occurance_counter)
                                {
                                    if (organizer_occurance.Value > 1)
                                    {
                                        e.Organizers.Remove(e.Organizers.Single(t => t.UserName == organizer_occurance.Key));
                                    }
                                }

                                if (modifying_event.Organizers == null)
                                    modifying_event.Organizers = new List<Student>();

                                // 更新组织者列表
                                var deleting_organizers = modifying_event.Organizers.Where(t => e.Organizers.All(s => s.UserName != t.UserName));

                                foreach (var deleting_organizer in deleting_organizers)
                                {
                                    modifying_event.Organizers.Remove(deleting_organizer);
                                }

                                foreach (var organizer in e.Organizers)
                                {
                                    if (modifying_event.Organizers.All(t => t.UserName != organizer.UserName))
                                    {
                                        modifying_event.Organizers.Add(organizer);
                                    }
                                }
                            }
                            else
                            {
                                if (modifying_event.Organizers != null)
                                    modifying_event.Organizers.Clear();
                            }
                        }

                        string plan_url = HtmlHelpersExtensions.RenameAndSaveFile(PlanUrl, ConfigurationManager.EventPlanFolder);
                        string poster_url = HtmlHelpersExtensions.RenameAndSaveFile(PosterUrl, ConfigurationManager.EventPosterFolder);

                        if (!String.IsNullOrWhiteSpace(plan_url))
                            modifying_event.PlanUrl = plan_url;
                        if (!String.IsNullOrWhiteSpace(poster_url))
                            modifying_event.PosterUrl = poster_url;

                        modifying_event.Title = e.Title;

                        if (has_modified_sub_events)
                        {
                            // 删除已被删除的子活动
                            if (modifying_event.SubEvents != null)
                            {
                                IEnumerable<SubEvent> deleting_sub_events = null;

                                if (e.SubEvents == null)
                                {
                                    deleting_sub_events = modifying_event.SubEvents.ToList();
                                }
                                else
                                {
                                    deleting_sub_events = modifying_event.SubEvents.Where(t => e.SubEvents.All(s => s.Id != t.Id));
                                }

                                foreach (var deleting_sub_event in deleting_sub_events)
                                {
                                    event_helper.DeleteSubEvent(deleting_sub_event, modifying_event.SubEvents, true);
                                }
                            }

                            if (e.SubEvents != null)
                            {
                                foreach (SubEvent sub_event in e.SubEvents)
                                {
                                    // 合并重复物资
                                    if (sub_event.AssetApplications != null)
                                    {
                                        foreach (AssetApplication application in sub_event.AssetApplications)
                                        {
                                            asset_helper.JoinApplicatedAssets(application);
                                        }
                                    }

                                    // 合并重复场地
                                    if (sub_event.LocationApplications != null)
                                    {
                                        foreach (LocationApplication application in sub_event.LocationApplications)
                                        {
                                            location_helper.JoinLocations(application);
                                        }
                                    }

                                    sub_event.Times = db.Times.ToList().ToList().Where(t => t.IsCoveredBy(sub_event.StartTime, sub_event.EndTime)).ToList();

                                    // 添加子活动
                                    if (sub_event.Id == 0)
                                    {
                                        sub_event.Event = modifying_event;

                                        if (sub_event.AssetApplications != null)
                                        {
                                            foreach (var asset_app in sub_event.AssetApplications)
                                            {
                                                event_helper.NewAssetApplication(asset_app, sub_event);
                                            }
                                        }

                                        if (sub_event.LocationApplications != null)
                                        {
                                            foreach (var location_app in sub_event.LocationApplications)
                                            {
                                                event_helper.NewLocationApplication(location_app, sub_event);
                                            }
                                        }

                                        modifying_event.SubEvents.Add(sub_event);
                                    }
                                    else // 修改原有子活动
                                    {
                                        SubEvent modifying_sub_event = modifying_event.SubEvents.FirstOrDefault(t => t.Id == sub_event.Id);

                                        modifying_sub_event.Date = sub_event.Date;
                                        modifying_sub_event.EndTime = sub_event.EndTime;
                                        modifying_sub_event.StartTime = sub_event.StartTime;
                                        modifying_sub_event.Title = sub_event.Title;

                                        event_helper.UpdateTimes(modifying_sub_event.Times, sub_event.Times);

                                        // 创建物资申请
                                        if (modifying_sub_event.AssetApplications == null || modifying_sub_event.AssetApplications.Count == 0)
                                        {
                                            if (sub_event.AssetApplications != null)
                                            {
                                                foreach (var asset_app in sub_event.AssetApplications)
                                                {
                                                    event_helper.NewAssetApplication(asset_app, modifying_sub_event);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // 删除物资申请
                                            if (sub_event.AssetApplications == null)
                                            {
                                                foreach (var application in modifying_sub_event.AssetApplications.ToList())
                                                {
                                                    db.AssetApplications.Delete(application);
                                                }
                                            }
                                            else // 修改物资申请
                                            {
                                                foreach (var new_application in sub_event.AssetApplications)
                                                {
                                                    foreach (var orig_application in modifying_sub_event.AssetApplications)
                                                    {
                                                        event_helper.UpdateAssetApplication(orig_application, new_application);
                                                    }
                                                }
                                            }
                                        }

                                        // 创建场地申请
                                        if (modifying_sub_event.LocationApplications == null || modifying_sub_event.LocationApplications.Count == 0)
                                        {
                                            if (sub_event.LocationApplications != null)
                                            {
                                                foreach (var location_app in sub_event.LocationApplications)
                                                {
                                                    event_helper.NewLocationApplication(location_app, modifying_sub_event);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // 删除场地申请
                                            if (sub_event.LocationApplications == null)
                                            {
                                                foreach (var application in modifying_sub_event.LocationApplications.ToList())
                                                {
                                                    db.LocationApplications.Delete(application);
                                                }

                                            }
                                            else // 修改场地申请
                                            {
                                                foreach (var new_application in sub_event.LocationApplications)
                                                {
                                                    foreach (var orig_application in modifying_sub_event.LocationApplications)
                                                    {
                                                        event_helper.UpdateLocationApplication(orig_application, new_application);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (ScmRoleProvider.HasMembershipIn(modifying_event.ClubId, null, new string[] { "会长" }))
                {
                    if (modifying_event != null)
                        modifying_event.ChiefEventOrganizerId = e.ChiefEventOrganizerId;
                }

                db.SaveChanges();

                return Json(new { success = true, msg = "保存成功" });
            }

            return Json(new { success = false, msg = "保存失败" });
        }
        public ActionResult Verify(int id, bool is_passed, string reject_reason)
        {
            LocationHelpers helper = new LocationHelpers(unitOfWork);
            LocationApplication application = unitOfWork.LocationApplications.Find(id);

            if (application != null)
            {
                helper.VerifyLocationApplication(application, is_passed, false);

                if (!is_passed)
                {
                    application.RejectReason = new ApplicationRejectReason
                    {
                        Reason = reject_reason
                    };
                }

                unitOfWork.SaveChanges();

                return RedirectToAction("List");
            }

            return View("InvalidOperationError");
        }