public static string GetEventStatus(Event e)
        {
            string status_code = e.Status.ToLower();

            switch (status_code)
            {
                case Application.NOT_SUBMITTED:
                    return "未提交";
                case Application.FAILED:
                    return "被驳回";
                case Application.NOT_VERIFIED:
                    return "未审批";
                case Application.PASSED:
                    return GetStatus(e.StartDate, e.EndDate);
                case Application.CANCELED:
                    return "已取消";
                case Application.TERMINATED:
                    return "已终止";
                default:
                    return "未知";
            }
        }
        public void DeleteEvent(Event e, bool save = true)
        {
            // 删除与该活动相关的资金申请
            if (e.FundApplication != null)
            {
                db.FundApplications.Delete(e.FundApplication);
            }

            // 删除该活动的海报
            if (!String.IsNullOrWhiteSpace(e.PosterUrl))
            {
                HtmlHelpersExtensions.DeleteFileFrom(ConfigurationManager.EventPosterFolder, e.PosterUrl);
            }

            // 删除该活动的策划书
            if (!String.IsNullOrWhiteSpace(e.PlanUrl))
            {
                HtmlHelpersExtensions.DeleteFileFrom(ConfigurationManager.EventPlanFolder, e.PlanUrl);
            }

            // 删除该活动的所有子活动
            if (e.SubEvents != null)
            {
                foreach (var sub_event in e.SubEvents)
                {
                    DeleteSubEvent(sub_event, e.SubEvents, true);
                }
            }

            db.Events.Delete(e);

            if (save)
            {
                db.SaveChanges();
            }
        }
        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 Create(Event e)
        {
            // 只有会长才能创建新活动。
            if (ScmRoleProvider.HasMembershipIn(e.ClubId, null, new string[] { "会长" }))
            {
                if (ModelState.IsValid)
                {
                    e.Status = Application.NOT_SUBMITTED;

                    db.Events.Add(e);
                    db.SaveChanges();

                    return Json(new { success = true, msg = e.Title + "创建成功", url = "List?pass_filter=NotSubmitted&club_id=" + e.ClubId });
                }

                return Json(new { success = false, msg = "创建活动失败" });
            }

            return Json(new { success = false, msg = "非法操作!您没有创建新活动的权限" });
        }
 private bool HasAccessToCriticalDetails(Event e)
 {
     return User.IsInRole("社联") ||
                     ScmRoleProvider.HasMembershipIn(e.ClubId, null, new string[] { "会长" }) || ScmRoleProvider.IsOrganizerOf(e.Id);
 }