Exemple #1
0
        //点击登录按钮,实现登录
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (txtUserName.Text.Length <= 0)
            {
                MessageBox.Show("请输入用户名!");
            }
            else if (pwdPassword.Password.Length <= 0)
            {
                MessageBox.Show("请输入密码!");
            }
            else
            {
                Operator op = new Operator();
                string userName = txtUserName.Text.Trim();

                Application.Current.Properties["OperatorName"] = userName;

                string password = pwdPassword.Password;
                //OperatorBLL bll = new OperatorBLL();
                LoginResult result = new OperatorBLL().UserLoginResult(userName, password);
                OperatorBLL bll = new OperatorBLL();
                if (result == LoginResult.ErrorNameOrPwd)
                {
                    op = bll.GetOperatorByUserName(userName);
                    if (op != null)
                    {
                        new OperationLogBLL().Insert(op.Id, "尝试登陆失败!");
                    }
                        MessageBox.Show("用户名或密码错误");
                }
                else
                {
                    //MessageBox.Show("登录成功");
                    //获得登录用户的Id,存到Application.Current.Properties里面的在程序其他地方也可以取
                    op = bll.GetOperatorByUserName(userName);
                    Application.Current.Properties["OperatorId"] = op.Id;
                    new OperationLogBLL().Insert(op.Id, "登陆成功!");
                    //关闭LoginWindow,进入主窗口
                    DialogResult = true;
                }
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //判断是插入 还是编辑
            //添加
            if (IsInsert)
            {

                if (txtUserName.Text.Length <= 0)
                {
                    MessageBox.Show("请输入用户名!");
                }
                if (pwdPassword.Password.Length <= 0)
                {
                    MessageBox.Show("请输入密码!");
                }
                else if (pwdRePassword.Password.Length <= 0)
                {
                    MessageBox.Show("请再次输入密码!");
                }
                else if (pwdPassword.Password != pwdRePassword.Password)
                {
                    MessageBox.Show("两次输入密码不一致!");
                }
                else
                {
                    //添加用户
                    string userName = txtUserName.Text.Trim();
                    string password = pwdPassword.Password;
                    OperatorBLL bll = new OperatorBLL();
                    int i = 0, j = 0;
                    //打开Distributed Transaction Coordinator服务
                    // 引用System.Transaction
                    //用分布式事务实现添加新用户的的同时添加改账户的余额
                    using (TransactionScope ts = new TransactionScope())
                    {

                        Operator oper = new OperatorBLL().GetOperatorByUserName(userName);
                        //数据库中是否已有此用户名
                        if (oper == null)    //没有就添加
                        {
                            i = bll.AddOpertator(userName, password);
                            Operator op = bll.GetOperatorByUserName(userName);
                            new BalanceBLL().AddNew(op.Id);
                        }
                        //若有是否已删除
                        else
                        {
                            //恢复已删除用户

                            if (oper.IsDeleted)
                            {

                                new OperatorBLL().ReuseOperator(oper.Id);  //恢复
                                j = new OperatorBLL().EditOperator(oper.Id, password);//更改密码
                                new BalanceBLL().ReuseByOperatorId(oper.Id);   //恢复余额
                                new BalanceBLL().Update(oper.Id, 0);          //初始化余额为0

                            }
                            else
                            {
                                MessageBox.Show("用户名重复,请更换用户名!");
                            }
                        }

                        ts.Complete();
                    }
                    if (i >= 1 || j > 0)
                    {

                        MessageBox.Show("成功添加用户!");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加用户失败!");
                        this.Close();
                    }
                }
            }
            //编辑
            else
            {
                if (pwdPassword.Password.Length <= 0)
                {
                    MessageBox.Show("请输入密码!");
                }
                else if (pwdRePassword.Password.Length <= 0)
                {
                    MessageBox.Show("请再次输入密码!");
                }
                else if (pwdPassword.Password != pwdRePassword.Password)
                {
                    MessageBox.Show("两次输入密码不一致!");
                }
                else
                {
                    txtUserName.IsReadOnly = true;
                    string pwd = pwdPassword.Password;
                    Guid operatorId = LoginWindow.GetOperatorId();
                    int i = new OperatorBLL().EditOperator(operatorId, pwd);
                    if (i >= 1)
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                    }
                    if (i <= 0)
                    {
                        MessageBox.Show("修改出错");
                    }
                }
            }
        }