using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace WCS.Entity
{
    /// <summary>
    /// 实体类通用基础类
    /// </summary>
    [Serializable]
    public abstract class OBJ
    {
        /// <summary>
        /// ID
        /// </summary>
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnDescription = "ID")]
        [Column(Order = 0)]
        public virtual int ID { get; set; }

        /// <summary>
        /// 是否可用
        /// </summary>
        [SugarColumn(ColumnDescription = "是否可用")]
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 创建用户
        /// 仅记录用户ID
        /// </summary>
        [SugarColumn(ColumnDescription = "创建用户", Length = 50)]
        public string AddWho { get; set; }

        /// <summary>
        /// 创建时间
        /// 新增数据时自动获取服务器时间
        /// </summary>
        [SugarColumn(ColumnDescription = "创建时间", InsertServerTime = true)]
        public virtual DateTime AddTime { get; set; }

        /// <summary>
        /// 更新用户
        /// 仅记录用户ID
        /// </summary>
        [SugarColumn(ColumnDescription = "更新用户", Length = 50, IsNullable = true)]
        public string EditWho { get; set; }

        /// <summary>
        /// 更新时间
        /// 更新数据时自动获取服务器时间
        /// </summary>
        [SugarColumn(ColumnDescription = "更新时间", UpdateServerTime = true, IsNullable = true)]
        public DateTime EditTime { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        [SugarColumn(ColumnDescription = "版本号", ColumnDataType = "timestamp", IsNullable = true, IsOnlyIgnoreInsert = true, IsOnlyIgnoreUpdate = true)]
        public byte[] VER { get; set; }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Copy<T>() where T : OBJ
        {
            var instance = Activator.CreateInstance<T>();
            foreach (var property in GetType().GetProperties())
            {
                var obj = property.GetValue(this);
                typeof(T).GetProperty(property.Name)!.SetValue(instance, obj);
            }
            return instance;
        }
    }
}
