|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BPA.Helper;
- using System.Collections.ObjectModel;
- using BPASmartClient.CustomResource.Pages.Enums;
-
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Nfc;
- using System.Threading;
- using BPA.Helper;
-
- namespace BPASmartClient.CustomResource.Pages.ViewModel
- {
- public class NfcSetViewModel : NotifyBase
- {
- public NfcSetViewModel()
- {
- NfcServer.GetInstance.EnableLogin = true;
- permissions.Clear();
- Enum.GetNames(typeof(Permission)).ToList()?.ForEach(item =>
- {
- if (item != "管理员") permissions.Add(new PermissionSelect() { PermissionName = item });
- });
-
- UserAddCommand = new BPARelayCommand(() =>
- {
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return;
- }
- if (CardNum == null || CardNum?.Length <= 0)
- {
- Info = "Error:请将卡放到刷卡区;";
- return;
- }
- var per = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
- if (per != null)
- {
- Info = "Error:用户已存在!;";
- return;
- }
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return;
- }
-
- if (UserName == string.Empty)
- {
- Info = "Error:请添加用户名;";
- return;
- }
-
- var name = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == UserName);
- if (name != null)
- {
- Info = "Error:用户名已存在!;";
- }
- var res = permissions.FirstOrDefault(p => p.PermissionSelected == true);
- if (res != null)
- {
- int index = Array.FindIndex(Global.userManager.userInfos.ToArray(), p => p.CardId.ToString() == CardNum);
- if (index == -1)
- {
- Global.userManager.userInfos.Add(new UserInfo
- {
- CardId = CardNum,
- UserName = UserName,
- Password = CurrentPassword,
- permission = (Permission)Enum.Parse(typeof(Permission), res.PermissionName)
- });
- Config.GetInstance.SaveUser();
- Info = $"Info:ID {CardNum} 添加成功;";
- MessageNotify.GetInstance.ShowRunLog($" ID {CardNum} 用户添加成功");
- Thread.Sleep(1000);
- ActionManage.GetInstance.Send("Exit");
- }
- else
- Info = $"Info:ID {NFCHelper.GetInstance.GetReadResult.CardNum} 已存在;";
-
- }
- });
-
- UserCancelCommand = new BPARelayCommand(() =>
- {
- if (CardNum != null && CardNum != string.Empty)
- {
- var res = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
-
- if (res != null)
- {
- Global.userManager.userInfos.Remove(res);
- Config.GetInstance.SaveUser();
- Info = $"Info:ID {CardNum} 注销成功;";
- MessageNotify.GetInstance.ShowRunLog($"ID {CardNum} 用户注销成功");
- Thread.Sleep(1000);
- ActionManage.GetInstance.Send("Exit");
- }
- else
- {
- Info = "Info:当前卡在系统中不存在;";
- return;
- }
- }
- else
- {
- Info = "Error:请将卡放到刷卡区;";
- }
- });
-
- NfcServer.GetInstance.Update = new Action(() =>
- {
- CardNum = NFCHelper.GetInstance.GetReadResult.CardNum;
-
- });
- }
-
- private bool Verify()
- {
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return false;
- }
- if (CardNum == null || CardNum?.Length <= 0)
- {
- Info = "Error:请将卡放到刷卡区;";
- return false;
- }
- return true;
- }
-
- public ObservableCollection<PermissionSelect> permissions { get; set; } = new ObservableCollection<PermissionSelect>();
-
- public string CardNum { get { return _mCardNum; } set { _mCardNum = value; OnPropertyChanged(); } }
- private string _mCardNum;
-
- public string Info { get { return _mInfo; } set { _mInfo = value; OnPropertyChanged(); } }
- private string _mInfo;
-
- public string UserName { get { return _userName; } set { _userName = value; OnPropertyChanged(); } }
- private string _userName = string.Empty;
-
- public string CurrentPassword { get { return _currentPassword; } set { _currentPassword = value; OnPropertyChanged(); } }
- private string _currentPassword = "888888";
-
- public string CheckPassword { get { return _checkPassword; } set { _checkPassword = value; OnPropertyChanged(); } }
- private string _checkPassword = "888888";
-
- public BPARelayCommand SavePermission { get; set; }
-
- public BPARelayCommand DeletePermission { get; set; }
-
- public BPARelayCommand UserAddCommand { get; set; }
-
- public BPARelayCommand UserCancelCommand { get; set; }
-
- }
-
- public class PermissionSelect : NotifyBase
- {
-
- public string PermissionName { get { return _mPermissionName; } set { _mPermissionName = value; OnPropertyChanged(); } }
- private string _mPermissionName;
-
-
- public bool PermissionSelected { get { return _mPermissionSelected; } set { _mPermissionSelected = value; OnPropertyChanged(); } }
- private bool _mPermissionSelected = false;
-
- }
- }
|