Przeglądaj źródła

添加过滤代码

luocaiyang 3 lat temu
rodzic
commit
c09ae304cf

+ 56 - 12
wispro.sp.api/Controllers/PerformanceItemController.cs

@@ -503,6 +503,31 @@ namespace wispro.sp.api.Controllers
             
         }
 
+        private string GetExpress(IList<FieldCondition> conditions)
+        {
+            string str = "";
+
+            foreach(var c in conditions)
+            {
+                if (string.IsNullOrEmpty(str))
+                {
+                    str = c.ToExpressString("s");
+                }
+                else
+                {
+                    if(c.LogicOperate == LogicEnum.And)
+                    {
+                        str = $"({str}) && {c.ToExpressString("s")}";
+                    }
+                    else
+                    {
+                        str = $"({str}) || {c.ToExpressString("s")}";
+                    }
+                }
+            }
+
+            return str;
+        }
         [HttpPost]
         public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
         {
@@ -510,34 +535,53 @@ namespace wispro.sp.api.Controllers
             ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
 
             string strExpress = "";
-            if (queryFilter.userId > 0)
-            {
-                strExpress = $"(s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == {queryFilter.userId}).Count() > 0))";
-            }
+            
 
-            if (string.IsNullOrEmpty(strExpress))
+            if (!string.IsNullOrEmpty(strExpress))
             {
                 strExpress = $"{strExpress} && s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
             }
+            else
+            {
+                strExpress = $"s.CalMonth.Status == {Convert.ToInt32(queryFilter.jxType)}";
+            }
 
-            
+            if (queryFilter.ConditionTree != null)
+            {
+                string strTem = GetExpress(queryFilter.ConditionTree);
+
+                if (!string.IsNullOrEmpty(strTem))
+                {
+                    strExpress = $"{strExpress} && ({strTem})";
+                }
+            }
 
             var interpreter = new Interpreter();
             Expression<Func<PerformanceItem, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<PerformanceItem, bool>>(strExpress, "s");
 
-            var response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
-                
+            IQueryable<PerformanceItem> response;
+            if (queryFilter.userId > 0)
+            {
+                 response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere).Where(s => (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Id == queryFilter.userId).Count() > 0 || s.ReviewerId == queryFilter.userId));
 
+            }
+            else
+            {
+                 response = Context.PerformanceItems.Where<PerformanceItem>(dynamicWhere);
 
-            response = response.OrderConditions<PerformanceItem>(queryFilter.Sorts);
-            ret.TotalCount = response.Count();
+            }
 
-            var retList = response
+            int totals;
+            response = response
                 .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
                 .Include(pi => pi.Reviewer)
                 .Include(pi => pi.Customer)
-                .Skip<PerformanceItem>((queryFilter.PageIndex - 1) * queryFilter.PageSize).Take(queryFilter.PageSize).ToList<PerformanceItem>();
+                .Include(pi => pi.CalMonth)
+                .OrderConditions<PerformanceItem>(queryFilter.Sorts)
+                .Pager<PerformanceItem>(queryFilter.PageIndex,queryFilter.PageSize,out totals);
+            ret.TotalCount = totals;
 
+            var retList = response.ToList<PerformanceItem>();
 
             #region 将某些属性设为null,避免循环取值造成返回json过大
             foreach (PerformanceItem item in retList)

+ 307 - 0
wispro.sp.share/EFCoreExt.cs

@@ -0,0 +1,307 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace wispro.sp.share
+{
+    [Serializable]
+    public class ExpressTree {
+        private ConditionTreeNode root ;
+
+        
+        public void AddCondition(LogicEnum logic, FieldCondition fieldCondition)
+        {
+            FieldConditionNode rightNode = new FieldConditionNode() { FieldCondition = fieldCondition };
+            if (root == null)
+            {
+                Console.WriteLine("根节点为空时添加!");
+                root = rightNode;
+            }
+            else
+            {
+                Console.WriteLine("根节点为空时添加!");
+                OperateNode root1 = new OperateNode() { Operator = logic };
+                Console.WriteLine("\t设定右节点!");
+                root1.Right = rightNode;
+                Console.WriteLine("\t设定左节点!");
+                root1.Left = root;
+                Console.WriteLine("\t设定根节点为操作节点!");
+                root = root1;
+                Console.WriteLine((root != null)?"\t根节点不为空": "\t根节点为空");
+
+            }
+
+            //Console.WriteLine($"表达式: {ToExpressString("s")}");
+        }
+
+        public string ToExpressString(string prefix)
+        {
+            if(root == null)
+            {
+                return null;
+            }
+            else
+            {
+                if(root is FieldConditionNode)
+                {
+                    return ((FieldConditionNode)root).FieldCondition.ToExpressString(prefix);
+                }
+                else
+                {
+                    OperateNode operate = (OperateNode)root;
+                    if(root.Right != null && root.Left != null)
+                    {
+                        ExpressTree leftTree = new ExpressTree() { root = root.Left };
+                        ExpressTree rightTree = new ExpressTree() { root = root.Right };
+                        switch (operate.Operator)
+                        {
+                            case LogicEnum.And:
+                                return $"({leftTree.ToExpressString(prefix)}) && ({rightTree.ToExpressString(prefix)})";
+                                break;
+                            case LogicEnum.Or:
+                                return $"({leftTree.ToExpressString(prefix)}) || ({rightTree.ToExpressString(prefix)})";
+                                break;
+                            default:
+                                return "";
+                        }
+                    }
+                    else
+                    {
+                        throw (new ApplicationException("无效的表达是树!"));
+                    }
+                }
+            }
+        }
+    }
+    
+    [Serializable]
+    public class ConditionTreeNode
+    {
+        public ConditionTreeNode Right { get; set; }
+        public ConditionTreeNode Left { get; set; }
+
+        
+    }
+
+    [Serializable]
+    public class OperateNode : ConditionTreeNode
+    {
+        public LogicEnum Operator { get; set; }
+
+    }
+
+    [Serializable]
+    public class FieldConditionNode : ConditionTreeNode
+    {
+        public FieldCondition FieldCondition { get; set; }
+    }
+
+    [Serializable]
+    public class OrderField
+    {
+        /// <summary>
+        /// 排序栏位
+        /// </summary>
+        public string FieldName { get; set; }
+
+        /// <summary>
+        /// 排序 0:顺序;1:倒序
+        /// </summary>
+        public int Sort { get; set; }
+    }
+
+    [Serializable]
+    public class FieldCondition
+    {
+        /// <summary>
+        /// 字段名称
+        /// </summary>
+        public string FieldName { get; set; }
+        /// <summary>
+        /// 值
+        /// </summary>
+        public string Value { get; set; }
+        /// <summary>
+        /// 值类型
+        /// </summary>
+        public string ValueType { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public OperatorEnum Operator { get; set; }
+
+        public LogicEnum LogicOperate { get; set; }
+
+        public string ToExpressString(string prefix)
+        {
+            bool isString = (ValueType == typeof(string).ToString());
+
+            switch (Operator) {
+                case OperatorEnum.Contains:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName}.Contains(\"{Value}\")";
+                    }
+                    else
+                    {
+                        throw (new ApplicationException("Contains 操作只对字符字段有效!"));
+                    }
+                    break;
+                case OperatorEnum.NotContains:
+                    if (isString)
+                    {
+                        return $"!{prefix}.{FieldName}.Contains(\"{Value}\")";
+                    }
+                    else
+                    {
+                        throw (new ApplicationException("Contains 操作只对字符字段有效!"));
+                    }
+                    break;
+                case OperatorEnum.Equal:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} == (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} == ({Value})";
+                    }
+                    break;
+                case OperatorEnum.NotEqual:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} != (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} != ({Value})";
+                    }
+                    break;
+                case OperatorEnum.Greater:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} > (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} > ({Value})";
+                    }
+                    break;
+                case OperatorEnum.GreaterEqual:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} >= (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} >= ({Value})";
+                    }
+                    break;
+                case OperatorEnum.Less :
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} < (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} < ({Value})";
+                    }
+                    break;
+                case OperatorEnum.LessEqual:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName} <= (\"{Value}\")";
+                    }
+                    else
+                    {
+                        return $"{prefix}.{FieldName} <= ({Value})";
+                    }
+                    break;
+                
+                case OperatorEnum.StartsWith:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName}.StartsWith(\"{Value}\")";
+                    }
+                    else
+                    {
+                        throw (new ApplicationException("StartsWith 操作只对字符字段有效!"));
+                    }
+                    break;
+                case OperatorEnum.EndWith:
+                    if (isString)
+                    {
+                        return $"{prefix}.{FieldName}.EndWith(\"{Value}\")";
+                    }
+                    else
+                    {
+                        throw (new ApplicationException("EndWith 操作只对字符字段有效!"));
+                    }
+                    break;
+                //case OperatorEnum.In:
+                //    break;
+                //case OperatorEnum.Between:
+                //    break;
+                default:
+                    throw (new ApplicationException("还未实现的操作符号!"));
+                
+
+            }
+        }
+    }
+
+    [Serializable]
+    public enum OperatorEnum
+    {
+        Contains,
+        Equal,
+        Greater,
+        GreaterEqual,
+        Less,
+        LessEqual,
+        NotEqual,
+        In,
+        Between,
+        StartsWith,
+        EndWith,
+        NotContains
+    }
+
+    [Serializable]
+    public enum LogicEnum
+    {
+        And,
+        Or
+    }
+
+    public static class EFCoreExt
+    {
+        public static IQueryable<T> OrderConditions<T>(this IQueryable<T> query, IList<OrderField> orderConditions)
+        {
+            foreach (var orderinfo in orderConditions)
+            {
+                var t = typeof(T);
+                var propertyInfo = t.GetProperty(orderinfo.FieldName);
+                var parameter = Expression.Parameter(t);
+                Expression propertySelector = Expression.Property(parameter, propertyInfo);
+
+                var orderby = Expression.Lambda<Func<T, object>>(propertySelector, parameter);
+                if (orderinfo.Sort == 1)
+                    query = query.OrderByDescending(orderby);
+                else
+                    query = query.OrderBy(orderby);
+
+            }
+            return query;
+        }
+
+        public static IQueryable<T> Pager<T>(this IQueryable<T> query, int pageindex, int pagesize, out int itemCount)
+        {
+            itemCount = query.Count();
+            return query.Skip((pageindex - 1) * pagesize).Take(pagesize);
+        }
+    }
+}

+ 3 - 9
wispro.sp.share/QueryFilter.cs

@@ -1,11 +1,5 @@
-using AntDesign.TableModels;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using wispro.sp.entity;
-using wispro.sp.utility;
+using System.Collections.Generic;
+
 
 namespace wispro.sp.share
 {
@@ -13,7 +7,7 @@ namespace wispro.sp.share
     {
         public int userId { get; set; }
 
-        public ConditionTreeNode ConditionTree { get; set; }
+        public IList<FieldCondition> ConditionTree { get; set; }
 
         public jxType jxType { get; set; }
 

+ 0 - 1
wispro.sp.share/wispro.sp.share.csproj

@@ -10,7 +10,6 @@
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\wispro.sp.utility\wispro.sp.utility.csproj" />
     <ProjectReference Include="..\wospro.sp.entity\wispro.sp.entity.csproj" />
   </ItemGroup>
 

+ 0 - 107
wispro.sp.utility/EFCoreExt.cs

@@ -1,107 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Linq.Expressions;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace wispro.sp.utility
-{
-    public class ConditionTreeNode
-    {
-        public ConditionTreeNode Right { get; set; }
-        public ConditionTreeNode Left { get; set; }
-    }
-
-    public class OperateNode : ConditionTreeNode
-    {
-        public LogicEnum Operator { get; set; }
-
-    }
-
-    public class FieldConditionNode : ConditionTreeNode
-    {
-        public FieldCondition FieldCondition { get; set; }
-    }
-
-    public class OrderField { 
-        /// <summary>
-        /// 排序栏位
-        /// </summary>
-        public string FieldName { get; set; }
-
-        /// <summary>
-        /// 排序 0:顺序;1:倒序
-        /// </summary>
-        public int Sort { get; set; }
-    }
-    public class FieldCondition
-    {
-        /// <summary>
-        /// 字段名称
-        /// </summary>
-        public string FieldName { get; set; }
-        /// <summary>
-        /// 值
-        /// </summary>
-        public string Value { get; set; }
-        /// <summary>
-        /// 值类型
-        /// </summary>
-        public string ValueType { get; set; }
-        /// <summary>
-        /// 
-        /// </summary>
-        public OperatorEnum Operator { get; set; }
-    }
-
-    public enum OperatorEnum
-    {
-        Contains,
-        Equal,
-        Greater,
-        GreaterEqual,
-        Less,
-        LessEqual,
-        NotEqual,
-        In,
-        Between,
-        StartsWith,
-        EndWith,
-        NotContains
-    }
-
-    public enum LogicEnum
-    {
-        And,
-        Or
-    }
-
-    public static class EFCoreExt
-    {
-        public static IQueryable<T> OrderConditions<T>(this IQueryable<T> query, IList<OrderField> orderConditions)
-        {
-            foreach (var orderinfo in orderConditions)
-            {
-                var t = typeof(T);
-                var propertyInfo = t.GetProperty(orderinfo.FieldName);
-                var parameter = Expression.Parameter(t);
-                Expression propertySelector = Expression.Property(parameter, propertyInfo);
-
-                var orderby = Expression.Lambda<Func<T, object>>(propertySelector, parameter);
-                if (orderinfo.Sort == 1)
-                    query = query.OrderByDescending(orderby);
-                else
-                    query = query.OrderBy(orderby);
-
-            }
-            return query;
-        }
-
-        public static IQueryable<T> Pager<T>(this IQueryable<T> query, int pageindex, int pagesize, out int itemCount)
-        {
-            itemCount = query.Count();
-            return query.Skip((pageindex - 1) * pagesize).Take(pagesize);
-        }
-    }
-}

+ 0 - 27
wispro.sp.utility/ExcelHelper.cs

@@ -12,33 +12,6 @@ namespace wispro.sp.utility
     public class ExcelHelper
     {
 
-        //string[] temList = new string[] {"PACN1917256",
-        //                                    "PAUS2010223",
-        //                                    "PAUS1914936",
-        //                                    "PACN1815861",
-        //                                    "PACN1913445",
-        //                                    "PACN1816576",
-        //                                    "PACN2113860",
-        //                                    "PACN1812755-结案",
-        //                                    "PACN2015701",
-        //                                    "PCTCN1815005",
-        //                                    "PACN1913883-加快",
-        //                                    "PACN1915086-加快",
-        //                                    "PACN1810141",
-        //                                    "PACN1815247",
-        //                                    "PACN1812737",
-        //                                    "PACN1813923",
-        //                                    "PACN1912513",
-        //                                    "PACN1912504",
-        //                                    "PAEPO2112337",
-        //                                    "PAUS2111949",
-        //                                    "PATW2111951",
-        //                                    "PAEPO2111950",
-        //                                    "PAEPO2111788",
-        //                                    "PAEPO2013035",
-        //                                    "PAEPO1918035",
-        //                                    "PAEPO1813034",
-        //                                    "PAGB1711461" };
 
         public DataTable MerageExcel(string desFileName,string[] excelFiles)
         {

+ 2 - 1
wispro.sp.web/Pages/AppCase/MyCaselist.razor.cs

@@ -145,7 +145,8 @@ namespace wispro.sp.web.Pages.AppCase
         private async Task HandleTableChange(QueryModel<PerformanceItem> queryModel)
         {
             _loading = true;
-            var data = await _ItemService.GetMyList(_userService.CurrentUser.Userid.Value, jxType.doing, _pageIndex, _pageSize);
+            var data = await _ItemService.Query(_userService.CurrentUser.Userid.Value, jxType.doing, queryModel);
+            
             _Datas = data.Results;
             _total = data.TotalCount;
             _loading = false;

+ 96 - 1
wispro.sp.web/Services/PerformanceItemServices.cs

@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Components.Authorization;
+using AntDesign.TableModels;
+using Microsoft.AspNetCore.Components.Authorization;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -41,6 +42,100 @@ namespace wispro.sp.web.Services
             return data;
         }
 
+        public async Task<ListApiResponse<PerformanceItem>> Query(int userid, jxType type, QueryModel<PerformanceItem> queryModel)
+        {
+            QueryFilter query = new QueryFilter();
+            query.userId = userid;
+            query.jxType = type;
+            query.PageIndex = queryModel.PageIndex;
+            query.PageSize = queryModel.PageSize;
+
+            query.ConditionTree = new List<FieldCondition>();
+
+            foreach(var filter in queryModel.FilterModel)
+            {
+                foreach(var f in filter.Filters)
+                {
+                    FieldCondition condition = new FieldCondition() { FieldName = filter.FieldName};
+                    condition.Value = f.Value.ToString();
+                    condition.ValueType = typeof(PerformanceItem).GetProperty(filter.FieldName).PropertyType.ToString();
+                    switch (f.FilterCompareOperator)
+                    {
+                        case AntDesign.TableFilterCompareOperator.Contains:
+                            condition.Operator = OperatorEnum.Contains;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.EndsWith:
+                            condition.Operator = OperatorEnum.EndWith;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.Equals:
+                            condition.Operator = OperatorEnum.EndWith;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.GreaterThan:
+                            condition.Operator = OperatorEnum.Greater ;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.GreaterThanOrEquals:
+                            condition.Operator = OperatorEnum.GreaterEqual;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.LessThan:
+                            condition.Operator = OperatorEnum.Less;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.LessThanOrEquals:
+                            condition.Operator = OperatorEnum.LessEqual;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.NotContains:
+                            condition.Operator = OperatorEnum.NotContains;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.NotEquals:
+                            condition.Operator = OperatorEnum.NotEqual;
+                            break;
+                        case AntDesign.TableFilterCompareOperator.StartsWith:
+                            condition.Operator = OperatorEnum.StartsWith;
+                            break;
+                        
+                    }
+
+                    switch (f.FilterCondition)
+                    {
+                        case AntDesign.TableFilterCondition.And:
+                            condition.LogicOperate = LogicEnum.And;
+                            //expressTree.AddCondition. (LogicEnum.And, condition);
+                            //Console.WriteLine($"添加条件:{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType} AND");
+                            break;
+                        case AntDesign.TableFilterCondition.Or:
+                            condition.LogicOperate = LogicEnum.Or;
+                            //expressTree.AddCondition(LogicEnum.Or, condition);
+                            //Console.WriteLine($"添加条件:{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType}  OR");
+                            break;
+                        default:
+                            condition.LogicOperate = LogicEnum.And;
+                            //expressTree.AddCondition(LogicEnum.And, condition);
+                            //Console.WriteLine($"添加条件:{condition.FieldName} {condition.Operator} {condition.Value} {condition.ValueType}  AND");
+                            break;
+                    }
+
+                    //Console.WriteLine( expressTree.ToString());
+                    query.ConditionTree.Add(condition);
+                    
+                }
+            }
+
+            
+
+            query.Sorts =new  List<OrderField>();
+            foreach(var sort in queryModel.SortModel)
+            {
+                Console.WriteLine($"{sort.FieldName}\t{sort.Sort}");
+                if (!string.IsNullOrEmpty(sort.Sort))
+                {
+                    query.Sorts.Add(new OrderField() { FieldName = sort.FieldName, Sort = (sort.Sort == "descend" ? 1 : 0) });
+                }
+            }
+            var data = await _httpClient.PostAsJsonAsync($"http://localhost:39476/api/PerformanceItem/QueryFilter",query);
+
+            var ret =  await data.Content.ReadFromJsonAsync<ListApiResponse<PerformanceItem>>();
+            return ret;
+        }
+
         public async Task<List<StaffStatistics>> CalMyStatistics(int year, int month, int? userid = null)
         {
 

+ 16 - 0
wispro.sp.winClient/Form1.cs

@@ -235,8 +235,24 @@ namespace wispro.sp.winClient
                 await SaveBasePointRule(rule);
             }
         }
+
+        public async Task TestQueryFilter()
+        {
+            QueryFilter filter = new QueryFilter();
+            filter.ConditionTree = new ExpressTree();
+            string ValueType = typeof(PerformanceItem).GetProperty("CaseNo").PropertyType.ToString();
+            FieldCondition condition1 = new FieldCondition() { FieldName = "CaseNo", Operator = OperatorEnum.Contains, Value = "PACN", ValueType = ValueType };
+            FieldCondition condition2 = new FieldCondition() { FieldName = "CaseNo", Operator = OperatorEnum.Contains, Value = "PAUS", ValueType = ValueType };
+
+            filter.ConditionTree.AddCondition(LogicEnum.And,condition1);
+            filter.ConditionTree.AddCondition(LogicEnum.Or, condition2);
+
+            System.Diagnostics.Debug.WriteLine(filter.ConditionTree.ToExpressString("s")); ;
+
+        }
         private async void button3_Click(object sender, EventArgs e)
         {
+            await TestQueryFilter();
 
             await InitRules();