114 lines
3.9 KiB
C#
114 lines
3.9 KiB
C#
using DepartmentContract.BindingModels;
|
||
using DepartmentContract.Logics.IGenericEntityLogic;
|
||
using DepartmentContract.ViewModels;
|
||
using SecurityContract.BindingModels;
|
||
using SecurityContract.Logics.IGenericEntityLogic;
|
||
using SecurityContract.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using ToolsDesktop.BaseControls;
|
||
using ToolsDesktop.Controls;
|
||
using ToolsDesktop.Helpers;
|
||
using ToolsDesktop.Interfaces;
|
||
using ToolsDesktop.Models;
|
||
using ToolsModule.BusinessLogics;
|
||
using ToolsModule.Extensions;
|
||
|
||
namespace DepartmentWindowsDesktop.EntityControls
|
||
{
|
||
/// <summary>
|
||
/// Реализация контрола для студента
|
||
/// </summary>
|
||
public partial class ControlStudentElement :
|
||
GenericControlEntityElement<StudentGetBindingModel, StudentSetBindingModel, StudentListViewModel, StudentViewModel, IStudentLogic>,
|
||
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<string, (string Title, EventHandler Event)>
|
||
{
|
||
{ "ToolStripMenuItemAddUser", ("Добавить пользователя", (object sender, EventArgs e) => { AddUser(); }) },
|
||
{ "ToolStripMenuItemPasswordReset", ("Сброс пароля пользователя", (object sender, EventArgs e) => { PasswordReset(); }) }
|
||
}
|
||
};
|
||
|
||
/// <summary>
|
||
/// Поиск пользователя под учетку, либо добавление нового, если не найдено
|
||
/// </summary>
|
||
private void AddUser()
|
||
{
|
||
var model = new StudentSetBindingModel();
|
||
if (FillModel(model))
|
||
{
|
||
var logic = DependencyManager.Instance.Resolve<IUserLogic>();
|
||
var userName = $"{model.LastName}{(model.FirstName.IsNotEmpty() ? $" {model.FirstName[0]}." : string.Empty)}{(model.Patronymic.IsNotEmpty() ? $"{model.Patronymic[0]}." : string.Empty)}";
|
||
var user = logic.GetElement(new UserGetBindingModel { Login = model.NumberOfBook });
|
||
if (user == null)
|
||
{
|
||
if (logic.Errors.Count > 0)
|
||
{
|
||
DialogHelper.MessageException(logic.Errors, "Ошибка при создании пользователя");
|
||
return;
|
||
}
|
||
user = logic.Create(new UserSetBindingModel
|
||
{
|
||
UserName = model.NumberOfBook,
|
||
PasswordHash = userName,
|
||
Avatar = model.Photo
|
||
});
|
||
if (user == null)
|
||
{
|
||
DialogHelper.MessageException(logic.Errors, "Ошибка при создании пользователя");
|
||
return;
|
||
}
|
||
}
|
||
model.UserId = user.Id;
|
||
var controls = tabPageMain.Controls.Find($"ControlUserId", true);
|
||
if (controls != null)
|
||
{
|
||
(controls[0] as AbstractBaseControl).SetValue(model);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Сброс пароля пользователя
|
||
/// </summary>
|
||
private void PasswordReset()
|
||
{
|
||
if (_element == null)
|
||
{
|
||
return;
|
||
}
|
||
var model = new StudentSetBindingModel();
|
||
if (FillModel(model))
|
||
{
|
||
var logic = DependencyManager.Instance.Resolve<IUserLogic>();
|
||
var user = logic.GetElement(new UserGetBindingModel { Id = _element.UserId });
|
||
if (user == null)
|
||
{
|
||
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
||
return;
|
||
}
|
||
user.PasswordHash = model.NumberOfBook;
|
||
user = logic.Update(Mapper.MapToClass<UserViewModel, UserSetBindingModel>(user, true));
|
||
if (user == null)
|
||
{
|
||
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
||
return;
|
||
}
|
||
DialogHelper.MessageInformation("Пароль сброшен", "Успех");
|
||
}
|
||
}
|
||
}
|
||
} |