添加项目文件。
This commit is contained in:
286
SamplePreSystem.UI/ViewModel/ConfigManager/UsersViewModel.cs
Normal file
286
SamplePreSystem.UI/ViewModel/ConfigManager/UsersViewModel.cs
Normal file
@@ -0,0 +1,286 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using SamplePre.Models.Ext;
|
||||
using SamplePre.Models.Tables;
|
||||
using SamplePre.ProcessBll.BLL;
|
||||
using SamplePre.UIWpf.ConfigManager.chirld;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace SamplePreSystem.UI.ViewModel.ConfigManager
|
||||
{
|
||||
public partial class UsersViewModel : ObservableObject
|
||||
{
|
||||
|
||||
|
||||
UsersBll usersBll = new UsersBll();
|
||||
|
||||
// 关闭窗口的委托
|
||||
public Action<bool> CloseAction { get; set; }
|
||||
|
||||
public UsersViewModel()
|
||||
{
|
||||
//获取用户数据
|
||||
GetData();
|
||||
|
||||
}
|
||||
|
||||
private List<config_user_ext> _userList = new List<config_user_ext>();
|
||||
|
||||
public List<config_user_ext> UserList
|
||||
{
|
||||
get { return _userList; }
|
||||
set {
|
||||
SetProperty(ref _userList, value);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private config_user_ext _selectUser;
|
||||
|
||||
public config_user_ext SelectUser
|
||||
{
|
||||
get { return _selectUser; }
|
||||
set { _selectUser = value; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
private config_user_ext _configUser =new config_user_ext();
|
||||
|
||||
public config_user_ext ConfigUser
|
||||
{
|
||||
get { return _configUser; }
|
||||
set {
|
||||
SetProperty(ref _configUser, value);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户
|
||||
/// </summary>
|
||||
public void GetData()
|
||||
{
|
||||
|
||||
UserList = usersBll.GetUsers();
|
||||
|
||||
}
|
||||
|
||||
private List<config_role_ext> roles = new List<config_role_ext>();
|
||||
|
||||
public List<config_role_ext> Roles
|
||||
{
|
||||
get { return roles; }
|
||||
set {
|
||||
SetProperty(ref roles, roles);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 绑定角色数据
|
||||
/// </summary>
|
||||
public void InitCheckedListBox()
|
||||
{
|
||||
Roles.Clear();
|
||||
var roleList = usersBll.GetRoles();
|
||||
|
||||
|
||||
|
||||
|
||||
foreach (var item in roleList)
|
||||
{
|
||||
config_role_ext model = new config_role_ext();
|
||||
model.id = item.id;
|
||||
model.name = item.name;
|
||||
model.description = item.description;
|
||||
model.IsSelected = false;
|
||||
|
||||
Roles.Add(model);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(ConfigUser.id))
|
||||
{
|
||||
//获取用户的角色
|
||||
var userRoles = usersBll.GetUserRoles(ConfigUser.id);
|
||||
|
||||
foreach(var item in Roles)
|
||||
{
|
||||
if(userRoles.Where(p=>p.role_id == item.id).Count() > 0)
|
||||
{
|
||||
item.IsSelected = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private string isEnable = "启用";
|
||||
|
||||
public string IsEnable
|
||||
{
|
||||
get { return isEnable; }
|
||||
set {
|
||||
|
||||
SetProperty(ref isEnable, isEnable);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public bool SaveUser()
|
||||
{
|
||||
if (ConfigUser == null) return false;
|
||||
|
||||
//ConfigUser.username = this.txtUserName.Text;
|
||||
//ConfigUser.password = this.txtPwd.Text;
|
||||
ConfigUser.is_enabled = IsEnable == "启用" ? true : false;
|
||||
|
||||
if (string.IsNullOrEmpty(ConfigUser.id))
|
||||
{
|
||||
//new
|
||||
ConfigUser.id = Guid.NewGuid().ToString();
|
||||
|
||||
//选中的角色
|
||||
var roles = GetCheckedData();
|
||||
|
||||
if (usersBll.SaveUserData(ConfigUser, roles) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//update
|
||||
|
||||
//选中的角色
|
||||
var roles = GetCheckedData();
|
||||
|
||||
if (usersBll.UpdateUserData(ConfigUser, roles) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<config_user_role> GetCheckedData()
|
||||
{
|
||||
List<config_user_role> userRoles = new List<config_user_role>();
|
||||
|
||||
|
||||
foreach (var role in Roles)
|
||||
{
|
||||
if(role.IsSelected)
|
||||
{
|
||||
//config_role item = cbRoles.GetItem(i) as config_role;
|
||||
|
||||
config_user_role configUserRole = new config_user_role();
|
||||
configUserRole.role_id = role.id.ToString();
|
||||
configUserRole.user_id = ConfigUser.id;
|
||||
|
||||
userRoles.Add(configUserRole);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
|
||||
public bool DeleUser()
|
||||
{
|
||||
if(SelectUser != null)
|
||||
{
|
||||
config_user user = SelectUser;
|
||||
|
||||
if (MessageBox.Show($"是否真的删除{user.username}", "删除提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
|
||||
{
|
||||
if (usersBll.DeleteUser(user) < 0)
|
||||
{
|
||||
MessageBox.Show("用户删除失败!");
|
||||
return false;
|
||||
}
|
||||
//刷新用户
|
||||
GetData();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
[RelayCommand]
|
||||
public void BtnAddClick()
|
||||
{
|
||||
ConfigUser = new config_user_ext();
|
||||
|
||||
//初始化角色数据
|
||||
InitCheckedListBox();
|
||||
|
||||
NewUserWindow frm = new NewUserWindow(this);
|
||||
if (frm.ShowDialog() == true)
|
||||
{
|
||||
//刷新数据
|
||||
GetData();
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void BtnEditClick()
|
||||
{
|
||||
ConfigUser = SelectUser;
|
||||
//初始化角色数据
|
||||
InitCheckedListBox();
|
||||
|
||||
NewUserWindow frm = new NewUserWindow(this);
|
||||
if (frm.ShowDialog() == true)
|
||||
{
|
||||
//刷新数据
|
||||
GetData();
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void BtnDeleClick()
|
||||
{
|
||||
DeleUser();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void BtnSaveUserClick()
|
||||
{
|
||||
|
||||
if (SaveUser() == true)
|
||||
{
|
||||
this.CloseAction?.Invoke(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user