Esempio n. 1
0
    private void SaveUser4Page()
    {
        using (Session session = new Session())
        {
            try
            {
                User user = new User();
                user.UserName = this.txtUserName.Text.Trim();
                user.Password = this.txtPassword.Text;
                user.Email    = txtEmail.Text.Trim();
                user.Ext      = txtExt.Text.Trim();
                user.FullName = txtFullName.Text.Trim();
                if (Org.UseDefaultOrg(session))
                {
                    user.OrgId = Org.DefaultOrg(session);
                }
                else
                {
                    user.OrgId = Cast.Int(this.txtOrgId.Text, -1);
                }
                user.Status     = Cast.Enum <UserStatus>(this.rblStatus.SelectedValue);
                user.UserType   = Cast.Enum <OrgType>(this.ddlUserType.SelectedValue);
                user.ModifyBy   = SecuritySession.CurrentUser.UserId;
                user.ModifyTime = DateTime.Now;

                if (IsAddNew())
                {
                    //检查帐号是否重复
                    int existsUserCount = session.CreateEntityQuery <Magic.Sys.User>()
                                          .Where(Magic.Framework.ORM.Query.Exp.Eq("UserName", user.UserName))
                                          .Count();
                    if (existsUserCount > 0)
                    {
                        WebUtil.ShowMsg(this, string.Format("用户帐号{0}已经存在", user.UserName), "错误");
                        return; //这里的return语句,using会保证session释放
                    }
                    //UserId是自增长主键,新增时不要设置它的值,新增完毕后框架会自动设置好这个值
                    user.CreateBy   = SecuritySession.CurrentUser.UserId;
                    user.CreateTime = DateTime.Now;
                    user.Create(session);
                }
                else
                {
                    user.UserId = Cast.Int(this.txtUserId.Value, -1); //UserId是主键,更新时需要设置
                    user.Update(session, "Email", "Ext", "FullName", "OrgId", "Password", "Status", "UserType", "ModifyBy", "ModifyTime");
                }
                this.txtUserId.Value = user.UserId.ToString(); //如果是新增用户,新增完毕后将用户ID设置到以页面隐藏控件(新增后UserId已经被赋值了)
                WebUtil.ShowMsg(this, "用户保存成功", "操作成功");
            }
            catch (Exception ex)
            {
                logger.Info("保存User", ex);
                WebUtil.ShowMsg(this, "发生未处理的异常,请刷新页面重新操作,或者联系系统管理员");
            }
        }
    }