using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Linq.Expressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace XYY.Common.Standard
{
    public class QueryModel
    {
        public enum Method
        {
            等于,
            不等于,
            模糊,
            小于,
            大于,
            小于等于,
            大于等于,
            包含,
            不包含
        }

        public string Key { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public List<QuerySort> QuerySort { get; set; }
        public List<QueryParamer> QueryParamer { get; set; }
        public List<DataPermission> DataPermission { get; set; }

        public List<string> GroupBy { get; set; }

        private string _field { get; set; }
        private string _method { get; set; }
        private object _value { get; set; }
        public QueryModel Field<T>(Expression<Func<T, object>> expr) where T : class
        {
            switch (expr.Body.NodeType)
            {
                case ExpressionType.MemberAccess:
                    _field = (expr.Body as System.Linq.Expressions.MemberExpression).Member.Name;
                    break;
                default:
                    _field = ((expr.Body as System.Linq.Expressions.UnaryExpression).Operand as MemberExpression).Member.Name;
                    break;
            }
            return this;
        }
        public QueryModel Operator(Method method)
        {
            switch (method)
            {
                case Method.等于: _method = "Eq"; break;
                case Method.不等于: _method = "NotEq"; break;
                case Method.模糊: _method = "Co"; break;
                case Method.小于: _method = "Le"; break;
                case Method.大于: _method = "Gr"; break;
                case Method.小于等于: _method = "LeE"; break;
                case Method.大于等于: _method = "GrE"; break;
                case Method.包含: _method = "In"; break;
                case Method.不包含: _method = "NotIn"; break;
            }
            return this;
        }
        public QueryModel Value(object value)
        {
            _value = value;
            return this;
        }
        public void Build()
        {
            var query = new QueryParamer()
            {
                Filed = _field,
                Method = _method,
                Value = _value.ToString()
            };

            if (QueryParamer == null)
            {
                QueryParamer = new List<QueryParamer>()
                {
                    query
                };
            }
            else
            {
                QueryParamer.Add(query);
            }
        }
    }

    public class DataPermission
    {
        /// <summary>
        /// 用户编号
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 数据权限点（站点，供应商，仓库等）
        /// </summary>
        public int PermissioinType { get; set; }
        /// <summary>
        /// 对应值
        /// </summary>
        public string Value { get; set; }
    }
}
