DepartmentProject/DepartmentPortal/Common/DesktopTools/Controls/GenericControlEntityElement.cs

177 lines
5.4 KiB
C#

using DesktopTools.BaseControls;
using ModuleTools.Attributes;
using ModuleTools.BindingModels;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Extensions;
using ModuleTools.ViewModels;
using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace DesktopTools.Controls
{
public partial class GenericControlEntityElement<G, S, L, E, BL> : AbstractControlViewEntityElement
where G : GetBindingModel, new()
where S : SetBindingModel, new()
where L : ListViewModel<E>
where E : ElementViewModel
where BL : GenericBusinessLogic<G, S, L, E>
{
/// <summary>
/// Объект бизнес-логики для получения данных
/// </summary>
private readonly BL _businessLogic;
private event Action<int> SetTitleWidth;
private event Action<object> SetValues;
private event Action DropValues;
private event Func<bool> CheckValues;
private event Action<object> GetValues;
private E _element = null;
private E Element { get { return _element; } set { _element = value; SetValues?.Invoke(_element); _haveChages = false; } }
public GenericControlEntityElement()
{
InitializeComponent();
InitEvents();
_businessLogic = DependencyManager.Instance.Resolve<BL>();
}
public override void Open(Guid? id)
{
if (panelContainer.Controls.Count == 0)
{
Configurate();
}
Element = _businessLogic.GetElement(new G { Id = id });
}
private void InitEvents()
{
toolStripButtonSave.Click += (object sender, EventArgs e) => { Save(); };
toolStripButtonReload.Click += (object sender, EventArgs e) => { DropValues?.Invoke(); };
toolStripButtonClose.Click += (object sender, EventArgs e) => { Close(); };
}
private void Configurate()
{
int positionY = 5;
int positionX = 5;
int interval = 15;
int titleWidth = 0;
foreach (var property in typeof(E).GetProperties())
{
var attribute = property.GetCustomAttribute<ViewModelOnElementPropertyAttribute>();
if (attribute != null)
{
AbstractBaseControl control = null;
switch (attribute.ControlType)
{
case ControlType.ControlString:
control = new BaseControlString(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.MaxLength);
break;
case ControlType.ControlText:
control = new BaseControlText(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.MaxLength, attribute.Height);
break;
case ControlType.ControlInt:
control = new BaseControlInt(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.MinValue, attribute.MaxValue);
break;
case ControlType.ControlDecimal:
control = new BaseControlDecimal(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.MinValue,
attribute.MaxValue, attribute.DecimalPlaces);
break;
case ControlType.ControlBool:
control = new BaseControlBool(property.Name, attribute.MustHaveValue, attribute.ReadOnly);
break;
case ControlType.ControlDateTime:
control = new BaseControlDateTime(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.MinDate,
attribute.MaxDate, attribute.CustomDateFormat);
break;
case ControlType.ControlImage:
control = new BaseControlImage(property.Name, attribute.MustHaveValue, attribute.ReadOnly, attribute.Width, attribute.Height);
break;
case ControlType.ControlEnum:
control = new BaseControlEnum(property.Name, attribute.MustHaveValue, attribute.ReadOnly, property.PropertyType);
break;
case ControlType.ControlGuid:
if (attribute.ControlTypeObject.IsNotEmpty())
{
control = new BaseControlGuid(property.Name, attribute.MustHaveValue, attribute.ReadOnly,
DependencyManager.Instance.Resolve(Type.GetType(attribute.ControlTypeObject)) as AbstractControlViewEntityList);
}
break;
}
if (control == null)
{
continue;
}
var widthTitle = control.SetTitle(attribute.DisplayName);
if (widthTitle > titleWidth)
{
titleWidth = widthTitle;
}
control.OnValueChangeEvent += ValueChange;
SetValues += control.SetValue;
DropValues += control.DropValue;
CheckValues += control.CheckValue;
SetTitleWidth += control.SetTitleWidth;
GetValues += control.GetValue;
control.Location = new System.Drawing.Point(positionX, positionY);
control.Width = Width - positionX * 2;
control.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left;
positionY += control.Height + interval;
panelContainer.Controls.Add(control);
}
}
SetTitleWidth(titleWidth);
}
protected override bool Save()
{
if (CheckValues.GetInvocationList().Select(x => (bool)x.DynamicInvoke()).ToList().Any(x => !x))
{
return false;
}
var model = Element == null ? new S() : Mapper.MapToClass<E, S>(Element, true);
GetValues(model);
if (model != null)
{
if (Element == null)
{
Element = _businessLogic.Create(model);
}
else
{
Element = _businessLogic.Update(model);
}
if (Element == null)
{
throw new Exception($"Ошибка при сохранении: {_businessLogic.Errors.LastOrDefault().Message}");
}
_haveChages = false;
MessageBox.Show("Сохранение прошло успешно", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
return false;
}
}
}