using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using 全自动水压检测仪; using 全自动水压检测仪.DATA; using 呼吸过滤器气雾直径测试仪; namespace 全自动水压检测仪 { internal static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); PasswordExpirationService expirationService = new PasswordExpirationService(); bool isExpired = expirationService.IsPasswordExpired(DateTime.Parse("3000-2-6")); bool isExpiringSoon = expirationService.IsPasswordExpiringSoon(DateTime.Parse("3000-2-6")); //if (isExpired) //{ // MessageBox.Show("授权已过期,请联系经销商处理"); // Application.Run(new ChangPassword()); //} if (isExpiringSoon && !isExpired) { MessageBox.Show("授权即将过期,请联系经销商处理"); try { // 测试密码加密功能 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("程序启动 - 密码加密测试"); System.Diagnostics.Debug.WriteLine("========================================"); PasswordHelper.TestPasswordEncryption(); // 检查是否有重置参数 bool forceReset = args.Length > 0 && args[0].ToLower() == "--reset-users"; if (forceReset) { var result = MessageBox.Show( "确定要重置默认用户吗?\n\n这将删除并重新创建:\n• admin / admin123 (管理员)\n• csi / 123456 (普通用户)\n\n其他用户不受影响。", "重置默认用户", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { DatabaseInitializer.Initialize(); DatabaseInitializer.ForceResetDefaultUsers(); MessageBox.Show("默认用户已重置!\n\n管理员: admin / admin123\n普通用户: csi / 123456", "重置成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } // 初始化数据库表和默认管理员账户 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("初始化数据库"); System.Diagnostics.Debug.WriteLine("========================================"); DatabaseInitializer.Initialize(); // 检查并修复损坏的密码数据 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("检查密码数据完整性"); System.Diagnostics.Debug.WriteLine("========================================"); CheckAndFixPasswordData(); } catch (Exception ex) { MessageBox.Show($"数据库初始化失败:{ex.Message}\n\n请检查:\n1. MySQL服务是否启动\n2. 数据库连接配置是否正确\n3. 数据库 fullautowaterpressure 是否存在\n\n应用程序将退出。", "初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 显示登录窗体 using (var loginForm = new LoginForm()) { if (loginForm.ShowDialog() == DialogResult.OK) { // 登录成功,根据用户权限显示对应窗体 if (LoginData.IsAdmin()) { // 管理员登录,显示 ScanImport 页面 Application.Run(new ScanImport()); } else { // 普通用户登录,显示 NormalTemperatureMode 页面 Application.Run(new NormalTemperatureMode()); } } // 登录失败或取消,直接退出 } } if (!isExpired && !isExpiringSoon) { try { // 测试密码加密功能 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("程序启动 - 密码加密测试"); System.Diagnostics.Debug.WriteLine("========================================"); PasswordHelper.TestPasswordEncryption(); // 检查是否有重置参数 bool forceReset = args.Length > 0 && args[0].ToLower() == "--reset-users"; if (forceReset) { var result = MessageBox.Show( "确定要重置默认用户吗?\n\n这将删除并重新创建:\n• admin / admin123 (管理员)\n• csi / 123456 (普通用户)\n\n其他用户不受影响。", "重置默认用户", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { DatabaseInitializer.Initialize(); DatabaseInitializer.ForceResetDefaultUsers(); MessageBox.Show("默认用户已重置!\n\n管理员: admin / admin123\n普通用户: csi / 123456", "重置成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } return; } // 初始化数据库表和默认管理员账户 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("初始化数据库"); System.Diagnostics.Debug.WriteLine("========================================"); DatabaseInitializer.Initialize(); // 检查并修复损坏的密码数据 System.Diagnostics.Debug.WriteLine("\n========================================"); System.Diagnostics.Debug.WriteLine("检查密码数据完整性"); System.Diagnostics.Debug.WriteLine("========================================"); CheckAndFixPasswordData(); } catch (Exception ex) { MessageBox.Show($"数据库初始化失败:{ex.Message}\n\n请检查:\n1. MySQL服务是否启动\n2. 数据库连接配置是否正确\n3. 数据库 fullautowaterpressure 是否存在\n\n应用程序将退出。", "初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 显示登录窗体 using (var loginForm = new LoginForm()) { if (loginForm.ShowDialog() == DialogResult.OK) { // 登录成功,根据用户权限显示对应窗体 if (LoginData.IsAdmin()) { // 管理员登录,显示 ScanImport 页面 Application.Run(new ScanImport()); } else { // 普通用户登录,显示 NormalTemperatureMode 页面 Application.Run(new NormalTemperatureMode()); } } // 登录失败或取消,直接退出 } } } /// /// 检查并修复损坏的密码数据 /// private static void CheckAndFixPasswordData() { try { var userRepo = new UserRepository(); // 检查默认用户 var adminUser = userRepo.GetUserByUsername("admin"); var csiUser = userRepo.GetUserByUsername("csi"); bool needsReset = false; // 检查 admin 用户 if (adminUser != null) { string storedPassword = adminUser.PasswordHash ?? ""; System.Diagnostics.Debug.WriteLine($"\n检查 admin 用户:"); System.Diagnostics.Debug.WriteLine($" 当前密码值: '{storedPassword}'"); System.Diagnostics.Debug.WriteLine($" 密码长度: {storedPassword.Length}"); // 检查是否为空 if (string.IsNullOrEmpty(storedPassword)) { System.Diagnostics.Debug.WriteLine($" 状态: 密码为空"); needsReset = true; } // 检查是否是旧的加密格式(包含 Base64 特殊字符) else if (storedPassword.Length > 20 && (storedPassword.Contains("+") || storedPassword.Contains("/") || storedPassword.Contains("="))) { System.Diagnostics.Debug.WriteLine($" 状态: 旧的加密格式"); needsReset = true; } // 检查是否是正确的明文密码 else if (storedPassword != "admin123") { System.Diagnostics.Debug.WriteLine($" 状态: 密码不正确(应该是 'admin123')"); needsReset = true; } else { System.Diagnostics.Debug.WriteLine($" 状态: ✓ 正确"); } } else { System.Diagnostics.Debug.WriteLine($"\nadmin 用户不存在"); needsReset = true; } // 检查 cgx 用户 if (csiUser != null) { string storedPassword = csiUser.PasswordHash ?? ""; System.Diagnostics.Debug.WriteLine($"\n检查 csi 用户:"); System.Diagnostics.Debug.WriteLine($" 当前密码值: '{storedPassword}'"); System.Diagnostics.Debug.WriteLine($" 密码长度: {storedPassword.Length}"); if (string.IsNullOrEmpty(storedPassword)) { System.Diagnostics.Debug.WriteLine($" 状态: 密码为空"); needsReset = true; } else if (storedPassword.Length > 20 && (storedPassword.Contains("+") || storedPassword.Contains("/") || storedPassword.Contains("="))) { System.Diagnostics.Debug.WriteLine($" 状态: 旧的加密格式"); needsReset = true; } else if (storedPassword != "123") { System.Diagnostics.Debug.WriteLine($" 状态: 密码不正确(应该是 '123')"); needsReset = true; } else { System.Diagnostics.Debug.WriteLine($" 状态: ✓ 正确"); } } else { System.Diagnostics.Debug.WriteLine($"\ncsi 用户不存在"); needsReset = true; } if (needsReset) { System.Diagnostics.Debug.WriteLine("\n检测到密码数据异常,自动重置默认用户..."); DatabaseInitializer.ForceResetDefaultUsers(); System.Diagnostics.Debug.WriteLine("密码数据已修复!"); System.Diagnostics.Debug.WriteLine(" admin 密码: admin123"); System.Diagnostics.Debug.WriteLine(" csi 密码: 123456"); } else { System.Diagnostics.Debug.WriteLine("\n密码数据检查通过 ✓"); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"密码数据检查失败: {ex.Message}"); System.Diagnostics.Debug.WriteLine($"详细信息: {ex}"); // 不影响程序启动,继续运行 } } } }