0

在规则定义中的 THEN 中,是否可以调用将调用 db(repos) 并构建新的 dto 集合的服务?我们有日志来对其应用一些业务规则,为我们应用业务规则是根据规则从其他数据库表中收集数据,然后构建新的日志 dto 以保存在某处

例如 RULE1 getlogsforuser,我们将有一条规则,首先查看日志,然后获取用户表和其他表并返回 logOutputDTO

例如 RULE2 getLogsForActiveCompanyyear2020 我们将有一个规则,首先查看日志,然后获取 copmany 表和其他表并返回 logOutputDTO

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Kaba.Framework.Data.Helpers
{
    public class Filter
    {   
        public enum FilterOperation
        {
            Contains,
            DoesNotContain,
            StartsWith,    
            EndsWith,      
            
            EqualTo,       
            NotEqualTo,  
            
            GreaterThan,   
            LessThan,      
            GreaterThanOrEqualTo,
            LessThanOrEqualTo,   
            
            Between,             
            NotBetween,

            In,
            NotIn,          
            
            IsEmpty,             
            IsNotEmpty,          
            
            IsNull,              
            IsNotNull,           
            
            And,                 
            Or,
            Not
        }  
        

        //private Filter()
        //{

        //}

        public string PropertyName { get; set; }
        public FilterOperation Operation { get; set; }
        public object[] Values { get; set; }
        public List<Filter> Filters { get; set; }


        public static Filter Contains(string propertyName, string value)
        {
            return new Filter { Operation = FilterOperation.Contains, PropertyName = propertyName, Values = new object[] { value } };
        }

        public static Filter DoesNotContain(string propertyName, string value)
        {
            return new Filter { Operation = FilterOperation.DoesNotContain, PropertyName = propertyName, Values = new object[] { value } };
        }
            public static Filter StartsWith(string propertyName, string value)
        {
            return new Filter { Operation = FilterOperation.StartsWith, PropertyName = propertyName, Values = new object[] { value } };
        }    
            public static Filter EndsWith  (string propertyName, string value)
        {
            return new Filter { Operation = FilterOperation.EndsWith, PropertyName = propertyName, Values = new object[] { value } };
        }

        public static Filter EqualTo(string propertyName, object value)
        {
            return value == null
                ? IsNull(propertyName)
                : new Filter { Operation = FilterOperation.EqualTo, PropertyName = propertyName, Values = new[] { value } };
        }
        

        public static Filter NotEqualTo(string propertyName, object value)
        {
            return value == null
              ? IsNotNull(propertyName)
              : new Filter { Operation = FilterOperation.NotEqualTo, PropertyName = propertyName, Values = new[] { value } };
        }
        public static Filter GreaterThan(string propertyName, object value)
        {
            return new Filter { Operation = FilterOperation.GreaterThan, PropertyName = propertyName, Values = new[] { value } };
        }
        public static Filter LessThan(string propertyName, object value)
        {
            return new Filter { Operation = FilterOperation.LessThan, PropertyName = propertyName, Values = new[] { value } };
        }
        public static Filter GreaterThanOrEqualTo(string propertyName, object value)
        {
            return new Filter { Operation = FilterOperation.GreaterThanOrEqualTo, PropertyName = propertyName, Values = new[] { value } };
        }
        public static Filter LessThanOrEqualTo(string propertyName, object value)
        {
            return new Filter { Operation = FilterOperation.LessThanOrEqualTo, PropertyName = propertyName, Values = new[] { value } };
        }
        public static Filter Between(string propertyName,  object lo, object hi)
        {
            return new Filter { Operation = FilterOperation.Between, PropertyName = propertyName, Values = new[] { lo, hi } };
        }
        public static Filter NotBetween(string propertyName, object lo, object hi)
        {
            return new Filter { Operation = FilterOperation.NotBetween, PropertyName = propertyName, Values = new[] { lo, hi } };
        }
        public static Filter In<T>(string propertyName, IEnumerable<T> values)
        {
            return new Filter { Operation = FilterOperation.In, PropertyName = propertyName, Values = ConvertCollectionItemsFromEnumToInteger(values) };
        }
        public static Filter In<R, T>(Expression<Func<R, object>> propertyExpression, IEnumerable<T> values)
        {
            return new Filter { Operation = FilterOperation.In, PropertyName = Reflect<R>.PropertyName(propertyExpression), Values = ConvertCollectionItemsFromEnumToInteger(values) };
        }
        public static Filter NotIn<T>(string propertyName, IEnumerable<T> values)
        {
            return new Filter { Operation = FilterOperation.NotIn, PropertyName = propertyName, Values = ConvertCollectionItemsFromEnumToInteger(values) };
        }

        public static Filter IsEmpty(string propertyName)
        {
            return new Filter { Operation = FilterOperation.IsEmpty, PropertyName = propertyName};
        }
        public static Filter IsNotEmpty(string propertyName)
        {
            return new Filter { Operation = FilterOperation.IsNotEmpty, PropertyName = propertyName};
        }

        public static Filter IsNull(string propertyName)
        {
            return new Filter { Operation = FilterOperation.IsNull, PropertyName = propertyName };
        }
        public static Filter IsNull<T>(Expression<Func<T, object>> propertyExpression)
        {
            return new Filter { Operation = FilterOperation.IsNull, PropertyName = Reflect<T>.PropertyName(propertyExpression) };
        }

        public static Filter IsNotNull(string propertyName)
        {
            return new Filter { Operation = FilterOperation.IsNotNull, PropertyName = propertyName };
        }
        public static Filter IsNotNull<T>(Expression<Func<T, object>> propertyExpression)
        {
            return new Filter { Operation = FilterOperation.IsNotNull, PropertyName = Reflect<T>.PropertyName(propertyExpression) };
        }

        // logical operations
        public static Filter And(params Filter[] filters)
        {
            return new Filter { Operation = FilterOperation.And, Filters = filters.ToList() };
        }

        public static Filter Or(params Filter[] filters)
        {
            return new Filter { Operation = FilterOperation.Or, Filters = filters.ToList() };
        }

        public static Filter Not(Filter filter)
        {
            return new Filter { Operation = FilterOperation.Not, Filters = new[] { filter }.ToList() };
        }
        private static object[] ConvertCollectionItemsFromEnumToInteger<T>(IEnumerable<T> values)
        {
            return typeof(T).IsEnum ?
                     values.Select(x => Convert.ToInt32(Enum.Parse(typeof(T), x.ToString()) as Enum)).Cast<object>().ToArray() :
                     values.Cast<object>().ToArray();
        }
    }
}
4

0 回答 0