DepartmentProject/DepartmentPortal/Department/DepartmentWindowsDesktop/EntityControls/Employee/ControlEmployeeElement.cs

122 lines
4.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.ManagmentDependency;
using ToolsModule.ManagmentExtension;
using ToolsModule.ManagmentMapping;
namespace DepartmentWindowsDesktop.EntityControls
{
/// <summary>
/// Реализация контрола для сотрудника
/// </summary>
public partial class ControlEmployeeElement :
GenericControlEntityElement<EmployeeGetBindingModel, EmployeeSetBindingModel, EmployeeListViewModel, EmployeeViewModel, IEmployeeLogic>,
IGenericControlEntityElement
{
public ControlEmployeeElement()
{
InitializeComponent();
Title = "Сотрудник";
ControlId = new Guid("9aadbb72-dde5-483f-9bba-021127b42c49");
_genericControlViewEntityElement = this;
}
public IControl GetInstanceGenericControl() => new ControlEmployeeElement() { 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 EmployeeSetBindingModel();
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 result = logic.GetList(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 = logic.Create(new UserSetBindingModel
{
UserName = userName,
PasswordHash = model.DateBirth.ToShortDateString(),
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);
}
}
}
}
/// <summary>
/// Сброс пароля пользователя
/// </summary>
private void PasswordReset()
{
if (_element == null)
{
return;
}
var model = new EmployeeSetBindingModel();
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.DateBirth.ToShortDateString();
user = logic.Update(Mapper.MapToClass<UserViewModel, UserSetBindingModel>(user, true));
if (user == null)
{
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
return;
}
DialogHelper.MessageInformation("Пароль сброшен", "Успех");
}
}
}
}