Falcon.SugarApi/Falcon.SugarApi/DatabaseDefinitions/SugarBasicTable.cs

38 lines
1.2 KiB
C#

using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
namespace Falcon.SugarApi.DatabaseDefinitions
{
/// <summary>
/// 基础表.只定义表基础主键和创建时间
/// </summary>
public abstract class SugarBasicTable:EntityGuidId
{
/// <summary>
/// 构造数据表基类
/// </summary>
public SugarBasicTable() {
this.CreateTime = this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now;
}
/// <summary>
/// 创建时间
/// </summary>
[Required]
[SugarColumn(IsOnlyIgnoreUpdate = true,ColumnDescription = "创建时间")]
public DateTime CreateTime { get; set; } = DateTime.Now;
/// <summary>
/// 将数据设置为新数据。
/// <para>更新Id、创建时间</para>
/// </summary>
/// <param name="now">当前时间,如果采用非本机时间可以提供</param>
/// <returns>本条数据</returns>
public virtual SugarBasicTable SetNew(DateTime? now = null) {
this.Id = Guid.NewGuid();
this.CreateTime = now ?? (this is IGetDataTimeNow gtn ? gtn.GetDataTimeNow() : DateTime.Now);
return this;
}
}
}