新增IGetDataTimeNow接口,返回当前系统时间

This commit is contained in:
Falcon 2024-08-21 15:36:07 +08:00
parent 0540eadce8
commit 121b2ca127
5 changed files with 77 additions and 2 deletions

View File

@ -0,0 +1,41 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Falcon.SugarApi.Test
{
[TestClass]
public class IGetDataTimeNowTest
{
[TestMethod]
public void nowTest() {
var year = DateTime.Now.Year;
Console.WriteLine($"base year {year}");
var ic1 = new c1();
Console.WriteLine($"c1 year {ic1.now.Year}");
var ic2 = new c2();
Console.WriteLine($"c2 year {ic2.now.Year}");
var ic3 = new c3();
Console.WriteLine($"c3 year {ic3.now.Year}");
Assert.IsTrue(ic1.now.Year == year);
Assert.IsTrue(ic3.now.Year == year);
Assert.IsTrue(ic2.now.Year == year + 1);
}
}
public class c1
{
public c1() {
this.now = this is IGetDataTimeNow gdn ? gdn.GetDataTimeNow() : DateTime.Now;
}
public DateTime now { get; set; }
}
public class c2:c1, IGetDataTimeNow
{
public DateTime GetDataTimeNow() {
return DateTime.Now.AddYears(1);
}
}
public class c3:c1 { }
}

View File

@ -9,6 +9,12 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary>
public abstract class SugarBasicTable:EntityGuidId
{
/// <summary>
/// 构造数据表基类
/// </summary>
public SugarBasicTable() {
this.CreateTime = this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now;
}
/// <summary>
/// 创建时间
/// </summary>
@ -24,7 +30,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <returns>本条数据</returns>
public virtual SugarBasicTable SetNew(DateTime? now = null) {
this.Id = Guid.NewGuid();
this.CreateTime = now ?? DateTime.Now;
this.CreateTime = now ?? (this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now);
return this;
}
}

View File

@ -9,6 +9,12 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary>
public abstract class SugarBasicTable2:EntityStringId
{
/// <summary>
/// 构造数据表基类
/// </summary>
public SugarBasicTable2() {
this.CreateTime = this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now;
}
/// <summary>
/// 创建时间
/// </summary>

View File

@ -9,6 +9,12 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary>
public abstract class SugarBasicTable3:EntityStringId
{
/// <summary>
/// 构造数据表基类
/// </summary>
public SugarBasicTable3() {
this.CreateTime = this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now;
}
/// <summary>
/// 记录状态
/// </summary>
@ -18,6 +24,6 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 创建时间
/// </summary>
[Required, SugarColumn(ColumnDescription = "创建时间")]
public DateTime CreateTime { get; set; } = DateTime.Now;
public DateTime CreateTime { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace Falcon.SugarApi
{
/// <summary>
/// 获取当前时间接口
/// </summary>
public interface IGetDataTimeNow
{
/// <summary>
/// 获取当前时间
/// </summary>
/// <returns></returns>
DateTime GetDataTimeNow();
}
}