private void Window_Loaded(object sender, RoutedEventArgs e) { Operator operators = new OperatorBLL().GetOperatorById(LoginWindow.GetOperatorId()); //绑定数据源 columnOperator.ItemsSource = new OperatorBLL().ListAll(); gridShowLog.ItemsSource = new OperationLogBLL().Search(LoginWindow.GetOperatorId()); }
//点击登录按钮,实现登录 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 Window_Loaded(object sender, RoutedEventArgs e) { if (IsInsert) { } else { //修改密码时 动态为tbName 的Text属性赋值 tbName.Text = "您要修改的用户"; txtUserName.IsReadOnly = true; //把当前用户名填到txtUsername中 EditingId = LoginWindow.GetOperatorId(); Operator op = new OperatorBLL().GetOperatorById(EditingId); txtUserName.Text = op.UserName; } }
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("修改出错"); } } } }
//删除账户信息 private void menuDeletedOperator_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("确认删除此账户的数据吗?", "提醒", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { bool isAddBalanceSuccess; bool isOperatorSuccess; using (TransactionScope ts = new TransactionScope()) { isAddBalanceSuccess = new BalanceBLL().DeleteByOperatorId(LoginWindow.GetOperatorId()); isOperatorSuccess = new OperatorBLL().DeleteOperator(LoginWindow.GetOperatorId()); ts.Complete(); } if (isOperatorSuccess && isAddBalanceSuccess) { MessageBox.Show("用户删除成功!"); this.Close(); //注销重启 Application.Current.Shutdown(); System.Reflection.Assembly.GetEntryAssembly(); string startpath = System.IO.Directory.GetCurrentDirectory(); System.Diagnostics.Process.Start(startpath + "/PAMSystem.UI.exe"); } else { MessageBox.Show("用户删除失败!"); } } }