using Mysoft.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mysoft.DataObject
{
    public abstract partial class MyObject
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public virtual string MetaTypeId { get; protected set; }

        public MetaType GetMetaType()
        {
            return MetaType.GetMetaType(this);
        }
    }

    public abstract partial class MyObject
    {
        public virtual int Insert()
        {
            this.Id = Guid.NewGuid().ToString();
            return DbQuery.Execute2NonQuery(SqlCreator.InsertSql(this.GetMetaType()), this);
        }

        public virtual int Update()
        {
            return DbQuery.Execute2NonQuery(SqlCreator.UpdateByIdSql(this.GetMetaType()), this);
        }

        public virtual int Delete()
        {
            return DbQuery.Execute2NonQuery(SqlCreator.DeleteByIdSql(this.GetMetaType()), new { Id = Id });
        }

        public virtual int Save()
        {
            if (string.IsNullOrEmpty(Id))
            {
                return Insert();
            }
            return Update();
        }
    }
}
