240 lines
7.4 KiB
C#
240 lines
7.4 KiB
C#
using MaterialDesignThemes.Wpf;
|
||
using Prism.Commands;
|
||
using Prism.Events;
|
||
using Prism.Mvvm;
|
||
using Prism.Regions;
|
||
using Prism.Services.Dialogs;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using DM_Weight.Models;
|
||
using DM_Weight.select;
|
||
using DM_Weight.util;
|
||
using log4net;
|
||
using DM_Weight.msg;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class UserManagerWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
|
||
{
|
||
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(UserManagerWindowViewModel));
|
||
private int _pageNum = 1;
|
||
public int PageNum
|
||
{
|
||
get => _pageNum;
|
||
set
|
||
{
|
||
SetProperty(ref _pageNum, value);
|
||
RequestData();
|
||
}
|
||
}
|
||
|
||
private int _pageCount = 1;
|
||
public int PageCount
|
||
{
|
||
get => _pageCount;
|
||
set
|
||
{
|
||
SetProperty(ref _pageCount, value);
|
||
}
|
||
}
|
||
|
||
private int _pageSize = 8;
|
||
public int PageSize
|
||
{
|
||
get => _pageSize;
|
||
set
|
||
{
|
||
SetProperty(ref _pageSize, value);
|
||
}
|
||
}
|
||
|
||
private int _totalCount = 0;
|
||
public int TotalCount
|
||
{
|
||
get => _totalCount;
|
||
set
|
||
{
|
||
SetProperty(ref _totalCount, value);
|
||
}
|
||
}
|
||
private List<UserList> _userList;
|
||
public List<UserList> UserList
|
||
{
|
||
get { return _userList; }
|
||
set { SetProperty(ref _userList, value); }
|
||
}
|
||
private UserList? _user;
|
||
public UserList? User
|
||
{
|
||
get { return _user; }
|
||
set { SetProperty(ref _user, value); }
|
||
}
|
||
private readonly IDialogService _dialogService;
|
||
EventAggregator _eventAggregator;
|
||
public UserManagerWindowViewModel(IDialogService dialogService, EventAggregator eventAggregator)
|
||
{
|
||
_dialogService = dialogService;
|
||
_eventAggregator = eventAggregator;
|
||
}
|
||
|
||
private string? _searchValue;
|
||
|
||
/// <summary>
|
||
/// 查询条件 查询字段值
|
||
/// </summary>
|
||
public string? SearchValue
|
||
{
|
||
get { return _searchValue; }
|
||
set
|
||
{
|
||
SetProperty(ref _searchValue, value);
|
||
RequestData();
|
||
}
|
||
}
|
||
public bool KeepAlive => false;
|
||
|
||
|
||
public DelegateCommand Query
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
RequestData();
|
||
});
|
||
}
|
||
|
||
|
||
|
||
public DelegateCommand OpenFingerDialog
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("User", User);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "FingerprintDialog", dialogParameters, DoDialogResult, "RootDialog");
|
||
});
|
||
}
|
||
|
||
public DelegateCommand OpenEditUserDialog
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("User", User);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "EditUserDialog", dialogParameters, DoDialogResult2, "RootDialog");
|
||
});
|
||
}
|
||
|
||
public DelegateCommand OpenInsertUserDialog
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "EditUserDialog", dialogParameters, DoDialogResult2, "RootDialog");
|
||
});
|
||
}
|
||
|
||
private void DoDialogResult(IDialogResult dialogResult)
|
||
{
|
||
// 委托 被动执行 被子窗口执行
|
||
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
|
||
if (dialogResult.Result == ButtonResult.OK)
|
||
{
|
||
RequestData();
|
||
}
|
||
}
|
||
private void DoDialogResult2(IDialogResult dialogResult)
|
||
{
|
||
// 委托 被动执行 被子窗口执行
|
||
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
|
||
if (dialogResult.Result == ButtonResult.OK)
|
||
{
|
||
RequestData();
|
||
}
|
||
}
|
||
public DelegateCommand DelUserDialog
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "DelUserDialog", dialogParameters, DoDialogResultDel, "RootDialog");
|
||
});
|
||
}
|
||
private void DoDialogResultDel(IDialogResult dialogResult)
|
||
{
|
||
// 委托 被动执行 被子窗口执行
|
||
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
|
||
if (dialogResult.Result == ButtonResult.OK)
|
||
{
|
||
//删除用户
|
||
if (User != null)
|
||
{
|
||
User.MachineId = "DM_Del";
|
||
SqlSugarHelper.Db.Updateable<UserList>(User).UpdateColumns(u => new { u.MachineId }).ExecuteCommand();
|
||
|
||
}
|
||
else
|
||
{
|
||
//未选择用户
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = $"请先选择用户",
|
||
Type = MsgType.ERROR
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
|
||
}
|
||
RequestData();
|
||
}
|
||
}
|
||
//这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作
|
||
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
|
||
{
|
||
continuationCallback(true);
|
||
}
|
||
|
||
|
||
//接收导航传过来的参数
|
||
public void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
logger.Info("进入OnNavigatedTo");
|
||
//查询表格数据
|
||
RequestData();
|
||
logger.Info("结束OnNavigatedTo");
|
||
}
|
||
|
||
|
||
|
||
void RequestData()
|
||
{
|
||
int totalcount = 0;
|
||
UserList = SqlSugarHelper.Db.Queryable<UserList>()
|
||
.Includes<RoleDm>(ul => ul.Role)
|
||
.Where(ul => ul.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3"))
|
||
.WhereIF(!String.IsNullOrEmpty(SearchValue), (di) => di.Nickname.Contains(SearchValue))
|
||
.ToPageList(PageNum, PageSize, ref totalcount)
|
||
//.ToList()
|
||
;
|
||
TotalCount = totalcount;
|
||
}
|
||
|
||
//每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
|
||
public bool IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//这个方法用于拦截请求
|
||
public void OnNavigatedFrom(NavigationContext navigationContext)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|