DatabaseInitializer.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TeamAAS_VP.Interfaces;
  8. using TeamAAS_VP.Models;
  9. namespace TeamAAS_VP.Data
  10. {
  11. // 数据库初始化实现
  12. public class DatabaseInitializer : IDatabaseInitializer
  13. {
  14. private readonly ISqlSugarClient _db;
  15. public DatabaseInitializer(ISqlSugarClient db)
  16. {
  17. _db = db;
  18. }
  19. public void InitializeDatabase()
  20. {
  21. try
  22. {
  23. // 创建数据库(如果不存在)
  24. _db.DbMaintenance.CreateDatabase();
  25. // 创建表
  26. _db.CodeFirst.InitTables(typeof(User), typeof(ProductionRecord), typeof(UserLoginRecord), typeof(AlarmRecord), typeof(LockResult), typeof(ProductWithMes));
  27. // 创建索引
  28. CreateIndexes();
  29. // 如果用户表为空,插入三个基础用户
  30. var userCount = _db.Queryable<User>().Count();
  31. if (userCount == 0)
  32. {
  33. var op = new User { UserName = "操作员", UserPassword = "", userPart = Enums.UserPart.Operator, CreateTime = DateTime.Now, IsRemember = false };
  34. var eng = new User { UserName = "工程师", UserPassword = "10086", userPart = Enums.UserPart.Engineer, CreateTime = DateTime.Now, IsRemember = false };
  35. var admin = new User { UserName = "管理员", UserPassword = "team123456", userPart = Enums.UserPart.Administrator, CreateTime = DateTime.Now, IsRemember = false };
  36. _db.Insertable(new List<User> { op, eng, admin }).ExecuteCommand();
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. // 记录日志
  42. throw new Exception($"数据库初始化失败: {ex.Message}", ex);
  43. }
  44. }
  45. public bool DatabaseExists()
  46. {
  47. return _db.DbMaintenance.GetDataBaseList().Contains("AlignerDB");
  48. }
  49. private void CreateIndexes()
  50. {
  51. try
  52. {
  53. // Ensure LockResult indexes for common queries
  54. // 单列索引:Timestamp, ProductNumber, LockPassed
  55. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "Timestamp" }, "IX_LockResult_Timestamp", false); } catch { }
  56. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "ProductNumber" }, "IX_LockResult_ProductNumber", false); } catch { }
  57. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "LockPassed" }, "IX_LockResult_LockPassed", false); } catch { }
  58. // 新增索引:Number, FeederNumber, NozzleNumber
  59. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "Number" }, "IX_LockResult_Number", false); } catch { }
  60. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "FeederNumber" }, "IX_LockResult_FeederNumber", false); } catch { }
  61. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "NozzleNumber" }, "IX_LockResult_NozzleNumber", false); } catch { }
  62. // 复合索引:ProductNumber + Timestamp(支持按产品+时间范围查询)
  63. try { _db.DbMaintenance.CreateIndex("LockResult", new string[] { "ProductNumber", "Timestamp" }, "IX_LockResult_ProductNumber_Timestamp", false); } catch { }
  64. // 你可以在这里为其他表添加索引,例如 ProductionRecord 的 Timestamp 或 ProductName
  65. try { _db.DbMaintenance.CreateIndex("ProductionRecord", new string[] { "ProductName", "Timestamp" }, "IX_ProductionRecord_ProductName_Timestamp", false); } catch { }
  66. }
  67. catch
  68. {
  69. // 忽略索引创建中的错误(如已存在),调用方仍然可以继续
  70. }
  71. }
  72. }
  73. }