示例#1
0
 public void SaveForm(string keyValue, UserRoleEntity userRoleEntity)
 {
     if (!string.IsNullOrEmpty(keyValue))
     {
         Base_UserRole role = Base_UserRole.SingleOrDefault("where UserRoleId=@0", keyValue);
         role            = EntityConvertTools.CopyToModel <UserRoleEntity, Base_UserRole>(userRoleEntity, role);
         role.UserRoleId = keyValue;
         role.Update();
     }
     else
     {
         Base_UserRole role = EntityConvertTools.CopyToModel <UserRoleEntity, Base_UserRole>(userRoleEntity, null);
         role.Insert();
     }
 }
示例#2
0
        /// <summary>
        /// 保存用户表单(新增、修改)
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="userEntity">用户实体</param>
        /// <returns></returns>
        public string SaveForm(string keyValue, UserEntity userEntity)
        {
            try
            {
                using (var tran = QSDMS_SQLDB.GetInstance().GetTransaction())
                {
                    #region 基本信息
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        userEntity.Modify(keyValue);
                        userEntity.Password = null;
                        Base_User model = Base_User.SingleOrDefault("where UserId=@0", keyValue);
                        model        = EntityConvertTools.CopyToModel <UserEntity, Base_User>(userEntity, model);
                        model.UserId = keyValue;
                        model.Update();
                    }
                    else
                    {
                        userEntity.Create();
                        keyValue               = userEntity.UserId;
                        userEntity.Secretkey   = Md5Helper.MD5(CommonHelper.CreateNo(), 16).ToLower();
                        userEntity.Password    = Md5Helper.MD5(DESEncrypt.Encrypt(userEntity.Password, userEntity.Secretkey).ToLower(), 32).ToLower();
                        userEntity.EnabledMark = 1;
                        userEntity.DeleteMark  = 0;
                        Base_User model = EntityConvertTools.CopyToModel <UserEntity, Base_User>(userEntity, null);
                        model.Insert();
                    }
                    #endregion

                    #region 默认添加 角色、岗位、职位
                    Base_UserRelation.Delete("where UserId=@0 and IsDefault=1", userEntity.UserId);
                    List <UserRelationEntity> userRelationEntitys = new List <UserRelationEntity>();
                    //角色 这里多个角色逻辑处理
                    //if (!string.IsNullOrEmpty(userEntity.RoleId))
                    //{
                    //    userRelationEntitys.Add(new UserRelationEntity
                    //    {
                    //        Category = (int)QSDMS.Model.Enums.UserCategoryEnum.角色,
                    //        UserRelationId = Guid.NewGuid().ToString(),
                    //        UserId = userEntity.UserId,
                    //        ObjectId = userEntity.RoleId,
                    //        CreateDate = DateTime.Now,
                    //        CreateUserId = OperatorProvider.Provider.Current().UserId,
                    //        CreateUserName = OperatorProvider.Provider.Current().UserName,
                    //        IsDefault = 1,
                    //    });
                    //}
                    //一个用户多个角色
                    if (!string.IsNullOrEmpty(userEntity.RoleId))
                    {
                        Base_UserRole.Delete("where UserId=@0", userEntity.UserId);
                        string[] roles = userEntity.RoleId.Split(',');
                        for (int i = 0; i < roles.Length; i++)
                        {
                            //用户角色表
                            string roleid   = roles[i];
                            var    userrole = new UserRoleEntity();
                            userrole.UserRoleId = Util.Util.NewUpperGuid();
                            userrole.UserId     = userEntity.UserId;
                            userrole.RoleId     = roleid.Split('|')[0];
                            userrole.RoleName   = roleid.Split('|')[1];
                            Base_UserRole model = EntityConvertTools.CopyToModel <UserRoleEntity, Base_UserRole>(userrole, null);
                            model.Insert();

                            //用户关系表
                            userRelationEntitys.Add(new UserRelationEntity
                            {
                                Category       = (int)QSDMS.Model.Enums.UserCategoryEnum.角色,
                                UserRelationId = Guid.NewGuid().ToString(),
                                UserId         = userEntity.UserId,
                                ObjectId       = userrole.RoleId,
                                CreateDate     = DateTime.Now,
                                CreateUserId   = OperatorProvider.Provider.Current().UserId,
                                CreateUserName = OperatorProvider.Provider.Current().UserName,
                                IsDefault      = 1,
                            });
                        }
                    }
                    //岗位
                    if (!string.IsNullOrEmpty(userEntity.DutyId))
                    {
                        userRelationEntitys.Add(new UserRelationEntity
                        {
                            Category       = (int)QSDMS.Model.Enums.UserCategoryEnum.岗位,
                            UserRelationId = Guid.NewGuid().ToString(),
                            UserId         = userEntity.UserId,
                            ObjectId       = userEntity.DutyId,
                            CreateDate     = DateTime.Now,
                            CreateUserId   = OperatorProvider.Provider.Current().UserId,
                            CreateUserName = OperatorProvider.Provider.Current().UserName,
                            IsDefault      = 1,
                        });
                    }
                    //职位
                    if (!string.IsNullOrEmpty(userEntity.PostId))
                    {
                        userRelationEntitys.Add(new UserRelationEntity
                        {
                            Category       = (int)QSDMS.Model.Enums.UserCategoryEnum.职位,
                            UserRelationId = Guid.NewGuid().ToString(),
                            UserId         = userEntity.UserId,
                            ObjectId       = userEntity.PostId,
                            CreateDate     = DateTime.Now,
                            CreateUserId   = OperatorProvider.Provider.Current().UserId,
                            CreateUserName = OperatorProvider.Provider.Current().UserName,
                            IsDefault      = 1,
                        });
                    }
                    //插入用户关系表
                    foreach (UserRelationEntity userRelationItem in userRelationEntitys)
                    {
                        Base_UserRelation model = EntityConvertTools.CopyToModel <UserRelationEntity, Base_UserRelation>(userRelationItem, null);
                        model.Insert();
                    }
                    #endregion

                    Base_UserAuthorize.Delete("where UserId=@0", userEntity.UserId);
                    //插入用户对应数据权限
                    if (!string.IsNullOrEmpty(userEntity.AuthorizeDataId))
                    {
                        string[] uthorizeDatas = userEntity.AuthorizeDataId.Split(',');
                        for (int i = 0; i < uthorizeDatas.Length; i++)
                        {
                            string objectid      = uthorizeDatas[i];
                            var    userAuthorize = new UserAuthorizeEntity();
                            userAuthorize.UserAuthorizeId = Util.Util.NewUpperGuid();
                            userAuthorize.UserId          = userEntity.UserId;
                            userAuthorize.ObjectId        = objectid.Split('|')[0];
                            userAuthorize.ObjectName      = objectid.Split('|')[1];
                            Base_UserAuthorize model = EntityConvertTools.CopyToModel <UserAuthorizeEntity, Base_UserAuthorize>(userAuthorize, null);
                            model.Insert();
                        }
                    }

                    tran.Complete();
                }
                return(keyValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }