123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.EntityFrameworkCore.ChangeTracking;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- using System.IO;
- using System.Security.Claims;
- using Microsoft.AspNetCore.Components;
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
- using System.Data;
- using Microsoft.Extensions.Configuration;
- namespace YLShipBuildLandMap.Entity
- {
- public partial class YLShipBuildLandMapContext : DbContext
- {
- static DbContextOptions<YLShipBuildLandMapContext> dbContextOption = new DbContextOptions<YLShipBuildLandMapContext>();
- static DbContextOptionsBuilder<YLShipBuildLandMapContext> dbContextOptionBuilder = new DbContextOptionsBuilder<YLShipBuildLandMapContext>(dbContextOption);
- public static YLShipBuildLandMapContext New()
- {
- var configuration = new ConfigurationBuilder()
- .SetBasePath(Directory.GetCurrentDirectory())
- .AddJsonFile("appsettings.json")
- .Build();
- var connection = configuration.GetSection("ConnectionStrings").GetSection("DataContext").Get<string>();
- return new YLShipBuildLandMapContext(dbContextOptionBuilder.UseSqlServer(connection, builder =>
- {
- }).Options);
- }
- public static IServiceProvider Services { get; private set; }
- public static void SetServices(IServiceProvider services)
- {
- Services = services;
- }
- public override int SaveChanges(bool acceptAllChangesOnSuccess)
- {
- LoggingWrite();
- return base.SaveChanges(acceptAllChangesOnSuccess);
- }
- public override int SaveChanges()
- {
- return this.SaveChanges(true);
- }
- public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
- {
- LoggingWrite();
- var tn = base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
- return tn;
- }
- protected void LoggingWrite()
- {
- IHttpContextAccessor httpContextAccessor = Services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
- List<SysLog> logList = new List<SysLog>();
- if (httpContextAccessor != null)
- {
- var currHttpContext = httpContextAccessor.HttpContext;
- if (this.ChangeTracker.Entries() == null)
- {
- return;
- }
- this.ChangeTracker.Entries().ToList().ForEach(it =>
- {
- if (it.State == EntityState.Added || it.State == EntityState.Modified || it.State == EntityState.Deleted)
- {
- if (!(it.Entity is SysLog))
- {
- var log = LoggingChange(it, currHttpContext);
- if (log != null)
- {
- logList.Add(log);
- }
- }
- }
- });
- }
- this.SysLog.AddRange(logList);
- }
- protected SysLog LoggingChange(EntityEntry entity, HttpContext httpContext)
- {
- SysLog sysLog = new SysLog();
- sysLog.LogId = Guid.NewGuid();
- sysLog.LogTime = DateTime.Now;
- if (httpContext == null || httpContext.User == null)
- {
- string ip = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
- sysLog.LogType = 1;
- sysLog.ClientIp = ip;
- sysLog.LogDescribe = entity.State.ToString();
- sysLog.LogTitle = "";
- sysLog.LogUser = null;
- var entityType = entity.Entity.GetType();
- var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList();
- Dictionary<string, Object> colums = new Dictionary<string, object>();
- var mapping = this.Model.FindEntityType(entityType);
- list_methods.ForEach(it =>
- {
- colums.Add(it.Name, it.GetValue(entity.Entity));
- });
- sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums });
- return sysLog;
- }
- try
- {
- string ip = httpContext.Connection.RemoteIpAddress.ToString();
- var roleClaim = httpContext.User.FindFirst(ClaimTypes.Name);
- if (roleClaim != null)
- {
- sysLog.LogType = 1;
- sysLog.ClientIp = ip;
- sysLog.LogDescribe = entity.State.ToString();
- sysLog.LogTitle = httpContext.Request.Path.HasValue ? httpContext.Request.Path.Value : "";
- sysLog.LogUser = roleClaim.Value;
- var entityType = entity.Entity.GetType();
- var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList();
- Dictionary<string, Object> colums = new Dictionary<string, object>();
- var mapping = this.Model.FindEntityType(entityType);
- list_methods.ForEach(it =>
- {
- colums.Add(it.Name, it.GetValue(entity.Entity));
- });
- sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums });
- }
- }
- catch (Exception ex)
- {
- sysLog.LogText = ex.Message;
- }
- return sysLog;
- }
- public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
- {
- return this.SaveChangesAsync(true, cancellationToken);
- }
- [DbFunction(Name = "fn_GetPy", Schema = "dbo")]
- public static string GetPy(string str)
- {
- throw new NotImplementedException();
- }
- [DbFunction(Name = "fn_GetPyFirst", Schema = "dbo")]
- public static string GetPyFirst(string str)
- {
- throw new NotImplementedException();
- }
- [DbFunction(Name = "fn_ReplaceTime", Schema = "dbo")]
- public static DateTime ReplaceTime(DateTime? dateTimeBase, TimeSpan? timeSpan)
- {
- var dateBase = dateTimeBase.Value.Date;
- if (timeSpan.HasValue)
- {
- return dateBase.Add(timeSpan.Value);
- }
- else
- {
- return dateBase;
- }
- }
- }
- }
|