diff --git a/Helpers/SimApiUtil.cs b/Helpers/SimApiUtil.cs index b766d15..49d0dad 100644 --- a/Helpers/SimApiUtil.cs +++ b/Helpers/SimApiUtil.cs @@ -10,6 +10,11 @@ namespace SimApi.Helpers { public static class SimApiUtil { + public static DateTime CstNow + { + get => DateTime.UtcNow.AddHours(8); + } + /// /// 检测手机号是否正确 /// @@ -60,7 +65,7 @@ namespace SimApi.Helpers /// public static DateTime GetCstNow() { - return DateTime.UtcNow.AddHours(8); + return CstNow; } } } \ No newline at end of file diff --git a/Logger/SimApiLogger.cs b/Logger/SimApiLogger.cs index ba4f6b3..86b6787 100644 --- a/Logger/SimApiLogger.cs +++ b/Logger/SimApiLogger.cs @@ -1,5 +1,6 @@ using System; using Microsoft.Extensions.Logging; +using SimApi.Helpers; namespace SimApi.Logger { @@ -30,7 +31,7 @@ namespace SimApi.Logger _ => defautColor }; Console.WriteLine( - $"[ {Name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]"); + $"[ {Name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]"); Console.ForegroundColor = defautColor; Console.WriteLine($"{state}"); if (exception != null) diff --git a/Models/SimApiBaseModel.cs b/Models/SimApiBaseModel.cs index d3a077f..38fb09c 100644 --- a/Models/SimApiBaseModel.cs +++ b/Models/SimApiBaseModel.cs @@ -1,23 +1,36 @@ using System; using System.Linq; using System.Text.Json; +using SimApi.Helpers; namespace SimApi.Models { public class SimApiBaseModel { - protected virtual string[] MapperIgnoreField { get; set; } = {"Id", "CreatedAt", "UpdatedAt"}; + protected virtual string[] MapperIgnoreField { get; set; } = + { + "Id", "CreatedAt", "UpdatedAt" + }; + protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt"; public void MapData(TS source, bool mapAll = false) { //获取要赋值的源数据不为null的项目 var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null) - .Select(x => new {x.Name, x.PropertyType}) + .Select(x => new + { + x.Name, + x.PropertyType + }) .ToDictionary(x => x.Name, x => x.PropertyType); //获取目标对象的属性信息 - var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType}) + var targetProps = GetType().GetProperties().Select(x => new + { + x.Name, + x.PropertyType + }) .ToDictionary(x => x.Name, x => x.PropertyType); foreach (var sp in sourceProps) { @@ -37,11 +50,19 @@ namespace SimApi.Models { //获取要赋值的源数据不为null的项目 var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null) - .Select(x => new {x.Name, x.PropertyType}) + .Select(x => new + { + x.Name, + x.PropertyType + }) .ToDictionary(x => x.Name, x => x.PropertyType); //获取目标对象的属性信息 - var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType}) + var targetProps = GetType().GetProperties().Select(x => new + { + x.Name, + x.PropertyType + }) .ToDictionary(x => x.Name, x => x.PropertyType); foreach (var sp in sourceProps) { @@ -63,7 +84,7 @@ namespace SimApi.Models /// public void UpdateTime() { - GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now); + GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow); } } } \ No newline at end of file