using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using MTWorkHR.Core.Entities; using MTWorkHR.Core.Entities.Base; using MTWorkHR.Core.Global; using MTWorkHR.Infrastructure.Entities; using MTWorkHR.Infrastructure.Data; using System.Reflection.Emit; using MTWorkHR.Core.Entities.User; namespace MTWorkHR.Infrastructure.DBContext { public class HRDataContext : IdentityDbContext { private readonly GlobalInfo _globalInfo; public HRDataContext(DbContextOptions options, GlobalInfo globalInfo) : base(options) { this._globalInfo = globalInfo; } public DbSet AttachmentTypes { get; set; } public DbSet UserAttachments { get; set; } public DbSet Permissions { get; set; } public DbSet RolePermissions { get; set; } //------------------------------------- public DbSet Companies { get; set; } public DbSet Projects { get; set; } // public DbSet AttachmentTypes { get; set; } public DbSet UserTaskStatuses { get; set; } public DbSet UserTasks { get; set; } public DbSet UserTaskAttachments { get; set; } public DbSet UserTaskHistories { get; set; } public DbSet Teams { get; set; } public DbSet Meetings { get; set; } public DbSet Attendances { get; set; } public DbSet OrderTypes { get; set; } public DbSet LeaveTypes { get; set; } public DbSet OrderAllocations { get; set; } public DbSet OrderRequests { get; set; } public DbSet ChatMessages { get; set; } public DbSet HubConnections { get; set; } //-------------------Lookups--------------------------- public DbSet CountryLookups { get; set; } public DbSet Qualifications { get; set; } public DbSet Universities { get; set; } public DbSet Industries { get; set; } public DbSet JobTitles { get; set; } public DbSet LoginOTPs { get; set; } public DbSet Cities { get; set; } //----------- public DbSet Contracts { get; set; } //------------------------Logs------------------------ public DbSet UserLogs { get; set; } public DbSet AuthLogs { get; set; } public DbSet FileLogs { get; set; } public DbSet RoleLogs { get; set; } public DbSet SettingLogs { get; set; } public DbSet UserTaskLogs { get; set; } public DbSet TeamLogs { get; set; } public DbSet MeetingLogs { get; set; } public DbSet AttendanceLogs { get; set; } //---------------------------------------- protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); SetGlobalFilters(builder); builder.ApplyConfigurationsFromAssembly(typeof(HRDataContext).Assembly); CreateGlobalIndexes(builder); } private void SetGlobalFilters(ModelBuilder builder) { builder.SetQueryFilterOnAllEntities(p => !p.IsDeleted); builder.SetQueryFilterOnAllEntities(b => b.CompanyId == _globalInfo.CompanyId); } #region SaveChanges public override Task SaveChangesAsync(CancellationToken cancellationToken = default) { BeforeSaveProccess(); return base.SaveChangesAsync(cancellationToken); } public override int SaveChanges() { BeforeSaveProccess(); return base.SaveChanges(); } public override int SaveChanges(bool acceptAllChangesOnSuccess) { BeforeSaveProccess(); return base.SaveChanges(acceptAllChangesOnSuccess); } private void BeforeSaveProccess() { var changes = from e in this.ChangeTracker.Entries() where e.State != EntityState.Unchanged select e; foreach (var change in changes) { if (change.State == EntityState.Added) { if (change.Entity is IAudit) { ((IAudit)change.Entity).CreateUser = _globalInfo.UserId; ((IAudit)change.Entity).CreateDate = DateTime.Now; } if ( _globalInfo.CompanyId > 0 && change.Entity is IHaveCompany) { ((IHaveCompany)change.Entity).CompanyId = (long)_globalInfo.CompanyId; } } else if (change.State == EntityState.Modified) { if (_globalInfo.CompanyId > 0 && change.Entity is IHaveCompany) { var originalValues = this.Entry(change.Entity).OriginalValues; var original = (long)originalValues["CompanyId"]; if (original != (long)_globalInfo.CompanyId) throw new AppException(ExceptionEnum.NotAuthorized); } if (change.Entity is IAudit && ((change.Entity is IFullAudit && !((IFullAudit)change.Entity).IsDeleted) || change.Entity is not IFullAudit)) { ((IAudit)change.Entity).UpdateUser = _globalInfo.UserId; ((IAudit)change.Entity).UpdateDate = DateTime.Now; } if (change.Entity is IFullAudit && ((IFullAudit)change.Entity).IsDeleted) { ((IFullAudit)change.Entity).DeleteUserId = _globalInfo.UserId; } } else if (change.State == EntityState.Deleted) { } } } private void CreateGlobalIndexes(ModelBuilder builder) { builder.HasIndexOnAllEntities(p => p.CompanyId); } #endregion } }