From 03ae5d9a6570586f2f30362d7fa3da3bbf25c3b3 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期四, 10 十月 2024 16:48:32 +0800 Subject: [PATCH] 添加单点登录,修改界面单表修改数据-修改人修改时间-问题 --- WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs | 68 ++++++++++++++++++++++++++-------- 1 files changed, 52 insertions(+), 16 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs index 3ece807..bebf842 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs @@ -1,6 +1,7 @@ 锘縰sing Castle.Components.DictionaryAdapter.Xml; using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; +using SkiaSharp; using SqlSugar; using System; using System.Collections.Generic; @@ -40,7 +41,7 @@ Console.WriteLine("************ WIDESEA DataBase Set *****************"); - Console.WriteLine($"Master DB ConId: {DBContext.ConnId}"); + //Console.WriteLine($"Master DB ConId: {DBContext.ConnId}"); Console.WriteLine($"Master DB Type: {DBContext.DbType}"); //Console.WriteLine($"Master DB ConnectString: {DBContext.ConnectionString}"); @@ -52,7 +53,7 @@ if (DBContext.DbType != SqlSugar.DbType.Oracle) { dbContext.Db.DbMaintenance.CreateDatabase(); - ConsoleHelper.WriteSuccessLine($"Database created successfully!"); + ConsoleHelper.WriteSuccessLine($"Database Created Successfully!"); } else { @@ -81,27 +82,62 @@ IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance; if (!dbMaintenance.IsAnyTable(t.Name, false)) { - Console.WriteLine(t.Name); + ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] Created Successfully"); dbContext.Db.CodeFirst.InitTables(t); - //dbContext.DbClient.QueryableByObject(t).ToList() == null + string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); - #region Sys_User + #region AddSeedData if (seedData != "涓嶅瓨鍦ㄧ浉搴旂殑鐩綍") { - List<Dictionary<string, object>> dic = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); - //dbContext.GetEntityDB<BlogArticle>().InsertRange(JsonHelper.ParseFormByJson<List<BlogArticle>>()); - //Console.WriteLine("Table:BlogArticle created success!"); + List<Dictionary<string, object>> dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); - for (int i = 0; i < dic.Count; i++) + if (dicFile.Count > 0) { - if (dic[i].ContainsKey("CreateDate")) - dic[i]["CreateDate"] = DateTime.Now; - else - dic[i].Add("CreateDate", DateTime.Now); - } + List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>(); - dbContext.Db.Insertable(dic).AS(t.Name).ExecuteCommand(); + List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); + var a = t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)); + + List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => columnNames.Contains(x.Name)).ToList(); + for (int j = 0; j < dicFile.Count; j++) + { + Dictionary<string, object> keyValuePairs = new Dictionary<string, object>(); + for (int i = 0; i < propertyInfos.Count; i++) + { + PropertyInfo propertyInfo = propertyInfos[i]; + SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); + if (sugarColumn != null) + { + if (!sugarColumn.IsIgnore) + { + keyValuePairs.Add(propertyInfo.Name, dicFile[j][propertyInfo.Name]); + } + } + } + dic.Add(keyValuePairs); + } + + if (dic.Count > 0) + { + for (int i = 0; i < dic.Count; i++) + { + if (dic[i].ContainsKey("CreateDate")) + dic[i]["CreateDate"] = DateTime.Now; + else + dic[i].Add("CreateDate", DateTime.Now); + } + string str = $"SET IDENTITY_INSERT {t.Name} ON;"; + + str += dbContext.Db.Insertable(dic).AS(t.Name).ToSqlString(); + + str += ($"SET IDENTITY_INSERT {t.Name} OFF;"); + + dbContext.Db.Ado.ExecuteCommand(str); + + ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] SeedData Added Successfully"); + } + } } #endregion } @@ -133,7 +169,7 @@ } } }); - ConsoleHelper.WriteSuccessLine($"Tables created successfully!"); + ConsoleHelper.WriteSuccessLine($"Tables Created Successfully!"); Console.WriteLine(); -- Gitblit v1.9.3