254 lines
9.6 KiB
C#
254 lines
9.6 KiB
C#
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 DM_Weight.Finger;
|
|
using DM_Weight.Models;
|
|
using DM_Weight.msg;
|
|
using DM_Weight.util;
|
|
|
|
namespace DM_Weight.ViewModels
|
|
{
|
|
public class EditUserDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
|
|
{
|
|
|
|
private readonly FingerprintUtil _fingerprintUtil;
|
|
IEventAggregator _eventAggregator;
|
|
public EditUserDialogViewModel(FingerprintUtil fingerprintUtil, IEventAggregator eventAggregator)
|
|
{
|
|
_fingerprintUtil = fingerprintUtil;
|
|
_eventAggregator = eventAggregator;
|
|
}
|
|
|
|
private UserList _userList = new UserList();
|
|
|
|
public UserList UserList
|
|
{
|
|
get => _userList;
|
|
set => SetProperty(ref _userList, value);
|
|
}
|
|
|
|
public UserList DMUserList=new UserList();
|
|
private string UserName;
|
|
|
|
public List<RoleDm> Roles { get; set; }
|
|
|
|
private string _oldPass;
|
|
public string OldPass
|
|
{
|
|
get => _oldPass;
|
|
set
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
throw new ArgumentException("请输入原密码");
|
|
}
|
|
if (!MD5.GetMD5Hash(value).ToLower().Equals(UserList.PassWord.ToLower()))
|
|
{
|
|
throw new ArgumentException("旧密码错误");
|
|
}
|
|
SetProperty(ref _oldPass, value);
|
|
}
|
|
}
|
|
private string _newPass;
|
|
public string NewPass
|
|
{
|
|
get => _newPass; set
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
throw new ArgumentNullException("请输入新密码");
|
|
}
|
|
SetProperty(ref _newPass, value);
|
|
}
|
|
}
|
|
|
|
private bool _onlyPassword = false;
|
|
private bool _editUser = true;
|
|
public bool OnlyPassword { get => _onlyPassword; set => SetProperty(ref _onlyPassword, value); }
|
|
public bool EditUser { get => _editUser; set => SetProperty(ref _editUser, value); }
|
|
|
|
public event Action<IDialogResult> RequestClose;
|
|
|
|
|
|
|
|
public bool KeepAlive => false;
|
|
|
|
private string _title = "编辑用户";
|
|
|
|
public string Title
|
|
{
|
|
get => _title;
|
|
set => SetProperty(ref _title, value);
|
|
}
|
|
|
|
|
|
public DelegateCommand SaveUser
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
try
|
|
{
|
|
if(string.IsNullOrEmpty(UserList.UserBarcode))
|
|
{
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = $"工号不能为空",
|
|
Type = MsgType.ERROR,
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
return;
|
|
}
|
|
// 修改密码
|
|
if (OnlyPassword)
|
|
{
|
|
DMUserList = SqlSugarHelper.Db.Queryable<UserList>().Where(r => r.MachineId == (ConfigurationManager.AppSettings["dm_machineId"]??"DM3") && r.UserName == UserName).OrderBy(r => r.Id).First();
|
|
|
|
if (MD5.GetMD5Hash(OldPass).ToLower().Equals(UserList.PassWord.ToLower()))
|
|
{
|
|
// 旧密码输入正确
|
|
UserList.PassWord = MD5.GetMD5Hash(NewPass);
|
|
SqlSugarHelper.Db.Updateable<UserList>(UserList).UpdateColumns(u => new { u.PassWord }).ExecuteCommand();
|
|
//修改交接柜用户密码
|
|
DMUserList.PassWord = UserList.PassWord;
|
|
SqlSugarHelper.Db.Updateable<UserList>(DMUserList).UpdateColumns(u => new { u.PassWord }).ExecuteCommand();
|
|
// 关闭当前窗口
|
|
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
}
|
|
// 保存用户
|
|
else
|
|
{
|
|
if (UserList.Id > 0)
|
|
{
|
|
DMUserList = SqlSugarHelper.Db.Queryable<UserList>().Where(r => r.MachineId == (ConfigurationManager.AppSettings["dm_machineId"]??"DM3") && r.UserName == UserName).OrderBy(r => r.Id).First();
|
|
|
|
// 更新
|
|
// 没有写密码
|
|
if (string.IsNullOrEmpty(NewPass))
|
|
{
|
|
SqlSugarHelper.Db.Updateable<UserList>(UserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.UserBarcode, u.RoleId }).ExecuteCommand();
|
|
//更新交接柜用户
|
|
DMUserList.Nickname = UserList.Nickname;
|
|
DMUserList.UserName = UserList.UserName;
|
|
DMUserList.UserBarcode = UserList.UserBarcode;
|
|
SqlSugarHelper.Db.Updateable<UserList>(DMUserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.UserBarcode, u.RoleId }).ExecuteCommand();
|
|
|
|
}
|
|
// 更改了密码
|
|
else
|
|
{
|
|
UserList.PassWord = MD5.GetMD5Hash(NewPass);
|
|
SqlSugarHelper.Db.Updateable<UserList>(UserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId }).ExecuteCommand();
|
|
|
|
|
|
DMUserList.PassWord = UserList.PassWord;
|
|
DMUserList.Nickname= UserList.Nickname;
|
|
DMUserList.UserName= UserList.UserName;
|
|
DMUserList.UserBarcode= UserList.UserBarcode;
|
|
SqlSugarHelper.Db.Updateable<UserList>(DMUserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId }).ExecuteCommand();
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UserList.PassWord = MD5.GetMD5Hash(NewPass);
|
|
UserList.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM5";
|
|
// 插入
|
|
SqlSugarHelper.Db.Insertable<UserList>(UserList).InsertColumns(u => new { u.Id, u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId, u.MachineId }).ExecuteCommand();
|
|
|
|
// 插入交接柜用户
|
|
int? roleId = SqlSugarHelper.Db.Queryable<RoleDm>().Where(r => r.MachineId == (ConfigurationManager.AppSettings["dm_machineId"]??"DM3")).OrderBy(r => r.Id).Select(r => r.Id).First();
|
|
DMUserList = UserList;
|
|
DMUserList.MachineId = ConfigurationManager.AppSettings["dm_machineId"] ?? "DM3";
|
|
DMUserList.RoleId = roleId;
|
|
SqlSugarHelper.Db.Insertable<UserList>(DMUserList).InsertColumns(u => new { u.Id, u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId, u.MachineId }).ExecuteCommand();
|
|
|
|
}
|
|
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = $"保存用户信息异常{ex.Message}",
|
|
Type = MsgType.ERROR,
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
}
|
|
});
|
|
}
|
|
|
|
void GetAllRole()
|
|
{
|
|
Roles = SqlSugarHelper.Db.Queryable<RoleDm>().Where(r => r.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM3")).OrderBy(r => r.Id).ToList();
|
|
RaisePropertyChanged("Roles");
|
|
}
|
|
|
|
|
|
public DelegateCommand CancelCommand
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
// 关闭当前窗口
|
|
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
|
});
|
|
}
|
|
|
|
|
|
public bool CanCloseDialog()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void OnDialogClosed()
|
|
{
|
|
|
|
}
|
|
|
|
public void OnDialogOpened(IDialogParameters parameters)
|
|
{
|
|
|
|
if (parameters.ContainsKey("EditPass"))
|
|
{
|
|
|
|
UserList = parameters.GetValue<UserList>("User");
|
|
OnlyPassword = parameters.GetValue<bool>("EditPass");
|
|
EditUser = false;
|
|
Title = "修改密码";
|
|
UserName = UserList.UserName;
|
|
}
|
|
else
|
|
{
|
|
if (parameters.ContainsKey("User"))
|
|
{
|
|
UserList = parameters.GetValue<UserList>("User");
|
|
UserName = UserList.UserName;
|
|
}
|
|
if (UserList.Id == null)
|
|
{
|
|
Title = "添加用户";
|
|
}
|
|
GetAllRole();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|