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

122 lines
4.3 KiB
C#
Raw Normal View History

2021-04-03 11:41:02 +04:00
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.BusinessLogics;
using DepartmentBusinessLogic.ViewModels;
using DesktopTools.BaseControls;
2021-04-03 11:41:02 +04:00
using DesktopTools.Controls;
using DesktopTools.Helpers;
2021-04-03 11:41:02 +04:00
using DesktopTools.Interfaces;
using DesktopTools.Models;
using ModuleTools.BusinessLogics;
using ModuleTools.Extensions;
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;
using System.Collections.Generic;
using System.Threading.Tasks;
2021-04-03 11:41:02 +04:00
namespace DepartmentWindowsDesktop.EntityControls
{
2021-04-03 13:41:19 +04:00
/// <summary>
/// Реализация контрола для сотрудника
/// </summary>
2021-04-03 11:41:02 +04:00
public partial class ControlEmployeeElement :
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() };
public ControlViewEntityElementConfiguration GetConfigControl() => new()
{
ControlOnMoveElem = new Dictionary<string, (string Title, EventHandler Event)>
{
{ "ToolStripMenuItemAddUser", ("Добавить пользователя", async (object sender, EventArgs e) => { await AddUserAsync(); }) },
{ "ToolStripMenuItemPasswordReset", ("Сброс пароля пользователя", async (object sender, EventArgs e) => { await PasswordResetAsync(); }) }
}
};
/// <summary>
/// Поиск пользователя под учетку, либо добавление нового, если не найдено
/// </summary>
private async Task AddUserAsync()
{
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)}";
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.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);
}
}
}
}
2021-04-12 12:35:05 +04:00
/// <summary>
/// Сброс пароля пользователя
/// </summary>
private async Task PasswordResetAsync()
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>();
var user = await logic.GetElementAsync(new UserGetBindingModel { Id = _element.UserId });
2021-04-12 12:35:05 +04:00
if (user == null)
{
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
return;
}
user.Password = model.DateBirth.ToShortDateString();
user = await logic.UpdateAsync(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
}
}