using System;
using System.Collections.Generic;

namespace ryCommonDb
{
    /// <summary>
    /// SQL字段类型
    /// </summary>
    public class SQLIitem
    {
        /// <summary>
        /// SQL字段类型
        /// </summary>
        /// <param name="_field"></param>
        /// <param name="_value"></param>
        public SQLIitem(string _field,object _value)
        {
            Field = _field;
            value = _value;
            len = 0;
        }
        /// <summary>
        /// SQL字段类型
        /// </summary>
        /// <param name="_field"></param>
        /// <param name="_value"></param>
        /// <param name="_len"></param>
        public SQLIitem(string _field, object _value,int _len)
        {
            Field = _field;
            value = _value;
            len = _len;
        }
        /// <summary>
        /// SQL字段名
        /// </summary>
        public string Field = "";
        /// <summary>
        /// SQL字段值
        /// </summary>
        public object value;
        /// <summary>
        /// SQL字段长度
        /// </summary>
        public int len = 0;
        /// <summary>
        /// 不是真的值，而是用于计算的值
        /// </summary>
        public bool isNoValue = false;
    }
    /// <summary>
    /// SQL快速操作类
    /// </summary>
    public class RyQuickSQL
    {
        /// <summary>
        /// SQL快速操作类
        /// </summary>
        /// <param name="_tableName"></param>
        public RyQuickSQL(string _tableName)
        { TableName = _tableName; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName{get;set;}
        /// <summary>
        /// 操作字段列表
        /// </summary>
        public List<SQLIitem> List { get; } = new List<SQLIitem>();
        /// <summary>
        /// 清理使用资源
        /// </summary>
        public void Free()
        {
            Clear();          
        }
        /// <summary>
        /// 清理使用资源
        /// </summary>
        public void Dispose()
        {
            Free();
        }
        /// <summary>
        /// 清理使用资源
        /// </summary>
        ~RyQuickSQL()
        {
            Free();
        }
        /// <summary>
        /// 操作参数列表
        /// </summary>
        public List<SQLIitem> List_param { get; } = new List<SQLIitem>();
        /// <summary>
        /// 操作参数列表
        /// </summary>
        private List<string> List_Nullparam { get; } = new List<string>();
        private void ListAdd(SQLIitem item)
        {
            if(!List.Contains(item))
               List.Add(item);
        }
        /// <summary>
        /// 添加字符串字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field,string value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加null值字段
        /// </summary>
        /// <param name="field"></param>
        public void AddNullField(string field)
        {
            if (!List_Nullparam.Contains(field))
                List_Nullparam.Add(field);
        }
        /// <summary>
        /// 添加日期字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, DateTime value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加int字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, int value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加double字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, double value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加decimal字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, decimal value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加byte[]字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, byte[] value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加bool字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, bool value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 添加用于计算的值
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddFieldCalc(string field, string value)
        {
            List_calcparam.Add(new SQLIitem(field, value) { isNoValue=true });
        }
        /// <summary>
        /// 添加Int64字段
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddField(string field, Int64 value)
        {
            ListAdd(new SQLIitem(field, value));
        }
        /// <summary>
        /// 清空内容
        /// </summary>
        public void Clear()
        {
            List.Clear();
            List_param.Clear();
            List_calcparam.Clear();
            List_Nullparam.Clear(); 
        }
        /// <summary>
        /// 数量
        /// </summary>
        public int Count
        {
            get { return List.Count + List_param.Count; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<SQLIitem> List_calcparam { get; set; } = new List<SQLIitem>();

        /// <summary>
        /// 输出多条件搜索。
        /// </summary>
        /// <param name="Fields">字段名,多个字段请用逗号分隔。</param>
        /// <param name="SearchText">多个条件之间请用空格隔开</param>
        /// <returns></returns>
        public string GetSearchSQL(string Fields, string SearchText)
        {
            string tmpSQL = "";
            string tmpFieldSQL = "(";
            string[] m_name = Fields.Split(",，".ToCharArray());
            string[] tmpFieldNames = new string[m_name.Length];
            for (int i = 0; i < m_name.Length; i++)
            {
                #region 提取字段信息
                tmpFieldNames[i] = m_name[i];
                if (i == 0)
                {
                    tmpFieldSQL += tmpFieldNames[i] + " like @SearchText";
                }
                else
                {
                    tmpFieldSQL += " or " + tmpFieldNames[i] + " like @SearchText";
                }
                #endregion
            }
            tmpFieldSQL += ")";
            string[] m_SearchText = SearchText.Split(' ');
            string tmpSearchSQL = "";
            for (int i = 0; i < m_SearchText.Length; i++)
            {
                if (i == 0)
                {
                    tmpSearchSQL = tmpFieldSQL.Replace("@SearchText", "@st_ry" + i.ToString());
                }
                else
                {
                    tmpSearchSQL += " and " + tmpFieldSQL.Replace("@SearchText", "@st_ry" + i.ToString());
                }
                AddParameter("@st_ry" + i.ToString(), "%" + m_SearchText[i] + "%");
            }
            return tmpSQL + tmpSearchSQL;
        }
        /// <summary>
        /// 获取插入SQL语句
        /// </summary>
        /// <returns></returns>
        public string GetInsertSQL()
        {
            string tmpSQL = "insert into " + TableName + " ";
            string tmpFieldSQL = "(";
            string valueSQL = "(";
            for (int i = 0; i <List.Count; i++)
            {
                SQLIitem item = (SQLIitem)List[i];
                tmpFieldSQL +=item.Field + ",";
                valueSQL += "@" + item.Field.TrimStart('[').TrimEnd(']') + ",";
            }
            for (int i = 0; i < List_Nullparam.Count; i++)
            {
                tmpFieldSQL += List_Nullparam[i] + ",";
                valueSQL += "null,";
            }
            return tmpSQL + tmpFieldSQL.Substring(0, tmpFieldSQL.Length - 1) + ") values" + valueSQL.Substring(0, valueSQL.Length - 1) + ")";
        }
        /// <summary>
        /// 获取更新SQL语句
        /// </summary>
        /// <returns></returns>
        public string GetUpdateSQL()
        {
            string tmpSQL = "update " + TableName + " set ";
            for (int i = 0; i < List.Count; i++)
            {
                SQLIitem item = (SQLIitem)List[i];
                tmpSQL += item.Field + "=" + "@" + item.Field.TrimStart('[').TrimEnd(']') + ",";
            }
            for (int i = 0; i < List_calcparam.Count; i++)
            {
                SQLIitem item = (SQLIitem)List_calcparam[i];
                tmpSQL += item.Field + "=" + item.value + ",";
            }
            for (int i = 0; i < List_Nullparam.Count; i++)
            {
                tmpSQL += List_Nullparam[i] + "=" + "null,";
            }
            return tmpSQL.Substring(0, tmpSQL.Length - 1);
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddParameter(string name,object value)
        {
            List_param.Add(new SQLIitem(name.TrimStart('@'), value));
        }
        /// <summary>
        /// 获取Ole参数
        /// </summary>
        /// <returns></returns>
        public System.Data.OleDb.OleDbParameter[] GetOleParameters()
        {
            System.Data.OleDb.OleDbParameter[] defPar = new System.Data.OleDb.OleDbParameter[List.Count+ List_param.Count];
            for (int i = 0; i < List.Count; i++)
            {
                SQLIitem item = (SQLIitem)List[i];
                defPar[i] =new System.Data.OleDb.OleDbParameter("@"+item.Field, item.value);
            }
            for (int i = List.Count; i < List.Count + List_param.Count; i++)
            {
                SQLIitem item = (SQLIitem)List_param[i- List.Count];
                defPar[i] = new System.Data.OleDb.OleDbParameter("@" + item.Field.TrimStart('@'), item.value);
            }
            return defPar;
        }
        /// <summary>
        /// 获取SQL参数
        /// </summary>
        /// <returns></returns>
        public System.Data.SqlClient.SqlParameter[] GetSqlParameter()
        {
            System.Data.SqlClient.SqlParameter[] defPar = new System.Data.SqlClient.SqlParameter[List.Count + List_param.Count];
            for (int i = 0; i < List.Count; i++)
            {
                SQLIitem item = (SQLIitem)List[i];
                defPar[i] = new System.Data.SqlClient.SqlParameter("@" + item.Field, item.value);
            }
            for (int i = List.Count; i < List.Count + List_param.Count; i++)
            {
                SQLIitem item = (SQLIitem)List_param[i - List.Count];
                defPar[i] = new System.Data.SqlClient.SqlParameter("@" + item.Field.TrimStart('@'), item.value);
            }
            return defPar;
        }
    }
}
