2021-03-29 23:16:11 +04:00
|
|
|
|
using DesktopTools.BaseControls;
|
|
|
|
|
using ModuleTools.Attributes;
|
|
|
|
|
using ModuleTools.BindingModels;
|
|
|
|
|
using ModuleTools.BusinessLogics;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Extensions;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
using ModuleTools.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace DesktopTools.Controls
|
|
|
|
|
{
|
2021-03-30 22:34:31 +04:00
|
|
|
|
public partial class GenericControlEntityElement<G, S, L, E, BL> : AbstractControlViewEntityElement
|
2021-03-29 23:16:11 +04:00
|
|
|
|
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;
|
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
private event Action<object> SetValues;
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
private event Action DropValues;
|
|
|
|
|
|
|
|
|
|
private event Func<bool> CheckValues;
|
|
|
|
|
|
|
|
|
|
private event Action<object> GetValues;
|
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
private E _element = null;
|
|
|
|
|
private E Element { get { return _element; } set { _element = value; SetValues?.Invoke(_element); _haveChages = false; } }
|
|
|
|
|
|
|
|
|
|
public GenericControlEntityElement()
|
2021-03-29 23:16:11 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitEvents();
|
|
|
|
|
_businessLogic = DependencyManager.Instance.Resolve<BL>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
public override void Open(Guid? id)
|
2021-03-29 23:16:11 +04:00
|
|
|
|
{
|
|
|
|
|
if (panelContainer.Controls.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Configurate();
|
|
|
|
|
}
|
2021-03-30 22:34:31 +04:00
|
|
|
|
Element = _businessLogic.GetElement(new G { Id = id });
|
2021-03-29 23:16:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
switch (attribute.ControlType)
|
2021-03-29 23:16:11 +04:00
|
|
|
|
{
|
2021-03-30 22:34:31 +04:00
|
|
|
|
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);
|
2021-03-29 23:16:11 +04:00
|
|
|
|
break;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-29 23:16:11 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (control == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var widthTitle = control.SetTitle(attribute.DisplayName);
|
|
|
|
|
if (widthTitle > titleWidth)
|
|
|
|
|
{
|
|
|
|
|
titleWidth = widthTitle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
control.OnValueChangeEvent += ValueChange;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
SetValues += control.SetValue;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
DropValues += control.DropValue;
|
|
|
|
|
CheckValues += control.CheckValue;
|
|
|
|
|
SetTitleWidth += control.SetTitleWidth;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
GetValues += control.GetValue;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
_haveChages = false;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|