From f164566d2df036f4a0bc251d28b4dfe561bebcc9 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sun, 21 Mar 2021 10:44:46 +0800 Subject: [PATCH] =?UTF-8?q?!13=20=E4=BC=98=E5=8C=96=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=EF=BC=8C=E5=9C=A8=E7=94=9F=E6=88=90=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E5=90=8E=E8=87=AA=E5=8A=A8=E5=9C=A8OpenAuthDBContext?= =?UTF-8?q?=E4=B8=AD=E6=96=B0=E5=A2=9EDbSet=20fix=20issue=20#I3BUF5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Helpers/FileHelper.cs | 67 ++++++++++++++++++++ OpenAuth.App/BuilderTableApp.cs | 3 + OpenAuth.App/DbExtension.cs | 88 +++++++++++++++----------- OpenAuth.App/FlowInstanceApp.cs | 23 ++++--- OpenAuth.App/IntAutoGenApp.cs | 40 ++++++++++++ OpenAuth.App/Test/TestFlow.cs | 28 ++++++-- OpenAuth.App/Test/TestIntAutoGenApp.cs | 31 +++++++++ 7 files changed, 229 insertions(+), 51 deletions(-) create mode 100644 OpenAuth.App/IntAutoGenApp.cs create mode 100644 OpenAuth.App/Test/TestIntAutoGenApp.cs diff --git a/Infrastructure/Helpers/FileHelper.cs b/Infrastructure/Helpers/FileHelper.cs index 653abb7e..02763216 100644 --- a/Infrastructure/Helpers/FileHelper.cs +++ b/Infrastructure/Helpers/FileHelper.cs @@ -1,8 +1,10 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using Infrastructure.Extensions; namespace Infrastructure.Helpers @@ -335,5 +337,70 @@ namespace Infrastructure.Helpers return str; } + + /// + /// 括号匹配算法 + /// + /// 原始字符串 + /// 左匹配符号 + /// 右匹配符号 + /// + private static string parenthesisMatch(string dataStr, char leftCode, char rightCode) + { + Stack stack = new Stack(); + + string cut_text = ""; + + for (int i = 0; i < dataStr.Length; ++i) + { + char ch = dataStr[i]; + if (stack.Count > 0) + { + cut_text += ch; + } + if (ch == leftCode) + { + stack.Push(ch); + } + if (ch == rightCode) + { + stack.Pop(); + if (0 == stack.Count) + { + return cut_text.Substring(0, cut_text.Length - 1); + } + } + } + return ""; + } + + /// + /// 替换内容(正则 + 括号匹配算法) + /// + /// 文件路径 + /// 追加内容 + public static void RegxAddContentByParenthesis(string path, string addStr) + { + FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); + StreamReader sr = new StreamReader(fs); + string originStr = sr.ReadToEnd(); + + string pattern = @"DbContext\s*?({.*)"; + if (Regex.IsMatch(originStr, pattern)) + { + Match match = Regex.Match(originStr, pattern, RegexOptions.Singleline); + string cut_str = parenthesisMatch(match.Groups[1].Value, '{', '}'); // 剪切内容(原内容) + string new_str = cut_str + "\r\n " + addStr + "\r\n"; // 实际更新内容 + originStr = originStr.Replace(cut_str, new_str); + } + + sr.Close(); + fs.Close(); + FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write); + StreamWriter sw = new StreamWriter(fs2); + sw.WriteLine(originStr); + sw.Close(); + fs2.Close(); + } } } diff --git a/OpenAuth.App/BuilderTableApp.cs b/OpenAuth.App/BuilderTableApp.cs index ca0bef82..ba39c4da 100644 --- a/OpenAuth.App/BuilderTableApp.cs +++ b/OpenAuth.App/BuilderTableApp.cs @@ -429,6 +429,9 @@ namespace OpenAuth.App mapPath + $"\\OpenAuth.Repository\\Domain\\", tableInfo.ClassName + ".cs", domainContent); + + string openAuthDBContextPath = mapPath + "\\OpenAuth.Repository\\OpenAuthDBContext.cs"; + FileHelper.RegxAddContentByParenthesis(openAuthDBContextPath, "public virtual DbSet<" + tableInfo.ClassName + "> " + tableInfo.TableName + "s { get; set; }"); } private bool IsMysql() diff --git a/OpenAuth.App/DbExtension.cs b/OpenAuth.App/DbExtension.cs index d1003c1c..31efead3 100644 --- a/OpenAuth.App/DbExtension.cs +++ b/OpenAuth.App/DbExtension.cs @@ -3,9 +3,13 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; +using Autofac.Extensions.DependencyInjection; using Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Internal; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; using OpenAuth.Repository; using OpenAuth.Repository.Domain; @@ -15,14 +19,14 @@ namespace OpenAuth.App { public class DbExtension { - private OpenAuthDBContext _context; + private List _contexts = new List(); private IOptions _appConfiguration; - public DbExtension(OpenAuthDBContext context, IOptions appConfiguration) + public DbExtension(IOptions appConfiguration, OpenAuthDBContext openAuthDbContext) { - _context = context; _appConfiguration = appConfiguration; + _contexts.Add(openAuthDbContext); //如果有多个DBContext,可以按OpenAuthDBContext同样的方式添加到_contexts中 } /// @@ -34,8 +38,12 @@ namespace OpenAuth.App { var result = new List(); const string domain = "openauth.repository.domain."; - var entity = _context.Model.GetEntityTypes() - .FirstOrDefault(u => u.Name.ToLower()==domain + moduleName.ToLower()); + IEntityType entity = null; + _contexts.ForEach(u => + { + entity = u.Model.GetEntityTypes() + .FirstOrDefault(u => u.Name.ToLower() == domain + moduleName.ToLower()); + }); if (entity == null) { throw new Exception($"未能找到{moduleName}对应的实体类"); @@ -74,14 +82,17 @@ namespace OpenAuth.App public List GetDbEntityNames() { var names = new List(); - var model = _context.Model; + var models = _contexts.Select(u =>u.Model); - // Get all the entity types information contained in the DbContext class, ... - var entityTypes = model.GetEntityTypes(); - foreach (var entityType in entityTypes) + foreach (var model in models) { - var tableNameAnnotation = entityType.GetAnnotation("Relational:TableName"); - names.Add(tableNameAnnotation.Value.ToString()); + // Get all the entity types information contained in the DbContext class, ... + var entityTypes = model.GetEntityTypes(); + foreach (var entityType in entityTypes) + { + var tableNameAnnotation = entityType.GetAnnotation("Relational:TableName"); + names.Add(tableNameAnnotation.Value.ToString()); + } } return names; @@ -109,8 +120,15 @@ namespace OpenAuth.App /// /// /// - private IList GetMySqlStructure(string tableName) + private IList GetMySqlStructure(string dbandTableName) { + if (!dbandTableName.Contains(".")) + { + throw new Exception("代码生成器使用mysql时,表名必需用【schemeName.tableName】形式"); + } + var splits = dbandTableName.Split("."); + var tableName = splits[1]; + var schemeName = splits[0]; var sql = $@"SELECT DISTINCT Column_Name AS ColumnName, '{ tableName}' as tableName, @@ -156,34 +174,22 @@ namespace OpenAuth.App FROM information_schema.COLUMNS WHERE - table_name = '{tableName}' {GetMysqlTableSchema()}"; - - var columns = _context.Query().FromSqlRaw(sql); - return columns.ToList(); - } - - /// - /// 获取mysql当前的数据库名称 - /// - /// - private string GetMysqlTableSchema() - { - try + table_name = '{tableName}' {schemeName}"; + + foreach (var context in _contexts) { - string dbName = _context.Database.GetDbConnection().ConnectionString.Split("Database=")[1].Split(";")[0]?.Trim(); - if (!string.IsNullOrEmpty(dbName)) + var columns = context.Query().FromSqlRaw(sql); + var columnList = columns?.ToList(); + if (columnList != null && columnList.Any()) { - dbName = $" and table_schema = '{dbName}' "; + return columnList; } - return dbName; - } - catch (Exception ex) - { - Console.WriteLine($"获取mysql数据库名异常:{ex.Message}"); - return ""; } + + return new List(); + } - + /// /// 获取SqlServer表结构信息 @@ -276,8 +282,16 @@ namespace OpenAuth.App WHERE obj.name = '{ tableName}') AS t ORDER BY t.colorder"; - var columns = _context.Query().FromSqlRaw(sql); - return columns.ToList(); + foreach (var context in _contexts) + { + var columns = context.Query().FromSqlRaw(sql); + var columnList = columns?.ToList(); + if (columnList != null && columnList.Any()) + { + return columnList; + } + } + return new List(); } } } \ No newline at end of file diff --git a/OpenAuth.App/FlowInstanceApp.cs b/OpenAuth.App/FlowInstanceApp.cs index 36bf4b98..182865b8 100644 --- a/OpenAuth.App/FlowInstanceApp.cs +++ b/OpenAuth.App/FlowInstanceApp.cs @@ -272,8 +272,8 @@ namespace OpenAuth.App FlowRuntime wfruntime = new FlowRuntime(flowInstance); - string resnode = ""; - resnode = string.IsNullOrEmpty(reqest.NodeRejectStep) ? wfruntime.RejectNode(reqest.NodeRejectType) : reqest.NodeRejectStep; + string rejectNode = ""; //驳回的节点 + rejectNode = string.IsNullOrEmpty(reqest.NodeRejectStep) ? wfruntime.RejectNode(reqest.NodeRejectType) : reqest.NodeRejectStep; var tag = new Tag { @@ -285,13 +285,13 @@ namespace OpenAuth.App wfruntime.MakeTagNode(wfruntime.currentNodeId, tag); flowInstance.IsFinish = 4;//4表示驳回(需要申请者重新提交表单) - if (resnode != "") + if (rejectNode != "") { flowInstance.PreviousId = flowInstance.ActivityId; - flowInstance.ActivityId = resnode; - flowInstance.ActivityType = wfruntime.GetNodeType(resnode); - flowInstance.ActivityName = wfruntime.Nodes[resnode].name; - flowInstance.MakerList = GetNodeMarkers(wfruntime.Nodes[resnode]);//当前节点可执行的人信息 + flowInstance.ActivityId = rejectNode; + flowInstance.ActivityType = wfruntime.GetNodeType(rejectNode); + flowInstance.ActivityName = wfruntime.Nodes[rejectNode].name; + flowInstance.MakerList = GetNodeMarkers(wfruntime.Nodes[rejectNode], flowInstance.CreateUserId); AddTransHistory(wfruntime); } @@ -416,11 +416,14 @@ namespace OpenAuth.App /// /// /// - private string GetNodeMarkers(FlowNode node) + private string GetNodeMarkers(FlowNode node,string flowinstanceCreateUserId="") { string makerList = ""; - - if (node.setInfo != null) + if (node.type == FlowNode.START && (!string.IsNullOrEmpty(flowinstanceCreateUserId))) //如果是开始节点,通常情况下是驳回到开始了 + { + makerList = flowinstanceCreateUserId; + } + else if (node.setInfo != null) { if (node.setInfo.NodeDesignate == Setinfo.ALL_USER)//所有成员 { diff --git a/OpenAuth.App/IntAutoGenApp.cs b/OpenAuth.App/IntAutoGenApp.cs new file mode 100644 index 00000000..7a21762b --- /dev/null +++ b/OpenAuth.App/IntAutoGenApp.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using OpenAuth.App.Interface; +using OpenAuth.App.Request; +using OpenAuth.Repository; +using OpenAuth.Repository.Domain; +using OpenAuth.Repository.Interface; + +namespace OpenAuth.App +{ + /// + /// 主键为numberic类型的业务,使用BaseLongApp基类 + /// + public class IntAutoGenApp :BaseIntAutoGenApp + { + public void Add(IntAutoGen application) + { + Repository.Add(application); + } + + public void Update(IntAutoGen application) + { + Repository.Update(application); + } + + + public async Task> GetList(QueryAppListReq request) + { + var applications = UnitWork.Find(null) ; + + return applications.ToList(); + } + + public IntAutoGenApp(IUnitWork unitWork, IRepository repository,IAuth auth) + : base(unitWork, repository, auth) + { + } + } +} \ No newline at end of file diff --git a/OpenAuth.App/Test/TestFlow.cs b/OpenAuth.App/Test/TestFlow.cs index 4c64affe..b1184153 100644 --- a/OpenAuth.App/Test/TestFlow.cs +++ b/OpenAuth.App/Test/TestFlow.cs @@ -1,4 +1,5 @@ -using System.Net.Http; +using System; +using System.Net.Http; using Infrastructure; using Infrastructure.Cache; using Microsoft.AspNetCore.Http; @@ -7,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using OpenAuth.App.Request; using OpenAuth.App.SSO; +using Yitter.IdGenerator; namespace OpenAuth.App.Test { @@ -19,7 +21,7 @@ namespace OpenAuth.App.Test var services = new ServiceCollection(); var cachemock = new Mock(); - cachemock.Setup(x => x.Get("tokentest")).Returns(new UserAuthSession { Account = "test3" }); + cachemock.Setup(x => x.Get("tokentest")).Returns(new UserAuthSession { Account = "admin" }); services.AddScoped(x => cachemock.Object); //模拟服务端httpContext @@ -33,6 +35,24 @@ namespace OpenAuth.App.Test return services; } + + [Test] + public void Create() + { + var options = new IdGeneratorOptions(){ WorkerId = 1}; + IIdGenerator idHelper = new YitIdGenerator(options); + var code = idHelper.NewLong().ToString(); + + var app = _autofacServiceProvider.GetService(); + app.CreateInstance(new AddFlowInstanceReq + { + SchemeId = "18a34903-175b-4cfb-9947-db67b538bbc8", + FrmType = 2, + FrmData = "{\"WorkDate\":\"2021-03-15\",\"Time\":\"8\",\"Reason\":\"dsdsds\"}", + CustomName = DateTime.Now.ToString(), + Code = code + }); + } [Test] @@ -41,8 +61,8 @@ namespace OpenAuth.App.Test var app = _autofacServiceProvider.GetService(); app.Verification(new VerificationReq { - FlowInstanceId = "76c72db4-d6c8-4734-856e-b6ffee08314a", - VerificationFinally = "1" + FlowInstanceId = "c2d6d4b9-527d-426e-98db-1d5dc905a994", + VerificationFinally = "3" }); } } diff --git a/OpenAuth.App/Test/TestIntAutoGenApp.cs b/OpenAuth.App/Test/TestIntAutoGenApp.cs new file mode 100644 index 00000000..353088d2 --- /dev/null +++ b/OpenAuth.App/Test/TestIntAutoGenApp.cs @@ -0,0 +1,31 @@ +using System; +using NUnit.Framework; +using Microsoft.Extensions.DependencyInjection; +using OpenAuth.Repository.Domain; + +namespace OpenAuth.App.Test +{ + public class TestIntAutoGenApp :TestBase + { + + [Test] + public void Add() + { + var app = _autofacServiceProvider.GetService(); + var intAutoGen = new IntAutoGen + { + Name = DateTime.Now.ToString() + }; + app.Add(intAutoGen); + Console.WriteLine($"new Id:{intAutoGen.Id}"); + } + + [Test] + public void Del() + { + var app = _autofacServiceProvider.GetService(); + app.Delete(new []{1, 2}); + } + + } +}