diff --git a/.gitignore b/.gitignore index 9a46b8c561cae6f45008b71ecc3262b66c71a28d..1ed9b70cf0c9d75742c64eb4b41cd0e0e5c4a07c 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ bld/ [Oo]bj/ [Ll]og/ build/output/ +build/*.zip # Visual Studio 2015/2017 cache/options directory .vs/ # Uncomment if you have tasks that create the project's static files in wwwroot diff --git a/OSharp.CodeGenerator.sln.DotSettings b/OSharp.CodeGenerator.sln.DotSettings index 0462b99076c69103ac71373fe397eb722d90e212..62a803fad228ebe6807c1af77f6a3de8c2317207 100644 --- a/OSharp.CodeGenerator.sln.DotSettings +++ b/OSharp.CodeGenerator.sln.DotSettings @@ -176,12 +176,13 @@ True True True - True - True - True - True - True - False + True + + + + + + False False True @@ -4173,6 +4174,7 @@ finally True True True + True True True True diff --git a/build/build.ps1 b/build/build.ps1 index a5966398126809e95b6c6ab12bb4a62fce1e948b..a964720276d53deb6158d4f4122321f91529e025 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -1,4 +1,4 @@ -function GetVersion() +锘縡unction GetVersion() { $file = "version.props" $xml = New-Object -TypeName XML @@ -16,25 +16,30 @@ function GetVersion() } $rootPath = Split-Path -Parent $MyInvocation.MyCommand.Definition -Write-Host ("当前目录:{0}" -f $rootPath) +Write-Host ("褰撳墠鐩綍锛歿0}" -f $rootPath) $version = GetVersion -Write-Host ("当前版本:{0}" -f $version) +Write-Host ("褰撳墠鐗堟湰锛歿0}" -f $version) $output = ".\output" if(Test-Path $output) { Remove-Item ("{0}\*.*" -f $output) - Write-Host ("清空 {0} 文件夹" -f $output) + Write-Host ("娓呯┖ {0} 鏂囦欢澶" -f $output) } else { New-Item -Path . -Name $nupkgs -ItemType "directory" -Force - Write-Host ("创建 {0} 文件夹" -f $output) + Write-Host ("鍒涘缓 {0} 鏂囦欢澶" -f $output) } -$props = @("OSharp.CodeGenerator") -foreach($prop in $props) +$proj = "OSharp.CodeGenerator" +$path = ("../src/{0}/{0}.csproj" -f $proj) +dotnet publish $path -c Release --force --output $output +Rename-Item $output $proj +$output = ("./{0}-{1}.zip" -f $proj,$version) +if(Test-Path $output) { - $path = ("../src/{0}/{0}.csproj" -f $prop) - dotnet publish $path -c Release --force --output $output + Remove-Item $output } +Compress-Archive -Path $proj -DestinationPath $output +Rename-Item $proj "output" diff --git a/build/version.props b/build/version.props index 22ffa24476d7ee0df82e6803483d484fa0c20c2a..3bb7a5d743e2f2bd847dff657cda5a8d49a2bff9 100644 --- a/build/version.props +++ b/build/version.props @@ -3,7 +3,7 @@ 5.0 5 -preview. - 412 + 414 $(VersionMain).$(VersionPrefix)$(VersionSuffix)$(VersionSuffixVersion) $(VersionMain).$(VersionPrefix) diff --git a/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj b/src/OSharp.CodeConsoles/OSharp.CodeConsoles.csproj index 853fe2914cec60661a56e319ebd313e759b05523..72dca7110baa6a6ed5aa04472b383be6125a36b1 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 6751134cc7b50ad5382e09d48207b71c71ab7cd7..ade818ef3db5a6363e17fafbeec43a92417dd180 100644 --- a/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj +++ b/src/OSharp.CodeGeneration/OSharp.CodeGeneration.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/src/OSharp.CodeGeneration/Services/DataPack.cs b/src/OSharp.CodeGeneration/Services/DataPack.cs index 79741cfcaac36e45edca992e81965269b7aaafbe..253e8ee3cfcd57e3fed83475907c85290cf5c0c0 100644 --- a/src/OSharp.CodeGeneration/Services/DataPack.cs +++ b/src/OSharp.CodeGeneration/Services/DataPack.cs @@ -1,3 +1,12 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) 2014-2021 OSharp. All rights reserved. +// +// http://www.osharp.org +// 閮槑閿 +// 2021-04-14 12:53 +// ----------------------------------------------------------------------- + using Microsoft.Extensions.DependencyInjection; using OSharp.CodeGeneration.Services.Seeds; diff --git a/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs b/src/OSharp.CodeGeneration/Services/DataService.CodeForeign.cs index 527b09b4aa950174a06d6520d891a52975a61af4..aef6d69f5668ef99787417122a0e3d0f286231e2 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.CodeGeneration/Templates/cs_Entity.cshtml b/src/OSharp.CodeGeneration/Templates/cs_Entity.cshtml index e8e764d8f40c7e47fbe0b0a37fdd127d62845dc3..d636408c67f6b984902247736ffbb7d9a2ad1545 100644 --- a/src/OSharp.CodeGeneration/Templates/cs_Entity.cshtml +++ b/src/OSharp.CodeGeneration/Templates/cs_Entity.cshtml @@ -162,7 +162,7 @@ namespace @(entity.Module.Namespace).Entities static string GetProperty(CodeProperty prop) { - string propName = "{0}.{1}.{2}".FormatWith(prop.Entity.Module.Name, prop.Entity.Name, prop.Name); + string propName = $"{prop.Entity.Module.Name}.{prop.Entity.Name}.{prop.Name}"; Listlist = new List(); //瀵艰埅灞炴э紝娣诲姞virtual @@ -170,20 +170,19 @@ namespace @(entity.Module.Namespace).Entities { list.Add("virtual"); } - //灞炴х被鍨 string propType = TypeHelper.ToSingleTypeName(prop.TypeName); if (propType == "ICollection<>") { if (prop.RelateEntity.IsNullOrEmpty()) { - throw new OsharpException("灞炴р渰0}鈥濈被鍨嬩负鈥淚Collection<>鈥濈殑瀵艰埅灞炴э紝鍏宠仈瀹炰綋涓嶈兘涓虹┖".FormatWith(propName)); + throw new OsharpException($"灞炴р渰propName}鈥濈被鍨嬩负鈥淚Collection<>鈥濈殑瀵艰埅灞炴э紝鍏宠仈瀹炰綋涓嶈兘涓虹┖"); } - propType = string.Format("ICollection<{0}>".FormatWith(TypeHelper.ToSingleTypeName(prop.RelateEntity))); + propType = string.Format($"ICollection<{TypeHelper.ToSingleTypeName(prop.RelateEntity)}>"); } //鍙┖鐨勫肩被鍨嬶紝娣诲姞?绗﹀彿 Type tmpType = Type.GetType(prop.TypeName); - if (prop.IsNullable && tmpType != null && tmpType.IsValueType) + if (prop.IsNullable && tmpType is {IsValueType: true }) { propType += "?"; } diff --git a/src/OSharp.CodeGeneration/Templates/cs_EntityConfiguration.cshtml b/src/OSharp.CodeGeneration/Templates/cs_EntityConfiguration.cshtml index e94145f0182b321b2867ec567eaa995d2198f27c..08b816437d598dbb67fdc5c8c8852a312e998089 100644 --- a/src/OSharp.CodeGeneration/Templates/cs_EntityConfiguration.cshtml +++ b/src/OSharp.CodeGeneration/Templates/cs_EntityConfiguration.cshtml @@ -59,7 +59,8 @@ namespace @(entity.Module.Project.NamespacePrefix).EntityConfiguration.@entity.M { @foreach (var foreign in entity.Foreigns) { - @GetForeign(foreign, entity) + @GetForeign(foreign, entity) + } EntityConfigurationAppend(builder); @@ -113,12 +114,12 @@ namespace @(entity.Module.Project.NamespacePrefix).EntityConfiguration.@entity.M if (!string.IsNullOrEmpty(foreign.OtherEntity)) { - line += string.Format("<{0}>", TypeHelper.ToSingleTypeName(foreign.OtherEntity)); + line += $"<{TypeHelper.ToSingleTypeName(foreign.OtherEntity)}>"; } line += "("; if (!string.IsNullOrEmpty(foreign.SelfNavigation)) { - line += string.Format("m => m.{0}", foreign.SelfNavigation); + line += $"m => m.{foreign.SelfNavigation}"; } line += ")"; switch (foreign.ForeignRelation) @@ -143,7 +144,7 @@ namespace @(entity.Module.Project.NamespacePrefix).EntityConfiguration.@entity.M { if (!string.IsNullOrEmpty(foreign.OtherNavigation)) { - line += string.Format("n => n.{0})", foreign.OtherNavigation); + line += $"n => n.{foreign.OtherNavigation})"; } else { @@ -154,9 +155,9 @@ namespace @(entity.Module.Project.NamespacePrefix).EntityConfiguration.@entity.M line += ".HasForeignKey"; if (foreign.ForeignRelation == ForeignRelation.OneToOne) { - line += string.Format("<{0}>", TypeHelper.ToSingleTypeName(entity.Name)); + line += $"<{TypeHelper.ToSingleTypeName(entity.Name)}>"; } - line += string.Format("(m => m.{0})", foreign.SelfForeignKey); + line += $"(m => m.{foreign.SelfForeignKey})"; } if (foreign.IsRequired) { @@ -164,7 +165,7 @@ namespace @(entity.Module.Project.NamespacePrefix).EntityConfiguration.@entity.M } if (foreign.DeleteBehavior != null) { - line += string.Format(".OnDelete(DeleteBehavior.{0})", foreign.DeleteBehavior.Value.ToString()); + line += $".OnDelete(DeleteBehavior.{foreign.DeleteBehavior.Value})"; } } return line; diff --git a/src/OSharp.CodeGeneration/Templates/cs_InputDto.cshtml b/src/OSharp.CodeGeneration/Templates/cs_InputDto.cshtml index c110f265ff201fac17aa6cbf03c15fbdf3032a71..adffd684b4876b6bed1ad25a81856d49fc8f8ac3 100644 --- a/src/OSharp.CodeGeneration/Templates/cs_InputDto.cshtml +++ b/src/OSharp.CodeGeneration/Templates/cs_InputDto.cshtml @@ -89,7 +89,7 @@ namespace @(entity.Module.Namespace).Dtos private static string GetAttributes(CodeProperty prop) { Listlist = new List(); - list.Add(string.Format("DisplayName(\"{0}\")", prop.Display)); + list.Add($"DisplayName(\"{prop.Display}\")"); if (prop.TypeName == "System.String") { if (prop.IsRequired.HasValue && prop.IsRequired.Value) @@ -98,8 +98,8 @@ namespace @(entity.Module.Namespace).Dtos } if (prop.MaxLength > 0) { - string min = prop.MinLength > -1 ? string.Format(", MinimumLength = {0}", prop.MinLength.Value) : ""; - list.Add(string.Format("StringLength({0}{1})", prop.MaxLength.Value, min)); + string min = prop.MinLength > -1 ? $", MinimumLength = {prop.MinLength.Value}" : ""; + list.Add($"StringLength({prop.MaxLength.Value}{min})"); } } return list.ExpandAndToString(", "); @@ -112,7 +112,7 @@ namespace @(entity.Module.Namespace).Dtos string propType = TypeHelper.ToSingleTypeName(prop.TypeName); //鍙┖鐨勫肩被鍨嬶紝娣诲姞?绗﹀彿 Type tmpType = Type.GetType(prop.TypeName); - if (prop.IsNullable && tmpType != null && tmpType.IsValueType) + if (prop.IsNullable && tmpType is {IsValueType: true }) { propType += "?"; } diff --git a/src/OSharp.CodeGeneration/Templates/cs_OutputDto.cshtml b/src/OSharp.CodeGeneration/Templates/cs_OutputDto.cshtml index cc96ea62beeb189a0db723446c16ec5c46588371..5197677294c60d447771b34c87cbeb980a12af19 100644 --- a/src/OSharp.CodeGeneration/Templates/cs_OutputDto.cshtml +++ b/src/OSharp.CodeGeneration/Templates/cs_OutputDto.cshtml @@ -169,7 +169,7 @@ namespace @(entity.Module.Namespace).Dtos string propType = TypeHelper.ToSingleTypeName(prop.TypeName); //鍙┖鐨勫肩被鍨嬶紝娣诲姞?绗﹀彿 Type tmpType = Type.GetType(prop.TypeName); - if (prop.IsNullable && tmpType != null && tmpType.IsValueType) + if (prop.IsNullable && tmpType is {IsValueType: true }) { propType += "?"; } @@ -178,7 +178,7 @@ namespace @(entity.Module.Namespace).Dtos else { string output = TypeHelper.ToSingleTypeName(prop.RelateEntity) + "OutputDto"; - list.Add(prop.TypeName == "ICollection<>" ? string.Format("ICollection<{0}>", output) : output); + list.Add(prop.TypeName == "ICollection<>" ? $"ICollection<{output}>" : output); } return list.ExpandAndToString(""); } @@ -188,7 +188,7 @@ namespace @(entity.Module.Namespace).Dtos Listlist = new List(); foreach (CodeProperty prop in props) { - list.Add(string.Format("{0} = entity.{0}", prop.Name)); + list.Add($"{prop.Name} = entity.{prop.Name}"); } list.AddIf("IsLocked = entity.IsLocked", entity.HasLocked); list.AddIf("CreatedTime = entity.CreatedTime", entity.HasCreatedTime); diff --git a/src/OSharp.CodeGeneration/Templates/ng_AlainComponent.cshtml b/src/OSharp.CodeGeneration/Templates/ng_AlainComponent.cshtml index f771a945b7fada33bc259007d8398f21eedae7bb..a3564d5a085bdcbbc5cae9e1c3e34f005b68fe64 100644 --- a/src/OSharp.CodeGeneration/Templates/ng_AlainComponent.cshtml +++ b/src/OSharp.CodeGeneration/Templates/ng_AlainComponent.cshtml @@ -118,8 +118,8 @@ export class @(entity.Name)Component extends STComponentBase implements OnInit { static string GetColumnProps(CodeProperty prop) { Listlist = new List(); - list.Add("title: '{0}'".FormatWith(prop.Display)); - list.Add("index: '{0}'".FormatWith(prop.Name)); + list.Add($"title: '{prop.Display}'"); + list.Add($"index: '{prop.Name}'"); list.AddIf("readOnly: true", prop.IsReadonly); list.AddIf("sort: true", prop.Sortable); list.AddIf("editable: true", prop.Updatable); @@ -163,7 +163,7 @@ export class @(entity.Name)Component extends STComponentBase implements OnInit { list.Add("'*': { spanLabelFixed: 100, grid: { span: 12 } }"); foreach (CodeProperty prop in props.Where(m => m.TypeName == "System.String")) { - list.Add(string.Format("${0}: {{ grid: {{ span: 24 }} }}", prop.Name)); + list.Add($"${prop.Name}: {{ grid: {{ span: 24 }} }}"); } return list.ExpandAndToString(",\r\n "); diff --git a/src/OSharp.CodeGeneration/Templates/ng_AlainOther.cshtml b/src/OSharp.CodeGeneration/Templates/ng_AlainOther.cshtml index 0ae94cb55105d93b70ae098341d31181d4520110..f07045a79bc63373c1fe242fb7ddd6037f3fbc74 100644 --- a/src/OSharp.CodeGeneration/Templates/ng_AlainOther.cshtml +++ b/src/OSharp.CodeGeneration/Templates/ng_AlainOther.cshtml @@ -44,7 +44,7 @@ Listlist = new List(); foreach (var entity in module.Entities.Where(m=>m.Listable)) { - list.Add(string.Format("{{ path: '{0}', component: {1}Component, canActivate: [ACLGuard], data: {{ title: '{2}绠$悊', reuse: true, guard: 'Root.Admin.{3}.{1}.Read' }} }},\r\n", entity.Name.UpperToLowerAndSplit(), entity.Name, entity.Display, module.Name)); + list.Add($"{{ path: '{entity.Name.UpperToLowerAndSplit()}', component: {entity.Name}Component, canActivate: [ACLGuard], data: {{ title: '{entity.Display}绠$悊', reuse: true, guard: 'Root.Admin.{module.Name}.{entity.Name}.Read' }} }},\r\n"); } return list.ExpandAndToString(""); } @@ -53,10 +53,10 @@ { Listlist = new List(); list.Add("{\r\n"); - list.Add(string.Format(" \"text\": \"{0}妯″潡\",\r\n", module.Display)); + list.Add($" \"text\": \"{module.Display}妯″潡\",\r\n"); list.Add( " \"group\": \"true\",\r\n"); list.Add( " \"icon\": \"anticon-border\",\r\n"); - list.Add(string.Format(" \"acl\": \"Root.Admin.{0}\",\r\n", module.Name)); + list.Add($" \"acl\": \"Root.Admin.{module.Name}\",\r\n"); string children = " \"children\": ["; ListchildList = new List(); foreach (var entity in module.Entities.Where(m => m.Listable)) @@ -77,9 +77,9 @@ { Listlist = new List(); list.Add("{\r\n"); - list.Add(string.Format(" \"text\": \"{0}绠$悊\",\r\n", entity.Display)); - list.Add(string.Format(" \"link\": \"/{0}/{1}\",\r\n", entity.Module.Name.UpperToLowerAndSplit(), entity.Name.UpperToLowerAndSplit())); - list.Add(string.Format(" \"acl\": \"Root.Admin.{0}.{1}\"\r\n", entity.Module.Name, entity.Name)); + list.Add($" \"text\": \"{entity.Display}绠$悊\",\r\n"); + list.Add($" \"link\": \"/{entity.Module.Name.UpperToLowerAndSplit()}/{entity.Name.UpperToLowerAndSplit()}\",\r\n"); + list.Add($" \"acl\": \"Root.Admin.{entity.Module.Name}.{entity.Name}\"\r\n"); list.Add(" }"); return list.ExpandAndToString(""); } diff --git a/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj b/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj index 79ff415333763a508eeaa4b3117b6d31d3e56601..cdb20f7ce3c9438c0dbdf7b53514405a99aaa5ab 100644 --- a/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj +++ b/src/OSharp.CodeGenerator/OSharp.CodeGenerator.csproj @@ -1,4 +1,4 @@ - + @@ -21,8 +21,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs b/src/OSharp.CodeGenerator/Views/Entities/EntityViewModel.cs index d111d1b15f1bfbd55433421c571e928396bde2ac..71a6aee2abb503fcfb29f578655b494aa1eefd99 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 0000000000000000000000000000000000000000..77af028566b24cd968de8b76ea4f4b693d62f2ef --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml @@ -0,0 +1,130 @@ + + + + + + + 瀹炰綋澶栭敭鍒楄〃 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 0000000000000000000000000000000000000000..ed10240b491219e25f76871907c78e087a5457e7 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListView.xaml.cs @@ -0,0 +1,28 @@ +锘縰sing 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 0000000000000000000000000000000000000000..1fff788f0a4988a527342165ae1b2da798c0d275 --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignListViewModel.cs @@ -0,0 +1,136 @@ +// ----------------------------------------------------------------------- +// +// 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 IObservableCollection OtherEntities { get; } = new BindableCollection(); + + public IObservableCollection OtherNavigations { get; } = new BindableCollection(); + + 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 0000000000000000000000000000000000000000..a763e25810a1e7fb48df5ee5c774bfda7564dfeb --- /dev/null +++ b/src/OSharp.CodeGenerator/Views/Entities/ForeignViewModel.cs @@ -0,0 +1,119 @@ +// ----------------------------------------------------------------------- +// +// 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; +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.CodeGenerator.Views.Templates; +using OSharp.Data; +using OSharp.Mapping; +using OSharp.Wpf.Stylet; + +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; } = true; + + 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 async void Delete() + { + if (MessageBox.Show($"鏄惁鍒犻櫎瀹炰綋澶栭敭鈥渰SelfNavigation??SelfForeignKey}鈥濓紵", "璇风‘璁", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel) + { + return; + } + OperationResult result = null; + await _provider.ExecuteScopedWorkAsync(async provider => + { + IDataContract contract = provider.GetRequiredService(); + result = await contract.DeleteCodeForeigns(Id); + }); + Helper.Notify(result); + if (!result.Succeeded) + { + return; + } + + ForeignListViewModel list = IoC.Get(); + list.Init(); + } + } +} diff --git a/src/OSharp.CodeGenerator/Views/MainView.xaml b/src/OSharp.CodeGenerator/Views/MainView.xaml index 57f6a42d0a9faf37317986515b830adfb3da4365..39c83de07f24bd4519d2c087559960524d969526 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 2e96e6328862fa0b8e54199c58fb126d6c66e4b7..1152435133082aa518dbf167275041f110bd39d9 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 3d193eb24ba1eb42a6087e4403152130f0a3bd10..69935209cff2ba7853c4bd33a39887a3838c060f 100644 --- a/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml +++ b/src/OSharp.CodeGenerator/Views/Projects/ProjectTemplateListView.xaml @@ -16,8 +16,8 @@ - - 椤圭洰妯℃澘鍒楄〃 + + 椤圭洰妯℃澘鍒楄〃