using DepartmentBusinessLogic.BindingModels; using DepartmentBusinessLogic.BusinessLogics; using DepartmentBusinessLogic.ViewModels; using DesktopTools.BaseControls; using DesktopTools.Controls; using DesktopTools.Helpers; using DesktopTools.Interfaces; using DesktopTools.Models; using ModuleTools.BusinessLogics; using ModuleTools.Extensions; using SecurityBusinessLogic.BindingModels; using SecurityBusinessLogic.BusinessLogics; using SecurityBusinessLogic.ViewModels; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace DepartmentWindowsDesktop.EntityControls { /// /// Реализация контрола для студента /// public partial class ControlStudentElement : GenericControlEntityElement, IGenericControlEntityElement { public ControlStudentElement() { InitializeComponent(); Title = "Студент"; ControlId = new Guid("cbddf92c-8c00-4ad2-93e8-33c0d998a899"); _genericControlViewEntityElement = this; } public IControl GetInstanceGenericControl() => new ControlStudentElement() { ControlId = Guid.NewGuid() }; public ControlViewEntityElementConfiguration GetConfigControl() => new() { ControlOnMoveElem = new Dictionary { { "ToolStripMenuItemAddUser", ("Добавить пользователя", async (object sender, EventArgs e) => { await AddUserAsync(); }) }, { "ToolStripMenuItemPasswordReset", ("Сброс пароля пользователя", async (object sender, EventArgs e) => { await PasswordResetAsync(); }) } } }; /// /// Поиск пользователя под учетку, либо добавление нового, если не найдено /// private async Task AddUserAsync() { var model = new StudentSetBindingModel(); if (FillModel(model)) { var logic = DependencyManager.Instance.Resolve(); var userName = $"{model.LastName}{(model.FirstName.IsNotEmpty() ? $" {model.FirstName[0]}." : string.Empty)}{(model.Patronymic.IsNotEmpty() ? $"{model.Patronymic[0]}." : string.Empty)}"; var result = await logic.GetListAsync(new UserGetBindingModel { UserNameForSearch = userName }); if (result != null) { if (result.List.Count > 1) { DialogHelper.MessageException("Существует несколько пользователей с такой сигнатурой", "Ошибка"); return; } if (result.List.Count == 1) { model.UserId = result.List[0].Id; } else { var newuser = await logic.CreateAsync(new UserSetBindingModel { Login = userName, Password = model.NumberOfBook, Avatar = model.Photo }); if (newuser == null) { DialogHelper.MessageException(logic.Errors, "Ошибка при создании пользователя"); return; } model.UserId = newuser.Id; } var controls = tabPageMain.Controls.Find($"ControlUserId", true); if (controls != null) { (controls[0] as AbstractBaseControl).SetValue(model); } } } } /// /// Сброс пароля пользователя /// private async Task PasswordResetAsync() { if (_element == null) { return; } var model = new StudentSetBindingModel(); if (FillModel(model)) { var logic = DependencyManager.Instance.Resolve(); var user = await logic.GetElementAsync(new UserGetBindingModel { Id = _element.UserId }); if (user == null) { DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя"); return; } user.Password = model.NumberOfBook; user = await logic.UpdateAsync(Mapper.MapToClass(user, true)); if (user == null) { DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя"); return; } DialogHelper.MessageInformation("Пароль сброшен", "Успех"); } } } }