375 lines
11 KiB
C#
375 lines
11 KiB
C#
using Cowain.Bake.BLL;
|
|
using Cowain.Bake.Common;
|
|
using Cowain.Bake.Common.Enums;
|
|
using Cowain.Bake.Model;
|
|
using Prism.Regions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Unity;
|
|
using Prism.Commands;
|
|
using System.Windows;
|
|
using HandyControl.Controls;
|
|
using Cowain.Bake.Model.Entity;
|
|
using System.ComponentModel;
|
|
using System.Windows.Input;
|
|
using System.Text.Json;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
namespace Cowain.Bake.UI.UserManagerment.ViewModels
|
|
{
|
|
class UserManagermentViewModel : ViewModelBase, INotifyPropertyChanged
|
|
{
|
|
public UserManagermentViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
|
|
{
|
|
this.PageTitle = "用户管理";
|
|
userManagermentService = _unityContainer.Resolve<UserService>();
|
|
SetRoleList();
|
|
}
|
|
|
|
//记忆当前所有的用户
|
|
public List<TUserManage> CurrentUserList { get; set; }
|
|
|
|
//公共服务字段
|
|
private UserService userManagermentService { get; set; }
|
|
|
|
public new event PropertyChangedEventHandler PropertyChanged;
|
|
public void OnPropertyChanged(string proName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(proName));
|
|
}
|
|
//datagrid数据源
|
|
private ObservableCollection<TUserManage> userList;
|
|
public ObservableCollection<TUserManage> UserList
|
|
{
|
|
get => userList ?? (userList = new ObservableCollection<TUserManage>());
|
|
set
|
|
{
|
|
if (value != null)
|
|
{
|
|
userList = value;
|
|
OnPropertyChanged("UserList");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//隐藏增加用户的布局
|
|
private Visibility addUserVisibility = Visibility.Collapsed;
|
|
|
|
public Visibility AddUserVisibility
|
|
{
|
|
get { return addUserVisibility; }
|
|
set
|
|
{
|
|
if (addUserVisibility != value)
|
|
{
|
|
addUserVisibility = value;
|
|
OnPropertyChanged("AddUserVisibility");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//用户搜索框
|
|
private string textBoxContent;
|
|
public string TextBoxContent
|
|
{
|
|
get { return textBoxContent; }
|
|
set
|
|
{
|
|
if (textBoxContent != value)
|
|
{
|
|
textBoxContent = value;
|
|
OnPropertyChanged("TextBoxContent");
|
|
}
|
|
}
|
|
}
|
|
//用户框
|
|
private string userNameBoxContent;
|
|
public string UserNameBoxContent
|
|
{
|
|
get { return userNameBoxContent; }
|
|
set
|
|
{
|
|
if (userNameBoxContent != value)
|
|
{
|
|
userNameBoxContent = value;
|
|
OnPropertyChanged("UserNameBoxContent");
|
|
}
|
|
}
|
|
}
|
|
//账号框
|
|
private string userIdBoxContent;
|
|
public string UserIdBoxContent
|
|
{
|
|
get { return userIdBoxContent; }
|
|
set
|
|
{
|
|
if (userIdBoxContent != value)
|
|
{
|
|
userIdBoxContent = value;
|
|
OnPropertyChanged("UserIdBoxContent");
|
|
}
|
|
}
|
|
}
|
|
//密码框
|
|
private string pwdBoxContent;
|
|
public string PwdBoxContent
|
|
{
|
|
get { return pwdBoxContent; }
|
|
set
|
|
{
|
|
if (pwdBoxContent != value)
|
|
{
|
|
pwdBoxContent = value;
|
|
OnPropertyChanged("PwdBoxContent");
|
|
}
|
|
}
|
|
}
|
|
|
|
//确认密码框
|
|
//private string confirmPwdBoxContent;
|
|
//public string ConfirmPwdBoxContent
|
|
//{
|
|
// get { return confirmPwdBoxContent; }
|
|
// set
|
|
// {
|
|
// if (confirmPwdBoxContent != value)
|
|
// {
|
|
// confirmPwdBoxContent = value;
|
|
// OnPropertyChanged("ConfirmPwdBoxContent");
|
|
// }
|
|
// }
|
|
//}
|
|
//角色下拉框
|
|
private string roleComboboxSelected;
|
|
public string RoleComboboxSelected
|
|
{
|
|
get { return roleComboboxSelected; }
|
|
set
|
|
{
|
|
if (roleComboboxSelected != value)
|
|
{
|
|
roleComboboxSelected = value;
|
|
OnPropertyChanged("RoleComboboxSelected");
|
|
}
|
|
}
|
|
}
|
|
|
|
private TUserManage selectedUser;
|
|
public TUserManage SelectedUser
|
|
{
|
|
get { return selectedUser; }
|
|
set
|
|
{
|
|
if (selectedUser != value)
|
|
{
|
|
selectedUser = value;
|
|
OnPropertyChanged("SelectedUser");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AsyncRefreshTask()
|
|
{
|
|
Application.Current?.Dispatcher?.Invoke(new Action(() =>
|
|
{
|
|
Refresh();
|
|
}));
|
|
}
|
|
|
|
public async override void Refresh()
|
|
{
|
|
UserList.Clear();
|
|
if (CurrentUserList!=null)
|
|
{
|
|
CurrentUserList.Clear();
|
|
|
|
}
|
|
var listUser = await System.Threading.Tasks.Task.Run(() =>
|
|
{
|
|
var userService = _unityContainer.Resolve<UserService>();
|
|
//从数据库查询任务
|
|
return userService.GetAllUsers();
|
|
});
|
|
|
|
listUser.ForEach(x => UserList.Add(x));
|
|
CurrentUserList = listUser;
|
|
}
|
|
|
|
public List<string> RoleList { get; set; }
|
|
public void SetRoleList()
|
|
{
|
|
RoleList = new List<string>();
|
|
foreach(ERole role in Enum.GetValues(typeof(ERole)))
|
|
{
|
|
RoleList.Add(role.GetDescription());
|
|
}
|
|
}
|
|
//用户查询
|
|
public DelegateCommand QueryUserCommand => new DelegateCommand(() =>
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(TextBoxContent))
|
|
{
|
|
Refresh();
|
|
return;
|
|
}
|
|
|
|
var queryList = userManagermentService.QueryUser(TextBoxContent);
|
|
if (queryList.Count > 0)
|
|
{
|
|
UserList.Clear();
|
|
UserList.Add(queryList[0]);
|
|
Growl.Info("查询成功!");
|
|
|
|
}
|
|
else
|
|
{
|
|
Growl.Info("没有数据!");
|
|
}
|
|
});
|
|
|
|
//更改用户是否有效
|
|
public DelegateCommand<string> UserValidCommand => new DelegateCommand<string>((y) =>
|
|
{
|
|
if (SelectedUser == null)
|
|
{
|
|
Growl.Info("请选择用户!");
|
|
return;
|
|
}
|
|
var result = HandyControl.Controls.MessageBox.Ask("确认更改?", "操作提示");
|
|
if (result == MessageBoxResult.Cancel)
|
|
{
|
|
return;
|
|
}
|
|
if (y == EValidStatus.Invalid.ToString())
|
|
{
|
|
if (SelectedUser.Valid!=false)
|
|
{
|
|
SelectedUser.Valid = false;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
}
|
|
else if (y == EValidStatus.Valid.ToString())
|
|
{
|
|
if (SelectedUser.Valid!=true)
|
|
{
|
|
SelectedUser.Valid = true;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
userManagermentService.Update(SelectedUser);
|
|
Refresh();
|
|
Growl.Success("更改成功!");
|
|
|
|
});
|
|
|
|
|
|
public DelegateCommand ShowAddUserCommand => new DelegateCommand(() =>
|
|
{
|
|
AddUserVisibility = Visibility.Visible;
|
|
|
|
});
|
|
|
|
public DelegateCommand AddUserCommand => new DelegateCommand(() =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(UserIdBoxContent))
|
|
{
|
|
Growl.Info("请输入账号!");
|
|
return;
|
|
}
|
|
if (CurrentUserList.Where(a => a.UserId == UserIdBoxContent).ToList().Count > 0)
|
|
{
|
|
Growl.Info("账号已经存在!");
|
|
return;
|
|
}
|
|
//if (PwdBoxContent != ConfirmPwdBoxContent)
|
|
//{
|
|
// Growl.Info("密码不一致!");
|
|
// return;
|
|
//}
|
|
if (string.IsNullOrWhiteSpace(RoleComboboxSelected))
|
|
{
|
|
Growl.Info("请选择用户角色!");
|
|
return;
|
|
}
|
|
TUserManage addUserItem = new TUserManage()
|
|
{
|
|
UserId = UserIdBoxContent,
|
|
UserName = UserNameBoxContent,
|
|
Password = PwdBoxContent,
|
|
//将枚举描述转换成对应int
|
|
RoleId = (int)EnumHelper.GetValueByDescription<ERole>(roleComboboxSelected),
|
|
Valid = true,
|
|
};
|
|
if (userManagermentService.Insert(addUserItem)>0)
|
|
{
|
|
UserList.Add(addUserItem);
|
|
AddUserVisibility = Visibility.Collapsed;
|
|
Growl.Success("新增成功!");
|
|
};
|
|
});
|
|
public DelegateCommand DeleteUserCommand => new DelegateCommand(() =>
|
|
{
|
|
if (SelectedUser == null)
|
|
{
|
|
Growl.Info("请选择用户!");
|
|
return;
|
|
}
|
|
|
|
var result = HandyControl.Controls.MessageBox.Ask($"用户名:{selectedUser.UserName},是否删除?", "操作提示");
|
|
if (result == MessageBoxResult.Cancel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (0 == userManagermentService.Delete(selectedUser))
|
|
{
|
|
Growl.Warning("删除失败!");
|
|
return;
|
|
}
|
|
|
|
Growl.Success($"删除用户【{selectedUser.UserName}】成功!");
|
|
_unityContainer.Resolve<LogService>().AddLog($"删除用户:{selectedUser.UserName}", E_LogType.Operate.ToString());
|
|
Refresh();
|
|
|
|
});
|
|
public DelegateCommand InitPwdCommand => new DelegateCommand(()=>
|
|
{
|
|
var result = HandyControl.Controls.MessageBox.Ask("是否初始化?","操作提示");
|
|
if (result == MessageBoxResult.Cancel)
|
|
{
|
|
return;
|
|
}
|
|
if (selectedUser != null)
|
|
{
|
|
selectedUser.Password = "123456";
|
|
userManagermentService.Update(selectedUser);
|
|
Growl.Success("初始化成功");
|
|
}
|
|
});
|
|
|
|
public DelegateCommand CancelSaveCommand => new DelegateCommand(() =>
|
|
{
|
|
AddUserVisibility = Visibility.Collapsed;
|
|
UserIdBoxContent = string.Empty;
|
|
UserNameBoxContent = string.Empty;
|
|
PwdBoxContent = string.Empty;
|
|
|
|
});
|
|
}
|
|
}
|