ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
using IWareDataAccess.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
 
namespace IWareDataAccess.Helper
{
    public static class PredicateBuilder
    {
        private const String str_Start = "Start";
        private const String str_End = "End";
 
        public static Expression<Func<T, bool>> True<T>()
        {
            return f => true;
        }
 
        public static Expression<Func<T, bool>> False<T>()
        {
            return f => false;
        }
 
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                            Expression<Func<T, bool>> expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>
                  (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
        }
 
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                             Expression<Func<T, bool>> expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>
                  (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
 
 
        }
 
 
        /// <summary>
        /// 获取筛选条件的查询表达式
        /// </summary>
        /// <typeparam name="T">查询对象</typeparam>
        /// <typeparam name="T2">返回对象</typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static Expression<Func<T2, bool>> GetFilterExpression<T, T2>(T t)
        {
            var f = PredicateBuilder.True<T2>();
            //便利所有属性
            Type type = t.GetType();
            foreach (var i in type.GetProperties())
            {
                var _noDbFilterAttribute = AttributeHelper.GetNoDbFilterAttributeByPro(i);
                if (_noDbFilterAttribute.Length > 0)
                {
                    continue;
                }
                object v = Helper.GetFieldValueByName(t, i.Name);
                if (i.PropertyType == typeof(String))
                {
                    string value;
                    if (v != null)
                    {
                        value = v.ToString();
                        f = f.And(x => Helper.GetFieldValueByName(x, i.Name) != null ? Helper.GetFieldValueByName(x, i.Name).ToString() == value : false);
                    }
                }
                else if (i.PropertyType == typeof(DateTime?))
                {
                    if (v != null)
                    {
                        DateTime value = DateTime.Parse(v.ToString());
 
                        if (i.Name.IndexOf(str_Start) > -1)
                        {
                            var name = GetTimeName(i.Name, str_Start);
                            //f = f.And(x => x.updateTime > value);
                            f = f.And(x => Helper.GetFieldValueByName(x, name) != null ? Convert.ToDateTime(Helper.GetFieldValueByName(x, name)) > value : false);
                        }
                        if (i.Name.IndexOf(str_End) > -1)
                        {
                            var name = GetTimeName(i.Name, str_End);
                            //f = f.And(x => x.updateTime < value);
                            f = f.And(x => Helper.GetFieldValueByName(x, name) != null ? Convert.ToDateTime(Helper.GetFieldValueByName(x, name)) < value : false);
                        }
                    }
                }
                else
                {
                    if (v != null)
                    {
                        f = f.And(x => Object.Equals(Helper.GetFieldValueByName(x, i.Name), v));
                    }
                }
            }
            return f;
        }
 
 
 
        /// <summary>
        /// 解析前台查询传过来的属性名
        /// </summary>
        /// <param name="inputName"></param>
        /// <returns></returns>
        private static string GetTimeName(string inputName, string indexName)
        {
            //updateTimeStart updateTimeEnd
            //doTimeStart doTimeEnd createTimeEnd
            //createTimeStart 
 
            int index = 0;
 
            index = inputName.IndexOf(indexName);
            if (index > -1)
            {
                return inputName.Substring(0, index);
            }
            return inputName;
        }
    }
}