YLShipOperationMISContext.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.EntityFrameworkCore.ChangeTracking;
  10. using Newtonsoft.Json.Linq;
  11. using Newtonsoft.Json;
  12. using System.IO;
  13. using System.Security.Claims;
  14. using Microsoft.AspNetCore.Components;
  15. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  16. using System.Data;
  17. using Microsoft.Extensions.Configuration;
  18. namespace YLShipBuildLandMap.Entity
  19. {
  20. public partial class YLShipBuildLandMapContext : DbContext
  21. {
  22. static DbContextOptions<YLShipBuildLandMapContext> dbContextOption = new DbContextOptions<YLShipBuildLandMapContext>();
  23. static DbContextOptionsBuilder<YLShipBuildLandMapContext> dbContextOptionBuilder = new DbContextOptionsBuilder<YLShipBuildLandMapContext>(dbContextOption);
  24. public static YLShipBuildLandMapContext New()
  25. {
  26. var configuration = new ConfigurationBuilder()
  27. .SetBasePath(Directory.GetCurrentDirectory())
  28. .AddJsonFile("appsettings.json")
  29. .Build();
  30. var connection = configuration.GetSection("ConnectionStrings").GetSection("DataContext").Get<string>();
  31. return new YLShipBuildLandMapContext(dbContextOptionBuilder.UseSqlServer(connection, builder =>
  32. {
  33. }).Options);
  34. }
  35. public static IServiceProvider Services { get; private set; }
  36. public static void SetServices(IServiceProvider services)
  37. {
  38. Services = services;
  39. }
  40. public override int SaveChanges(bool acceptAllChangesOnSuccess)
  41. {
  42. LoggingWrite();
  43. return base.SaveChanges(acceptAllChangesOnSuccess);
  44. }
  45. public override int SaveChanges()
  46. {
  47. return this.SaveChanges(true);
  48. }
  49. public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
  50. {
  51. LoggingWrite();
  52. var tn = base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
  53. return tn;
  54. }
  55. protected void LoggingWrite()
  56. {
  57. IHttpContextAccessor httpContextAccessor = Services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
  58. List<SysLog> logList = new List<SysLog>();
  59. if (httpContextAccessor != null)
  60. {
  61. var currHttpContext = httpContextAccessor.HttpContext;
  62. if (this.ChangeTracker.Entries() == null)
  63. {
  64. return;
  65. }
  66. this.ChangeTracker.Entries().ToList().ForEach(it =>
  67. {
  68. if (it.State == EntityState.Added || it.State == EntityState.Modified || it.State == EntityState.Deleted)
  69. {
  70. if (!(it.Entity is SysLog))
  71. {
  72. var log = LoggingChange(it, currHttpContext);
  73. if (log != null)
  74. {
  75. logList.Add(log);
  76. }
  77. }
  78. }
  79. });
  80. }
  81. this.SysLog.AddRange(logList);
  82. }
  83. protected SysLog LoggingChange(EntityEntry entity, HttpContext httpContext)
  84. {
  85. SysLog sysLog = new SysLog();
  86. sysLog.LogId = Guid.NewGuid();
  87. sysLog.LogTime = DateTime.Now;
  88. if (httpContext == null || httpContext.User == null)
  89. {
  90. string ip = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
  91. sysLog.LogType = 1;
  92. sysLog.ClientIp = ip;
  93. sysLog.LogDescribe = entity.State.ToString();
  94. sysLog.LogTitle = "";
  95. sysLog.LogUser = null;
  96. var entityType = entity.Entity.GetType();
  97. var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList();
  98. Dictionary<string, Object> colums = new Dictionary<string, object>();
  99. var mapping = this.Model.FindEntityType(entityType);
  100. list_methods.ForEach(it =>
  101. {
  102. colums.Add(it.Name, it.GetValue(entity.Entity));
  103. });
  104. sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums });
  105. return sysLog;
  106. }
  107. try
  108. {
  109. string ip = httpContext.Connection.RemoteIpAddress.ToString();
  110. var roleClaim = httpContext.User.FindFirst(ClaimTypes.Name);
  111. if (roleClaim != null)
  112. {
  113. sysLog.LogType = 1;
  114. sysLog.ClientIp = ip;
  115. sysLog.LogDescribe = entity.State.ToString();
  116. sysLog.LogTitle = httpContext.Request.Path.HasValue ? httpContext.Request.Path.Value : "";
  117. sysLog.LogUser = roleClaim.Value;
  118. var entityType = entity.Entity.GetType();
  119. var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList();
  120. Dictionary<string, Object> colums = new Dictionary<string, object>();
  121. var mapping = this.Model.FindEntityType(entityType);
  122. list_methods.ForEach(it =>
  123. {
  124. colums.Add(it.Name, it.GetValue(entity.Entity));
  125. });
  126. sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums });
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. sysLog.LogText = ex.Message;
  132. }
  133. return sysLog;
  134. }
  135. public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
  136. {
  137. return this.SaveChangesAsync(true, cancellationToken);
  138. }
  139. [DbFunction(Name = "fn_GetPy", Schema = "dbo")]
  140. public static string GetPy(string str)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. [DbFunction(Name = "fn_GetPyFirst", Schema = "dbo")]
  145. public static string GetPyFirst(string str)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. [DbFunction(Name = "fn_ReplaceTime", Schema = "dbo")]
  150. public static DateTime ReplaceTime(DateTime? dateTimeBase, TimeSpan? timeSpan)
  151. {
  152. var dateBase = dateTimeBase.Value.Date;
  153. if (timeSpan.HasValue)
  154. {
  155. return dateBase.Add(timeSpan.Value);
  156. }
  157. else
  158. {
  159. return dateBase;
  160. }
  161. }
  162. }
  163. }