using SqlSugar;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SqlSugar.BzTDengineCore
{
    public partial class TDengineQueryBuilder : QueryBuilder
    {
        #region Sql Template

        public override string PageTempalte
        {
            get
            {
                /*
                 SELECT * FROM TABLE WHERE CONDITION ORDER BY ID DESC LIMIT 10 offset 0
                 */
                var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {6} offset {5}";
                return template;
            }
        }

        public override string DefaultOrderByTemplate
        {
            get
            {
                return "ORDER BY NOW() ";
            }
        }

        #endregion Sql Template

        #region Common Methods

        public override string GetTableNameString
        {
            get
            {
                if (TableShortName != null && Context.CurrentConnectionConfig?.MoreSettings?.PgSqlIsAutoToLower == false)
                {
                    TableShortName = Builder.GetTranslationColumnName(TableShortName);
                }
                return base.GetTableNameString;
            }
        }

        public override bool IsComplexModel(string sql)
        {
            return Regex.IsMatch(sql, @"AS ""\w+\.\w+""") || Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
        }

        public override string ToSqlString()
        {
            base.AppendFilter();
            string oldOrderValue = OrderByValue;
            string result = null;
            sql = new StringBuilder();
            sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, Skip != null || Take != null ? null : GetOrderByString);
            if (IsCount) { return sql.ToString(); }
            if (Skip != null && Take == null)
            {
                if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
                result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, Skip != null || Take != null ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
            }
            else if (Skip == null && Take != null)
            {
                if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
                result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
            }
            else if (Skip != null && Take != null)
            {
                if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
                result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
            }
            else
            {
                result = sql.ToString();
            }
            OrderByValue = oldOrderValue;
            result = GetSqlQuerySql(result);
            if (result.IndexOf("-- No table") > 0)
            {
                return "-- No table";
            }
            if (TranLock != null)
            {
                result = result + TranLock;
            }
            return result;
        }

        #endregion Common Methods

        #region Get SQL Partial

        public override string GetSelectValue
        {
            get
            {
                string result = string.Empty;
                if (SelectValue == null || SelectValue is string)
                {
                    result = GetSelectValueByString();
                }
                else
                {
                    result = GetSelectValueByExpression();
                }
                if (SelectType == ResolveExpressType.SelectMultiple)
                {
                    SelectCacheKey = SelectCacheKey + string.Join("-", JoinQueryInfos.Select(it => it.TableName));
                }
                if (IsDistinct)
                {
                    result = "distinct " + result;
                }
                if (SubToListParameters != null && SubToListParameters.Any())
                {
                    result = SubToListMethod(result);
                }
                return result;
            }
        }

        #endregion Get SQL Partial
    }
}
