public ActionResult ApplyClub(string Msg = "") { if (!string.IsNullOrEmpty(Msg)) { ViewBag.Msg = Msg; } ApplyClubModel model = new ApplyClubModel(); model.clubTypes = db.ClubTypes.ToList(); return(View(model)); }
public ActionResult ApplyClub(ApplyClubModel model) { if (ModelState.IsValid) { try { UserNumber thisuser = db.UserNumbers.Find(User.Identity.Name); ApplyType applyType = db.ApplyTypes.Find((int)SQType.注册社团); if (applyType == null || applyType.Enable != 1) { throw new Exception("注册社团的申请通道暂未开通,请持续关注后续开通公告或联系管理员"); } ClubNumber club = db.ClubNumbers.Where(c => c.User.UserId == User.Identity.Name).OrderByDescending(c => c.CreateDate).FirstOrDefault(); if (club != null && club.CreateDate != null) { if (DateTime.Now.Subtract((DateTime)club.CreateDate).Days < 1) { throw new Exception("每个用户每天只能申请一次社团创建!用户已于" + club.CreateDate.ToString() + "创建过申请!"); } } ClubNumber newclub = GetRandomClubNumber(); if (newclub == null || newclub.State != (int)EnumState.未使用) { throw new Exception("社团账号已达上限,暂无法新建社团。请联系管理员"); } //ApplyAudit apply = new ApplyAudit() //{ // Type = applyType, // ApplicationDesc = model.ApplyDesc, // ApplicationFiled = model.ApplyFile, // ApplyDate=DateTime.Now, // ApplyUser=db.UserNumbers.Find(User.Identity.Name), // CheckState=(int)EnumAuditState.已创建, // AuditTimes=0 //}; newclub.Name = model.Name; if (model.Label?.Length > 1) { newclub.Label = model.Label.Substring(1); } newclub.Desc = model.Desc; newclub.ShortDesc = model.ShortDesc; newclub.CreateDate = DateTime.Now; newclub.Type = db.ClubTypes.Find(model.Type); newclub.User = thisuser; newclub.State = (int)EnumState.待提交; //添加关系 UserClubs uc = new UserClubs() { User = thisuser, Club = newclub, Status = (int)UCStatus.社长, CreateDate = DateTime.Now, State = (int)EnumState.正常 }; db.Entry(newclub).State = System.Data.Entity.EntityState.Modified; db.UserClubs.Add(uc); db.SaveChanges(); model = new ApplyClubModel(); model.ClubId = newclub.ClubId; ViewBag.Res = "ApplyOK"; } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } } model.clubTypes = db.ClubTypes.ToList(); return(View(model)); }