20230201_145_upperpc/InSituLaboratory/ViewModels/Pages/UserManagementViewModel.cs
2024-03-11 13:12:02 +08:00

208 lines
6.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using InSituLaboratory.Entities;
using InSituLaboratory.IService;
using InSituLaboratory.Models;
using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace InSituLaboratory.ViewModels.Pages
{
public class UserManagementViewModel : ViewModelBase
{
IUserService _userService;
IRoleService _roleService;
IDialogService _dialogService;
public UserManagementViewModel(IRegionManager regionManager, IUserService userService, IRoleService roleService, IDialogService dialogService) : base(regionManager)
{
PageTitle = "系统用户管理";
_userService = userService;
_roleService = roleService;
_dialogService = dialogService;
SelectRoleCommand = new DelegateCommand<UserModel>(DoSelectRole);
ResetPasswordCommand = new DelegateCommand<object>(DoResetPassword);
LockUserCommand = new DelegateCommand<object>(DoLockUser);
this.Refresh();
}
public ObservableCollection<UserModelEx> Users { get; set; } =
new ObservableCollection<UserModelEx>();
public DelegateCommand<UserModel> SelectRoleCommand { get; set; }
//重置密码
public DelegateCommand<object> ResetPasswordCommand { get; set; }
//锁定用户
public DelegateCommand<object> LockUserCommand { get; set; }
public override void Refresh()
{
Users.Clear();
var users = _userService.GetUsers(SearchKey).ToList();
// 数据里拿到的是角色的ID 没有角色名称
// 界面一般都会显示名称
int index = 1;
foreach (var user in users)
{
UserModelEx userModel = new UserModelEx
{
Index = index++,
UserId = user.UserId,
UserName = user.UserName,
RealName = user.RealName,
UserIcon = "pack://siteoforigin:,,,/Avatars/" + user.UserIcon,
Address = user.Address,
Age = user.Age,
Password = user.Password,
Gender = user.Gender,
Email = user.Email,
Status = user.Status,
LockButtonText = user.Status == 1 ? "锁定" : "启用"
};
var rs = _roleService.Query<SysRole>(r =>
user.Roles.Select(ur => ur.RoleId).ToList()
.Contains(r.RoleId)).ToList();
userModel.Roles = rs.Select(r => new RoleModel
{
RoleId = r.RoleId,
RoleName = r.RoleName,
}).ToList();
Users.Add(userModel);
}
}
//删除
public override void DoDelete(object model)
{
try
{
if (MessageBox.Show("是否确定删除此项?", "提示", MessageBoxButton.YesNo) ==
MessageBoxResult.Yes)
{
// 物理删除
_userService.Delete<SysUser>((model as UserModelEx).UserId);
MessageBox.Show("删除完成!", "提示");
Users.Remove(model as UserModelEx);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}
}
//编辑
public override void DoModify(object model)
{
DialogParameters ps = new DialogParameters();
ps.Add("model", model);
_dialogService.ShowDialog("ModifyUserView", ps, result =>
{
// 判断子窗口的返回状态如果OK刷新当前页面否则不管
if (result.Result == ButtonResult.OK)
{
this.Refresh();
}
});
}
/// <summary>
/// 重置用户密码
/// </summary>
/// <param name="model"></param>
public void DoResetPassword(object model)
{
try
{
if (MessageBox.Show("是否确定重置当前用户密码?", "提示", MessageBoxButton.YesNo) ==
MessageBoxResult.Yes)
{
var user = _userService.Find<SysUser>((model as UserModel).UserId);
user.Password = "123456";
_userService.Update<SysUser>(user);
MessageBox.Show("重置完成!", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}
}
/// <summary>
/// 锁定用户
/// </summary>
/// <param name="model"></param>
public void DoLockUser(object model)
{
try
{
var ui = model as UserModelEx;
var user = _userService.Find<SysUser>(ui.UserId);
if (ui.Status == 1)
{
if (MessageBox.Show("是否锁定前用户?", "提示", MessageBoxButton.YesNo) ==
MessageBoxResult.Yes)
{
user.Status = 0;
}
}
else
{
user.Status = 1;
}
_userService.Update<SysUser>(user);
MessageBox.Show("操作已完成!", "提示");
ui.Status = user.Status;
this.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}
}
/// <summary>
/// 编辑角色
/// </summary>
/// <param name="model"></param>
private void DoSelectRole(UserModel model)
{
DialogParameters ps = new DialogParameters();
ps.Add("model", model);
_dialogService.ShowDialog("SelectRoleView", ps, result =>
{
// 判断子窗口的返回状态如果OK刷新当前页面否则不管
if (result.Result == ButtonResult.OK)
{
this.Refresh();
}
});
}
}
}