using Dapper;
using Ewide.Core.Extension.DataFilter.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ewide.Core.Extension.DataFilter.WebPage
{
    public class SearchJsonFitlerInfoGetService : IFitlerInfoGetService
    {
        protected PageInputBase Input;
        protected IEnumerable<string> FilterFields;
        public DynamicParameters sqlParameters = new DynamicParameters();

        public SearchJsonFitlerInfoGetService(PageInputBase input, IEnumerable<string> filterFields, object param = null)
        {
            Input = input;
            FilterFields = filterFields;
            sqlParameters = new DynamicParameters(param);
        }


        public FilterInfo GetFilterInfo()
        {
            var result = new FilterInfo();
            result.SetSearchInfo(Input.SearchInfo, FilterFields);
            return result;
        }

        public string GetWhereSql(string baseSql)
        {
            var filterInfo = GetFilterInfo();
            baseSql = String.Format("SELECT * FROM ({0}) T ", baseSql);
            var searchInfoList = filterInfo.SearchInfos;
            if (searchInfoList.Count == 0)
                return baseSql;
            List<string> whereSqls = new List<string>();
            foreach (var elem in searchInfoList.Where(m => m != null))
            {
                var r = GetWhereSqlBuild(elem);
                if (String.IsNullOrWhiteSpace(r)) continue;
                whereSqls.Add(r);
            }
            StringBuilder result = new StringBuilder();

            if (whereSqls.Count > 0)
            {
                result.Append(" WHERE ");
                result.Append(String.Join(" AND ", whereSqls));
            }
            return baseSql + result.ToString();
        }

        #region 私有方法
        private string GetWhereSqlBuild(SearchInfo searchInfo)
        {
            string sqlT = "`{0}` {1} {2}";
            List<string> whereList = new List<string>();
            foreach (var elem in searchInfo.Value)
            {
                if (String.IsNullOrWhiteSpace(elem)) continue;

                var parameterName = "@" + searchInfo.Field + Guid.NewGuid().ToString("N");
                //AddParameter(parameterName, elem);
                sqlParameters.Add(parameterName, QueryTypeEnum.Like == searchInfo.QueryType ? "%" + elem + "%" : QueryTypeEnum.StartWith == searchInfo.QueryType ?  elem + "%" : QueryTypeEnum.EndWith == searchInfo.QueryType ? "%" + elem : elem);
                whereList.Add(String.Format(sqlT, searchInfo.Field, GetSearchOperatorStr(searchInfo.QueryType), parameterName));
            }
            if (whereList.Count == 0) return String.Empty;
            var resultT = "({0})";
            var result = String.Format(resultT, String.Join(" OR ", whereList));
            return result;
        }

        private string GetSearchOperatorStr(QueryTypeEnum searchOperator)
        {
            switch (searchOperator)
            {
                case QueryTypeEnum.GreaterThan:
                    return ">";
                case QueryTypeEnum.GreaterThanOrEqual:
                    return ">=";
                case QueryTypeEnum.LessThan:
                    return "<";
                case QueryTypeEnum.LessThanOrEqual:
                    return "<=";
                case QueryTypeEnum.Equal:
                    return "=";
                default:
                    return "LIKE";
            }
        }


        private void AddParameter(string key, object value)
        {
            //var parameter = _SqlParameters.ParameterNames.FirstOrDefault(m => m == key);
            //if (parameter != null)
            //{
            //    _SqlParameters.
            //}
            //else
            //{
            //    _SqlParameters.Add(key, value);
            //}
        }
        #endregion
    }
}
