using System.Text; using System.Text.Json; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using EntityCreationTool; Console.OutputEncoding = Encoding.UTF8; Banner(); // ───────────────────────────────────────────────────────────────────────────── // Load Configuration // ───────────────────────────────────────────────────────────────────────────── var config = new ConfigurationBuilder() .SetBasePath(AppContext.BaseDirectory) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); int assemblyId = int.Parse(config["AssemblyId"] ?? "-1"); int userId = int.Parse(config["UserId"] ?? "-1"); string connectionString = config["Gb5SystemDTO:Gb5System"] ?? throw new Exception("Gb5SystemDTO:Gb5System not found in appsettings.json."); var builder = new SqlConnectionStringBuilder(connectionString); // ───────────────────────────────────────────────────────────────────────────── // Load Batch // ───────────────────────────────────────────────────────────────────────────── string batchPath = Path.Combine(AppContext.BaseDirectory, "batch.json"); if (!File.Exists(batchPath)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" batch.json not found."); Console.ResetColor(); return; } var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var entries = JsonSerializer.Deserialize>( File.ReadAllText(batchPath), jsonOptions) ?? new List(); if (entries.Count == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(" batch.json is empty — nothing to process."); Console.ResetColor(); return; } // ───────────────────────────────────────────────────────────────────────────── // Pre-run Summary // ───────────────────────────────────────────────────────────────────────────── var ec = config.GetSection("EntityCreation"); string dto = ec["EntitySuffixes:DTO"] ?? "DTO"; string pick = ec["EntitySuffixes:Picklist"] ?? "PicklistDTO"; Console.WriteLine($" Server : {builder.DataSource}"); Console.WriteLine($" Database : {builder.InitialCatalog}"); Console.WriteLine($" AssemblyId : {assemblyId}"); Console.WriteLine($" UserId : {userId}"); Console.WriteLine($" Tables : {entries.Count}"); Console.WriteLine(); Console.WriteLine(" Each table will create (or skip if already exists):"); Console.WriteLine(); int pad = entries.Max(x => x.TableName.Length) + 2; foreach (var entry in entries) { string t = entry.TableName; Console.WriteLine($" Table : {t.PadRight(pad)} ModuleId={entry.ModuleId}"); Console.WriteLine($" DBOBJECT + DBOBJECTFIELDS (from INFORMATION_SCHEMA.COLUMNS)"); Console.WriteLine($" MENTITY Poco {t}"); Console.WriteLine($" MENTITY DTO {t}{dto}"); Console.WriteLine($" MENTITY Picklist {t}{pick}"); Console.WriteLine($" Poco.POCOTODTOLINKID + Poco.POCOTOPICKLISTLINKID linked"); Console.WriteLine(); } Console.WriteLine(); Console.Write($" Press Enter to process all {entries.Count} table(s), or Ctrl+C to cancel ... "); Console.ReadLine(); // ───────────────────────────────────────────────────────────────────────────── // Execute // ───────────────────────────────────────────────────────────────────────────── var stopwatch = System.Diagnostics.Stopwatch.StartNew(); var engine = new EntityCreationEngine(connectionString, config); var results = await engine.CreateBatchAsync(entries, assemblyId, userId); stopwatch.Stop(); // ───────────────────────────────────────────────────────────────────────────── // Report // ───────────────────────────────────────────────────────────────────────────── int created = results.Count(x => x.Success && x.Message == EntityCreationEngine.MSG_CREATED); int skipped = results.Count(x => x.Success && x.Message == EntityCreationEngine.MSG_SKIPPED); int failed = results.Count(x => !x.Success); Console.WriteLine(); Console.WriteLine("══════════════════════════════════════════════════════════════════"); Console.WriteLine(" BATCH COMPLETE"); Console.WriteLine("══════════════════════════════════════════════════════════════════"); WriteStat("Total", entries.Count, ConsoleColor.White); WriteStat("Created", created, ConsoleColor.Green); WriteStat("Skipped", skipped, ConsoleColor.Yellow); WriteStat("Failed", failed, failed > 0 ? ConsoleColor.Red : ConsoleColor.DarkGray); Console.WriteLine($" Elapsed : {stopwatch.Elapsed:mm\\:ss\\.fff}"); Console.WriteLine("══════════════════════════════════════════════════════════════════"); if (failed > 0) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAILED TABLES"); Console.WriteLine("─────────────"); foreach (var r in results.Where(x => !x.Success)) Console.WriteLine($" {r.TableName,-30} {r.Message}"); Console.ResetColor(); } // ───────────────────────────────────────────────────────────────────────────── static void WriteStat(string label, int value, ConsoleColor color) { Console.Write($" {label,-10}: "); Console.ForegroundColor = color; Console.WriteLine(value); Console.ResetColor(); } static void Banner() { Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine("╔════════════════════════════════════════════════════════════════════╗"); Console.WriteLine("║ GB5 ENTITY CREATION TOOL v2.0 (Batch Mode) ║"); Console.WriteLine("║ DBOBJECT → DBOBJECTFIELDS → DTO / Poco / Picklist (GB5) ║"); Console.WriteLine("╚════════════════════════════════════════════════════════════════════╝"); Console.ResetColor(); Console.WriteLine(); }