/// <summary> /// 添加用户信息 /// </summary> /// <param name="userInfo">添加用户信息实体</param> /// <returns>添加结果</returns> public byte Create(AddUserInfoViewModel userInfo) { using (DataSubmittedEntities db = new DataSubmittedEntities()) { //查找用户表中是否已经存在该用户 var user = db.OT_User.Where(a => a.Name == userInfo.UserName).Select(a => new { UserId = a.Id, IsDelete = a.IsDelete }).ToList(); //有记录,则返回用户已存在 if (user != null && user.Count > 0) { if (user[0].IsDelete == (byte)EUserStatus.Normal) { return((byte)EResult.IsRepeat); } else { return((byte)EResult.IsRepeatDel); }; } using (TransactionScope transaction = new TransactionScope()) { //构建并添加用户信息 OT_User userModel = new OT_User(); userModel.Id = Guid.NewGuid(); userModel.IsDelete = (byte)EUserStatus.Normal; userModel.Name = userInfo.UserName; userModel.Password = ConfigurationManager.AppSettings["StartPassword"]; db.OT_User.Add(userModel); //构建添加用户角色关系 foreach (Guid roleId in userInfo.RoleIdList) { OT_UserRole model = new OT_UserRole(); model.Id = Guid.NewGuid(); model.RoleId = roleId; model.UserId = userModel.Id; model.IsDelete = (byte)EDataStatus.Normal; db.OT_UserRole.Add(model); } return(Result.SaveChangesResult(db, transaction).ResultKey); } } }
public void TestAdd() { AddUserInfoViewModel model = new AddUserInfoViewModel(); model.UserName = "******"; List <Guid> list = new List <Guid>(); list.Add(Guid.Parse("7d03e715-c233-4047-9679-d3590d4d6f7c")); list.Add(Guid.Parse("557e6ac5-2b26-46d4-a821-fd86b2e2c6d1")); model.RoleIdList = list; us.Create(model); model.UserName = "******"; list.Clear(); list.Add(Guid.Parse("7d03e715-c233-4047-9679-d3590d4d6f7c")); model.RoleIdList = list; us.Create(model); }
public JsonResult Add(List <Guid> roleList, string userName) { if (!(roleList != null && roleList.Count > 0)) { return(Json((byte)EResult.IsNull1, JsonRequestBehavior.DenyGet)); } if (string.IsNullOrEmpty(userName.Trim())) { return(Json((byte)EResult.IsNull2, JsonRequestBehavior.DenyGet)); } AddUserInfoViewModel userInfoModel = new AddUserInfoViewModel(); userInfoModel.UserName = userName; userInfoModel.RoleIdList = roleList; byte result = userManage.Create(userInfoModel); ReportFactory.Instance.log.WriteLog(Common.OperationType.Update, string.Format("添加了新用户{0}", userName)); return(Json(result)); }
/// <summary> /// 学生信息添加 /// </summary> /// <param name="addUserInfo"></param> /// <returns></returns> public async Task <bool> Create(AddUserInfoViewModel addUserInfo) { try { var userInfo = new UserInfo() { UserName = addUserInfo.UserName, Sex = addUserInfo.Sex, Hobby = addUserInfo.Hobby, Phone = addUserInfo.Phone, Description = addUserInfo.Description }; _shoSchoolUserInfoContext.UserInfos.Add(userInfo); await _shoSchoolUserInfoContext.SaveChangesAsync(); return(true); } catch { return(false); } }
public async Task <IActionResult> AddUserInfo(/*[FromBody]*/ AddUserInfoViewModel userInfo) { var result = await _userInfoService.Create(userInfo); return(Json(result ? new { code = 0, resultMsg = "添加成功" } : new { code = 1, resultMsg = "网络打瞌睡了!" })); }