From edaee4d4db5076e1098edf0e0eef7a4dc3052acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 13 Apr 2021 09:37:51 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0osharp=E5=88=B05.0.5-prev?= =?UTF-8?q?iew.413?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj | 2 +- src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj | 4 ++-- src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj b/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj index 853fe29..72dca71 100644 --- a/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj +++ b/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj b/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj index 6751134..ade818e 100644 --- a/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj +++ b/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj b/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj index 79ff415..2166a38 100644 --- a/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj +++ b/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj @@ -21,8 +21,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - + + -- Gitee From 3aa2186650aac731087fb5e3df755ab0a4dcb979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Tue, 13 Apr 2021 21:19:36 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E5=A4=96=E9=94=AE=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OSharp.CodeGenerator.sln.DotSettings | 1 + .../Services/DataService.CodeForeign.cs | 2 +- .../Views/Entities/EntityViewModel.cs | 17 +-- .../Views/Entities/ForeignListView.xaml | 131 +++++++++++++++++ .../Views/Entities/ForeignListView.xaml.cs | 28 ++++ .../Views/Entities/ForeignListViewModel.cs | 132 ++++++++++++++++++ .../Views/Entities/ForeignViewModel.cs | 98 +++++++++++++ src/OSharp.CodeGenerator/Views/MainView.xaml | 1 + .../Views/MainViewModel.cs | 2 + .../Projects/ProjectTemplateListView.xaml | 4 +- .../Views/ViewModelLocator.cs | 3 +- 11 files changed, 404 insertions(+), 15 deletions(-) create mode 100644 src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml create mode 100644 src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml.cs create mode 100644 src/OSharp.CodeGenerator/Views/Entities/ForeignListViewModel.cs create mode 100644 src/OSharp.CodeGenerator/Views/Entities/ForeignViewModel.cs diff --git a/OSharp.CodeGenerator.sln.DotSettings b/OSharp.CodeGenerator.sln.DotSettings index 0462b99..072fdbd 100644 --- a/OSharp.CodeGenerator.sln.DotSettings +++ b/OSharp.CodeGenerator.sln.DotSettings @@ -4173,6 +4173,7 @@ finally True True True + True True True True diff --git a/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs b/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs index 527b09b..aef6d69 100644 --- a/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs +++ b/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs @@ -58,7 +58,7 @@ namespace OSharp.CodeGeneration.Services { return new OperationResult(OperationResultType.Error, $"编号为“{dto.EntityId}”的实体信息不存在"); } - if (await CheckCodeForeignExists(m => m.SelfNavigation == dto.SelfNavigation && m.EntityId == dto.EntityId, entity.Id)) + if (await CheckCodeForeignExists(m => m.SelfNavigation == dto.SelfNavigation && m.EntityId == dto.EntityId, dto.Id)) { return new OperationResult(OperationResultType.Error, $"实体“{entity.Name}”中名称为“{dto.SelfNavigation}”的外键信息已存在"); } diff --git a/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs b/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs index d111d1b..71a6aee 100644 --- a/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs +++ b/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs @@ -8,19 +8,15 @@ // ----------------------------------------------------------------------- using System; -using System.Windows; -using System.Windows.Controls; using FluentValidation; -using MahApps.Metro.Controls; - using OSharp.CodeGeneration.Services.Dtos; using OSharp.CodeGeneration.Services.Entities; using OSharp.CodeGenerator.Data; using OSharp.CodeGenerator.Views.Modules; -using OSharp.CodeGenerator.Views.Properties; using OSharp.Mapping; +using OSharp.Wpf.Stylet; using Stylet; @@ -32,10 +28,7 @@ namespace OSharp.CodeGenerator.Views.Entities public class EntityViewModel : Screen { private readonly IServiceProvider _provider; - - /// - /// 初始化一个类型的新实例 - /// + public EntityViewModel(IModelValidator validator, IServiceProvider provider) : base(validator) { @@ -79,7 +72,11 @@ namespace OSharp.CodeGenerator.Views.Entities public void ForeignKey() { - Helper.Output($"“{Name}” - ForeignKey"); + ForeignListViewModel list = IoC.Get(); + list.Entity = this; + list.Title = $"实体“{Display}[{Name}]”的外键管理"; + list.Init(); + list.IsShow = true; } public void Up() diff --git a/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml new file mode 100644 index 0000000..8302b69 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml @@ -0,0 +1,131 @@ + + + + + + + 实体外键列表 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml.cs b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml.cs new file mode 100644 index 0000000..ed10240 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace OSharp.CodeGenerator.Views.Entities +{ + /// + /// ForeignListView.xaml 的交互逻辑 + /// + public partial class ForeignListView : UserControl + { + public ForeignListView() + { + InitializeComponent(); + } + } +} diff --git a/src/OSharp.CodeGenerator/Views/Entities/ForeignListViewModel.cs b/src/OSharp.CodeGenerator/Views/Entities/ForeignListViewModel.cs new file mode 100644 index 0000000..abfc693 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListViewModel.cs @@ -0,0 +1,132 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) 2014-2021 OSharp. All rights reserved. +// +// http://www.osharp.org +// 郭明锋 +// 2021-04-13 11:10 +// ----------------------------------------------------------------------- + +using System; +using System.Linq; +using System.Linq.Expressions; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +using Notifications.Wpf.Core; + +using OSharp.CodeGeneration.Services; +using OSharp.CodeGeneration.Services.Dtos; +using OSharp.CodeGeneration.Services.Entities; +using OSharp.CodeGenerator.Data; +using OSharp.Data; +using OSharp.Mapping; +using OSharp.Wpf.Stylet; + +using Stylet; + + +namespace OSharp.CodeGenerator.Views.Entities +{ + [Singleton] + public class ForeignListViewModel : Screen + { + private readonly IServiceProvider _provider; + + public ForeignListViewModel(IServiceProvider provider) + { + _provider = provider; + } + + public bool IsShow { get; set; } + + public string Title { get; set; } + + public IObservableCollection Foreigns { get; set; } = new BindableCollection(); + + public EntityViewModel Entity { get; set; } + + public IObservableCollection SelfNavigations + { + get + { + string[] props = GetPropNames(m => m.EntityId == Entity.Id && m.IsNavigation); + return new BindableCollection(props); + } + } + + public IObservableCollection SelfForeignKeys + { + get + { + string[] props = GetPropNames(m => m.EntityId == Entity.Id && m.IsForeignKey); + return new BindableCollection(props); + } + } + + public ForeignRelation[] ForeignRelations { get; } = { ForeignRelation.ManyToOne, ForeignRelation.OneToMany, ForeignRelation.OneToOne, ForeignRelation.OwnsOne, ForeignRelation.OwnsMany }; + + public DeleteBehavior?[] DeleteBehaviors { get; } = { null, DeleteBehavior.ClientSetNull, DeleteBehavior.Restrict, DeleteBehavior.SetNull, DeleteBehavior.Cascade }; + + public void Init() + { + CodeForeign[] foreigns = new CodeForeign[0]; + _provider.ExecuteScopedWork(provider => + { + IDataContract contract = provider.GetRequiredService(); + foreigns = contract.CodeForeigns.Where(m => m.EntityId == Entity.Id).ToArray(); + }); + Foreigns.Clear(); + foreach (CodeForeign foreign in foreigns) + { + ForeignViewModel model = _provider.GetRequiredService(); + model = foreign.MapTo(model); + Foreigns.Add(model); + } + } + + public void New() + { + ForeignViewModel model = IoC.Get(); + model.EntityId = Entity.Id; + Foreigns.Add(model); + } + + public bool CanSave => Foreigns.All(m => !m.HasErrors); + public async void Save() + { + if (!CanSave) + { + Helper.Notify("实体外键信息验证失败", NotificationType.Warning); + return; + } + + CodeForeignInputDto[] dtos = Foreigns.Select(m => m.MapTo()).ToArray(); + OperationResult result = null; + await _provider.ExecuteScopedWorkAsync(async provider => + { + IDataContract contract = provider.GetRequiredService(); + result = await contract.UpdateCodeForeigns(dtos); + }); + Helper.Notify(result); + if (!result.Succeeded) + { + return; + } + + Init(); + } + + private string[] GetPropNames(Expression> exp) + { + string[] props = new string[0]; + _provider.ExecuteScopedWork(provider => + { + IDataContract contract = provider.GetRequiredService(); + props = contract.CodeProperties.Where(exp).Select(m => m.Name).ToArray(); + }); + return props; + } + } +} diff --git a/src/OSharp.CodeGenerator/Views/Entities/ForeignViewModel.cs b/src/OSharp.CodeGenerator/Views/Entities/ForeignViewModel.cs new file mode 100644 index 0000000..b23be45 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignViewModel.cs @@ -0,0 +1,98 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) 2014-2021 OSharp. All rights reserved. +// +// http://www.osharp.org +// 郭明锋 +// 2021-04-13 11:09 +// ----------------------------------------------------------------------- + +using System; +using System.Linq; +using System.Windows.Controls; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +using OSharp.CodeGeneration.Services; +using OSharp.CodeGeneration.Services.Dtos; +using OSharp.CodeGeneration.Services.Entities; +using OSharp.CodeGenerator.Data; +using OSharp.Mapping; + +using Stylet; + + +namespace OSharp.CodeGenerator.Views.Entities +{ + [MapTo(typeof(CodeForeignInputDto))] + [MapFrom(typeof(CodeForeign))] + public class ForeignViewModel : Screen + { + private readonly IServiceProvider _provider; + + public ForeignViewModel(IServiceProvider provider) + { + _provider = provider; + } + + public Guid Id { get; set; } + + public string SelfNavigation { get; set; } + + public string SelfForeignKey { get; set; } + + public string OtherEntity { get; set; } + + public string OtherNavigation { get; set; } + + public ForeignRelation ForeignRelation { get; set; } + + public DeleteBehavior? DeleteBehavior { get; set; } + + public bool IsRequired { get; set; } + + public Guid EntityId { get; set; } + + public void SelfNavigationChanged(SelectionChangedEventArgs args) + { + if (args.AddedItems.Count == 0 || args.AddedItems[0] is not string propName) + { + return; + } + + _provider.ExecuteScopedWork(provider => + { + IDataContract contract = provider.GetRequiredService(); + CodeProperty selfNav = contract.CodeProperties.First(m => m.Name == propName && m.IsNavigation && m.EntityId == EntityId); + CodeProperty selfKey = contract.CodeProperties + .FirstOrDefault(m => m.EntityId == EntityId && m.IsForeignKey && m.RelateEntity == selfNav.RelateEntity); + SelfForeignKey = selfKey?.Name; + OtherEntity = selfKey?.RelateEntity; + }); + } + + public void SelfForeignKeyChanged(SelectionChangedEventArgs args) + { + if (args.AddedItems.Count == 0 || args.AddedItems[0] is not string propName) + { + return; + } + + _provider.ExecuteScopedWork(provider => + { + IDataContract contract = provider.GetRequiredService(); + CodeProperty selfKey = contract.CodeProperties.First(m => m.Name == propName && m.IsForeignKey && m.EntityId == EntityId); + CodeProperty selfNav = contract.CodeProperties + .FirstOrDefault(m => m.EntityId == EntityId && m.IsNavigation && m.RelateEntity == selfKey.RelateEntity); + SelfNavigation = selfNav?.Name; + OtherEntity = selfNav?.RelateEntity; + }); + } + + public void Delete() + { + Helper.Output($"“{SelfNavigation}” - Delete"); + } + } +} diff --git a/src/OSharp.CodeGenerator/Views/MainView.xaml b/src/OSharp.CodeGenerator/Views/MainView.xaml index 57f6a42..39c83de 100644 --- a/src/OSharp.CodeGenerator/Views/MainView.xaml +++ b/src/OSharp.CodeGenerator/Views/MainView.xaml @@ -46,6 +46,7 @@ + diff --git a/src/OSharp.CodeGenerator/Views/MainViewModel.cs b/src/OSharp.CodeGenerator/Views/MainViewModel.cs index 2e96e63..1152435 100644 --- a/src/OSharp.CodeGenerator/Views/MainViewModel.cs +++ b/src/OSharp.CodeGenerator/Views/MainViewModel.cs @@ -51,6 +51,8 @@ namespace OSharp.CodeGenerator.Views public EntityListViewModel EntityList { get; set; } = IoC.Get(); + public ForeignListViewModel ForeignList { get; set; } = IoC.Get(); + public PropertyListViewModel PropertyList { get; set; } = IoC.Get(); public TemplateListViewModel TemplateList { get; set; } = IoC.Get(); diff --git a/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml b/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml index 3d193eb..6993520 100644 --- a/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml +++ b/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml @@ -16,8 +16,8 @@ - - 项目模板列表 + + 项目模板列表