huanghongfeng
12 小时以前 b7f53ba61fbc1844d7b92ff852762b140d46ab54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
 
namespace WIDESEA_Core
{
    public class PageDataOptions
    {
        public int Page { get; set; }
        public int Rows { get; set; }
        public int Total { get; set; }
        public string TableName { get; set; }
        public string Sort { get; set; }
        /// <summary>
        /// 排序方式
        /// </summary>
        public string Order { get; set; }
        public string Wheres { get; set; }
        public bool Export { get; set; }
        public object Value { get; set; }
        /// <summary>
        /// 查询条件
        /// </summary>
        public List<SearchParameters> Filter { get; set; }
 
        public string ValidatePageOptions(PropertyInfo[] entityProperties)
        {
            string where = string.Empty;
            List<SearchParameters> searchParametersList = new List<SearchParameters>();
            if (this.Filter != null && this.Filter.Count > 0)
            {
                searchParametersList.AddRange(Filter);
            }
            else if (!string.IsNullOrEmpty(Wheres))
            {
                try
                {
                    searchParametersList = Wheres.DeserializeObject<List<SearchParameters>>();
                    Filter = searchParametersList;
                }
                catch { }
            }
            for (int i = 0; i < searchParametersList.Count; i++)
            {
                if (string.IsNullOrEmpty(searchParametersList[i].Value))
                {
                    continue;
                }
 
                PropertyInfo? property = entityProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault();
 
                if (property == null) continue;
 
                List<(bool, string, object)> results = property.ValidationValueForDbType(searchParametersList[i].Value.Split(',')).ToList();
                if (results == null || results.Count() == 0)
                {
                    continue;
                }
                for (int j = 0; j < results.Count(); j++)
                {
                    if (j == 0)
                    {
                        where += "(";
                    }
                    LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition();
                    if (expressionType == LinqExpressionType.Equal)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.Equal} '{results[j].Item3}'";
                    }
                    else if (expressionType == LinqExpressionType.ThanOrEqual)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.ThanOrEqual} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.LessThanOrEqual)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.LessOrequal} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.GreaterThan)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.GT} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.LessThan)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.LT} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.Contains)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.like} '%{searchParametersList[i].Value}%'";
                    }
                    else
                    {
                        where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'";
                    }
 
                    if (j == results.Count() - 1)
                    {
                        where += ")";
                    }
                    else
                    {
                        where += " or ";
                    }
                }
                if (i < searchParametersList.Count - 1)
                    where += " and ";
            }
            return where;
        }
 
 
        public Dictionary<string, OrderByType> GetPageDataSort(PropertyInfo[] propertyInfo)
        {
            try
            {
                if (!string.IsNullOrEmpty(Sort))
                {
                    if (Sort.Contains(","))
                    {
                        List<string> sortArr = Sort.Split(",").Where(x => propertyInfo.Any(p => p.Name == x)).ToList();
                        Dictionary<string, OrderByType> sortDic = new Dictionary<string, OrderByType>();
                        foreach (var item in sortArr)
                        {
                            sortDic[item] = Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc;
                        }
                        return sortDic;
                    }
                    else if (propertyInfo.Any(x => x.Name == Sort.FirstLetterToLower() || x.Name == Sort.FirstLetterToUpper()))
                    {
                        Dictionary<string, OrderByType> result = new Dictionary<string, OrderByType>();
 
                        if (Order == "asc")
                        {
                            result.Add(Sort, OrderByType.Asc);
                        }
                        else
                        {
                            result.Add(Sort, OrderByType.Desc);
                        }
 
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
 
                throw;
            }
            return new Dictionary<string, OrderByType> { { "CreateDate", Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc } };
        }
    }
    public class SearchParameters
    {
        public string Name { get; set; }
        public string Value { get; set; }
        //查询类型:LinqExpressionType
        public string DisplayType { get; set; }
    }
}