schangxiang@126.com
2025-05-07 f73d8786777c7ebcf53a93bf46c2268892a28502
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System.Linq.Expressions;
using System.Reflection;
 
namespace CmsQueryExtensions.Extension
{
    /// <summary>
    /// 生成Where条件扩展
    /// </summary>
    public class WhereConditionsExtensions
    {
        public static FunReturnResultModel<Expression<Func<T, bool>>> GetWhereConditions<T, TParam>(TParam param)
        {
            FunReturnResultModel<Expression<Func<T, bool>>> result = new FunReturnResultModel<Expression<Func<T, bool>>>();
            try
            {
                Expression<Func<T, bool>> conditions = PredicateExtensions.True<T>();
 
                var errMsg = "";
 
                #region 1、通用查询searchVal
 
                //判断通用查询searchVal
                var pro_value_searchVal = ClassHelper.GetPropertyValue<TParam>(param, SystemCommonVar.searchVal, out errMsg);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    result.IsSuccess = false;
                    result.ErrMsg = errMsg;
                    return result;
                }
                if (!string.IsNullOrEmpty(pro_value_searchVal))
                {//表示 通用查询searchVal不为空,需要查询
                    var pro_value_searchValMode = ClassHelper.GetPropertyValue<TParam>(param, SystemCommonVar.searchVal_FilterMode, out errMsg);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        result.IsSuccess = false;
                        result.ErrMsg = errMsg;
                        return result;
                    }
 
                    var pro_value_searchFormInputAttrs = ClassHelper.GetPropertyValueForList<TParam>(param, SystemCommonVar.searchFormInputAttrs, out errMsg);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        result.IsSuccess = false;
                        result.ErrMsg = errMsg;
                        return result;
                    }
                    if (pro_value_searchFormInputAttrs != null && pro_value_searchFormInputAttrs.Count > 0)
                    {
                        var i_pro_value_searchValMode = Convert.ToInt32(((SearchFilterModeEnum)Enum.Parse(typeof(SearchFilterModeEnum), pro_value_searchValMode)));
                        if (i_pro_value_searchValMode == Convert.ToInt32(SearchFilterModeEnum.模糊查询))
                        {//模糊查询
                            try
                            {
                                var new_conditions = PredicateExtensions.GetConditionExpressionForFuzzyQuery<T>(pro_value_searchFormInputAttrs.ToArray(), pro_value_searchVal);
                                conditions = conditions.And(new_conditions);
                            }
                            catch (Exception ex)
                            {
                                result.data = null;
                                result.IsSuccess = false;
                                result.ErrMsg = ex.Message;
                                return result;
                            }
                        }
                        else if (i_pro_value_searchValMode == Convert.ToInt32(SearchFilterModeEnum.精准查询))
                        {//精准查询
                            var new_conditions = PredicateExtensions.GetConditionExpressionForPreciseQuery<T>(pro_value_searchFormInputAttrs.ToArray(), pro_value_searchVal);
                            conditions = conditions.And(new_conditions);
                            /*
                          pro_value_searchFormInputAttrs.ForEach(x =>
                          {
                            if (!string.IsNullOrEmpty(x))
                            {
                                var myParam = Expression.Parameter(typeof(T));
                                var condition =
Expression.Lambda<Func<T, bool>>(
    Expression.Equal(
        Expression.Property(myParam, x),
        Expression.Constant(pro_value_searchVal, typeof(string))
    ),
    myParam
); // for LINQ to SQl/Entities skip Compile() call
                                conditions = conditions.And(condition);
 
                        }
 
                        });
                             //*/
                        }
                        else
                        {
                            result.IsSuccess = false;
                            result.ErrMsg = "通用查询配置不正确";
                            return result;
                        }
                    }
                }
 
                #endregion
 
 
                #region 2、高级查询 
 
                var high_pros = FilterHigh_pros<TParam>(param);
                List<HighSearchForDateTimeRangeModel> hsmForDatetimeList = new List<HighSearchForDateTimeRangeModel>();
                List<HighSearchModel> hsmList = GetHighSearchModelList<TParam>(param, high_pros, ref hsmForDatetimeList);
                if (hsmList.Count > 0)
                {
                    var high_conditions = PredicateExtensions.GetConditionExpressionForHighFieldByAnd<T>(hsmList);
                    conditions = conditions.And(high_conditions);
                }
                if (hsmForDatetimeList.Count > 0)
                {
                    var high_conditions = PredicateExtensions.GetConditionExpressionForHighFieldByAnd<T>(hsmForDatetimeList);
                    conditions = conditions.And(high_conditions);
                }
                #endregion
 
 
                result.data = conditions;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
 
        /// <summary>
        /// 过滤掉Mode后缀的属性 和一些特殊属性的,如DateTimeRange
        /// </summary>
        /// <typeparam name="TParam"></typeparam>
        /// <param name="param"></param>
        /// <param name="high_pros"></param>
        /// <returns></returns>
        private static List<HighSearchModel> GetHighSearchModelList<TParam>(TParam param, List<PropertyInfo> high_pros, ref List<HighSearchForDateTimeRangeModel> hsmForDatetimeList)
        {
            List<HighSearchModel> hsmList = new List<HighSearchModel>();
            string errMsg = "";
            SearchFilterModeEnum _svmEnum = default(SearchFilterModeEnum);
            var hsmValue = "";
            var pro_value = "";
            foreach (var pro in high_pros)
            {
                if (!pro.Name.Contains(SystemCommonVar.highSearchModeSuffix))
                {//不是Mode后缀的字段
                 //判断是不是有特性特性的值
                    var _highSearchRangeAttribute = ClassHelper.GetHighSearchRangeAttributeByPro(pro);
                    if (_highSearchRangeAttribute.Length > 0)
                    {
                        object v = ClassHelper.GetPropertyValue<TParam>(param, pro);
                        if (v == null)
                        {
                            continue;
                        }
                        List<string> arr_value = v as List<string>;
                        if (arr_value.Count < 2)
                        {
                            continue;
                        }
                        //string[] arr_value = (string[])v;
                        //string[] arr_value = Convert.ToString(v).Split(',');
                        hsmForDatetimeList.Add(new HighSearchForDateTimeRangeModel()
                        {
                            fieldName = pro.Name,
                            start_fieldValue = arr_value[0],
                            end_fieldValue = arr_value[1]
                        });
                        continue;
                    }
 
                    pro_value = ClassHelper.GetPropertyValueByObject<TParam>(param, pro);
                    if (string.IsNullOrEmpty(pro_value))
                    {
                        continue;
                    }
 
                    if (!ClassHelper.IsExistNoAutoQueryAttribute(pro))
                    {
                        //查找是相应Mode后缀的字段的值
                        try
                        {
                            //修复查询异常bug 【Editby shaocx,2025-05-03】
                            SearchFilterModeEnum? _svmEnum_obj = null;
                            _svmEnum_obj = (SearchFilterModeEnum?)ClassHelper.GetPropertyValueForReObject<TParam>(param, pro.Name + SystemCommonVar.highSearchModeSuffix, out errMsg);
                            if (_svmEnum_obj != null) { _svmEnum = _svmEnum_obj.Value; }
                            else
                            {
                                _svmEnum = SearchFilterModeEnum.不筛选;
                            }
                        }
                        catch (Exception ex)
                        {
                            //处理异常信息 【Editby shaocx,2025-05-03】
                            _svmEnum = SearchFilterModeEnum.不筛选;
                        }
 
                        //if (!string.IsNullOrEmpty(errMsg))
                        //{
                        //    throw new Exception(errMsg);
                        //}
                        //if (hsmValue == "0" || string.IsNullOrEmpty(hsmValue))
                        //{
                        //    continue;
                        //}
                        //_svmEnum = (SearchFilterModeEnum)Enum.Parse(typeof(SearchFilterModeEnum), hsmValue);
                        if (_svmEnum == SearchFilterModeEnum.不筛选)
                        {
                            continue;
                        }
                        hsmList.Add(new HighSearchModel()
                        {
                            fieldName = pro.Name,
                            fieldValue = pro_value,
                            filterMode = _svmEnum
                        });
                    }
                }
            }
            return hsmList;
        }
 
 
        /// <summary>
        /// 过滤一些不需要的字段,如Page、PageSize等
        /// </summary>
        /// <typeparam name="TParam"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        private static List<PropertyInfo> FilterHigh_pros<TParam>(TParam param)
        {
            var all_high_pros = ClassHelper.GetPropertyInfoList<TParam>(param);
            var high_pros = new List<PropertyInfo>();
            //排除不用的
            for (int i = 0; i < all_high_pros.Length; i++)
            {
                if (!SystemCommonVar.commnParamFatherPros.Contains(all_high_pros[i].Name))
                {
                    high_pros.Add(all_high_pros[i]);
                }
            }
            return high_pros;
        }
    }
}