152 lines
3.8 KiB
C#
152 lines
3.8 KiB
C#
using DesktopTools.BaseControls;
|
|
using ModuleTools.Attributes;
|
|
using ModuleTools.BindingModels;
|
|
using ModuleTools.BusinessLogics;
|
|
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> : BaseControlViewEntityElement
|
|
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 DropValues;
|
|
|
|
private event Func<bool> CheckValues;
|
|
|
|
private event Action<object> GetValues;
|
|
private E Element { get; set; }
|
|
|
|
public GenericControlEntityElement(Guid? id)
|
|
{
|
|
InitializeComponent();
|
|
InitEvents();
|
|
_businessLogic = DependencyManager.Instance.Resolve<BL>();
|
|
Element = _businessLogic.GetElement(new G { Id = id });
|
|
}
|
|
|
|
|
|
public override void Open()
|
|
{
|
|
base.Open();
|
|
if (panelContainer.Controls.Count == 0)
|
|
{
|
|
Configurate();
|
|
}
|
|
//LoadList();
|
|
}
|
|
|
|
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 (property.PropertyType.Name.ToLower())
|
|
{
|
|
case "string":
|
|
control = new BaseControlText(property.Name);
|
|
break;
|
|
case "int32":
|
|
control = new BaseControlInt(property.Name);
|
|
break;
|
|
}
|
|
if (control == null)
|
|
{
|
|
continue;
|
|
}
|
|
if (Element != null)
|
|
{
|
|
control.SetValueToControl(property.GetValue(Element));
|
|
}
|
|
|
|
var widthTitle = control.SetTitle(attribute.DisplayName);
|
|
if (widthTitle > titleWidth)
|
|
{
|
|
titleWidth = widthTitle;
|
|
}
|
|
|
|
control.OnValueChangeEvent += ValueChange;
|
|
DropValues += control.DropValue;
|
|
CheckValues += control.CheckValue;
|
|
SetTitleWidth += control.SetTitleWidth;
|
|
GetValues += control.FillPropertyToObject;
|
|
|
|
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;
|
|
|
|
control.SetReadOnly(attribute.ReadOnly);
|
|
control.SetMustCheckValue(attribute.MustHaveValue);
|
|
|
|
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}");
|
|
}
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|