public ActionResult Give(string userID, string phoneNumber, int count) { var fUser = db.Users.FirstOrDefault(s => s.Id == userID); if (fUser == null) { return(Json(Comm.ToJsonResult("NoFound", $"转出人不存在"))); } var tUser = db.Users.FirstOrDefault(s => s.PhoneNumber == phoneNumber); var enableGive = new Enums.UserType[] { Enums.UserType.Proxy, Enums.UserType.ProxySec }; if (tUser == null) { return(Json(Comm.ToJsonResult("NoFound", $"手机号不存在"))); } else if (!enableGive.Contains(tUser.UserType)) { return(Json(Comm.ToJsonResult("Error", $"转送对象没有权限接收"))); } if (tUser.Id == userID) { return(Json(Comm.ToJsonResult("Error", $"不能转给自己"))); } var codes = db.RegistrationCodes .Where(s => s.OwnUser == userID && s.UseEndDateTime == null).Take(count) .ToList(); if (codes.Count < count) { return(Json(Comm.ToJsonResult("NoEnough", $"剩余数量不足以完成该提交", new { Lave = codes.Count }))); } foreach (var item in codes) { item.OwnUser = tUser.Id; } db.SaveChanges(); var tLog = new RegistrationCodeLog { Count = count, CreateDateTime = DateTime.Now, From = userID, UserID = tUser.Id }; var fLog = new RegistrationCodeLog { Count = -count, CreateDateTime = DateTime.Now, From = tUser.Id, UserID = userID }; db.RegistrationCodeLogs.Add(tLog); db.RegistrationCodeLogs.Add(fLog); db.SaveChanges(); var lave = db.RegistrationCodes.Where(s => s.OwnUser == userID && s.UseTime == null).Count(); return(Json(Comm.ToJsonResult("Success", $"转码成功", new { Lave = lave }))); }
public ActionResult Create(RegistrationCodeCreate model, int length = 10) { var user = db.Users.FirstOrDefault(s => s.Id == model.OwnUser); if (model.Count < 1) { ModelState.AddModelError("Count", "数量不可小于1"); } if (string.IsNullOrWhiteSpace(model.OwnUser)) { ModelState.AddModelError("OwnUser", "请选择拥有用户"); } if (ModelState.IsValid) { var codelist = db.RegistrationCodes.Select(s => s.Code).ToList(); var list = new List <RegistrationCode>(); var creteDateTime = DateTime.Now; while (list.Count < model.Count) { var code = CreateCode(length); if (list.Where(s => s.Code == code).Count() <= 0) { if (!codelist.Contains(code)) { list.Add(new RegistrationCode() { Code = code, CreateTime = creteDateTime, CreateUser = UserID, OwnUser = model.OwnUser, ActiveEndDateTime = model.ActiveDateTime?.AddHours(24).AddSeconds(-1), UseEndDateTime = model.UseEndDateTime?.AddHours(24).AddSeconds(-1), }); } } } db.RegistrationCodes.AddRange(list); db.SaveChanges(); var codeLog = new RegistrationCodeLog { Count = list.Count, CreateDateTime = DateTime.Now, From = null, Remark = "", UserID = model.OwnUser, }; db.RegistrationCodeLogs.Add(codeLog); db.SaveChanges(); if (this.GetReturnUrl() != null) { return(Redirect(this.GetReturnUrl())); } return(RedirectToAction("Index", "UserManage", null)); } if (string.IsNullOrWhiteSpace(model.OwnUser)) { var userlist = db.Users.Where(s => s.UserType == Enums.UserType.Proxy) .Select(s => new SelectListItem() { Text = s.UserName, Value = s.Id }).ToList(); ViewBag.UserList = userlist; } else { model.Own = user; } return(View(model)); }