233 lines
7.3 KiB
C#
233 lines
7.3 KiB
C#
using FATrace.Model;
|
|
using FATrace.WPLApp.Core;
|
|
using FATrace.WPLApp.Services;
|
|
using FreeSql;
|
|
using Prism.Commands;
|
|
using Prism.Services.Dialogs;
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace FATrace.WPLApp.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// 用户新增/编辑 弹窗 VM
|
|
/// </summary>
|
|
public class DialogUserEditViewModel : DialogViewModel
|
|
{
|
|
private readonly IFreeSql _fsql;
|
|
private readonly ILogService _log;
|
|
|
|
private string _mode = UserManageViewModel.DialogModes.Add;
|
|
private long _editUserId;
|
|
|
|
public DialogUserEditViewModel(IFreeSql fsql, ILogService log)
|
|
{
|
|
_fsql = fsql;
|
|
_log = log;
|
|
|
|
AccessLevelOptions = new ObservableCollection<string>(new[]
|
|
{
|
|
UserManageViewModel.AccessLevels.Admin,
|
|
UserManageViewModel.AccessLevels.Operator,
|
|
UserManageViewModel.AccessLevels.Guest
|
|
});
|
|
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync(), () => !IsBusy)
|
|
.ObservesProperty(() => IsBusy);
|
|
CancelCommand = new DelegateCommand(() => OnDialogClosed(ButtonResult.Cancel));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 等级下拉选项
|
|
/// </summary>
|
|
public ObservableCollection<string> AccessLevelOptions { get; }
|
|
|
|
private string? _userName;
|
|
/// <summary>
|
|
/// 用户名
|
|
/// </summary>
|
|
public string? UserName
|
|
{
|
|
get => _userName;
|
|
set { _userName = value; RaisePropertyChanged(); }
|
|
}
|
|
|
|
private string? _password;
|
|
/// <summary>
|
|
/// 密码(当前按现有模型使用明文;如后续需哈希,可在保存处替换)
|
|
/// </summary>
|
|
public string? Password
|
|
{
|
|
get => _password;
|
|
set { _password = value; RaisePropertyChanged(); }
|
|
}
|
|
|
|
private string? _accessLevel;
|
|
/// <summary>
|
|
/// 等级
|
|
/// </summary>
|
|
public string? AccessLevel
|
|
{
|
|
get => _accessLevel;
|
|
set { _accessLevel = value; RaisePropertyChanged(); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
public DelegateCommand SaveCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 取消
|
|
/// </summary>
|
|
public DelegateCommand CancelCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 打开弹窗时初始化
|
|
/// </summary>
|
|
/// <param name="parameters">参数</param>
|
|
public override void OnDialogOpened(IDialogParameters parameters)
|
|
{
|
|
_mode = parameters.GetValue<string>(UserManageViewModel.DialogKeys.Mode) ?? UserManageViewModel.DialogModes.Add;
|
|
Title = _mode == UserManageViewModel.DialogModes.Edit ? "编辑用户" : "新增用户";
|
|
|
|
if (_mode == UserManageViewModel.DialogModes.Edit)
|
|
{
|
|
_editUserId = parameters.GetValue<long>(UserManageViewModel.DialogKeys.UserId);
|
|
LoadUser(_editUserId);
|
|
}
|
|
else
|
|
{
|
|
_editUserId = 0;
|
|
UserName = string.Empty;
|
|
Password = string.Empty;
|
|
AccessLevel = UserManageViewModel.AccessLevels.Operator;
|
|
}
|
|
}
|
|
|
|
private void LoadUser(long id)
|
|
{
|
|
try
|
|
{
|
|
var user = _fsql.Select<TbUser>().Where(a => a.Id == id).First();
|
|
if (user == null)
|
|
{
|
|
MessageBox.Show("用户不存在或已被删除", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
return;
|
|
}
|
|
|
|
UserName = user.UserName;
|
|
Password = user.Password;
|
|
AccessLevel = user.AccessLevel;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error($"加载用户失败: {ex}");
|
|
MessageBox.Show($"加载用户失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private async Task SaveAsync()
|
|
{
|
|
if (IsBusy) return;
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(UserName))
|
|
{
|
|
MessageBox.Show("请输入用户名", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Password))
|
|
{
|
|
MessageBox.Show("请输入密码", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(AccessLevel))
|
|
{
|
|
MessageBox.Show("请选择等级", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
IsBusy = true;
|
|
|
|
if (_mode == UserManageViewModel.DialogModes.Add)
|
|
{
|
|
await AddAsync();
|
|
}
|
|
else
|
|
{
|
|
await UpdateAsync();
|
|
}
|
|
|
|
OnDialogClosed(ButtonResult.OK);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error($"保存用户失败: {ex}");
|
|
MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
finally
|
|
{
|
|
IsBusy = false;
|
|
}
|
|
}
|
|
|
|
private async Task AddAsync()
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
var exists = _fsql.Select<TbUser>().Where(a => a.UserName == UserName).Any();
|
|
if (exists)
|
|
{
|
|
throw new InvalidOperationException("用户名已存在");
|
|
}
|
|
|
|
var entity = new TbUser
|
|
{
|
|
UserName = UserName!,
|
|
Password = Password!,
|
|
AccessLevel = AccessLevel!,
|
|
CreateTime = DateTime.Now
|
|
};
|
|
|
|
_fsql.Insert(entity).ExecuteAffrows();
|
|
});
|
|
|
|
_log.Info($"新增用户成功: {UserName}");
|
|
}
|
|
|
|
private async Task UpdateAsync()
|
|
{
|
|
var id = _editUserId;
|
|
if (id <= 0) throw new InvalidOperationException("编辑用户Id无效");
|
|
|
|
await Task.Run(() =>
|
|
{
|
|
var exists = _fsql.Select<TbUser>().Where(a => a.UserName == UserName && a.Id != id).Any();
|
|
if (exists)
|
|
{
|
|
throw new InvalidOperationException("用户名已存在");
|
|
}
|
|
|
|
var aff = _fsql.Update<TbUser>()
|
|
.Set(a => a.UserName, UserName!)
|
|
.Set(a => a.Password, Password!)
|
|
.Set(a => a.AccessLevel, AccessLevel!)
|
|
.Where(a => a.Id == id)
|
|
.ExecuteAffrows();
|
|
|
|
if (aff <= 0)
|
|
{
|
|
throw new InvalidOperationException("更新失败:用户不存在或未发生变化");
|
|
}
|
|
});
|
|
|
|
_log.Info($"更新用户成功: Id={id}, UserName={UserName}");
|
|
}
|
|
}
|
|
}
|