| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using AutoProperties;
- using Comal.Classes;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Integration.Logikal;
- using InABox.WPF;
- using PRSDesktop.Integrations.Logikal;
- namespace PRSDesktop.Integrations.Common;
- public class IntegrationBOMWindowViewModel : DependencyObject
- {
-
- private ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour> _bom;
- public static DependencyProperty BOMProperty = DependencyProperty.Register(
- nameof(BOM),
- typeof(ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>),
- typeof(IntegrationBOMWindowViewModel),
- new FrameworkPropertyMetadata(BOMChanged));
-
- private static void BOMChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not IntegrationBOMWindowViewModel model)
- return;
-
- var bom =
- e.NewValue as ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass,
- LogikalLabour>;
-
- ExtractMappings<ProductStyleIntegrationCode, LogikalFinish, ProductStyle, ProductStyleLink, ProductStyleIntegrationSource>(
- bom?.Finishes, x => x.Code, x => x.Description, x => x.Code, s => model.Styles = s);
-
- ExtractMappings<ProductIntegrationCode,LogikalProfile,Product,ProductLink, ProductIntegrationSource>(
- bom?.Profiles, x => x.Code, x => x.Description, x => x.Code, p => model.Profiles = p);
-
- ExtractMappings<ProductIntegrationCode,LogikalComponent,Product,ProductLink, ProductIntegrationSource>(
- bom?.Components, x => x.Code, x => x.Description, x => x.Code, c => model.Components = c);
-
- ExtractMappings<ProductIntegrationCode,LogikalGlass,Product,ProductLink, ProductIntegrationSource>(
- bom?.Glass, x => x.Code, x => x.Description, x => x.Code, g => model.Glass = g);
-
- ExtractMappings<ActivityIntegrationCode,LogikalLabour,Activity,ActivityLink, ActivityIntegrationSource>(
- bom?.Labour, x => x.Code, x => x.Description, x => x.Code, l => model.Labour = l);
- }
-
- public ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>? BOM
- {
- get => GetValue(BOMProperty) as ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>;
- set => SetValue(BOMProperty, value);
- }
-
-
- private static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
- nameof(Styles),
- typeof(List<ProductStyleIntegrationCode>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductStyleIntegrationCode>? Styles
- {
- get => GetValue(StylesProperty) as List<ProductStyleIntegrationCode>;
- set => SetValue(StylesProperty, value);
- }
-
-
- private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register(
- nameof(Profiles),
- typeof(List<ProductIntegrationCode>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationCode>? Profiles
- {
- get => GetValue(ProfilesProperty) as List<ProductIntegrationCode>;
- set => SetValue(ProfilesProperty, value);
- }
-
-
- private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register(
- nameof(Components),
- typeof(List<ProductIntegrationCode>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationCode>? Components
- {
- get => GetValue(ComponentsProperty) as List<ProductIntegrationCode>;
- set => SetValue(ComponentsProperty, value);
- }
-
-
- private static readonly DependencyProperty GlassProperty = DependencyProperty.Register(
- nameof(Glass),
- typeof(List<ProductIntegrationCode>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationCode>? Glass
- {
- get => GetValue(GlassProperty) as List<ProductIntegrationCode>;
- set => SetValue(GlassProperty, value);
- }
-
-
- private static readonly DependencyProperty LabourProperty = DependencyProperty.Register(
- nameof(Labour),
- typeof(List<ActivityIntegrationCode>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ActivityIntegrationCode>? Labour
- {
- get => GetValue(LabourProperty) as List<ActivityIntegrationCode>;
- set => SetValue(LabourProperty, value);
- }
-
- private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register(
- nameof(Sections),
- typeof(Dictionary<string, BitmapImage>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- private Dictionary<string,BitmapImage> _sections = new()
- {
- { nameof(Styles), PRSDesktop.Resources.palette.AsBitmapImage(64, 64) },
- { nameof(Profiles), PRSDesktop.Resources.profile.AsBitmapImage(64, 64) },
- { nameof(Components), PRSDesktop.Resources.fixings.AsBitmapImage(64, 64) },
- { nameof(Glass), PRSDesktop.Resources.glass.AsBitmapImage(64, 64) },
- { nameof(Labour), PRSDesktop.Resources.quality.AsBitmapImage(64, 64) },
- };
- public Dictionary<string, BitmapImage> Sections
- {
- get => (Dictionary<string, BitmapImage>)GetValue(SectionsProperty);
- set => SetValue(SectionsProperty, value);
- }
-
- private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register(
- nameof(SelectedSection),
- typeof(KeyValuePair<string, BitmapImage>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public KeyValuePair<string, BitmapImage> SelectedSection
- {
- get => (KeyValuePair<string, BitmapImage>)GetValue(SelectedSectionProperty);
- set => SetValue(SelectedSectionProperty, value);
- }
-
- public IntegrationBOMWindowViewModel()
- {
- Sections = _sections;
- SelectedSection = Sections.First();
- }
-
- private static void ExtractMappings<TCode,TLogikal,TEntity,TLink,TMapping>(
- IEnumerable<TLogikal>? items,
- Func<TLogikal,string?> logikalcode,
- Func<TLogikal,string?> logikaldescription,
- Expression<Func<TEntity,object?>> entitycode,
- Action<List<TCode>> update)
- where TCode : BaseIntegrationCode<TEntity,TLink>, new()
- where TEntity : Entity, IRemotable,IPersistent, new()
- where TLink :EntityLink<TEntity>
- where TMapping : BaseIntegrationSource<TEntity,TLink>, IRemotable, IPersistent, new()
- {
- Task.Run<List<TCode>>(() =>
- {
- var f = entitycode.Compile();
-
- var results = new List<TCode>();
- if (items == null)
- return results;
- var sourceitems = new Dictionary<string, string>(
- items.Select(x =>
- new KeyValuePair<string, string>(logikalcode(x) ?? "", logikaldescription(x) ?? "")
- ).Distinct()
- );
- MultiQuery query = new();
- query.Add<TEntity>(
- new Filter<TEntity>(entitycode).InList(sourceitems.Keys.ToArray()),
- Columns.Required<TEntity>().Add(x => x.ID).Add(entitycode)
- );
- query.Add<TMapping>(
- new Filter<TMapping>(x => x.Code).InList(sourceitems.Keys.ToArray()),
- Columns.Required<TMapping>().Add(x => x.ID).Add(x => x.Code)
- );
- query.Query();
- var mappings = query.Get<TMapping>().ToDictionary<TMapping,string,Guid>(x=>x.Code ?? "",x=>x.ID);
- var entities = query.Get<TEntity>().ToDictionary(entitycode, x=>x.ID);
- foreach (var sourceitem in sourceitems)
- {
- var result = new TCode()
- {
- Code = sourceitem.Key,
- Description = sourceitem.Value
- };
- if (mappings.ContainsKey(sourceitem.Key))
- {
- result.Entity.ID = mappings[sourceitem.Key];
- CoreUtils.SetPropertyValue(result.Entity,CoreUtils.GetFullPropertyName(entitycode,"."),sourceitem.Key);
- }
- else if (entities.ContainsKey(sourceitem.Key))
- {
- result.Entity.ID = entities[sourceitem.Key];
- CoreUtils.SetPropertyValue(result.Entity,CoreUtils.GetFullPropertyName(entitycode,"."),sourceitem.Key);
- }
- results.Add(result);
- }
- return results.ToList<TCode>();
- }
- ).ContinueWith(
- (task) => update?.Invoke(task.Result),
- TaskScheduler.FromCurrentSynchronizationContext()
- );
- }
-
-
- }
|