2021-04-03 11:41:02 +04:00
|
|
|
|
using DepartmentBusinessLogic.BindingModels;
|
|
|
|
|
using DepartmentBusinessLogic.BusinessLogics;
|
|
|
|
|
using DepartmentBusinessLogic.ViewModels;
|
2021-04-08 14:27:21 +04:00
|
|
|
|
using DesktopTools.BaseControls;
|
2021-04-03 11:41:02 +04:00
|
|
|
|
using DesktopTools.Controls;
|
2021-04-08 14:27:21 +04:00
|
|
|
|
using DesktopTools.Helpers;
|
2021-04-03 11:41:02 +04:00
|
|
|
|
using DesktopTools.Interfaces;
|
2021-04-08 10:37:47 +04:00
|
|
|
|
using DesktopTools.Models;
|
2022-03-18 22:38:52 +04:00
|
|
|
|
using ToolsModule.BusinessLogics;
|
|
|
|
|
using ToolsModule.Extensions;
|
2021-04-08 14:27:21 +04:00
|
|
|
|
using SecurityBusinessLogic.BindingModels;
|
|
|
|
|
using SecurityBusinessLogic.BusinessLogics;
|
2021-04-12 12:35:05 +04:00
|
|
|
|
using SecurityBusinessLogic.ViewModels;
|
2021-04-03 11:41:02 +04:00
|
|
|
|
using System;
|
2021-04-08 14:27:21 +04:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-03 11:41:02 +04:00
|
|
|
|
|
|
|
|
|
namespace DepartmentWindowsDesktop.EntityControls
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация контрола для сотрудника
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class ControlEmployeeElement :
|
2021-04-03 11:41:02 +04:00
|
|
|
|
GenericControlEntityElement<EmployeeGetBindingModel, EmployeeSetBindingModel, EmployeeListViewModel, EmployeeViewModel, EmployeeBusinessLogic>,
|
|
|
|
|
IGenericControlEntityElement
|
|
|
|
|
{
|
|
|
|
|
public ControlEmployeeElement()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Title = "Сотрудник";
|
|
|
|
|
ControlId = new Guid("9aadbb72-dde5-483f-9bba-021127b42c49");
|
|
|
|
|
_genericControlViewEntityElement = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IControl GetInstanceGenericControl() => new ControlEmployeeElement() { ControlId = Guid.NewGuid() };
|
2021-04-08 10:37:47 +04:00
|
|
|
|
|
2021-04-08 14:27:21 +04:00
|
|
|
|
public ControlViewEntityElementConfiguration GetConfigControl() => new()
|
|
|
|
|
{
|
|
|
|
|
ControlOnMoveElem = new Dictionary<string, (string Title, EventHandler Event)>
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
{ "ToolStripMenuItemAddUser", ("Добавить пользователя", (object sender, EventArgs e) => { AddUser(); }) },
|
|
|
|
|
{ "ToolStripMenuItemPasswordReset", ("Сброс пароля пользователя", (object sender, EventArgs e) => { PasswordReset(); }) }
|
2021-04-08 14:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-11 20:49:26 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Поиск пользователя под учетку, либо добавление нового, если не найдено
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void AddUser()
|
2021-04-08 14:27:21 +04:00
|
|
|
|
{
|
|
|
|
|
var model = new EmployeeSetBindingModel();
|
|
|
|
|
if (FillModel(model))
|
|
|
|
|
{
|
|
|
|
|
var logic = DependencyManager.Instance.Resolve<UserBusinessLogic>();
|
|
|
|
|
var userName = $"{model.LastName}{(model.FirstName.IsNotEmpty() ? $" {model.FirstName[0]}." : string.Empty)}{(model.Patronymic.IsNotEmpty() ? $"{model.Patronymic[0]}." : string.Empty)}";
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = logic.GetList(new UserGetBindingModel { UserNameForSearch = userName });
|
2021-04-08 14:27:21 +04:00
|
|
|
|
if (result != null)
|
|
|
|
|
{
|
|
|
|
|
if (result.List.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException("Существует несколько пользователей с такой сигнатурой", "Ошибка");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (result.List.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
model.UserId = result.List[0].Id;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var newuser = logic.Create(new UserSetBindingModel
|
2021-04-08 14:27:21 +04:00
|
|
|
|
{
|
2022-03-18 12:47:34 +04:00
|
|
|
|
UserName = userName,
|
|
|
|
|
PasswordHash = model.DateBirth.ToShortDateString(),
|
2021-04-08 14:27:21 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-12 12:35:05 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сброс пароля пользователя
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void PasswordReset()
|
2021-04-12 12:35:05 +04:00
|
|
|
|
{
|
|
|
|
|
if (_element == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var model = new EmployeeSetBindingModel();
|
|
|
|
|
if (FillModel(model))
|
|
|
|
|
{
|
|
|
|
|
var logic = DependencyManager.Instance.Resolve<UserBusinessLogic>();
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var user = logic.GetElement(new UserGetBindingModel { Id = _element.UserId });
|
2021-04-12 12:35:05 +04:00
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-18 12:47:34 +04:00
|
|
|
|
user.PasswordHash = model.DateBirth.ToShortDateString();
|
2022-03-15 22:02:13 +04:00
|
|
|
|
user = logic.Update(Mapper.MapToClass<UserViewModel, UserSetBindingModel>(user, true));
|
2021-04-12 12:35:05 +04:00
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DialogHelper.MessageInformation("Пароль сброшен", "Успех");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-03 11:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
}
|