Ejemplo n.º 1
0
 /// <summary>
 /// 角色分配
 /// </summary>
 /// <param name="listInfo">用户角色关系实体集合</param>
 /// <returns>分配结果</returns>
 public byte RoleAssign(RoleAssignViewModel listInfo)
 {
     using (DataSubmittedEntities db = new DataSubmittedEntities())
     {
         using (TransactionScope transaction = new TransactionScope())
         {
             //删除用户角色关系
             var list = db.OT_UserRole.Where(a => listInfo.UserIdList.Contains(a.UserId)).ToList();
             db.OT_UserRole.RemoveRange(list);
             //构建并添加用户角色关系
             foreach (Guid uesrId in listInfo.UserIdList)
             {
                 foreach (Guid roleId in listInfo.RoleIdList)
                 {
                     if (roleId == null)
                     {
                         return (byte)EResult.IsNull3;
                     }
                     OT_UserRole userRoleInfo = new OT_UserRole();
                     userRoleInfo.Id = Guid.NewGuid();
                     userRoleInfo.IsDelete = (byte)EDataStatus.Normal;
                     userRoleInfo.RoleId = roleId;
                     userRoleInfo.UserId = uesrId;
                     db.OT_UserRole.Add(userRoleInfo);
                 }
             }
             return Result.SaveChangesResult(db, transaction).ResultKey;
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo">修改用户信息实体</param>
        /// <returns>结果</returns>
        public CustomResult Update(UpdateUserInfoViewModel userInfo)
        {
            using (DataSubmittedEntities db = new DataSubmittedEntities())
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    CustomResult pReturnValue = new CustomResult();

                    //查找用户表中是否已经存在该用户
                    var user = db.OT_User.Where(a => a.Name == userInfo.UserName&a.Id!=userInfo.UserId).Select(a => new
                    {
                        UserId = a.Id,
                        IsDelete = a.IsDelete
                    }).ToList();

                    //有记录,则返回用户已存在
                    if (user != null && user.Count > 0)
                    {
                        if (user[0].IsDelete == (byte)EUserStatus.Normal)
                        {
                            pReturnValue.ResultKey = (byte)EResult.IsRepeat;
                            pReturnValue.ResultValue = Wttech.DataSubmitted.Common.Resources.TipInfo.AddFaileHasUser;
                        }
                        else
                        {
                            pReturnValue.ResultKey = (byte)EResult.IsRepeatDel;
                            pReturnValue.ResultValue = Wttech.DataSubmitted.Common.Resources.TipInfo.AddFaileHasDelUser;
                            
                        }
                        return pReturnValue;
                    }

                    //修改用户名
                    var listUserName = db.OT_User.Where(a => a.Id == userInfo.UserId).ToList();

                    //如果用户不存在,这返回,说明数据不是正常输入
                    if (!(listUserName != null && listUserName.Count > 0))
                    {
                        pReturnValue.ResultKey = (byte)EResult.IsNull4;
                    }

                    listUserName[0].Name = userInfo.UserName;

                    //获取用户角色关系
                    var list = db.OT_UserRole.Where(a => a.UserId == userInfo.UserId & a.IsDelete == (byte)EDataStatus.Normal).ToList();

                    //删除用户角色关系
                    foreach (var model in list)
                    {
                        model.IsDelete = (byte)EDataStatus.IsDelete;
                    }

                    //添加用户角色关系
                    foreach (Guid roleId in userInfo.RoleIdList)
                    {
                        OT_UserRole userRoleInfo = new OT_UserRole();
                        userRoleInfo.Id = Guid.NewGuid();
                        userRoleInfo.IsDelete = (byte)EDataStatus.Normal;
                        userRoleInfo.RoleId = roleId;
                        userRoleInfo.UserId = userInfo.UserId;
                        db.OT_UserRole.Add(userRoleInfo);
                    }
                    pReturnValue = Result.SaveChangesResult(db, transaction);
                    return pReturnValue;
                }
            }
        }
Ejemplo n.º 3
0
        /// <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;
                }
            }
        }