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; using SqlSugar; 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 List Roles { get; set; } //交接柜用户 private UserList JiaojieUser; private string UserName; //private RoleDm JiaojieRole; 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 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 (OnlyPassword) { JiaojieUser= SqlSugarHelper.Db.Queryable().Where(r => r.MachineId == (ConfigurationManager.AppSettings["jj_machineId"]??"DM5")&&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).UpdateColumns(u => new { u.PassWord }).ExecuteCommand(); //修改交接柜用户密码 JiaojieUser.PassWord = UserList.PassWord; SqlSugarHelper.Db.Updateable(JiaojieUser).UpdateColumns(u => new { u.PassWord }).ExecuteCommand(); // 关闭当前窗口 RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } else { } } // 保存用户 else { if (UserList.Id > 0) { JiaojieUser = SqlSugarHelper.Db.Queryable().Where(r => r.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5") && r.UserName == UserName).OrderBy(r => r.Id).First(); // 更新 // 没有写密码 if (string.IsNullOrEmpty(NewPass)) { SqlSugarHelper.Db.Updateable(UserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.UserBarcode, u.RoleId }).ExecuteCommand(); //更新交接柜用户 JiaojieUser.Nickname = UserList.Nickname; JiaojieUser.UserName= UserList.UserName; JiaojieUser.UserBarcode= UserList.UserBarcode; SqlSugarHelper.Db.Updateable(JiaojieUser).UpdateColumns(u => new { u.Nickname, u.UserName, u.UserBarcode, u.RoleId }).ExecuteCommand(); } // 更改了密码 else { UserList.PassWord = MD5.GetMD5Hash(NewPass); SqlSugarHelper.Db.Updateable(UserList).UpdateColumns(u => new { u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId }).ExecuteCommand(); JiaojieUser.PassWord= UserList.PassWord; JiaojieUser.Nickname= UserList.Nickname; JiaojieUser.UserName= UserList.UserName; JiaojieUser.UserBarcode= UserList.UserBarcode; SqlSugarHelper.Db.Updateable(JiaojieUser).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"] ?? "DM3"; // 插入 SqlSugarHelper.Db.Insertable(UserList).InsertColumns(u => new { u.Id, u.Nickname, u.UserName, u.PassWord, u.UserBarcode, u.RoleId, u.MachineId }).ExecuteCommand(); // 插入交接柜用户 int? roleId = SqlSugarHelper.Db.Queryable().Where(r => r.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5")).OrderBy(r => r.Id).Select(r=>r.Id).First(); JiaojieUser = UserList; JiaojieUser.MachineId = ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5"; JiaojieUser.RoleId= roleId; SqlSugarHelper.Db.Insertable(JiaojieUser).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().Publish(alertMsg); } }); } void GetAllRole() { Roles = SqlSugarHelper.Db.Queryable().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("User"); OnlyPassword = parameters.GetValue("EditPass"); EditUser = false; Title = "修改密码"; UserName=UserList.UserName; } else { if (parameters.ContainsKey("User")) { UserList = parameters.GetValue("User"); UserName = UserList.UserName; } if (UserList.Id == null) { Title = "添加用户"; } GetAllRole(); } } } }