208 lines
6.5 KiB
C#
208 lines
6.5 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.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace FATrace.WPLApp.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// 称重用户新增/编辑 弹窗 VM
|
|
/// </summary>
|
|
public class DialogWeightUserEditViewModel : DialogViewModel
|
|
{
|
|
private readonly IFreeSql _fsql;
|
|
private readonly ILogService _log;
|
|
|
|
private string _mode = WeightUserManageViewModel.DialogModes.Add;
|
|
private long _editUserId;
|
|
|
|
public DialogWeightUserEditViewModel(IFreeSql fsql, ILogService log)
|
|
{
|
|
_fsql = fsql;
|
|
_log = log;
|
|
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync(), () => !IsBusy)
|
|
.ObservesProperty(() => IsBusy);
|
|
CancelCommand = new DelegateCommand(() => OnDialogClosed(ButtonResult.Cancel));
|
|
}
|
|
|
|
private string? _checkName;
|
|
/// <summary>
|
|
/// 确认者
|
|
/// </summary>
|
|
public string? CheckName
|
|
{
|
|
get => _checkName;
|
|
set { _checkName = value; RaisePropertyChanged(); }
|
|
}
|
|
|
|
private string? _opName;
|
|
/// <summary>
|
|
/// 操作者
|
|
/// </summary>
|
|
public string? OpName
|
|
{
|
|
get => _opName;
|
|
set { _opName = value; RaisePropertyChanged(); }
|
|
}
|
|
|
|
private string? _password;
|
|
/// <summary>
|
|
/// 密码(当前按现有模型使用明文;如后续需哈希,可在保存处替换)
|
|
/// </summary>
|
|
public string? Password
|
|
{
|
|
get => _password;
|
|
set { _password = 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>(WeightUserManageViewModel.DialogKeys.Mode) ?? WeightUserManageViewModel.DialogModes.Add;
|
|
Title = _mode == WeightUserManageViewModel.DialogModes.Edit ? "编辑称重用户" : "新增称重用户";
|
|
|
|
if (_mode == WeightUserManageViewModel.DialogModes.Edit)
|
|
{
|
|
_editUserId = parameters.GetValue<long>(WeightUserManageViewModel.DialogKeys.UserId);
|
|
LoadUser(_editUserId);
|
|
}
|
|
else
|
|
{
|
|
_editUserId = 0;
|
|
CheckName = string.Empty;
|
|
OpName = string.Empty;
|
|
Password = string.Empty;
|
|
}
|
|
}
|
|
|
|
private void LoadUser(long id)
|
|
{
|
|
try
|
|
{
|
|
var user = _fsql.Select<TbWeightUser>().Where(a => a.Id == id).First();
|
|
if (user == null)
|
|
{
|
|
MessageBox.Show("称重用户不存在或已被删除", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
return;
|
|
}
|
|
|
|
CheckName = user.CheckName;
|
|
OpName = user.OpName;
|
|
Password = user.Password;
|
|
}
|
|
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(CheckName))
|
|
{
|
|
MessageBox.Show("请输入确认者", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(OpName))
|
|
{
|
|
MessageBox.Show("请输入操作者", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Password))
|
|
{
|
|
MessageBox.Show("请输入密码", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
return;
|
|
}
|
|
|
|
IsBusy = true;
|
|
|
|
if (_mode == WeightUserManageViewModel.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 entity = new TbWeightUser
|
|
{
|
|
CheckName = CheckName!,
|
|
OpName = OpName!,
|
|
Password = Password!,
|
|
CreateTime = DateTime.Now
|
|
};
|
|
|
|
_fsql.Insert(entity).ExecuteAffrows();
|
|
});
|
|
|
|
_log.Info($"新增称重用户成功: {CheckName}/{OpName}");
|
|
}
|
|
|
|
private async Task UpdateAsync()
|
|
{
|
|
var id = _editUserId;
|
|
if (id <= 0) throw new InvalidOperationException("编辑称重用户Id无效");
|
|
|
|
await Task.Run(() =>
|
|
{
|
|
var aff = _fsql.Update<TbWeightUser>()
|
|
.Set(a => a.CheckName, CheckName!)
|
|
.Set(a => a.OpName, OpName!)
|
|
.Set(a => a.Password, Password!)
|
|
.Where(a => a.Id == id)
|
|
.ExecuteAffrows();
|
|
|
|
if (aff <= 0)
|
|
{
|
|
throw new InvalidOperationException("更新失败:称重用户不存在或未发生变化");
|
|
}
|
|
});
|
|
|
|
_log.Info($"更新称重用户成功: Id={id}, CheckName={CheckName}, OpName={OpName}");
|
|
}
|
|
}
|
|
}
|