113 lines
3.1 KiB
C#
113 lines
3.1 KiB
C#
using InSituLaboratory.Entities;
|
|
using InSituLaboratory.IService;
|
|
using InSituLaboratory.Models;
|
|
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.Dialogs
|
|
{
|
|
public class SelectUserViewModel : DialogViewModelBase
|
|
{
|
|
private List<UserModel> _users;
|
|
|
|
public ObservableCollection<UserModel> Users { get; set; } =
|
|
new ObservableCollection<UserModel>();
|
|
|
|
|
|
private string _fileterText;
|
|
|
|
/// <summary>
|
|
/// 关键字搜索用户
|
|
/// </summary>
|
|
public string FilterText
|
|
{
|
|
get { return _fileterText; }
|
|
set
|
|
{
|
|
_fileterText = value;
|
|
|
|
Users.Clear();
|
|
var us = _users.Where(u =>
|
|
string.IsNullOrEmpty(value) ||
|
|
u.RealName.Contains(value) ||
|
|
u.UserName.Contains(value)
|
|
).ToList();
|
|
us.ForEach(u => Users.Add(u));
|
|
}
|
|
}
|
|
|
|
|
|
IUserService _userService;
|
|
IRoleService _roleService;
|
|
public SelectUserViewModel(IUserService userService, IRoleService roleService)
|
|
{
|
|
_userService = userService;
|
|
_roleService = roleService;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 选择用户弹窗
|
|
/// </summary>
|
|
private int _rid = 0;
|
|
public override void OnDialogOpened(IDialogParameters parameters)
|
|
{
|
|
this.Title = "选择用户";
|
|
|
|
_rid = parameters.GetValue<int>("rid");
|
|
var uids = parameters.GetValue<List<int>>("uids");
|
|
var us = _userService.GetUsers("").ToList();
|
|
_users = us.Select(u => new UserModel
|
|
{
|
|
IsSelected = uids.Contains(u.UserId),
|
|
UserId = u.UserId,
|
|
UserName = u.UserName,
|
|
RealName = u.RealName,
|
|
UserIcon = "pack://siteoforigin:,,,/Avatars/" + u.UserIcon
|
|
}).ToList();
|
|
|
|
_users.ForEach(u => Users.Add(u));
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
public override void DoSave()
|
|
{
|
|
try
|
|
{
|
|
//现将当前角色中的所有用户都清掉,然后加入当前已选中的用户
|
|
var role = _roleService.Find<SysRole>(_rid);
|
|
if (role == null) return;
|
|
|
|
role.Users.Clear();
|
|
foreach (var user in _users)
|
|
{
|
|
if (!user.IsSelected) continue;
|
|
|
|
role.Users.Add(new RoleUser
|
|
{
|
|
RoleId = _rid,
|
|
UserId = user.UserId,
|
|
SysRole = role
|
|
});
|
|
}
|
|
_roleService.Update(role);
|
|
|
|
base.DoSave();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "提示");
|
|
}
|
|
}
|
|
}
|
|
}
|