From e98b07c84a2a496da895ef6b523b29ccc75e004d Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期五, 23 八月 2024 11:27:01 +0800 Subject: [PATCH] 更新QuartzJob种子数据插入 --- WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Seed/QuartzJobCreateDataTabel.cs | 70 +++++++++++++++++++++++++--------- 1 files changed, 51 insertions(+), 19 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Seed/QuartzJobCreateDataTabel.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Seed/QuartzJobCreateDataTabel.cs index c59941a..20b2651 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Seed/QuartzJobCreateDataTabel.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Seed/QuartzJobCreateDataTabel.cs @@ -15,6 +15,7 @@ *----------------------------------------------------------------*/ #endregion << 鐗� 鏈� 娉� 閲� >> +using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; @@ -30,12 +31,14 @@ { public class QuartzJobCreateDataTabel { + private static string SeedDataFolder = "WIDESEAWCS_DB.DBSeed.Json/{0}.tsv"; + /// <summary> /// 鏄犲皠QuartzJob鏁版嵁搴撹〃 /// </summary> /// <param name="dbContext"></param> /// <returns></returns> - public static async Task SeedAsync(DBContext dbContext) + public static async Task SeedAsync(DBContext dbContext, string WebRootPath) { try { @@ -49,7 +52,7 @@ .Select(type => type.AsType()) .Where(x => x.IsClass && x.Namespace is "WIDESEAWCS_QuartzJob.Models" && x.GetCustomAttribute<SugarTable>() != null) .ToList(); - + SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder); modelTypes.ForEach(t => { //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString(); @@ -60,33 +63,62 @@ { Console.WriteLine(t.Name); dbContext.Db.CodeFirst.InitTables(t); - } - else - { - List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); - if (t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)) != null) + + string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); + + #region AddSeedData + if (seedData != "涓嶅瓨鍦ㄧ浉搴旂殑鐩綍") { - bool isChange = true; - List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => !columnNames.Contains(x.Name)).ToList(); - for (int i = 0; i < propertyInfos.Count; i++) + List<Dictionary<string, object>> dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); + + if (dicFile.Count > 0) { - PropertyInfo propertyInfo = propertyInfos[i]; - SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); - if (sugarColumn != null) + List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>(); + + 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++) { - if (!sugarColumn.IsIgnore) + Dictionary<string, object> keyValuePairs = new Dictionary<string, object>(); + for (int i = 0; i < propertyInfos.Count; i++) { - if (!sugarColumn.IsNullable) + PropertyInfo propertyInfo = propertyInfos[i]; + SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); + if (sugarColumn != null) { - isChange = false; - break; + 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"); } } - if (isChange) - dbContext.Db.CodeFirst.InitTables(t); } + #endregion } }); ConsoleHelper.WriteSuccessLine($"QuartzJob Tables Created Successfully!"); -- Gitblit v1.9.3