using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using IRaCIS.Core.Domain.Models;

namespace IRaCIS.Core.Infra.EFCore
{
    #region AuditContext
    public class AuditContext : DbContext
    {
        //传递委托进来写日志

        private readonly string _connectionString;

        public AuditContext(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseSqlServer(_connectionString);

        public DbSet<SaveChangesAudit> SaveChangesAudits { get; set; }
    }

    public class SaveChangesAudit
    {
        public Guid Id { get; set; }
        //public Guid AuditId { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public bool Succeeded { get; set; } = false;
        public string ErrorMessage { get; set; } = string.Empty;

        public ICollection<EntityAudit> Entities { get; } = new List<EntityAudit>();
    }

    public class EntityAudit: IAuditAddWithUserName
    {
        public Guid Id { get; set; }
        public Guid AlterId { get; set; }=Guid.Empty;
        public Guid SaveChangesAuditId { get; set; }
        public EntityState State { get; set; }
        public string AuditMessage { get; set; }




        public SaveChangesAudit SaveChangesAudit { get; set; }

        public string CreateUser { get; set; } = string.Empty;
        public Guid CreateUserId { get; set; }=Guid.Empty;
        public DateTime CreateTime { get; set; }=DateTime.Now;
    }
    #endregion
}
