2022-03-20 10:10:44 +04:00
|
|
|
|
using System;
|
|
|
|
|
using ToolsModule.ManagmentSecurity;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
|
2022-03-20 10:10:44 +04:00
|
|
|
|
namespace ToolsModule.ManagmentEntity
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Базовый класс для логики сущности
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="G"></typeparam>
|
|
|
|
|
/// <typeparam name="S"></typeparam>
|
|
|
|
|
/// <typeparam name="L"></typeparam>
|
|
|
|
|
/// <typeparam name="E"></typeparam>
|
2022-03-19 19:58:10 +04:00
|
|
|
|
public class GenericBusinessLogic<G, S, L, E> : CoreBusinessLogic, IGenericEntityLogic<G, S, L, E>
|
2021-03-28 19:15:55 +04:00
|
|
|
|
where G : GetBindingModel
|
|
|
|
|
where S : SetBindingModel
|
|
|
|
|
where L : ListViewModel<E>
|
|
|
|
|
where E : ElementViewModel
|
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис с хранилищем данных
|
|
|
|
|
/// </summary>
|
2022-03-19 19:58:10 +04:00
|
|
|
|
protected IGenericEntityService<G, S> Service { get; set; }
|
2021-03-28 19:15:55 +04:00
|
|
|
|
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возможен ли просмотр без авторизации
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected bool _allowSimpleView = true;
|
|
|
|
|
|
2022-03-19 19:58:10 +04:00
|
|
|
|
public GenericBusinessLogic(IGenericEntityService<G, S> service, string entity, AccessOperation serviceOperation)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Service = service;
|
|
|
|
|
_entity = entity;
|
|
|
|
|
_serviceOperation = serviceOperation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public L GetList(G model)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
model.HaveRight = !NoAccess(model, AccessType.View);
|
2021-03-28 20:26:25 +04:00
|
|
|
|
if (model.HaveRight && !_allowSimpleView)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
throw new MethodAccessException("Нет прав на получение списка");
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Read(model);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
Errors.AddRange(result.Errors);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as L;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка получения (Stack)", ex.StackTrace));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public E GetElement(G model)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
model.HaveRight = !NoAccess(model, AccessType.View);
|
2021-03-28 20:26:25 +04:00
|
|
|
|
if (model.HaveRight && !_allowSimpleView)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
throw new MethodAccessException("Нет прав на получение списка");
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Read(model);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
Errors.AddRange(result.Errors);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка получения (Stack)", ex.StackTrace));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public virtual E Create(S model)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Change))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Create(model);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
Errors.AddRange(result.Errors);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка создания", ex.Message));
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка создания (Stack)", ex.StackTrace));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Изменение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public virtual E Update(S model)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Change))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Update(model);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
Errors.AddRange(result.Errors);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка изменения", ex.Message));
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка изменения (Stack)", ex.StackTrace));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public virtual bool Delete(G model)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Delete))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Delete(model);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
Errors.AddRange(result.Errors);
|
2021-03-28 19:15:55 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка удаления", ex.Message));
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка удаления (Stack)", ex.StackTrace));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-28 17:58:52 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Восстанолвение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public virtual E Restore(G model)
|
2021-04-28 17:58:52 +04:00
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Change))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var result = Service.Restore(model);
|
2021-04-28 17:58:52 +04:00
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(result.Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.Result as E;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-03-19 11:53:10 +04:00
|
|
|
|
Errors.Add(("Ошибка восстановления", ex.Message));
|
|
|
|
|
Errors.Add(("Ошибка восстановления (Stack)", ex.StackTrace));
|
2021-04-28 17:58:52 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|