using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RDH.Data
{
    public abstract  class BaseBLL
    {
        public BaseBLL() { InitMaps(); }

        internal Dictionary<string, string> ColumnPropMaps = new Dictionary<string, string>();
        internal abstract String TABLE_NAME {get;}//TB_NAME
        internal abstract string ENTITY_NAME { get; }
        internal abstract String KEY_COLUMN { get; }//KEY_COLUMN
        internal virtual string SNAPSHOT_BASE { get { return string.Empty; } }
        /// <summary>
        /// 获取或设置表的别名
        /// </summary>
        internal virtual String AliasTableName 
        {
            get;
            set;
        }
        internal abstract void InitMaps();

      

        protected virtual string BuildBaseSql() 
        {

            StringBuilder selectValues = new StringBuilder();
          
            foreach (var c in ColumnPropMaps)
            {
                selectValues.AppendFormat("\"{0}\" \"{1}\",", c.Key, c.Value);
            }

            selectValues.Remove(selectValues.Length - 1, 1);
            return string.Format("SELECT {1} FROM \"{0}\" ", TABLE_NAME, selectValues);
        }

        protected virtual string BuildGetSql()
        {

            StringBuilder selectValues = new StringBuilder();
            string keyWhere = string.Format("\"{0}\" = :{1}", KEY_COLUMN, ColumnPropMaps[KEY_COLUMN]);
            foreach (var c in ColumnPropMaps)
            {
                selectValues.AppendFormat("\"{0}\" \"{1}\",", c.Key, c.Value);
            }

            selectValues.Remove(selectValues.Length - 1, 1);
            return string.Format("SELECT {1} FROM \"{0}\" WHERE {2}", TABLE_NAME, selectValues, keyWhere);

        }

        protected virtual string BuildInsertSql()
        {

            StringBuilder cols = new StringBuilder();
            StringBuilder parms = new StringBuilder();
            foreach (var c in ColumnPropMaps)
            {
                cols.AppendFormat("\"{0}\",", c.Key);
                parms.AppendFormat(":{0},", c.Value);
            }

            cols.Remove(cols.Length - 1, 1);
            parms.Remove(parms.Length - 1, 1);
            return string.Format("INSERT INTO \"{0}\" ({1}) VALUES ({2})", TABLE_NAME, cols, parms);
        }

        protected virtual string BuildUpdateSql()
        {
            StringBuilder setters = new StringBuilder();
            string keyWhere = string.Format("\"{0}\" = :{1}", KEY_COLUMN, ColumnPropMaps[KEY_COLUMN]);
            foreach (var c in ColumnPropMaps)
            {
                if (c.Key != KEY_COLUMN)
                    setters.AppendFormat("\"{0}\" = :{1},", c.Key, c.Value);
            }
            setters.Remove(setters.Length - 1, 1);

            return string.Format("UPDATE \"{0}\" SET {1} WHERE {2}", TABLE_NAME, setters, keyWhere);
        }

        protected virtual string BuildDeleteSql()
        {
            string keyWhere = string.Format("\"{0}\" = :{1}", KEY_COLUMN, ColumnPropMaps[KEY_COLUMN]);
            return string.Format("DELETE FROM \"{0}\" WHERE {1}", TABLE_NAME, keyWhere);
        }


        protected virtual string BuildGetCurrentSql()
        {

            StringBuilder selectValues = new StringBuilder();
            string keyWhere = string.Format("\"{0}\" = :{1}  ", "KEY", "Key");
            foreach (var c in ColumnPropMaps)
            {
                selectValues.AppendFormat("\"{0}\" \"{1}\",", c.Key, c.Value);
            }

            selectValues.Remove(selectValues.Length - 1, 1);
            return string.Format("SELECT {1} FROM \"{0}\" WHERE {2}", TABLE_NAME, selectValues, keyWhere);

        }

        protected virtual string BuildInsertSnapshotBaseSql()
        {

            StringBuilder cols = new StringBuilder();
            StringBuilder parms = new StringBuilder();
            
                cols.AppendFormat("\"{0}\",", "KEY");
                parms.AppendFormat(":{0},", "Key");
            

            cols.Remove(cols.Length - 1, 1);
            parms.Remove(parms.Length - 1, 1);
            return string.Format("INSERT INTO \"{0}\" ({1}) VALUES ({2})", SNAPSHOT_BASE, cols, parms);
        }



    }
}
