HRDataContext.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore;
  3. using MTWorkHR.Core.Entities;
  4. using MTWorkHR.Core.Entities.Base;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Infrastructure.Entities;
  7. using MTWorkHR.Infrastructure.Data;
  8. using System.Reflection.Emit;
  9. using MTWorkHR.Core.Entities.User;
  10. namespace MTWorkHR.Infrastructure.DBContext
  11. {
  12. public class HRDataContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
  13. {
  14. private readonly GlobalInfo _globalInfo;
  15. public HRDataContext(DbContextOptions<HRDataContext> options, GlobalInfo globalInfo) : base(options) {
  16. this._globalInfo = globalInfo;
  17. }
  18. public DbSet<AttachmentType> AttachmentTypes { get; set; }
  19. public DbSet<UserAttachment> UserAttachments { get; set; }
  20. public DbSet<Permission> Permissions { get; set; }
  21. public DbSet<RolePermission> RolePermissions { get; set; }
  22. //-------------------------------------
  23. public DbSet<Company> Companies { get; set; }
  24. public DbSet<Project> Projects { get; set; }
  25. // public DbSet<AttachmentType> AttachmentTypes { get; set; }
  26. public DbSet<UserTaskStatus> UserTaskStatuses { get; set; }
  27. public DbSet<UserTask> UserTasks { get; set; }
  28. public DbSet<UserTaskAttachment> UserTaskAttachments { get; set; }
  29. public DbSet<UserTaskHistory> UserTaskHistories { get; set; }
  30. public DbSet<Team> Teams { get; set; }
  31. public DbSet<Meeting> Meetings { get; set; }
  32. public DbSet<Attendance> Attendances { get; set; }
  33. public DbSet<OrderType> OrderTypes { get; set; }
  34. public DbSet<LeaveType> LeaveTypes { get; set; }
  35. public DbSet<OrderAllocation> OrderAllocations { get; set; }
  36. public DbSet<OrderRequest> OrderRequests { get; set; }
  37. public DbSet<ChatMessage> ChatMessages { get; set; }
  38. public DbSet<HubConnection> HubConnections { get; set; }
  39. //-------------------Lookups---------------------------
  40. public DbSet<CountryLookup> CountryLookups { get; set; }
  41. public DbSet<Qualification> Qualifications { get; set; }
  42. public DbSet<University> Universities { get; set; }
  43. public DbSet<Industry> Industries { get; set; }
  44. public DbSet<JobTitle> JobTitles { get; set; }
  45. public DbSet<LoginOTP> LoginOTPs { get; set; }
  46. public DbSet<City> Cities { get; set; }
  47. //-----------
  48. public DbSet<Contract> Contracts { get; set; }
  49. public DbSet<Nationality> Nationalities{ get; set; }
  50. public DbSet<SubscriptionConfiguration> SubscriptionConfiguration { get; set; }
  51. //------------------------Logs------------------------
  52. public DbSet<UserLog> UserLogs { get; set; }
  53. public DbSet<AuthLog> AuthLogs { get; set; }
  54. public DbSet<FileLog> FileLogs { get; set; }
  55. public DbSet<RoleLog> RoleLogs { get; set; }
  56. public DbSet<SettingLog> SettingLogs { get; set; }
  57. public DbSet<UserTaskLog> UserTaskLogs { get; set; }
  58. public DbSet<TeamLog> TeamLogs { get; set; }
  59. public DbSet<MeetingLog> MeetingLogs { get; set; }
  60. public DbSet<AttendanceLog> AttendanceLogs { get; set; }
  61. //----------------------------------------
  62. protected override void OnModelCreating(ModelBuilder builder)
  63. {
  64. base.OnModelCreating(builder);
  65. SetGlobalFilters(builder);
  66. builder.ApplyConfigurationsFromAssembly(typeof(HRDataContext).Assembly);
  67. CreateGlobalIndexes(builder);
  68. }
  69. private void SetGlobalFilters(ModelBuilder builder)
  70. {
  71. builder.SetQueryFilterOnAllEntities<ISoftDelete>(p => !p.IsDeleted);
  72. builder.SetQueryFilterOnAllEntities<IHaveCompany>(b => b.CompanyId == _globalInfo.CompanyId);
  73. }
  74. #region SaveChanges
  75. public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
  76. {
  77. BeforeSaveProccess();
  78. return base.SaveChangesAsync(cancellationToken);
  79. }
  80. public override int SaveChanges()
  81. {
  82. BeforeSaveProccess();
  83. return base.SaveChanges();
  84. }
  85. public override int SaveChanges(bool acceptAllChangesOnSuccess)
  86. {
  87. BeforeSaveProccess();
  88. return base.SaveChanges(acceptAllChangesOnSuccess);
  89. }
  90. private void BeforeSaveProccess()
  91. {
  92. var changes = from e in this.ChangeTracker.Entries()
  93. where e.State != EntityState.Unchanged
  94. select e;
  95. foreach (var change in changes)
  96. {
  97. if (change.State == EntityState.Added)
  98. {
  99. if (change.Entity is IAudit)
  100. {
  101. ((IAudit)change.Entity).CreateUser = _globalInfo.UserId;
  102. ((IAudit)change.Entity).CreateDate = DateTime.Now;
  103. }
  104. if ( _globalInfo.CompanyId > 0 && change.Entity is IHaveCompany)
  105. {
  106. ((IHaveCompany)change.Entity).CompanyId = (long)_globalInfo.CompanyId;
  107. }
  108. }
  109. else if (change.State == EntityState.Modified)
  110. {
  111. if (_globalInfo.CompanyId > 0 && change.Entity is IHaveCompany)
  112. {
  113. var originalValues = this.Entry(change.Entity).OriginalValues;
  114. var original = (long)originalValues["CompanyId"];
  115. if (original != (long)_globalInfo.CompanyId)
  116. throw new AppException(ExceptionEnum.NotAuthorized);
  117. }
  118. if (change.Entity is IAudit
  119. && ((change.Entity is IFullAudit && !((IFullAudit)change.Entity).IsDeleted) || change.Entity is not IFullAudit))
  120. {
  121. ((IAudit)change.Entity).UpdateUser = _globalInfo.UserId;
  122. ((IAudit)change.Entity).UpdateDate = DateTime.Now;
  123. }
  124. if (change.Entity is IFullAudit && ((IFullAudit)change.Entity).IsDeleted)
  125. {
  126. ((IFullAudit)change.Entity).DeleteUserId = _globalInfo.UserId;
  127. }
  128. }
  129. else if (change.State == EntityState.Deleted)
  130. {
  131. }
  132. }
  133. }
  134. private void CreateGlobalIndexes(ModelBuilder builder)
  135. {
  136. builder.HasIndexOnAllEntities<IHaveCompany>(p => p.CompanyId);
  137. }
  138. #endregion
  139. }
  140. }