using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using GummingCommon; using GummingEntity; using GummingSupport; using GummingControl; using System.Text; using GummingBusiness; using Newtonsoft.Json; using GummingLine; namespace Gumming { public class SysUserViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public SysUserView View { get; set; } private bool IsConfirm; private bool isLoading; private AppEnum.PathType _CurrentPathType; public AppEnum.PathType CurrentPathType { get { return _CurrentPathType; } set { _CurrentPathType = value; OnPropertyChanged("CurrentPathType"); } } #endregion #region constructor public SysUserViewModule() { View = new SysUserView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } private void InitializeCommands() { NewCommand = new DelegateCommand(OnNewCommand); ModifyCommand = new DelegateCommand(OnModifyCommand); ModifyPasswordCommand = new DelegateCommand(OnModifyPasswordCommand); DeleteCommand = new DelegateCommand(OnDeleteCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; PagerControlViewer = new PagerControlViewModule(); PagerControlViewer.OnPager += new PagerControlViewModule.PagerHandler(PagerControlView_OnPager); StartLoadTestItem(); } ~SysUserViewModule() { } #endregion #region Binding Properties public string WindowTitle { get; private set; } private string _TotalRecords; public string TotalRecords { get { return _TotalRecords; } set { _TotalRecords = value; OnPropertyChanged("TotalRecords"); } } private ObservableCollection _SysUsers; public ObservableCollection SysUsers { get { return _SysUsers; } set { _SysUsers = value; OnPropertyChanged("SysUsers"); } } private SysUserEntity _SelectedUser; public SysUserEntity SelectedUser { get { return _SelectedUser; } set { _SelectedUser = value; OnPropertyChanged("SelectedUser"); } } private PagerControlViewModule _PagerControlViewer; public PagerControlViewModule PagerControlViewer { get { return _PagerControlViewer; } set { _PagerControlViewer = value; OnPropertyChanged("PagerControlViewer"); } } #endregion #region Binding Commands private ICommand _NewCommand; public ICommand NewCommand { get { return _NewCommand; } set { _NewCommand = value; OnPropertyChanged("NewCommand"); } } private ICommand _ModifyCommand; public ICommand ModifyCommand { get { return _ModifyCommand; } set { _ModifyCommand = value; OnPropertyChanged("ModifyCommand"); } } private ICommand _ModifyPasswordCommand; public ICommand ModifyPasswordCommand { get { return _ModifyPasswordCommand; } set { _ModifyPasswordCommand = value; OnPropertyChanged("ModifyPasswordCommand"); } } private ICommand _DeleteCommand; public ICommand DeleteCommand { get { return _DeleteCommand; } set { _DeleteCommand = value; OnPropertyChanged("DeleteCommand"); } } #endregion #region Private Methods private void PagerControlView_OnPager(int currentpagenumber, int currentrows) { pagenumber = currentpagenumber; rows = currentrows; if (!isLoading) { Thread t = new Thread(StartLoadTestItem); t.IsBackground = true; t.Start(); } } private void StartLoadTestItem() { try { isLoading = true; SysUsers = new ObservableCollection(); var record = SysUserDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and 1=1 " }); List users = (List)record.rows; View.Dispatcher.Invoke((Action)(() => { foreach (var s in users) { var role = Global.SysRoles.FirstOrDefault(q => q.RoleCode == s.RoleId); if (role != null) { s.RoleName = role.RoleName; s.RoleDesc = role.RoleDesc; } SysUsers.Add(s); } OnPropertyChanged("HartSysUser"); TotalRecords = string.Format("设置记录数: {0}", record.records);//.ToString("D5")); PagerControlViewer.SetPager(rows, record.page, record.total, record.Count, record.records); isLoading = false; })); } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } finally { CloseProgressView(); } } private void OnNewCommand(Object sender) { SysUserEditViewModule userEditor = new SysUserEditViewModule(); userEditor.ShowDialog(); if (userEditor.IsConfirm) { StartLoadTestItem(); } } private void OnModifyCommand(Object sender) { if (SelectedUser == null) { ShowErrorBox("至少选择一个用户进行修改!"); return; } if (SelectedUser.UserType == 1) { ShowErrorBox("不能修改默认用户!"); return; } SysUserEditViewModule userEditor = new SysUserEditViewModule(); userEditor.UserId = SelectedUser.RecId; userEditor.UserName = SelectedUser.UserName; userEditor.PasswordText = Encryption.Decrypt(SelectedUser.Password); userEditor.PasswordText2 = userEditor.PasswordText; userEditor.SelectedUserRole = userEditor.UserRoles.FirstOrDefault(q=>q.RoleCode == SelectedUser.RoleId); userEditor.ShowDialog(); if (userEditor.IsConfirm) { SelectedUser.UserName = userEditor.UserName; SelectedUser.Password = Encryption.Encrypt(userEditor.PasswordText); SelectedUser.RoleId = userEditor.SelectedUserRole.RoleCode; } } private void OnModifyPasswordCommand(Object sender) { if (SelectedUser == null) { ShowErrorBox("至少选择一个用户进行修改!"); return; } PasswordEditViewModule userEditor = new PasswordEditViewModule(); userEditor.UserId = SelectedUser.RecId; userEditor.RoleId = SelectedUser.RoleId; userEditor.UserName = SelectedUser.UserName; userEditor.PasswordText = Encryption.Decrypt(SelectedUser.Password); userEditor.PasswordText2 = userEditor.PasswordText; userEditor.ShowDialog(); if (userEditor.IsConfirm) { SelectedUser.UserName = userEditor.UserName; SelectedUser.Password = Encryption.Encrypt(userEditor.PasswordText); SelectedUser.RoleId = userEditor.RoleId; } } private void OnDeleteCommand(Object sender) { if (!SysUsers.Any(q=>q.IsChecked)) { ShowErrorBox("至少选择一个用户进行删除!"); return; } string deleteItems = string.Empty; var checkedItems = SysUsers.Where(q => q.IsChecked).ToList(); for (int i = 0; i < checkedItems.Count; i++) { deleteItems = deleteItems + checkedItems[i].UserName + ","; } deleteItems = deleteItems.TrimEnd(','); IMessageBoxService confirm = new MessageBoxServiceViewModule(); confirm.ShowOkCancel(string.Format("确定要删除 {0} 这些用户吗 ?", deleteItems), CustomDialogIcons.OK); if (confirm.DialogState == DialogState.Ok) { for (int i = 0; i < checkedItems.Count; i++) { if (checkedItems[i].UserType != 1) { SysUserDA.Delete(checkedItems[i].RecId); } } StartLoadTestItem(); } } #endregion } }